Skip to content

Commit 9493709

Browse files
authored
Merge pull request #425 from fnordahl/issue/424
#425 When using SCP to a machine with an IPv6 address the IPv6 address must be encapsulated in brackets ('[]'). Fixes #424
2 parents 151dd5a + fbfe950 commit 9493709

1 file changed

Lines changed: 31 additions & 4 deletions

File tree

juju/machine.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import ipaddress
23
import logging
34
import os
45

@@ -139,6 +140,22 @@ async def set_annotations(self, annotations):
139140
"""
140141
return await _set_annotations(self.tag, annotations, self.connection)
141142

143+
def _format_addr(self, addr):
144+
"""Validate and format IP address.
145+
146+
:param addr: IPv6 or IPv4 address
147+
:type addr: str
148+
:returns: Address string, optionally encapsulated in brackets ([])
149+
:rtype: str
150+
:raises: ValueError
151+
"""
152+
ipaddr = ipaddress.ip_address(addr)
153+
if isinstance(ipaddr, ipaddress.IPv6Address):
154+
fmt = '[{}]'
155+
else:
156+
fmt = '{}'
157+
return fmt.format(ipaddr)
158+
142159
async def scp_to(self, source, destination, user='ubuntu', proxy=False,
143160
scp_opts=''):
144161
"""Transfer files to this machine.
@@ -153,8 +170,13 @@ async def scp_to(self, source, destination, user='ubuntu', proxy=False,
153170
if proxy:
154171
raise NotImplementedError('proxy option is not implemented')
155172

156-
address = self.dns_name
157-
destination = '%s@%s:%s' % (user, address, destination)
173+
try:
174+
# if dns_name is an IP address format it appropriately
175+
address = self._format_addr(self.dns_name)
176+
except ValueError:
177+
# otherwise we assume it to be a DNS resolvable string
178+
address = self.dns_name
179+
destination = '{}@{}:{}'.format(user, address, destination)
158180
await self._scp(source, destination, scp_opts)
159181

160182
async def scp_from(self, source, destination, user='ubuntu', proxy=False,
@@ -171,8 +193,13 @@ async def scp_from(self, source, destination, user='ubuntu', proxy=False,
171193
if proxy:
172194
raise NotImplementedError('proxy option is not implemented')
173195

174-
address = self.dns_name
175-
source = '%s@%s:%s' % (user, address, source)
196+
try:
197+
# if dns_name is an IP address format it appropriately
198+
address = self._format_addr(self.dns_name)
199+
except ValueError:
200+
# otherwise we assume it to be a DNS resolvable string
201+
address = self.dns_name
202+
source = '{}@{}:{}'.format(user, address, source)
176203
await self._scp(source, destination, scp_opts)
177204

178205
async def _scp(self, source, destination, scp_opts):

0 commit comments

Comments
 (0)