forked from mtusi2026/mtusi2026lab7
109 lines
3.1 KiB
Python
109 lines
3.1 KiB
Python
import json
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
|
|
from bootstrap import PRIVATE, ROOT, write
|
|
from provision import bao, compose
|
|
|
|
|
|
def snapshot():
|
|
ids = compose("ps", "-a", "-q").splitlines()
|
|
result = {}
|
|
for item in json.loads(subprocess.check_output(["docker", "inspect", *ids])):
|
|
name = item["Config"]["Labels"]["com.docker.compose.service"]
|
|
if (
|
|
"com.docker.compose.oneoff" in item["Config"]["Labels"]
|
|
and item["Config"]["Labels"]["com.docker.compose.oneoff"] == "True"
|
|
):
|
|
continue
|
|
result[item["Name"].lstrip("/")] = dict(
|
|
service=name,
|
|
started_at=item["State"]["StartedAt"],
|
|
image=item["Image"],
|
|
status=item["State"]["Status"],
|
|
)
|
|
return result
|
|
|
|
|
|
def unseal():
|
|
init = json.loads((PRIVATE / "operator/bao-init.json").read_text())
|
|
for _ in range(20):
|
|
try:
|
|
bao("sys/unseal", {"key": init["keys"][0]})
|
|
return
|
|
except Exception:
|
|
time.sleep(1)
|
|
raise RuntimeError("Operator unseal failed")
|
|
|
|
|
|
def restart():
|
|
before = snapshot()
|
|
compose("restart", "openbao")
|
|
unseal()
|
|
compose("restart", "gateway")
|
|
roles = (
|
|
"db",
|
|
"redis",
|
|
"broker",
|
|
"minio",
|
|
"otel-collector",
|
|
"prometheus",
|
|
"loki",
|
|
"tempo",
|
|
"grafana",
|
|
"api-agent",
|
|
"worker-agent",
|
|
"dispatcher-agent",
|
|
"api",
|
|
"worker",
|
|
"dispatcher",
|
|
"lb",
|
|
)
|
|
compose("restart", "--timeout", "5", *roles)
|
|
after = snapshot()
|
|
changed = {
|
|
name: row["started_at"] != before[name]["started_at"]
|
|
for name, row in after.items()
|
|
if name in before
|
|
}
|
|
if not changed or not all(changed.values()):
|
|
raise RuntimeError("Not all intended containers restarted")
|
|
write(
|
|
ROOT / "evidence/restart-containers.json",
|
|
json.dumps(
|
|
dict(before=before, after=after, all_start_times_changed=True, volumes_preserved=True),
|
|
indent=2,
|
|
)
|
|
+ "\n",
|
|
)
|
|
|
|
|
|
def stop_one():
|
|
target = os.environ["TARGET_INSTANCE_ID"]
|
|
selected = []
|
|
for identifier in compose("ps", "-q", "api").splitlines():
|
|
alias = (
|
|
subprocess.check_output(["docker", "exec", identifier, "cat", "/tmp/instance-id"])
|
|
.decode()
|
|
.strip()
|
|
)
|
|
if alias == target:
|
|
selected.append(identifier)
|
|
if len(selected) != 1:
|
|
raise RuntimeError("Instance alias did not resolve to exactly one running replica")
|
|
before = snapshot()
|
|
subprocess.run(["docker", "stop", "--time", "1", selected[0]], capture_output=True, check=True)
|
|
history = ROOT / "evidence/failover-containers.json"
|
|
events = json.loads(history.read_text()) if history.exists() else []
|
|
events.append(
|
|
dict(alias=target, stopped_container=selected[0], before=before, after=snapshot())
|
|
)
|
|
write(history, json.dumps(events, indent=2) + "\n")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
{"restart": restart, "stop-one": stop_one, "unseal": unseal}[sys.argv[1]]()
|