Реализовать lab3 на оценку 4: балансировка и отказ API

This commit is contained in:
Daria0w0
2026-10-23 17:14:32 +03:00
parent 0ab3a341aa
commit a67e7996ec
88 changed files with 5105 additions and 21 deletions
+38
View File
@@ -0,0 +1,38 @@
"""Explicit local Compose hooks. No credentials or session tokens are printed."""
import os
from pathlib import Path
import subprocess
import sys
ROOT = Path(__file__).resolve().parent.parent
def docker(*args, capture=False):
return subprocess.run(["docker", *args], cwd=ROOT, check=True,
text=True, capture_output=capture)
def main():
action = sys.argv[1]
if action == "restart":
# Stop all running application components before restarting shared stores.
docker("compose", "--env-file", ".env", "stop", "lb", "api", "redis", "postgres")
docker("compose", "--env-file", ".env", "up", "-d", "--wait", "--wait-timeout", "60")
elif action == "stop-one":
target = os.environ["TARGET_INSTANCE_ID"]
containers = docker("compose", "ps", "-q", "api", capture=True).stdout.split()
matches = []
for container in containers:
alias = docker("exec", container, "/server", "instance-id", capture=True).stdout.strip()
if alias == target:
matches.append(container)
if len(matches) != 1:
raise RuntimeError("Expected exactly one API container for target alias")
# One second grace keeps this explicit failure hook within the test budget.
docker("stop", "--time", "1", matches[0])
else:
raise ValueError("Unknown action")
if __name__ == "__main__":
main()