Files
mtusi2026lab7/scripts/stop-one.py
T
2026-11-20 15:30:33 +03:00

39 lines
1.6 KiB
Python

"""Сопоставить X-Instance-Id реальному контейнеру и оставить его остановленным."""
import json
import os
import time
from pathlib import Path
from replicas import containers, describe, docker, instance_id
target = os.environ["TARGET_INSTANCE_ID"]
running = [c for c in containers() if describe(c)["Running"]]
if len(running) < 2:
raise SystemExit("Need at least two running API containers")
for container in running:
if instance_id(container) == target:
before = describe(container)
started = time.monotonic()
docker("stop", "--time", "10", container)
after = describe(container)
assert not after["Running"]
event = {"container": container[:12], "instance_id": target, "started_at": before["StartedAt"], "finished_at": after["FinishedAt"], "stop_seconds": round(time.monotonic()-started,3), "exit_code": after["ExitCode"]}
path=Path(__file__).resolve().parents[1]/"evidence/failover-events.jsonl"
with path.open("a") as stream:stream.write(json.dumps(event)+"\n")
print(
json.dumps(
{
"container": container[:12],
"instance_id": target,
"started_at": before["StartedAt"],
"finished_at": after["FinishedAt"],
"stop_seconds": round(time.monotonic() - started, 3),
"exit_code": after["ExitCode"],
}
)
)
break
else:
raise SystemExit("Requested instance not found in this Compose project")