83 lines
2.4 KiB
Python
83 lines
2.4 KiB
Python
"""Read-only discovery and scoped Compose operations for the local lab3 stack."""
|
|
|
|
import json
|
|
import subprocess
|
|
import time
|
|
import urllib.error
|
|
import urllib.request
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def compose(*args, env=None):
|
|
return subprocess.run(
|
|
["docker", "compose", "--env-file", ".env", *args],
|
|
cwd=ROOT,
|
|
env=env,
|
|
check=True,
|
|
capture_output=True,
|
|
text=True,
|
|
timeout=90,
|
|
).stdout
|
|
|
|
|
|
def inspect(container):
|
|
template = (
|
|
'{"id":{{json .Id}},"started_at":{{json .State.StartedAt}},'
|
|
'"pid":{{.State.Pid}},"running":{{json .State.Running}},'
|
|
'"image":{{json .Image}},"memory":{{.HostConfig.Memory}},'
|
|
'"nano_cpus":{{.HostConfig.NanoCpus}},'
|
|
'"published_ports":{{json .HostConfig.PortBindings}}}'
|
|
)
|
|
r = subprocess.run(
|
|
["docker", "inspect", "--format", template, container],
|
|
capture_output=True,
|
|
text=True,
|
|
check=True,
|
|
timeout=5,
|
|
)
|
|
return json.loads(r.stdout)
|
|
|
|
|
|
def instance(container):
|
|
code = (
|
|
"import urllib.request; r=urllib.request.urlopen("
|
|
"'http://127.0.0.1:8080/health/live',timeout=1); print(r.headers['X-Instance-Id'])"
|
|
)
|
|
r = subprocess.run(
|
|
["docker", "exec", container, "python", "-c", code],
|
|
capture_output=True,
|
|
text=True,
|
|
check=True,
|
|
timeout=3,
|
|
)
|
|
value = inspect(container)
|
|
value["instance"] = r.stdout.strip()
|
|
return value
|
|
|
|
|
|
def instances():
|
|
ids = compose("ps", "-q", "api").split()
|
|
with ThreadPoolExecutor(max_workers=8) as pool:
|
|
return list(pool.map(instance, ids))
|
|
|
|
|
|
def wait_lb(base="http://127.0.0.1:8083", count=2, timeout=45):
|
|
deadline, seen = time.monotonic() + timeout, set()
|
|
while time.monotonic() < deadline:
|
|
try:
|
|
current = {value["instance"] for value in instances()}
|
|
if len(current) != count:
|
|
time.sleep(0.2)
|
|
continue
|
|
with urllib.request.urlopen(base + "/health/ready", timeout=2) as response:
|
|
seen.add(response.headers["X-Instance-Id"])
|
|
if current <= seen:
|
|
return sorted(current)
|
|
except (OSError, urllib.error.URLError, subprocess.SubprocessError):
|
|
pass
|
|
time.sleep(0.1)
|
|
raise RuntimeError("Expected healthy API replicas were not observed through LB")
|