Implement lab4 S3 attachments and reliability checks
This commit is contained in:
Executable
+71
@@ -0,0 +1,71 @@
|
||||
#!/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 = [
|
||||
"docker", "compose",
|
||||
"-f", "compose.yaml",
|
||||
"-f", "compose.s3.yaml",
|
||||
"--env-file", ".env",
|
||||
]
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user