forked from mtusi2026/mtusi2026lab3
30 lines
1.0 KiB
Python
30 lines
1.0 KiB
Python
import os
|
|
import re
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
STOP_TIMEOUT_SECONDS = 8
|
|
|
|
|
|
def main() -> int:
|
|
target = os.environ.get("TARGET_INSTANCE_ID", "").strip().lower()
|
|
if not re.fullmatch(r"[0-9a-f]{12,64}", target):
|
|
print("TARGET_INSTANCE_ID must be a container id (X-Instance-Id)", file=sys.stderr)
|
|
return 2
|
|
running = subprocess.run(["docker", "compose", "ps", "-q", "api"], cwd=ROOT,
|
|
capture_output=True, text=True, check=True).stdout.split()
|
|
matches = [container for container in running if container.startswith(target)]
|
|
if len(matches) != 1:
|
|
print(f"no running api container with id {target}", file=sys.stderr)
|
|
return 1
|
|
subprocess.run(["docker", "stop", "--time", str(STOP_TIMEOUT_SECONDS), matches[0]],
|
|
cwd=ROOT, check=True, stdout=subprocess.DEVNULL)
|
|
print(f"stopped api container {matches[0][:12]}")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|