forked from mtusi2026/mtusi2026lab7
48 lines
1.8 KiB
Python
48 lines
1.8 KiB
Python
"""Operator hook for the unmodified public restart and failover scenarios."""
|
|
import json
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
|
|
from bao import PRIVATE, ROOT, compose, unseal
|
|
|
|
|
|
def inspect(ids):
|
|
command = ["docker", "inspect", "--format",
|
|
'{{json .Config.Hostname}} {{json .State.StartedAt}} {{json .State.Status}} {{json .Id}}', *ids]
|
|
result = subprocess.run(command, check=True, text=True, capture_output=True, cwd=ROOT)
|
|
return [json.loads("[" + ",".join(line.split()) + "]") for line in result.stdout.splitlines()]
|
|
|
|
|
|
def save(name, data):
|
|
directory = PRIVATE / "runtime"
|
|
directory.mkdir(exist_ok=True)
|
|
(directory / name).write_text(json.dumps(data, indent=2) + "\n", encoding="utf-8")
|
|
|
|
|
|
def restart():
|
|
stores = ("db", "valkey", "broker-store", "minio", "openbao",
|
|
"prometheus-engine", "loki-engine", "tempo-engine", "grafana")
|
|
processes = ("api", "worker", "dispatcher", "lb", "collector-engine")
|
|
ids = compose("ps", "-q", *stores, *processes).split()
|
|
save("restart-before.json", inspect(ids))
|
|
compose("restart", "--no-deps", *stores)
|
|
unseal()
|
|
compose("restart", "--no-deps", *processes)
|
|
save("restart-after.json", inspect(ids))
|
|
|
|
|
|
def stop_one(target=None):
|
|
target = target or os.environ["TARGET_INSTANCE_ID"]
|
|
for node in inspect(compose("ps", "-q", "api").split()):
|
|
if node[0] == target and node[2] == "running":
|
|
save("stop-before.json", node)
|
|
subprocess.run(["docker", "stop", "--time", "7", node[3]], check=True, capture_output=True, cwd=ROOT)
|
|
save("stop-after.json", inspect([node[3]])[0])
|
|
return
|
|
raise RuntimeError("requested instance was not a running API replica")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
{"restart": restart, "stop-one": stop_one}[sys.argv[1]]()
|