forked from mtusi2026/mtusi2026lab3
108 lines
4.6 KiB
Python
108 lines
4.6 KiB
Python
"""Общие операции с репликами текущего Compose-проекта."""
|
|
|
|
from contextlib import contextmanager
|
|
from dataclasses import dataclass
|
|
import fcntl
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import ssl
|
|
import subprocess
|
|
import tempfile
|
|
import time
|
|
from urllib.error import HTTPError, URLError
|
|
from urllib.request import urlopen
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Replica:
|
|
"""Состояние контейнера API."""
|
|
|
|
id: str
|
|
image: str
|
|
instance_id: str
|
|
running: bool
|
|
health: str | None
|
|
|
|
|
|
class ComposeProject:
|
|
"""Работает только с выбранным Compose-проектом."""
|
|
|
|
def __init__(self) -> None:
|
|
self.command = ["docker", "compose", "--env-file", ".env"]
|
|
self.config = json.loads(self.run("config", "--format", "json"))
|
|
self.name = self.config["name"]
|
|
|
|
def docker(self, *args: str, timeout: int = 90, env=None) -> str:
|
|
"""Выполняет Docker-команду с ограничением времени."""
|
|
result = subprocess.run(
|
|
["docker", *args], cwd=ROOT, text=True, capture_output=True,
|
|
timeout=timeout, env={**os.environ, **(env or {})},
|
|
)
|
|
if result.returncode:
|
|
raise RuntimeError(result.stderr.strip() or result.stdout.strip())
|
|
return result.stdout.strip()
|
|
|
|
def run(self, *args: str, timeout: int = 90, env=None) -> str:
|
|
"""Выполняет команду Compose с текущими overrides."""
|
|
return self.docker(*self.command[1:], *args, timeout=timeout, env=env)
|
|
|
|
def replicas(self) -> list[Replica]:
|
|
"""Читает состояние API без окружения контейнеров."""
|
|
ids = self.run("ps", "--all", "--quiet", "api").splitlines()
|
|
if not ids:
|
|
return []
|
|
template = (
|
|
'{"id":{{json .Id}},"image":{{json .Image}},'
|
|
'"hostname":{{json .Config.Hostname}},"running":{{json .State.Running}},'
|
|
'"health":{{if .State.Health}}{{json .State.Health.Status}}{{else}}null{{end}}}'
|
|
)
|
|
rows = self.docker("inspect", "--format", template, *ids).splitlines()
|
|
configured = str(self.config["services"]["api"].get("environment", {}).get("INSTANCE_ID") or "").strip()
|
|
return [
|
|
Replica(row["id"], row["image"], configured or row["hostname"], row["running"], row["health"])
|
|
for row in map(json.loads, rows)
|
|
]
|
|
|
|
def wait_healthy(self, ids: set[str], timeout: float = 40) -> None:
|
|
"""Ожидает готовность конкретных реплик."""
|
|
deadline = time.monotonic() + timeout
|
|
while time.monotonic() < deadline:
|
|
current = {replica.id: replica for replica in self.replicas()}
|
|
if not ids <= current.keys():
|
|
raise RuntimeError("Контейнер API исчез во время развёртывания")
|
|
if all(current[id].running and current[id].health == "healthy" for id in ids):
|
|
return
|
|
if any(not current[id].running for id in ids):
|
|
raise RuntimeError("Контейнер API завершился до готовности")
|
|
time.sleep(0.25)
|
|
raise TimeoutError("API не стал healthy за отведённое время")
|
|
|
|
def wait_routed(self, instance_id: str, base_url: str, ca_file=None) -> None:
|
|
"""Ожидает ответа реплики через балансировщик."""
|
|
context = ssl.create_default_context(cafile=ca_file)
|
|
deadline = time.monotonic() + 40
|
|
while time.monotonic() < deadline:
|
|
try:
|
|
with urlopen(base_url.rstrip("/") + "/health/live", timeout=3, context=context) as response:
|
|
if response.status == 200 and response.headers.get("X-Instance-Id") == instance_id:
|
|
return
|
|
except (HTTPError, URLError, TimeoutError, OSError):
|
|
pass
|
|
time.sleep(0.1)
|
|
raise TimeoutError("Реплика не появилась в ответах балансировщика")
|
|
|
|
|
|
@contextmanager
|
|
def deployment_lock(project: ComposeProject):
|
|
"""Исключает параллельные изменения одного стенда."""
|
|
path = Path(tempfile.gettempdir()) / f"{project.name}-deployment.lock"
|
|
with path.open("a") as lock:
|
|
try:
|
|
fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
except BlockingIOError:
|
|
raise RuntimeError("Другой процесс уже изменяет этот Compose-проект") from None
|
|
yield
|