Skip to content

Commit ef5edf7

Browse files
committed
fix: use self.addresses
1 parent a7d9e48 commit ef5edf7

1 file changed

Lines changed: 23 additions & 18 deletions

File tree

juju/machine.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def _format_addr(self, addr):
7070
return fmt.format(ipaddr)
7171

7272
async def scp_to(self, source, destination, user='ubuntu', proxy=False,
73-
scp_opts=''):
73+
scp_opts='', wait_for_active=True, timeout=None):
7474
"""Transfer files to this machine.
7575
7676
:param str source: Local path of file(s) to transfer
@@ -79,11 +79,13 @@ async def scp_to(self, source, destination, user='ubuntu', proxy=False,
7979
:param bool proxy: Proxy through the Juju API server
8080
:param scp_opts: Additional options to the `scp` command
8181
:type scp_opts: str or list
82+
:param bool wait_for_active: Wait until the machine is ready to take in ssh commands.
83+
:param int timeout: Time in seconds to wait until the machine becomes ready.
8284
"""
8385
if proxy:
8486
raise NotImplementedError('proxy option is not implemented')
85-
if not self.dns_name:
86-
raise JujuError("Machine address not yet ready, please call await machine.wait()")
87+
if wait_for_active:
88+
await block_until(lambda: self.addresses, timeout=timeout)
8789
try:
8890
# if dns_name is an IP address format it appropriately
8991
address = self._format_addr(self.dns_name)
@@ -94,7 +96,7 @@ async def scp_to(self, source, destination, user='ubuntu', proxy=False,
9496
await self._scp(source, destination, scp_opts)
9597

9698
async def scp_from(self, source, destination, user='ubuntu', proxy=False,
97-
scp_opts=''):
99+
scp_opts='', wait_for_active=True, timeout=None):
98100
"""Transfer files from this machine.
99101
100102
:param str source: Remote path of file(s) to transfer
@@ -103,11 +105,13 @@ async def scp_from(self, source, destination, user='ubuntu', proxy=False,
103105
:param bool proxy: Proxy through the Juju API server
104106
:param scp_opts: Additional options to the `scp` command
105107
:type scp_opts: str or list
108+
:param bool wait_for_active: Wait until the machine is ready to take in ssh commands.
109+
:param int timeout: Time in seconds to wait until the machine becomes ready.
106110
"""
107111
if proxy:
108112
raise NotImplementedError('proxy option is not implemented')
109-
if not self.dns_name:
110-
raise JujuError("Machine address not yet ready, please call await machine.wait()")
113+
if wait_for_active:
114+
await block_until(lambda: self.addresses, timeout=timeout)
111115
try:
112116
# if dns_name is an IP address format it appropriately
113117
address = self._format_addr(self.dns_name)
@@ -137,20 +141,21 @@ async def _scp(self, source, destination, scp_opts):
137141
raise JujuError("command failed: %s" % cmd)
138142

139143
async def ssh(
140-
self, command, user='ubuntu', proxy=False, ssh_opts=None):
144+
self, command, user='ubuntu', proxy=False, ssh_opts=None,wait_for_active=True, timeout=None):
141145
"""Execute a command over SSH on this machine.
142146
143147
:param str command: Command to execute
144148
:param str user: Remote username
145149
:param bool proxy: Proxy through the Juju API server
146150
:param str ssh_opts: Additional options to the `ssh` command
147-
151+
:param bool wait_for_active: Wait until the machine is ready to take in ssh commands.
152+
:param int timeout: Time in seconds to wait until the machine becomes ready.
148153
"""
149154
if proxy:
150155
raise NotImplementedError('proxy option is not implemented')
151156
address = self.dns_name
152-
if not address:
153-
raise JujuError("Machine address not yet ready, please call await machine.wait()")
157+
if wait_for_active:
158+
await block_until(lambda: self.addresses, timeout=timeout)
154159
destination = "{}@{}".format(user, address)
155160
_, id_path = juju_ssh_key_paths()
156161
cmd = [
@@ -171,13 +176,14 @@ async def ssh(
171176
# stdout is a bytes-like object, returning a string might be more useful
172177
return stdout.decode()
173178

174-
async def wait(self, timeout: int = 300) -> None:
175-
"""Waits until the machine is ready to take ssh/scp commands.
176-
177-
:param int timeout: Timeout in seconds.
179+
180+
@property
181+
def addresses(self) -> typing.List[str]:
182+
"""Returns the machine addresses.
183+
178184
"""
179-
await block_until(lambda: self.safe_data["addresses"] and
180-
self.agent_status == "started", timeout=timeout)
185+
return self.safe_data['addresses'] or []
186+
181187

182188
@property
183189
def agent_status(self):
@@ -233,11 +239,10 @@ def dns_name(self):
233239
234240
May return None if no suitable address is found.
235241
"""
236-
addresses = self.safe_data['addresses'] or []
237242
ordered_addresses = []
238243
ordered_scopes = ['public', 'local-cloud', 'local-fan']
239244
for scope in ordered_scopes:
240-
for address in addresses:
245+
for address in self.addresses:
241246
if scope == address['scope']:
242247
ordered_addresses.append(address)
243248
for address in ordered_addresses:

0 commit comments

Comments
 (0)