ЛР 5 — асинхронная обработка и версии pipeline
This commit is contained in:
Executable
+94
@@ -0,0 +1,94 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Student hook for teacher tests: explicit disposable Compose project only."""
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
PROJECT = os.environ.get("COMPOSE_PROJECT_NAME")
|
||||
API = os.environ.get("LAB5_TEST_API", "api")
|
||||
LB = os.environ.get("LAB5_TEST_LB", "lb")
|
||||
DB = os.environ.get("LAB5_TEST_DB", "postgres")
|
||||
STORES = os.environ.get("LAB5_TEST_STORES", "redis s3").split()
|
||||
|
||||
|
||||
def run(argv, timeout=30):
|
||||
result = subprocess.run(argv, cwd=ROOT, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=timeout)
|
||||
if result.returncode:
|
||||
raise RuntimeError("Docker command failed")
|
||||
return result.stdout.decode().strip()
|
||||
|
||||
|
||||
def main():
|
||||
if not PROJECT or PROJECT == "highload-lab5":
|
||||
raise RuntimeError("disposable COMPOSE_PROJECT_NAME is required")
|
||||
compose = ["docker", "compose", "--project-name", PROJECT]
|
||||
for file in json.loads(os.environ.get("LAB5_TEST_COMPOSE_FILES", "[]")):
|
||||
compose.extend(["--file", file])
|
||||
|
||||
def ids(service):
|
||||
return run(compose + ["ps", "-q", service]).split()
|
||||
|
||||
mode = os.environ.get("LAB5_HOOK_MODE")
|
||||
if mode in ("unready", "ready"):
|
||||
cid = os.environ.get("TARGET_CONTAINER_ID")
|
||||
if cid not in ids(API):
|
||||
# Compose ps abbreviates IDs whereas inspect returns the full ID.
|
||||
if not cid or not any(cid.startswith(i) for i in ids(API)):
|
||||
raise RuntimeError("selected API is not in the disposable project")
|
||||
net = PROJECT + "_default"
|
||||
if mode == "unready":
|
||||
run(["docker", "network", "disconnect", net, cid])
|
||||
else:
|
||||
current = json.loads(run(["docker", "inspect", "-f", "{{json .NetworkSettings.Networks}}", cid]))
|
||||
if net not in current:
|
||||
run(["docker", "network", "connect", "--alias", API, net, cid])
|
||||
return
|
||||
if mode == "stop-one":
|
||||
alias = os.environ.get("TARGET_INSTANCE_ID")
|
||||
matches = [i for i in ids(API) if run(["docker", "inspect", "-f", "{{.Config.Hostname}}", i]) == alias]
|
||||
if len(matches) != 1:
|
||||
raise RuntimeError("could not resolve exactly one running API alias")
|
||||
run(["docker", "stop", "--signal", "SIGTERM", "--time", "5", matches[0]], 10)
|
||||
return
|
||||
if os.environ.get("LAB5_HOOK_MODE") != "restart":
|
||||
raise RuntimeError("LAB5_HOOK_MODE must be restart or stop-one")
|
||||
|
||||
def restart(selected, budget):
|
||||
if not selected:
|
||||
raise RuntimeError("selected service is not running")
|
||||
run(["docker", "restart", "--time", "8", *selected], budget)
|
||||
|
||||
def healthy(selected):
|
||||
deadline = time.monotonic() + 25
|
||||
for cid in selected:
|
||||
while time.monotonic() < deadline:
|
||||
state = run(["docker", "inspect", "-f",
|
||||
"{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}", cid])
|
||||
if state in ("healthy", "running"):
|
||||
break
|
||||
time.sleep(0.25)
|
||||
else:
|
||||
raise RuntimeError("container did not become healthy")
|
||||
|
||||
stores = [ids(s) for s in (DB, *STORES)]
|
||||
if any(not g for g in stores):
|
||||
raise RuntimeError("missing configured persistence store")
|
||||
store_ids = [cid for group in stores for cid in group]
|
||||
apis = ids(API)
|
||||
restart(store_ids, 30)
|
||||
healthy(store_ids)
|
||||
restart(apis, 24)
|
||||
healthy(apis)
|
||||
restart(ids(LB), 12)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
main()
|
||||
except (RuntimeError, OSError, ValueError, subprocess.TimeoutExpired) as exc:
|
||||
print("Compose hook failed:", exc if isinstance(exc, RuntimeError) else type(exc).__name__, file=sys.stderr)
|
||||
sys.exit(1)
|
||||
Reference in New Issue
Block a user