Repository navigation
Expand file tree
/
Copy pathwait_devices
More file actions
executable file
·131 lines (105 loc) · 4.61 KB
/
Copy pathwait_devices
File metadata and controls
executable file
·131 lines (105 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python3
"""Block until every lab device accepts SSH (TCP 22).
`docker compose up -d` returns once device containers are *started*, not once
their SSH server is actually listening. FRR comes up in a second or two, but
Nokia SR Linux takes tens of seconds to boot its management stack. Running
discovery before a device is reachable makes that host fail with a connection
error and drop out of the 15-device count.
This polls each device's TCP 22 until it accepts a connection, so callers like
`make local-lab-discover` wait for the actual readiness condition instead of a
fixed sleep. The host list defaults to the devices in the selected scenario's
topology — not the discovery parameters: a subnet target also contains unused
addresses which must never block readiness.
Usage
-----
./wait_devices # mgmt IPs from $SCENARIO's topology
./wait_devices --scenario frr-only # ... from another scenario
./wait_devices 172.30.0.11 10.0.0.5 # explicit IPs (optionally ip:port)
./wait_devices --params other.json # /32 subnets from a params file
Exit codes
----------
0 every device accepts TCP 22
2 timed out before all devices became reachable
"""
from __future__ import annotations
import argparse
import json
import pathlib
import socket
import sys
import time
import labscripts
# Which scenario is selected and where its topology lives is `gen_device_configs`'
# knowledge (via `resolve_scenario`), loaded rather than restated here.
gen_device_configs = labscripts.load("gen_device_configs")
def hosts_from_topology(scenario: str | None = None) -> list[str]:
"""Extract the `mgmt_ip` of every device in the scenario's topology."""
devices = gen_device_configs.load_topology(scenario)
return [d["mgmt_ip"] for d in devices.values() if d.get("mgmt_ip")]
def hosts_from_params(path: pathlib.Path) -> list[str]:
"""Extract the address of every /32 subnet in a discovery params file.
Wider prefixes are skipped: they enumerate candidate addresses, not known
devices, and waiting on unused addresses would always time out.
"""
data = json.loads(path.read_text())
hosts = []
for subnet in data.get("subnets", []):
cidr = subnet.get("cidr", "")
address, _, prefixlen = cidr.partition("/")
if address and (not prefixlen or prefixlen == "32"):
hosts.append(address)
return hosts
def parse_target(target: str) -> tuple[str, int]:
"""Split `ip` or `ip:port` into (ip, port); port defaults to 22."""
if ":" in target:
ip, port = target.rsplit(":", 1)
return ip, int(port)
return target, 22
def accepts_tcp(ip: str, port: int) -> bool:
try:
with socket.create_connection((ip, port), timeout=3):
return True
except OSError:
return False
def main() -> int:
p = argparse.ArgumentParser(
prog="wait_devices",
description="Block until every lab device accepts SSH (TCP 22).",
)
p.add_argument("hosts", nargs="*", help="Device IPs (or ip:port). Default: mgmt IPs from the scenario topology.")
p.add_argument("--scenario", help="scenario whose topology to read; defaults to $SCENARIO")
p.add_argument("--params", type=pathlib.Path, default=None, help="Discovery params JSON to read /32 IPs from.")
p.add_argument("--timeout", type=int, default=240, help="Total wait budget in seconds (default: 240)")
p.add_argument("--interval", type=float, default=2.0, help="Poll interval in seconds (default: 2.0)")
args = p.parse_args()
if args.hosts:
targets = args.hosts
elif args.params is not None:
targets = hosts_from_params(args.params)
else:
targets = hosts_from_topology(args.scenario)
if not targets:
print("no device hosts to wait for", file=sys.stderr)
return 0
pending = [parse_target(t) for t in targets]
deadline = time.monotonic() + args.timeout
started = time.monotonic()
while pending:
still_pending = []
for ip, port in pending:
if accepts_tcp(ip, port):
elapsed = int(time.monotonic() - started)
print(f" [{elapsed:3d}s] ready: {ip}:{port}")
else:
still_pending.append((ip, port))
pending = still_pending
if not pending:
return 0
if time.monotonic() >= deadline:
missing = ", ".join(f"{ip}:{port}" for ip, port in pending)
print(f"timeout: {missing} not reachable within {args.timeout}s", file=sys.stderr)
return 2
time.sleep(args.interval)
return 0
if __name__ == "__main__":
sys.exit(main())