Files
mtusi2026lab7/scripts/compose_hook.py
T

55 lines
2.1 KiB
Python

"""Compose operator actions used by the public resilience tests."""
import argparse
import os
import subprocess
import sys
from pathlib import Path
from uuid import UUID
ROOT = Path(__file__).resolve().parents[1]
STORAGES = ("openbao", "postgres", "s3", "rabbitmq", "prometheus", "loki", "tempo", "grafana")
APP = ("api", "worker", "relay", "reconciler", "lb")
PROXIES = ("collector-scrape", "prometheus-tls", "loki-tls", "tempo-tls")
def run(*args, capture=False):
return subprocess.run(args, cwd=ROOT, check=True, text=True,
stdout=subprocess.PIPE if capture else None).stdout
def container_ids(*services):
return run("docker", "compose", "ps", "-q", *services, capture=True).splitlines()
def restart():
ids = container_ids(*STORAGES, *APP, "otel-collector", *PROXIES)
if not ids:
raise RuntimeError("Start the Compose stand before restarting it")
inspect = ("docker", "inspect", "--format", "{{.Name}} {{.State.Status}} {{.State.StartedAt}}", *ids)
run(*inspect)
run("docker", "compose", "restart", "-t", "5", *STORAGES, "otel-collector")
port = run("docker", "compose", "port", "openbao", "8200", capture=True).strip().rsplit(":", 1)[1]
run(sys.executable, "scripts/bao_operator.py", "unseal", "--address",
"https://localhost:" + port)
run("docker", "compose", "restart", "-t", "5", *PROXIES, *APP)
run(*inspect)
def stop_one():
target = str(UUID(os.environ.get("TARGET_INSTANCE_ID", "")))
matches = [
container for container in container_ids("api")
if run("docker", "exec", container, "cat", "/tmp/messenger-instance-id", capture=True).strip() == target
]
if len(matches) != 1:
raise RuntimeError("Expected exactly one running Compose API replica with the supplied alias")
run("docker", "stop", "-t", "3", matches[0])
run("docker", "inspect", "--format", "{{.Name}} {{.State.Status}} {{.State.FinishedAt}}", matches[0])
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("action", choices=("restart", "stop-one"))
{"restart": restart, "stop-one": stop_one}[parser.parse_args().action]()