"""Local Compose actions for the public resilience tests.""" import os from pathlib import Path import re import subprocess import sys ROOT = Path(__file__).resolve().parents[1] COMPOSE = ["docker", "compose", "--env-file", str(ROOT / ".env")] def run(*args, capture=False): return subprocess.run(args, cwd=ROOT, check=True, text=True, capture_output=capture).stdout def stop_one(): target = os.environ.get("TARGET_INSTANCE_ID", "") if not re.fullmatch(r"[0-9a-f]{32}", target): raise SystemExit("Invalid TARGET_INSTANCE_ID") ids = run(*COMPOSE, "ps", "-q", "api", capture=True).splitlines() matches = [cid for cid in ids if run("docker", "exec", cid, "python", "-c", "import urllib.request;print(urllib.request.urlopen('http://127.0.0.1:8080/health/live').headers['X-Instance-Id'])", capture=True).strip() == target] if len(matches) != 1 or len(ids) < 2: raise SystemExit("Expected one matching API and at least two running replicas") print(f"Stopping API container {matches[0][:12]}", flush=True) run("docker", "stop", "--time", "2", matches[0]) def restart(): # Keep postgres volume; Redis is a disposable cache. Never remove volumes. run(*COMPOSE, "stop", "nginx", "api", "cleanup", "worker", "dispatcher") run(*COMPOSE, "restart", "postgres", "redis", "minio", "broker", "openbao", "collector", "prometheus", "loki", "tempo", "grafana") run(*COMPOSE, "restart", "broker-tls", "collector-tls", "collector-scrape-tls", "loki-tls", "tempo-query-tls", "tempo-ingest-tls") run(sys.executable, str(ROOT / "scripts/bootstrap_bao.py")) run(*COMPOSE, "up", "-d", "--wait", "--wait-timeout", "60") if __name__ == "__main__": if sys.argv[1:] == ["stop-one"]: stop_one() elif sys.argv[1:] == ["restart"]: restart() else: raise SystemExit("Use stop-one or restart")