Files
mtusi2026lab4/scripts/compose_actions.py
T

39 lines
1.4 KiB
Python

"""Explicit local Compose hooks. No credentials or session tokens are printed."""
import os
from pathlib import Path
import subprocess
import sys
ROOT = Path(__file__).resolve().parent.parent
def docker(*args, capture=False):
return subprocess.run(["docker", *args], cwd=ROOT, check=True,
text=True, capture_output=capture)
def main():
action = sys.argv[1]
if action == "restart":
# Stop all running application components before restarting shared stores.
docker("compose", "--env-file", ".env", "stop", "lb", "api", "redis", "postgres")
docker("compose", "--env-file", ".env", "up", "-d", "--wait", "--wait-timeout", "60")
elif action == "stop-one":
target = os.environ["TARGET_INSTANCE_ID"]
containers = docker("compose", "ps", "-q", "api", capture=True).stdout.split()
matches = []
for container in containers:
alias = docker("exec", container, "/server", "instance-id", capture=True).stdout.strip()
if alias == target:
matches.append(container)
if len(matches) != 1:
raise RuntimeError("Expected exactly one API container for target alias")
# One second grace keeps this explicit failure hook within the test budget.
docker("stop", "--time", "1", matches[0])
else:
raise ValueError("Unknown action")
if __name__ == "__main__":
main()