Skip to content

Commit 4f736dd

Browse files
committed
Add dedicated Host.wait_for_* methods, using local_cmd and xapi-wait-init-complete commands
A superfluous use of wait_for around xapi-wait-init-complete is also removed from the Pool init method. This is because that master host is already available at this point since the corresponding Host object has been already been instantiated and master.is_master() was called. Both of those operations require a successful ssh command to return (cat /etc/xensource/pool.conf and cat /etc/xensource-inventory). Signed-off-by: Vincent Michel <vincent.michel@vates.tech>
1 parent 87e0663 commit 4f736dd

3 files changed

Lines changed: 44 additions & 15 deletions

File tree

lib/host.py

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
strtobool,
2525
to_xapi_bool,
2626
wait_for,
27+
wait_for_not,
2728
)
2829
from lib.netutil import wrap_ip
2930
from lib.pif import PIF
@@ -530,7 +531,36 @@ def restart_toolstack(self, verify: bool = False) -> None:
530531
logging.info("Restart toolstack on host %s" % self)
531532
self.ssh('xe-toolstack-restart')
532533
if verify:
533-
wait_for(self.is_enabled, "Wait for host enabled", timeout_secs=30 * 60)
534+
self.wait_for_xapi_enabled()
535+
536+
def wait_for_host_down(self, timeout_secs: int = 2 * 60) -> None:
537+
wait_for_not(
538+
lambda: commands.local_cmd(["ping", "-c1", self.hostname_or_ip], check=False).returncode == 0,
539+
"Wait for host down",
540+
timeout_secs=timeout_secs,
541+
retry_delay_secs=2,
542+
)
543+
544+
def wait_for_host_up(self, timeout_secs: int = 10 * 60) -> None:
545+
wait_for(
546+
lambda: commands.local_cmd(["ping", "-c1", self.hostname_or_ip], check=False).returncode == 0,
547+
"Wait for host up",
548+
timeout_secs=timeout_secs,
549+
retry_delay_secs=10,
550+
)
551+
552+
def wait_for_ssh_reachable(self, timeout_secs: int = 10 * 60) -> None:
553+
wait_for(
554+
lambda: commands.local_cmd(["nc", "-zw5", self.hostname_or_ip, "22"], check=False).returncode == 0,
555+
"Wait for ssh up on host",
556+
timeout_secs=timeout_secs,
557+
retry_delay_secs=5
558+
)
559+
560+
def wait_for_xapi_enabled(self, timeout_secs: int = 30 * 60) -> None:
561+
logging.info(f"Wait for XAPI to complete initialization on {self.hostname_or_ip}")
562+
self.ssh(f"xapi-wait-init-complete {timeout_secs}")
563+
assert self.is_enabled()
534564

535565
def is_enabled(self) -> bool:
536566
try:
@@ -654,12 +684,10 @@ def reboot(self, verify: bool = False) -> None:
654684
# error code. Instead, we schedule the reboot a few seconds later to let the ssh command return properly.
655685
self.ssh('systemd-run --on-active=2s reboot')
656686
if verify:
657-
wait_for(lambda: os.system(f"ping -c1 {self.hostname_or_ip} > /dev/null 2>&1"), "Wait for host down")
658-
wait_for(lambda: not os.system(f"ping -c1 {self.hostname_or_ip} > /dev/null 2>&1"),
659-
"Wait for host up", timeout_secs=10 * 60, retry_delay_secs=10)
660-
wait_for(lambda: not os.system(f"nc -zw5 {self.hostname_or_ip} 22"),
661-
"Wait for ssh up on host", timeout_secs=10 * 60, retry_delay_secs=5)
662-
wait_for(self.is_enabled, "Wait for XAPI to be ready", timeout_secs=30 * 60)
687+
self.wait_for_host_down()
688+
self.wait_for_host_up()
689+
self.wait_for_ssh_reachable()
690+
self.wait_for_xapi_enabled()
663691

664692
def management_network(self) -> str:
665693
return self.xe('network-list', {'bridge': self.inventory['MANAGEMENT_INTERFACE']}, minimal=True)

lib/pool.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from packaging import version
88

99
import lib.commands as commands
10-
from lib.common import HostAddress, _param_get, _param_set, safe_split, wait_for, wait_for_not
10+
from lib.common import HostAddress, _param_get, _param_set, safe_split, wait_for_not
1111
from lib.efi import EFIAuth
1212
from lib.host import Host
1313
from lib.sr import SR
@@ -30,10 +30,7 @@ def __init__(self, master_hostname_or_ip: HostAddress) -> None:
3030

3131
# wait for XAPI startup to be done, or we can get "Connection
3232
# refused (calling connect )" when calling self.hosts_uuids()
33-
wait_for(lambda: commands.ssh_with_result(master_hostname_or_ip,
34-
'xapi-wait-init-complete 60').returncode == 0,
35-
f"Wait for XAPI init to be complete on {master_hostname_or_ip}",
36-
timeout_secs=30 * 60)
33+
self.master.wait_for_xapi_enabled()
3734

3835
logging.info("Getting Pool info for %r", master_hostname_or_ip)
3936
for host_uuid in self.hosts_uuids():
@@ -294,7 +291,11 @@ def eject_host(self, host: Host) -> None:
294291
master.xe('pool-eject', {'host-uuid': host.uuid, 'force': True})
295292
wait_for_not(lambda: host.uuid in self.hosts_uuids(), f"Wait for host {host} to be ejected of pool {master}.")
296293
self.hosts = [h for h in self.hosts if h.uuid != host.uuid]
297-
wait_for(host.is_enabled, f"Wait for host {host} to restart in its own pool.", timeout_secs=10 * 60)
294+
# The ejected host should reboot
295+
host.wait_for_host_down()
296+
host.wait_for_host_up()
297+
host.wait_for_ssh_reachable()
298+
host.wait_for_xapi_enabled()
298299

299300
def network_named(self, network_name: str) -> str:
300301
return self.master.xe('network-list', {'name-label': network_name}, minimal=True)

tests/install/test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ def _test_firstboot(self, create_vms: list[VM], mode: str, *,
207207
# pool master must be reachable here
208208
pool = Pool(ip)
209209

210-
# wait for XAPI
211-
wait_for(pool.master.is_enabled, "Wait for XAPI to be ready", timeout_secs=30 * 60)
210+
# Master should be available now that the pool is instanciated
211+
assert pool.master.is_enabled()
212212

213213
if lsb_rel in ["8.2.1", "8.3.0", "8.4.0"]:
214214
SERVICES = ["control-domain-params-init",

0 commit comments

Comments
 (0)