This commit is contained in:
k00kzaAntonovaIra
2026-11-02 20:05:33 +03:00
parent 0ab3a341aa
commit e3ff3b25ce
145 changed files with 9724 additions and 473 deletions
+56 -32
View File
@@ -1,5 +1,6 @@
#!/usr/bin/env python3
"""Offline structural checks; full schema validation is a separate target."""
import ast
import json
from pathlib import Path
@@ -7,48 +8,71 @@ import re
ROOT = Path(__file__).resolve().parents[1]
def main():
lab = json.loads((ROOT/'lab.json').read_text())['number']
spec = json.loads((ROOT/'contracts/openapi.json').read_text())
assert spec['openapi']=='3.1.0'
assert spec['info']['version']=='1.%d.0'%lab
lab = json.loads((ROOT / "lab.json").read_text())["number"]
spec = json.loads((ROOT / "contracts/openapi.json").read_text())
assert spec["openapi"] == "3.1.0"
assert spec["info"]["version"] == "1.%d.0" % lab
operation_ids = set()
def walk(value):
if isinstance(value,dict):
if '$ref' in value:
pointer = value['$ref']
assert pointer.startswith('#/'), 'unexpected external reference'
if isinstance(value, dict):
if "$ref" in value:
pointer = value["$ref"]
assert pointer.startswith("#/"), "unexpected external reference"
target = spec
for part in pointer[2:].split('/'):
target = target[part.replace('~1','/').replace('~0','~')]
for part in pointer[2:].split("/"):
target = target[part.replace("~1", "/").replace("~0", "~")]
for v in value.values():
walk(v)
elif isinstance(value,list):
elif isinstance(value, list):
for v in value:
walk(v)
walk(spec)
for route,methods in spec['paths'].items():
placeholders = set(re.findall(r'\{([^}]+)\}',route))
for method,op in methods.items():
assert op['operationId'] not in operation_ids, 'duplicate operationId'
operation_ids.add(op['operationId'])
params = {p['name'] for p in op.get('parameters',[]) if p['in']=='path' and p.get('required')}
assert placeholders==params, 'path parameter mismatch: '+route
for response in op['responses'].values():
assert 'X-Request-Id' in response['headers']
if lab>=3:
assert 'X-Instance-Id' in response['headers']
assert ('/api/v1/auth/sessions' in spec['paths']) == (lab>=2)
assert ('/api/v1/attachments/{attachment_id}' in spec['paths']) == (lab>=4)
for file in list((ROOT/'tests').glob('*.py'))+list((ROOT/'scripts').glob('*.py')):
ast.parse(file.read_text(),filename=str(file))
for file in ROOT.rglob('*.json'):
if any(part in {'.git','.venv','node_modules','vendor','output'} for part in file.parts):
for route, methods in spec["paths"].items():
placeholders = set(re.findall(r"\{([^}]+)\}", route))
for method, op in methods.items():
assert op["operationId"] not in operation_ids, "duplicate operationId"
operation_ids.add(op["operationId"])
params = {
p["name"]
for p in op.get("parameters", [])
if p["in"] == "path" and p.get("required")
}
assert placeholders == params, "path parameter mismatch: " + route
for response in op["responses"].values():
assert "X-Request-Id" in response["headers"]
if lab >= 3:
assert "X-Instance-Id" in response["headers"]
assert ("/api/v1/auth/sessions" in spec["paths"]) == (lab >= 2)
assert ("/api/v1/attachments/{attachment_id}" in spec["paths"]) == (lab >= 4)
for file in list((ROOT / "tests").glob("*.py")) + list(
(ROOT / "scripts").glob("*.py")
):
ast.parse(file.read_text(encoding="utf-8"), filename=str(file))
for file in ROOT.rglob("*.json"):
if any(
part in {".git", ".venv", "node_modules", "vendor", "output"}
for part in file.parts
):
continue
json.loads(file.read_text())
for required in ('README.md','COURSE.md','CONTRIBUTING.md','REPORT.md','contracts/README.md','tests/smoke.py','tests/README.md'):
assert (ROOT/required).is_file(), 'missing '+required
print('PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.')
for required in (
"README.md",
"COURSE.md",
"CONTRIBUTING.md",
"REPORT.md",
"contracts/README.md",
"tests/smoke.py",
"tests/README.md",
):
assert (ROOT / required).is_file(), "missing " + required
print(
"PASS: offline skeleton structure, local refs and Python syntax; application NOT tested."
)
if __name__=='__main__':
if __name__ == "__main__":
main()
+90
View File
@@ -0,0 +1,90 @@
#!/usr/bin/env python3
"""Проверка срока жизни сессии, выхода и неверного пароля на запущенном API.
python scripts/check_expiry.py --base-url http://localhost:8081 --ttl 3
API должен работать с SESSION_TTL_SECONDS, равным --ttl.
"""
import argparse
import base64
import json
import sys
import time
import urllib.error
import urllib.request
import uuid
from datetime import datetime, timezone
def http(base, method, path, body=None, headers=None):
"""HTTP-запрос: (статус, заголовки, тело)."""
data = json.dumps(body).encode() if body is not None else None
request = urllib.request.Request(base + path, data=data, method=method, headers=dict(headers or {}))
if data is not None:
request.add_header("Content-Type", "application/json")
try:
with urllib.request.urlopen(request, timeout=10) as reply:
return reply.status, reply.headers, reply.read()
except urllib.error.HTTPError as error:
return error.code, error.headers, error.read()
def step(text, ok):
"""Напечатать результат шага; при неудаче завершить с кодом 1."""
print(("OK " if ok else "FAIL ") + text, flush=True)
if not ok:
sys.exit(1)
def main():
"""Выполнить проверку."""
parser = argparse.ArgumentParser()
parser.add_argument("--base-url", default="http://localhost:8081")
parser.add_argument("--ttl", type=float, default=3)
args = parser.parse_args()
base = args.base_url.rstrip("/")
username, password = "ttl_" + uuid.uuid4().hex[:8], "Expiry-" + uuid.uuid4().hex
basic = {"Authorization": "Basic " + base64.b64encode((username + ":" + password).encode()).decode()}
status, _, _ = http(base, "POST", "/api/v1/users",
{"username": username, "display_name": "TTL", "password": password})
step("регистрация -> %d" % status, status == 201)
status, _, raw = http(base, "POST", "/api/v1/auth/sessions", headers=basic)
session = json.loads(raw)
expires = datetime.fromisoformat(session["expires_at"].replace("Z", "+00:00"))
left = (expires - datetime.now(timezone.utc)).total_seconds()
step("вход -> %d, expires_at через %.1f с (TTL=%g)" % (status, left, args.ttl),
status == 201 and 0 < left <= args.ttl + 1)
bearer = {"Authorization": "Bearer " + session["token"]}
for n in range(3):
status, _, _ = http(base, "GET", "/api/v1/users/me", headers=bearer)
step("GET /users/me #%d до истечения -> %d" % (n + 1, status), status == 200)
time.sleep(args.ttl + 1)
status, headers, raw = http(base, "GET", "/api/v1/users/me", headers=bearer)
step("GET /users/me после TTL+1 с -> %d, WWW-Authenticate: %s, code=%s"
% (status, headers.get("WWW-Authenticate"), json.loads(raw)["error"]["code"]),
status == 401 and headers.get("WWW-Authenticate") == 'Bearer realm="messenger"')
status, _, raw = http(base, "POST", "/api/v1/auth/sessions", headers=basic)
bearer = {"Authorization": "Bearer " + json.loads(raw)["token"]}
step("новый вход -> %d" % status, status == 201)
status, _, _ = http(base, "DELETE", "/api/v1/auth/sessions/current", headers=bearer)
step("logout -> %d" % status, status == 204)
status, _, _ = http(base, "GET", "/api/v1/users/me", headers=bearer)
step("GET /users/me после logout -> %d" % status, status == 401)
status, _, _ = http(base, "DELETE", "/api/v1/auth/sessions/current", headers=bearer)
step("повторный logout -> %d" % status, status == 401)
wrong = {"Authorization": "Basic " + base64.b64encode((username + ":wrong-password-123").encode()).decode()}
status, headers, _ = http(base, "POST", "/api/v1/auth/sessions", headers=wrong)
step("вход с неверным паролем -> %d, WWW-Authenticate: %s" % (status, headers.get("WWW-Authenticate")),
status == 401 and headers.get("WWW-Authenticate", "").startswith("Basic"))
print("PASS: expiry (TTL=%g с), logout и неверный пароль на запущенном API" % args.ttl)
return 0
if __name__ == "__main__":
sys.exit(main())
+44
View File
@@ -0,0 +1,44 @@
#!/usr/bin/env python3
"""Статические ограничения стенда lab3; не заменяет docker compose config/nginx -t."""
import ast
import json
from pathlib import Path
import subprocess
import yaml
ROOT = Path(__file__).resolve().parents[1]
def main():
config = yaml.safe_load((ROOT / "compose.yaml").read_text())
services = config["services"]
assert {"api-a", "api-b", "migrate", "nginx", "db", "redis"} <= services.keys()
assert config["networks"]["data"]["internal"] is True
for name, service in services.items():
assert "container_name" not in service, name
assert float(service["cpus"]) > 0 and service["mem_limit"], name
if name != "nginx":
assert "ports" not in service, name
for name in ("api-a", "api-b"):
assert services[name]["depends_on"]["migrate"]["condition"] == "service_completed_successfully"
assert services[name]["stop_grace_period"] == "10s"
assert services["migrate"]["command"] == ["python", "-m", "app.migrate"]
assert services["nginx"]["image"] == "nginx:1.28.0-alpine"
assert services["nginx"]["networks"] == ["edge"]
assert not any("docker.sock" in str(s) for s in services.values())
spec = json.loads((ROOT / "contracts/openapi.json").read_text())
operation = spec["paths"]["/api/v1/chats/{chat_id}/messages"]["post"]
assert any(p["name"] == "Idempotency-Key" and p["required"] for p in operation["parameters"])
for directory in ("app", "scripts", "student-tests", "unit-tests", "tests"):
for path in (ROOT / directory).rglob("*.py"):
ast.parse(path.read_text(), filename=str(path))
for path in list((ROOT / "scripts").glob("*.sh")) + list((ROOT / "infra").rglob("*.sh")):
subprocess.run(["sh", "-n", str(path)], check=True)
for filename in ("scripts/stop-one.sh", "scripts/restart.sh"):
assert (ROOT / filename).stat().st_mode & 0o111
print("PASS: lab3 static structure, resource/port isolation, contract, Python/sh syntax; runtime NOT tested.")
if __name__ == "__main__":
main()
+4
View File
@@ -0,0 +1,4 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
exec python3 scripts/lab3_evidence.py "$@"
+415
View File
@@ -0,0 +1,415 @@
#!/usr/bin/env python3
"""Явные эксперименты lab3: останавливает и восстанавливает API, заменяет контейнеры."""
import argparse
import http.client
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime, timezone
import json
import os
from pathlib import Path
import platform
import subprocess
import sys
import threading
import time
import uuid
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "tests"))
from client import Client, Failure, check
from resilience import history
from replicas import (
COMPOSE,
SERVICES,
compose,
inventory,
probe,
replace,
undrain,
wait_balanced,
wait_ready,
)
def save_json(path, value):
path.write_text(json.dumps(value, ensure_ascii=False, indent=2) + "\n")
def record_command(out, name, command, env=None):
"""Записать реальный вывод команды; не запрашивать полный inspect или env."""
started = time.monotonic()
result = subprocess.run(command, cwd=ROOT, text=True, capture_output=True, env=env)
(out / (name + ".txt")).write_text(
"$ "
+ " ".join(command)
+ "\n"
+ result.stdout
+ result.stderr
+ f"\nexit_code={result.returncode}; elapsed_seconds={time.monotonic() - started:.3f}\n"
)
check(result.returncode == 0, name + " failed; inspect evidence file")
def observe(c, actor):
seen = set()
for _ in range(50):
try:
resp = c.call("GET", "/api/v1/users/me", actor=actor, schema="User")
instance_id = resp["headers"].get("X-Instance-Id")
if instance_id:
seen.add(instance_id)
except Exception:
pass
if len(seen) >= 2:
break
time.sleep(0.1)
check(len(seen) >= 2, "one session must be accepted by at least two API processes")
return sorted(seen)
def concurrent_replay(base):
"""Одновременный один ключ через LB; один id, одна строка истории, обе реплики."""
c = Client(base)
alice, bob, eve, chat = c.bootstrap()
key = uuid.uuid4().hex
barrier = threading.Barrier(12)
def worker(_):
local = Client(base)
barrier.wait()
return local.message(alice, chat, "одновременный повтор", key=key)
with ThreadPoolExecutor(max_workers=12) as pool:
responses = list(pool.map(worker, range(12)))
ids = {r["json"]["id"] for r in responses}
instances = {r["headers"]["X-Instance-Id"] for r in responses}
check(len(ids) == 1, "concurrent replay created different messages")
check(len(instances) >= 2, "replay did not cross two API processes")
rows = history(c, bob, chat)
check(
len(rows) == 1 and rows[0] == responses[0]["json"],
"history must contain one exact response",
)
c.call(
"POST",
"/api/v1/chats/" + chat["id"] + "/messages",
409,
actor=alice,
body={"text": "другое тело"},
headers={"Idempotency-Key": key},
)
replay = c.message(alice, chat, "одновременный повтор", key=key)
check(replay["json"] == responses[0]["json"], "409 changed original result")
return {
"parallel_requests": 12,
"unique_message_ids": len(ids),
"history_rows": len(rows),
"instances": sorted(instances),
"conflict_status": 409,
}
def experiment(base, service, mode, duration):
"""Closed-loop чтение/запись во время stop или поочерёдной замены."""
c = Client(base, timeout=2)
alice, bob, eve, chat = c.bootstrap()
initial = c.message(alice, chat, "подтверждено до действия")["json"]
seen = observe(c, alice)
before = inventory()
target = probe(service)["instance_id"]
events, confirmed = [], [initial]
lock = threading.Lock()
halt = threading.Event()
started = time.monotonic()
def worker(number):
local = Client(base, timeout=2)
key, text = uuid.uuid4().hex, ""
while not halt.is_set():
if not text:
text = "load-" + key
for operation in ("write", "read"):
t0 = time.monotonic()
try:
response = (
local.message(alice, chat, text, key=key)
if operation == "write"
else local.call(
"GET",
"/api/v1/chats/" + chat["id"] + "/messages?limit=1",
actor=bob,
schema="MessagePage",
)
)
event = {
"start": t0,
"end": time.monotonic(),
"kind": operation,
"ok": True,
"instance": response["headers"]["X-Instance-Id"],
}
with lock:
if operation == "write":
confirmed.append(response["json"])
if operation == "write":
key, text = uuid.uuid4().hex, ""
except (Failure, OSError, http.client.HTTPException):
event = {
"start": t0,
"end": time.monotonic(),
"kind": operation,
"ok": False,
}
with lock:
events.append(event)
time.sleep(0.02)
with ThreadPoolExecutor(max_workers=2) as pool:
futures = [pool.submit(worker, n) for n in range(2)]
time.sleep(1)
action_started = time.monotonic()
try:
if mode == "stop":
env = {**os.environ, "TARGET_INSTANCE_ID": target}
# Вызываем replicas.py stop-target напрямую через текущий интерпретатор Python
result = subprocess.run(
[sys.executable, str(ROOT / "scripts/replicas.py"), "stop-target"],
cwd=ROOT,
env=env,
text=True,
capture_output=True,
timeout=10,
)
check(result.returncode == 0, "stop hook failed")
action_detail = result.stdout
else:
action_detail = replace(service)
action_finished = time.monotonic()
after_action = inventory()
if mode == "stop":
stopped = next(row for row in after_action if row["service"] == service)
check(
stopped["state"] == "exited" and stopped["pid"] == 0,
"target process is still running",
)
deadline = max(action_started + duration, action_finished + 5)
while time.monotonic() < deadline:
time.sleep(0.1)
finally:
halt.set()
for future in futures:
future.result()
finished = time.monotonic()
rows = history(c, bob, chat)
by_id = {row["id"]: row for row in rows}
check(
all(by_id.get(message["id"]) == message for message in confirmed),
"confirmed data was lost",
)
texts = [row["text"] for row in rows]
check(len(texts) == len(set(texts)), "uncertain retry created duplicate text")
tail = [e for e in events if e["start"] >= action_finished]
check(any(e["ok"] and e["kind"] == "write" for e in tail), "no writes after action")
check(any(e["ok"] and e["kind"] == "read" for e in tail), "no reads after action")
if mode == "stop":
check(
all(e.get("instance") != target for e in tail if e["ok"]),
"stopped instance still responds",
)
for kind in ("write", "read"):
recovered = min(e["end"] for e in tail if e["ok"] and e["kind"] == kind)
check(recovered - action_started <= 10, kind + " recovery exceeded 10s")
def metrics(items, elapsed):
good = [e for e in items if e["ok"]]
failed = len(items) - len(good)
latencies = sorted((e["end"] - e["start"]) * 1000 for e in good)
def percentile(fraction):
return (
round(latencies[max(0, int(len(latencies) * fraction + 0.999) - 1)], 3)
if latencies
else None
)
return {
"elapsed_seconds": round(elapsed, 3),
"attempts": len(items),
"successes": len(good),
"errors": failed,
"error_ratio": round(failed / len(items), 6) if items else None,
"successful_rps": round(len(good) / elapsed, 3),
"successful_latency_ms": {
"p50": percentile(0.5),
"p95": percentile(0.95),
"p99": percentile(0.99),
},
}
result = {
"mode": mode,
"service": service,
"target_instance": target,
"observed_with_one_session": seen,
"before": before,
"after_action": after_action,
"action": action_detail,
"action_seconds": round(action_finished - action_started, 3),
"concurrency": 2,
"total": metrics(events, finished - started),
"during_and_after_action": metrics(
[e for e in events if e["start"] >= action_started],
finished - action_started,
),
"confirmed_responses": len(confirmed),
"history_messages": len(rows),
"duplicates": 0,
"existing_session_works": True,
}
for kind in ("write", "read"):
after = [e for e in tail if e["ok"] and e["kind"] == kind]
result["first_" + kind + "_after_action_seconds"] = round(
min(e["end"] for e in after) - action_started, 3
)
result[kind] = metrics(
[e for e in events if e["kind"] == kind], finished - started
)
return result
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--base-url", default="http://localhost:8080")
parser.add_argument("--output", default="evidence/lab3")
parser.add_argument("--duration", type=float, default=20)
args = parser.parse_args()
check(15 <= args.duration <= 120, "duration must be 15..120 seconds")
out = ROOT / args.output
out.mkdir(parents=True, exist_ok=True)
save_json(out / "result.json", {"status": "RUNNING"})
try:
os.environ["BASE_URL"] = args.base_url
save_json(
out / "00_environment.json",
{
"at_utc": datetime.now(timezone.utc).isoformat(),
"platform": platform.platform(),
"cpu_count": os.cpu_count(),
"python": platform.python_version(),
"duration": args.duration,
"workload": "2 closed-loop clients, POST+GET; uncertain POST retried with same key/body",
},
)
record_command(out, "01_docker", ["docker", "version"])
record_command(out, "02_compose_config", COMPOSE + ["config", "--quiet"])
wait_balanced()
save_json(out / "03_processes.json", inventory())
record_command(out, "03_limits", COMPOSE + ["ps", "-q"])
ids = compose("ps", "-q", capture=True).splitlines()
record_command(
out,
"03_container_limits",
[
"docker",
"inspect",
"--format",
"{{.Name}} cpus={{.HostConfig.NanoCpus}} memory={{.HostConfig.Memory}} ports={{json .HostConfig.PortBindings}}",
*ids,
],
)
record_command(
out,
"04_smoke",
[sys.executable, "tests/smoke.py", "--base-url", args.base_url],
)
save_json(
out / "05_concurrent_idempotency.json", concurrent_replay(args.base_url)
)
try:
for service in SERVICES:
wait_balanced()
report = experiment(args.base_url, service, "stop", args.duration)
save_json(out / ("06_failover_" + service + ".json"), report)
compose("up", "-d", "--no-deps", service)
wait_balanced()
record_command(
out,
"07_public_failover",
[
sys.executable,
"tests/resilience.py",
"--mode",
"failover",
"--action-script",
"scripts/stop-one.sh",
"--base-url",
args.base_url,
],
)
for service in SERVICES:
compose("up", "-d", "--no-deps", service)
wait_balanced()
for service in SERVICES:
save_json(
out / ("08_rolling_" + service + ".json"),
experiment(args.base_url, service, "replace", args.duration),
)
record_command(
out,
"09_persistence",
[
sys.executable,
"tests/resilience.py",
"--mode",
"persistence",
"--action-script",
"scripts/restart.sh",
"--base-url",
args.base_url,
],
)
wait_balanced()
save_json(out / "10_processes_after.json", inventory())
record_command(
out,
"11_nginx",
COMPOSE
+ ["exec", "-T", "nginx", "nginx", "-T", "-c", "/opt/lb/nginx.conf"],
)
record_command(
out,
"12_student_tests",
COMPOSE + ["--profile", "test", "run", "--rm", "--build", "tests"],
)
save_json(
out / "result.json",
{"status": "PASS", "at_utc": datetime.now(timezone.utc).isoformat()},
)
except Exception:
save_json(
out / "result.json",
{
"status": "FAIL",
"note": "Inspect completed evidence files; failed runs are not evidence of passing.",
},
)
raise
finally:
for service in SERVICES:
compose("up", "-d", "--no-deps", service)
undrain(service)
print("PASS: evidence saved to " + str(out.relative_to(ROOT)))
except BaseException:
save_json(
out / "result.json",
{
"status": "FAIL",
"note": "Run did not finish successfully; inspect completed outputs.",
},
)
raise
if __name__ == "__main__":
main()
+47
View File
@@ -0,0 +1,47 @@
#!/usr/bin/env python3
"""Сохранить только реально выполненные локальные проверки без запуска инфраструктуры."""
from datetime import datetime, timezone
import json
from pathlib import Path
import shutil
import subprocess
import sys
import time
ROOT = Path(__file__).resolve().parents[1]
def main():
out = ROOT / "evidence/local"
out.mkdir(parents=True, exist_ok=True)
checks = [
("01_check", [sys.executable, "scripts/check_contract.py"]),
("02_openapi", [sys.executable, "scripts/validate_openapi.py"]),
("03_static_lab3", [sys.executable, "scripts/check_lab3.py"]),
("04_unit", [sys.executable, "-m", "pytest", "-q", "unit-tests", "-p", "no:cacheprovider"]),
("05_collection", [sys.executable, "-m", "pytest", "--collect-only", "-q", "-p", "no:cacheprovider"]),
("06_dependencies", [sys.executable, "-m", "pip", "check"]),
]
manifest = []
for name, command in checks:
started = time.monotonic()
run = subprocess.run(command, cwd=ROOT, text=True, capture_output=True)
(out / (name + ".txt")).write_text("$ " + " ".join(command) + "\n" + run.stdout + run.stderr + "\nexit_code=" + str(run.returncode) + "\n")
manifest.append({"name": name, "command": command, "exit_code": run.returncode,
"elapsed_seconds": round(time.monotonic() - started, 3)})
print(name, "PASS" if run.returncode == 0 else "FAIL", flush=True)
(out / "manifest.json").write_text(json.dumps({"at_utc": datetime.now(timezone.utc).isoformat(),
"python": sys.version.split()[0], "checks": manifest}, indent=2) + "\n")
(out / "runtime_status.json").write_text(json.dumps({
"docker_cli_available": shutil.which("docker") is not None,
"nginx_binary_available": shutil.which("nginx") is not None,
"compose_nginx_postgresql_redis_experiments": "NOT_RUN",
"reason": "This local checker does not run infrastructure; execute make lab3-evidence separately",
"required_command_on_docker_host": "make lab3-evidence",
}, indent=2) + "\n")
return int(any(check["exit_code"] != 0 for check in manifest))
if __name__ == "__main__":
sys.exit(main())
+159
View File
@@ -0,0 +1,159 @@
#!/usr/bin/env python3
"""Управление двумя API без публичных backend-портов и без Docker socket в LB."""
import argparse
import json
import os
from pathlib import Path
import subprocess
import time
ROOT = Path(__file__).resolve().parents[1]
SERVICES = ("api-a", "api-b")
COMPOSE = ["docker", "compose", "--env-file", ".env"]
PROBE = "import json,urllib.request; r=urllib.request.urlopen('http://127.0.0.1:8080/health/ready',timeout=2); print(json.dumps({'status':r.status,'instance_id':r.headers['X-Instance-Id']}))"
def compose(*args, capture=False):
result = subprocess.run(
COMPOSE + list(args),
cwd=ROOT,
check=True,
text=True,
stdout=subprocess.PIPE if capture else None,
)
return result.stdout.strip() if capture else ""
def probe(service):
"""Получить alias из реального контейнера; credentials не выводятся."""
return json.loads(
compose("exec", "-T", service, "python", "-c", PROBE, capture=True)
)
def wait_ready(service, timeout=60):
deadline = time.monotonic() + timeout
while time.monotonic() < deadline:
try:
return probe(service)
except (subprocess.CalledProcessError, ValueError):
time.sleep(0.5)
raise RuntimeError(service + " did not become ready")
def inventory():
"""ID контейнера, PID host, restart count, alias и состояние без env/secrets."""
rows = []
for service in SERVICES:
cid = compose("ps", "-a", "-q", service, capture=True)
if not cid:
rows.append({"service": service, "state": "absent"})
continue
raw = subprocess.run(
["docker", "inspect", "--format", "{{json .State}}", cid],
check=True,
text=True,
capture_output=True,
)
state = json.loads(raw.stdout)
row = {
"service": service,
"container_id": cid,
"pid": state["Pid"],
"state": state["Status"],
"started_at": state["StartedAt"],
}
if state["Running"]:
row.update(probe(service))
rows.append(row)
return rows
def drain(service):
"""Исключить backend, дождаться обновления upstream и старых workers."""
compose("exec", "-T", "nginx", "touch", "/tmp/drain-" + service)
deadline = time.monotonic() + 8
while time.monotonic() < deadline:
config = compose(
"exec", "-T", "nginx", "cat", "/tmp/backends.conf", capture=True
)
if service + ":8080" not in config:
time.sleep(5.2)
return
time.sleep(0.2)
raise RuntimeError("LB did not drain " + service)
def undrain(service):
compose("exec", "-T", "nginx", "rm", "-f", "/tmp/drain-" + service)
def replace(service):
"""Заменить одну реплику при заведомо готовой второй."""
other = next(s for s in SERVICES if s != service)
wait_ready(other)
before = probe(service)["instance_id"]
drain(service)
try:
compose("up", "-d", "--no-deps", "--force-recreate", service)
after = wait_ready(service)["instance_id"]
if before == after:
raise RuntimeError("process alias did not change")
finally:
undrain(service)
wait_balanced()
return {"service": service, "before": before, "after": after}
def wait_balanced(timeout=30):
"""Убедиться, что бэкенд отвечает через LB."""
import urllib.request
deadline = time.monotonic() + timeout
base = os.environ.get("BASE_URL", "http://localhost:8080")
while time.monotonic() < deadline:
try:
req = urllib.request.Request(
base + "/health/ready", headers={"Connection": "close"}
)
with urllib.request.urlopen(req, timeout=2) as reply:
if reply.status == 200:
return # Проходим дальше, раз LB отвечает
except OSError:
pass
time.sleep(0.1)
raise RuntimeError("LB did not expose ready processes")
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"action", choices=["status", "stop-target", "restore", "rolling"]
)
args = parser.parse_args()
if args.action == "status":
print(json.dumps(inventory(), indent=2))
elif args.action == "stop-target":
target = os.environ.get("TARGET_INSTANCE_ID", "")
if not target:
raise RuntimeError("TARGET_INSTANCE_ID is required")
matches = [s for s in SERVICES if probe(s)["instance_id"] == target]
if len(matches) != 1:
raise RuntimeError("target must identify exactly one live process")
service = matches[0]
compose("stop", "--timeout", "6", service)
print(json.dumps({"stopped_service": service, "instance_id": target}))
elif args.action == "restore":
for service in SERVICES:
compose("up", "-d", "--no-deps", service)
undrain(service)
wait_balanced()
print(json.dumps(inventory(), indent=2))
else:
for service in SERVICES:
print(json.dumps(replace(service)))
if __name__ == "__main__":
main()
+8
View File
@@ -0,0 +1,8 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
docker compose --env-file .env stop nginx api-a api-b redis db
docker compose --env-file .env up -d --wait db redis
docker compose --env-file .env run --rm --no-deps migrate
docker compose --env-file .env up -d --no-deps --force-recreate api-a api-b
docker compose --env-file .env start nginx
+64
View File
@@ -0,0 +1,64 @@
#!/usr/bin/env python3
"""Выполнить команду и сохранить её вывод в файл вместе с датой, коммитом и кодом возврата.
python scripts/run_and_save.py evidence/01.txt -- python scripts/check_contract.py
"""
import os
import subprocess
import sys
import time
from datetime import datetime, timezone
from pathlib import Path
def git_commit(root: Path) -> str:
"""Короткий hash текущего коммита или пояснение, почему его нет."""
try:
out = subprocess.run(["git", "rev-parse", "--short", "HEAD"], cwd=str(root), capture_output=True,
text=True, timeout=5)
return out.stdout.strip() if out.returncode == 0 and out.stdout.strip() else "нет (git не используется)"
except Exception:
return "нет (git не найден)"
def main() -> int:
"""Выполнить команду после --, записать отчёт и вернуть её код возврата."""
argv = sys.argv[1:]
if len(argv) < 3 or argv[1] != "--":
print(__doc__)
return 2
target = Path(argv[0])
command = argv[2:]
root = Path(__file__).resolve().parents[1]
env = dict(os.environ, PYTHONIOENCODING="utf-8", PYTHONUTF8="1")
started = time.monotonic()
try:
done = subprocess.run(command, capture_output=True, env=env, cwd=str(root))
output = (done.stdout + done.stderr).decode("utf-8", errors="replace")
code = done.returncode
except FileNotFoundError:
output = "Команда не найдена: %s\n" % command[0]
code = 127
elapsed = time.monotonic() - started
text = "\n".join([
"# Дата (UTC): " + datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S"),
"# Commit: " + git_commit(root),
"# Команда: " + " ".join(command),
"# ---------------------------------------- вывод ----------------------------------------",
output.rstrip(),
"# ---------------------------------------------------------------------------------------",
"# Код возврата: %d" % code,
"# Время выполнения: %.2f с" % elapsed,
"",
])
target.parent.mkdir(parents=True, exist_ok=True)
target.write_text(text, encoding="utf-8")
sys.stdout.buffer.write(text.encode("utf-8", errors="replace"))
print("\n[сохранено в %s]" % target)
return code
if __name__ == "__main__":
sys.exit(main())
+30
View File
@@ -0,0 +1,30 @@
#!/usr/bin/env python3
"""Один вход и несколько запросов через LB; секреты не выводятся."""
import argparse
import json
from pathlib import Path
import sys
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests"))
from client import Client, check
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--base-url", default="http://localhost:8080")
args = parser.parse_args()
c = Client(args.base_url)
person = c.user("instances")
seen = []
for _ in range(20):
response = c.call("GET", "/api/v1/users/me", actor=person, schema="User")
check(response["json"] == person["user"], "same token returned another user")
seen.append(response["headers"]["X-Instance-Id"])
print(json.dumps({"login_count": 1, "user_id": person["id"], "request_instances": seen,
"distinct_instances": len(set(seen))}, indent=2))
check(len(set(seen)) >= 2, "less than two API processes observed")
if __name__ == "__main__":
main()
+4
View File
@@ -0,0 +1,4 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
exec python3 scripts/replicas.py stop-target