46 lines
1.3 KiB
Python
46 lines
1.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Остановить одну API-реплику по TARGET_INSTANCE_ID (hostname контейнера)."""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
|
|
|
|
def main() -> int:
|
|
target = os.environ.get("TARGET_INSTANCE_ID", "").strip()
|
|
if not target:
|
|
print("TARGET_INSTANCE_ID required", file=sys.stderr)
|
|
return 1
|
|
out = subprocess.check_output(
|
|
[
|
|
"docker",
|
|
"ps",
|
|
"-q",
|
|
"--filter",
|
|
"label=com.docker.compose.project=mtusi-lab7",
|
|
"--filter",
|
|
"label=com.docker.compose.service=api",
|
|
],
|
|
text=True,
|
|
)
|
|
found = None
|
|
for line in out.splitlines():
|
|
cid = line.strip()
|
|
if not cid:
|
|
continue
|
|
host = subprocess.check_output(["docker", "inspect", "-f", "{{.Config.Hostname}}", cid], text=True).strip()
|
|
short = cid[:12]
|
|
if host == target or short == target:
|
|
found = cid
|
|
break
|
|
if not found:
|
|
print("api replica not found", file=sys.stderr)
|
|
return 1
|
|
subprocess.check_call(["docker", "stop", "--time", "5", found], stdout=subprocess.DEVNULL)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|