Skip to content

Commit 77bc0dc

Browse files
committed
fix: wait for machine address to be ready
1 parent f21bc42 commit 77bc0dc

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

juju/machine.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
import pyrfc3339
88

9-
from . import model, tag, jasyncio
9+
from . import jasyncio, model, tag
1010
from .annotationhelper import _get_annotations, _set_annotations
1111
from .client import client
1212
from .errors import JujuError
13-
from juju.utils import juju_ssh_key_paths
13+
from juju.utils import juju_ssh_key_paths, block_until
1414

1515
log = logging.getLogger(__name__)
1616

@@ -82,7 +82,8 @@ async def scp_to(self, source, destination, user='ubuntu', proxy=False,
8282
"""
8383
if proxy:
8484
raise NotImplementedError('proxy option is not implemented')
85-
85+
if not self.dns_name:
86+
raise JujuError("Machine address not yet ready, please call await machine.wait()")
8687
try:
8788
# if dns_name is an IP address format it appropriately
8889
address = self._format_addr(self.dns_name)
@@ -105,7 +106,8 @@ async def scp_from(self, source, destination, user='ubuntu', proxy=False,
105106
"""
106107
if proxy:
107108
raise NotImplementedError('proxy option is not implemented')
108-
109+
if not self.dns_name:
110+
raise JujuError("Machine address not yet ready, please call await machine.wait()")
109111
try:
110112
# if dns_name is an IP address format it appropriately
111113
address = self._format_addr(self.dns_name)
@@ -147,6 +149,8 @@ async def ssh(
147149
if proxy:
148150
raise NotImplementedError('proxy option is not implemented')
149151
address = self.dns_name
152+
if not address:
153+
raise JujuError("Machine address not yet ready, please call await machine.wait()")
150154
destination = "{}@{}".format(user, address)
151155
_, id_path = juju_ssh_key_paths()
152156
cmd = [
@@ -167,6 +171,14 @@ async def ssh(
167171
# stdout is a bytes-like object, returning a string might be more useful
168172
return stdout.decode()
169173

174+
async def wait(self, timeout: int=300) -> None:
175+
"""Waits until the machie is ready to take ssh/scp commands.
176+
177+
:param int timeout: Timeout in seconds to wait for.
178+
"""
179+
await block_until(lambda: self.safe_data["addresses"] and
180+
self.agent_status == "started", timeout=timeout)
181+
170182
@property
171183
def agent_status(self):
172184
"""Returns the current Juju agent status string.

0 commit comments

Comments
 (0)