67 lines
1.4 KiB
Bash
Executable File
67 lines
1.4 KiB
Bash
Executable File
#!/bin/sh
|
|
set -eu
|
|
|
|
cd "$(dirname "$0")/.."
|
|
|
|
exec python3 - <<'PY'
|
|
import os
|
|
import subprocess
|
|
|
|
target = os.environ.get("TARGET_INSTANCE_ID")
|
|
if not target:
|
|
raise SystemExit("TARGET_INSTANCE_ID is required")
|
|
|
|
compose = ["./scripts/dc"]
|
|
|
|
containers = subprocess.check_output(
|
|
compose + ["ps", "-q", "api"],
|
|
text=True,
|
|
timeout=10,
|
|
).split()
|
|
|
|
if len(containers) < 2:
|
|
raise SystemExit("At least two running API containers are required")
|
|
|
|
probe = (
|
|
"import urllib.request; "
|
|
"r=urllib.request.urlopen("
|
|
"'http://127.0.0.1:8080/health/ready', timeout=2); "
|
|
"print(r.headers['X-Instance-Id'])"
|
|
)
|
|
|
|
matches = []
|
|
for container in containers:
|
|
instance = subprocess.check_output(
|
|
["docker", "exec", container, "python", "-c", probe],
|
|
text=True,
|
|
timeout=4,
|
|
).strip()
|
|
|
|
if instance == target:
|
|
matches.append(container)
|
|
|
|
if len(matches) != 1:
|
|
raise SystemExit("Target instance was not resolved uniquely")
|
|
|
|
container = matches[0]
|
|
print("Stopping target API container:", container[:12], flush=True)
|
|
|
|
subprocess.run(
|
|
["docker", "stop", "--time", "5", container],
|
|
check=True,
|
|
stdout=subprocess.DEVNULL,
|
|
timeout=15,
|
|
)
|
|
|
|
running = subprocess.check_output(
|
|
["docker", "inspect", "-f", "{{.State.Running}}", container],
|
|
text=True,
|
|
timeout=5,
|
|
).strip()
|
|
|
|
if running != "false":
|
|
raise SystemExit("Target API container is still running")
|
|
|
|
print("Target API container is stopped", flush=True)
|
|
PY
|