forked from mtusi2026/mtusi2026lab7
59 lines
2.7 KiB
Python
59 lines
2.7 KiB
Python
"""Failover hook logic for tests/resilience.py --mode failover (lab3, updated for lab7).
|
|
|
|
Called by scripts/stop-one.sh (Linux/macOS) and scripts/stop-one.cmd (Windows).
|
|
The test puts X-Instance-Id of the replica that served its first message into
|
|
TARGET_INSTANCE_ID. Since lab7 that header is a random per-process alias, not the
|
|
container hostname (contracts/README.md: "use an opaque replica alias, no hostname/IP").
|
|
Each API process writes its alias into /tmp/instance-id inside its own container
|
|
(tmpfs), so the operator -- who already has Docker access -- can map alias to
|
|
container, while an API client cannot. This script reads that file in every running
|
|
container of the "api" service of this Compose project and stops the matching one.
|
|
Postgres, Valkey, S3, the broker, OpenBao, the balancer and volumes are not touched.
|
|
The replica stays stopped; restore it with `make restore` or `docker start <id>`.
|
|
|
|
docker stop sends SIGTERM: the replica first answers 503 on /health/ready for
|
|
SHUTDOWN_DRAIN_SECONDS (HAProxy takes it out of rotation), then finishes started
|
|
requests and exits. --time 8: SIGKILL after 8 s at most, so the hook always returns
|
|
within the 10 s the test allows.
|
|
|
|
This file is plain ASCII on purpose: scripts/check_contract.py reads every
|
|
scripts/*.py with the default encoding, which is cp1251 on a Russian Windows.
|
|
"""
|
|
import os
|
|
import re
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
STOP_TIMEOUT_SECONDS = 8
|
|
ALIAS_FILE = os.environ.get("INSTANCE_ID_FILE", "/tmp/instance-id")
|
|
|
|
|
|
def alias_of(container: str) -> str:
|
|
result = subprocess.run(["docker", "exec", container, "cat", ALIAS_FILE],
|
|
capture_output=True, text=True)
|
|
return result.stdout.strip() if result.returncode == 0 else ""
|
|
|
|
|
|
def main() -> int:
|
|
target = os.environ.get("TARGET_INSTANCE_ID", "").strip()
|
|
# The value comes from an HTTP header: accept only our own alias format.
|
|
if not re.fullmatch(r"[A-Za-z0-9_.-]{1,64}", target):
|
|
print("TARGET_INSTANCE_ID must be a replica alias (X-Instance-Id)", file=sys.stderr)
|
|
return 2
|
|
running = subprocess.run(["docker", "compose", "ps", "-q", "api"], cwd=ROOT,
|
|
capture_output=True, text=True, check=True).stdout.split()
|
|
matches = [container for container in running if alias_of(container) == target]
|
|
if len(matches) != 1:
|
|
print("no running api container with alias " + target, file=sys.stderr)
|
|
return 1
|
|
subprocess.run(["docker", "stop", "--time", str(STOP_TIMEOUT_SECONDS), matches[0]],
|
|
cwd=ROOT, check=True, stdout=subprocess.DEVNULL)
|
|
print("stopped api container " + matches[0][:12])
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|