forked from mtusi2026/mtusi2026lab3
35 lines
1.2 KiB
Python
35 lines
1.2 KiB
Python
"""Сопоставить X-Instance-Id реальному контейнеру и оставить его остановленным."""
|
|
|
|
import json
|
|
import os
|
|
import time
|
|
|
|
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"]
|
|
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")
|