forked from mtusi2026/mtusi2026lab4
34 lines
1.0 KiB
Python
34 lines
1.0 KiB
Python
#!/usr/bin/env python3
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
|
|
|
|
def main():
|
|
target = os.environ.get("TARGET_INSTANCE_ID")
|
|
if not target:
|
|
print("[stop-one] TARGET_INSTANCE_ID not set", file=sys.stderr)
|
|
return 1
|
|
|
|
names = subprocess.run(
|
|
["docker", "ps", "--filter", "name=mtusi2026lab4-api-", "--format", "{{.Names}}"],
|
|
capture_output=True, text=True, check=True,
|
|
).stdout.split()
|
|
|
|
for container in names:
|
|
logs = subprocess.run(
|
|
["docker", "logs", container],
|
|
capture_output=True, text=True, check=False,
|
|
)
|
|
combined = logs.stdout + logs.stderr
|
|
if f"INSTANCE_ID={target}" in combined:
|
|
print(f"[stop-one] stopping {container} (instance={target})", flush=True)
|
|
subprocess.run(["docker", "stop", "--time=2", container], check=True)
|
|
return 0
|
|
|
|
print(f"[stop-one] no container found for INSTANCE_ID={target}", file=sys.stderr)
|
|
return 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main()) |