Files
mtusi2026lab2/scripts/restart.py
T
Otto 44b6ec1052
Skeleton contracts / contracts (pull_request) Canceled after 0s
Lab 2: PostgreSQL storage and Redis sessions
2026-10-19 23:40:31 +03:00

35 lines
1.2 KiB
Python

"""Restart the complete lab stack; keep volumes and record actual start times."""
import subprocess
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
SERVICES = ["api", "postgres", "redis"]
def snapshot(filename):
ids = subprocess.check_output(
["docker", "compose", "ps", "--all", "-q", *SERVICES], cwd=ROOT, text=True
).split()
if len(ids) != 3:
raise RuntimeError("Expected one api, postgres and redis container")
template = ('{{index .Config.Labels "com.docker.compose.service"}} '
'{{.Id}} {{.State.StartedAt}} {{.State.Status}}')
lines = subprocess.check_output(
["docker", "inspect", "--format", template, *ids], cwd=ROOT, text=True
)
(ROOT / "evidence").mkdir(exist_ok=True)
(ROOT / "evidence" / filename).write_text(lines, encoding="utf-8")
def main():
snapshot("restart-before.txt")
subprocess.run(["docker", "compose", "stop", *SERVICES], cwd=ROOT, check=True, timeout=45)
subprocess.run(["docker", "compose", "up", "-d", "--wait", "api"],
cwd=ROOT, check=True, timeout=40)
snapshot("restart-after.txt")
if __name__ == "__main__":
main()