forked from mtusi2026/mtusi2026lab4
Перенесён стенд lab3 (API, Redis-сессии, идемпотентность, HAProxy, реплики API). Добавлены приватный bucket SeaweedFS с отдельным ключом API, загрузка PNG/JPEG с лимитами и полной проверкой, компенсация и reconciler, плавная остановка API, тесты и evidence.
131 lines
5.4 KiB
Python
131 lines
5.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Whole-stack restart: `docker compose down` (volumes kept) → `up` → same token reads the same file.
|
|
|
|
Stronger than scripts/restart.*: every container (API, LB, PostgreSQL, Redis, SeaweedFS,
|
|
s3-gw, one-shot bootstrap jobs) is removed and recreated; only named volumes survive.
|
|
Usage: python scripts/full_restart_evidence.py (writes evidence/full-restart.json)
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import base64
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
import urllib.error
|
|
import urllib.request
|
|
import uuid
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / "student-tests"))
|
|
import imagegen # noqa: E402
|
|
|
|
BASE = os.environ.get("BASE_URL", "http://127.0.0.1:8080").rstrip("/")
|
|
COMPOSE = ["docker", "compose", "--env-file", ".env"]
|
|
|
|
|
|
def sh(args) -> str:
|
|
return subprocess.run(args, cwd=ROOT, capture_output=True, text=True, encoding="utf-8", check=True).stdout
|
|
|
|
|
|
def request(method, path, body=None, token=None, raw=None, headers=None):
|
|
h = {"Accept": "application/json"}
|
|
if token:
|
|
h["Authorization"] = "Bearer " + token
|
|
if body is not None:
|
|
raw = json.dumps(body).encode()
|
|
h["Content-Type"] = "application/json"
|
|
h.update(headers or {})
|
|
req = urllib.request.Request(BASE + path, data=raw, headers=h, method=method)
|
|
try:
|
|
resp = urllib.request.urlopen(req, timeout=10)
|
|
except urllib.error.HTTPError as err:
|
|
resp = err
|
|
return resp.code, resp.headers, resp.read()
|
|
|
|
|
|
def actor(label):
|
|
username = f"rs_{label}_{uuid.uuid4().hex[:10]}"
|
|
password = "T3st-" + uuid.uuid4().hex
|
|
status, _, data = request("POST", "/api/v1/users", {"username": username, "display_name": label, "password": password})
|
|
assert status == 201
|
|
cred = base64.b64encode(f"{username}:{password}".encode()).decode()
|
|
status, _, data2 = request("POST", "/api/v1/auth/sessions", headers={"Authorization": "Basic " + cred})
|
|
assert status == 201
|
|
return json.loads(data)["id"], json.loads(data2)["token"]
|
|
|
|
|
|
def snapshot():
|
|
rows = sh(COMPOSE + ["ps", "-a", "--format", "{{.Service}} {{.ID}} {{.State}}"]).splitlines()
|
|
return sorted(rows)
|
|
|
|
|
|
def main():
|
|
alice_id, alice = actor("a")
|
|
bob_id, bob = actor("b")
|
|
status, _, data = request("POST", "/api/v1/chats", {"title": "restart", "member_ids": [alice_id, bob_id]}, token=alice)
|
|
chat_id = json.loads(data)["id"]
|
|
payload = imagegen.rgb_png(600, 400, noise=True)
|
|
status, _, body = request("POST", f"/api/v1/chats/{chat_id}/attachments?filename=keep.png", token=alice,
|
|
raw=payload, headers={"Content-Type": "image/png"})
|
|
assert status == 201, status
|
|
att = json.loads(body)
|
|
status, _, msg = request("POST", f"/api/v1/chats/{chat_id}/messages", {"text": "переживёт restart\u0000ok", "attachment_ids": [att["id"]]},
|
|
token=alice, headers={"Idempotency-Key": uuid.uuid4().hex})
|
|
assert status == 201
|
|
message = json.loads(msg)
|
|
before = snapshot()
|
|
volumes = sh(["docker", "volume", "ls", "--filter", "label=com.docker.compose.project=mtusi2026lab4", "--format", "{{.Name}}"]).split()
|
|
t0 = time.monotonic()
|
|
sh(COMPOSE + ["down"])
|
|
down_s = time.monotonic() - t0
|
|
after_down = snapshot()
|
|
sh(COMPOSE + ["up", "-d", "--scale", "api=2"])
|
|
deadline = time.monotonic() + 180
|
|
while True:
|
|
try:
|
|
if request("GET", "/health/ready")[0] == 200 and request("GET", "/api/v1/users/me", token=bob)[0] == 200:
|
|
break
|
|
except OSError:
|
|
pass
|
|
if time.monotonic() > deadline:
|
|
raise SystemExit("FAIL: stack not ready after restart")
|
|
time.sleep(1)
|
|
recovery_s = time.monotonic() - t0
|
|
after = snapshot()
|
|
meta = json.loads(request("GET", f"/api/v1/attachments/{att['id']}", token=bob)[2])
|
|
status, h, content = request("GET", f"/api/v1/attachments/{att['id']}/content", token=bob)
|
|
history = json.loads(request("GET", f"/api/v1/chats/{chat_id}/messages?limit=100", token=bob)[2])
|
|
s3_init_log = sh(COMPOSE + ["logs", "--no-log-prefix", "s3-init"]).strip().splitlines()[-3:]
|
|
result = {
|
|
"date": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
|
|
"volumes_kept": volumes,
|
|
"containers_before": before,
|
|
"containers_after_down": after_down,
|
|
"containers_after_up": after,
|
|
"down_seconds": round(down_s, 1),
|
|
"down_to_ready_seconds": round(recovery_s, 1),
|
|
"same_token_works": True,
|
|
"metadata_unchanged": meta == att,
|
|
"download_status": status,
|
|
"sha256_match": hashlib.sha256(content).hexdigest() == att["sha256"] == hashlib.sha256(payload).hexdigest(),
|
|
"message_preserved": any(m == message for m in history["items"]),
|
|
"s3_init_rerun_log_tail": s3_init_log,
|
|
}
|
|
ok = result["metadata_unchanged"] and status == 200 and result["sha256_match"] and result["message_preserved"]
|
|
result["passed"] = ok
|
|
out = ROOT / "evidence" / "full-restart.json"
|
|
out.write_text(json.dumps(result, ensure_ascii=False, indent=1), encoding="utf-8")
|
|
print(json.dumps({k: result[k] for k in ("down_to_ready_seconds", "metadata_unchanged", "download_status",
|
|
"sha256_match", "message_preserved", "passed")}))
|
|
print("WROTE", out)
|
|
return 0 if ok else 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|