feat: implement lab 5 queued image processing
This commit is contained in:
Executable
+818
@@ -0,0 +1,818 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Операторские команды стенда лабы 5 (свои, не skeleton). Python 3.9+, stdlib.
|
||||
|
||||
python3 scripts/lab5.py env
|
||||
.env из .env.example со случайными паролями и ключами S3 (make env);
|
||||
несекретные переменные, заданные в окружении (`make env
|
||||
HOST_PORT=8180`), берутся оттуда — так стенд встаёт рядом с чужим
|
||||
на других портах. Существующий .env не перезаписывается,
|
||||
дописываются только переменные шаблона, которых в нём нет.
|
||||
Печатает только имена;
|
||||
python3 scripts/lab5.py instances [--base-url URL] [--requests 12]
|
||||
вход одним синтетическим пользователем и серия GET /users/me с одним
|
||||
токеном через LB: X-Instance-Id каждого ответа и итог по репликам
|
||||
(шаг 1 сценария защиты лабы 3);
|
||||
python3 scripts/lab5.py lb-stats
|
||||
слоты HAProxy рабочего стенда: адрес, состояние проверки, запросы в
|
||||
полёте, всего, ошибки соединения, повторы, перенаправления;
|
||||
python3 scripts/lab5.py failover-each [--base-url URL] [--replicas 2]
|
||||
[--mode stop|kill]
|
||||
make test-failover для каждой реплики по очереди: перед прогоном все
|
||||
реплики возвращены (make replicas), тест останавливает реплику,
|
||||
ответившую на его первую запись; если это реплика, которую уже
|
||||
проверили, очередь LB сдвигается одним запросом и прогон
|
||||
повторяется. Печатает docker compose ps до и после, строку
|
||||
STOP_ONE_LOG и итог теста — это evidence «какой контейнер
|
||||
действительно остановлен»;
|
||||
python3 scripts/lab5.py attachment-failover [--base-url URL]
|
||||
[--replicas 2] [--mode stop|kill]
|
||||
загрузка tests/fixtures/sample.png через LB (202, затем ready),
|
||||
сообщение со ссылкой, остановка именно реплики, принявшей загрузку
|
||||
(scripts/stop-one.sh по X-Instance-Id ответа), скачивание вторым
|
||||
участником через другую реплику — тот же SHA-256, 404 постороннему;
|
||||
затем реплики возвращаются;
|
||||
python3 scripts/lab5.py defense [--base-url URL]
|
||||
сценарий защиты лабы 5, шаги 2–3: worker остановлен → загрузка 202 →
|
||||
сообщение со ссылкой 201 → content 409, текст 201 → scripts/restart.sh
|
||||
(LB, реплики, PostgreSQL, Valkey, S3, брокер) → worker запущен → ready,
|
||||
оригинал и thumbnail 256×160. Печатает id и StartedAt без токенов;
|
||||
python3 scripts/lab5.py pipeline
|
||||
задания по состояниям, возраст старейшего, сообщения в очереди;
|
||||
python3 scripts/lab5.py s3-check
|
||||
приватность bucket вне API (make s3-check): bootstrap в режиме
|
||||
check (права ключей API, worker и уборщика, чужой bucket,
|
||||
административные запросы), анонимные запросы с хоста и из сети
|
||||
стенда, порты контейнера S3 из сети, затем cleanup. Таблица «где →
|
||||
проверка → ожидаемо → получено», код 1 при нарушении.
|
||||
|
||||
Пароли синтетических пользователей и токены живут только в памяти и не
|
||||
печатаются. Работает с проектом compose репозитория (compose.yaml и .env)
|
||||
или с тем, что задают COMPOSE_PROJECT_NAME / COMPOSE_FILE /
|
||||
COMPOSE_ENV_FILES (стенды student-tests/stand.py).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import base64
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import secrets
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
import uuid
|
||||
from collections import Counter
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
FIXTURE = ROOT / "tests" / "fixtures" / "sample.png"
|
||||
LB_STATS = (
|
||||
"exec 3<>/dev/tcp/127.0.0.1/8404 && "
|
||||
"printf 'GET /stats;csv HTTP/1.0\\r\\n\\r\\n' >&3 && cat <&3"
|
||||
)
|
||||
STAT_FIELDS = ("addr", "status", "scur", "stot", "econ", "eresp", "wretr", "wredis")
|
||||
# пустые значения этих переменных шаблона — секреты: make env их генерирует
|
||||
SECRET_SUFFIXES = ("_PASSWORD", "_KEY")
|
||||
# порты API S3 и gRPC шлюза: единственные, что открыты в сеть стенда
|
||||
S3_NETWORK_PORTS = ("8333", "18333")
|
||||
|
||||
|
||||
def call(base: str, method: str, path: str, headers=None, body=None, raw=None):
|
||||
data = json.dumps(body).encode() if body is not None else raw
|
||||
request = urllib.request.Request(base + path, data=data, method=method)
|
||||
for key, value in (headers or {}).items():
|
||||
request.add_header(key, value)
|
||||
if body is not None:
|
||||
request.add_header("Content-Type", "application/json")
|
||||
try:
|
||||
with urllib.request.urlopen(request, timeout=10) as response:
|
||||
return response.status, dict(response.headers), response.read()
|
||||
except urllib.error.HTTPError as error:
|
||||
return error.code, dict(error.headers), error.read()
|
||||
|
||||
|
||||
def register(base: str, label: str = "ops") -> dict:
|
||||
"""Синтетический пользователь с паролем и входом: id и токен."""
|
||||
username = f"{label}_" + secrets.token_hex(6)
|
||||
password = secrets.token_urlsafe(18)
|
||||
status, _, data = call(
|
||||
base,
|
||||
"POST",
|
||||
"/api/v1/users",
|
||||
body={"username": username, "display_name": "Оператор", "password": password},
|
||||
)
|
||||
if status != 201:
|
||||
raise SystemExit(f"register -> {status}")
|
||||
user_id = json.loads(data)["id"]
|
||||
basic = base64.b64encode(f"{username}:{password}".encode()).decode()
|
||||
status, _, data = call(
|
||||
base,
|
||||
"POST",
|
||||
"/api/v1/auth/sessions",
|
||||
headers={"Authorization": "Basic " + basic},
|
||||
)
|
||||
if status != 201:
|
||||
raise SystemExit(f"login -> {status}")
|
||||
return {"id": user_id, "token": json.loads(data)["token"]}
|
||||
|
||||
|
||||
def login(base: str) -> str:
|
||||
return register(base)["token"]
|
||||
|
||||
|
||||
def bearer(actor: dict) -> dict:
|
||||
return {"Authorization": "Bearer " + actor["token"]}
|
||||
|
||||
|
||||
def instance_of(headers: dict) -> str:
|
||||
return headers.get("X-Instance-Id") or headers.get("x-instance-id") or "-"
|
||||
|
||||
|
||||
# --- env ------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _assignments(text: str) -> list[tuple[str, str]]:
|
||||
pairs = []
|
||||
for line in text.splitlines():
|
||||
match = re.match(r"([A-Z][A-Z0-9_]*)=(.*)$", line)
|
||||
if match:
|
||||
pairs.append((match.group(1), match.group(2)))
|
||||
return pairs
|
||||
|
||||
|
||||
def _value(name: str, value: str) -> str:
|
||||
"""Значение для нового .env: секрет — случайный, несекретная переменная
|
||||
из окружения (`make env HOST_PORT=8180`) — как задано, иначе шаблон."""
|
||||
if name.endswith(SECRET_SUFFIXES):
|
||||
# ключи S3 и пароли — случайный hex; длина ключа S3 — 64 символа
|
||||
return value or secrets.token_hex(32 if name.startswith("S3_") else 16)
|
||||
return os.environ.get(name, value)
|
||||
|
||||
|
||||
def env(_args: argparse.Namespace) -> int:
|
||||
template = (ROOT / ".env.example").read_text(encoding="utf-8")
|
||||
target = ROOT / ".env"
|
||||
if not target.exists():
|
||||
lines = []
|
||||
generated = []
|
||||
for line in template.splitlines():
|
||||
match = re.match(r"([A-Z][A-Z0-9_]*)=(.*)$", line)
|
||||
if match:
|
||||
value = _value(match.group(1), match.group(2))
|
||||
if value != match.group(2) and match.group(1).endswith(SECRET_SUFFIXES):
|
||||
generated.append(match.group(1))
|
||||
line = f"{match.group(1)}={value}"
|
||||
lines.append(line)
|
||||
target.write_text("\n".join(lines) + "\n", encoding="utf-8")
|
||||
target.chmod(0o600)
|
||||
print(f".env created; generated: {' '.join(generated) or '-'}")
|
||||
return 0
|
||||
existing = {name for name, _ in _assignments(target.read_text(encoding="utf-8"))}
|
||||
missing = [(n, v) for n, v in _assignments(template) if n not in existing]
|
||||
if missing:
|
||||
with target.open("a", encoding="utf-8") as handle:
|
||||
handle.write("\n# added by make env from .env.example\n")
|
||||
for name, value in missing:
|
||||
handle.write(f"{name}={_value(name, value)}\n")
|
||||
print("added to existing .env: " + " ".join(name for name, _ in missing))
|
||||
else:
|
||||
print(".env ready")
|
||||
return 0
|
||||
|
||||
|
||||
def instances(args: argparse.Namespace) -> int:
|
||||
token = login(args.base_url)
|
||||
seen: Counter = Counter()
|
||||
print(f"one session, {args.requests} x GET /api/v1/users/me via {args.base_url}")
|
||||
for number in range(1, args.requests + 1):
|
||||
status, headers, _ = call(
|
||||
args.base_url,
|
||||
"GET",
|
||||
"/api/v1/users/me",
|
||||
headers={"Authorization": "Bearer " + token},
|
||||
)
|
||||
instance = headers.get("X-Instance-Id") or headers.get("x-instance-id") or "-"
|
||||
seen[instance] += 1
|
||||
print(f" {number:2d}: {status} X-Instance-Id={instance}")
|
||||
print(f"replicas seen: {len(seen)} {dict(seen)}")
|
||||
return 0 if len(seen) >= 2 else 1
|
||||
|
||||
|
||||
def compose(*args: str, check: bool = True) -> subprocess.CompletedProcess:
|
||||
return subprocess.run(
|
||||
["docker", "compose", *args],
|
||||
cwd=ROOT,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=check,
|
||||
)
|
||||
|
||||
|
||||
def lb_rows() -> list[dict]:
|
||||
done = compose("exec", "-T", "haproxy", "bash", "-c", LB_STATS)
|
||||
lines = [
|
||||
line for line in done.stdout.splitlines() if line.startswith(("# ", "api,"))
|
||||
]
|
||||
header = lines[0][2:].split(",")
|
||||
rows = []
|
||||
for line in lines[1:]:
|
||||
# zip(strict=) — только с Python 3.10, скрипт работает и на 3.9
|
||||
row = dict(zip(header, line.split(","))) # noqa: B905
|
||||
if row["svname"] in ("BACKEND", "FRONTEND") or not row.get("addr"):
|
||||
continue
|
||||
rows.append(row)
|
||||
return rows
|
||||
|
||||
|
||||
def lb_stats(_args: argparse.Namespace) -> int:
|
||||
print("slot " + " ".join(f"{field:>12}" for field in STAT_FIELDS))
|
||||
for row in lb_rows():
|
||||
print(
|
||||
f"{row['svname']:<9} "
|
||||
+ " ".join(f"{row.get(f, ''):>12}" for f in STAT_FIELDS)
|
||||
)
|
||||
return 0
|
||||
|
||||
|
||||
def wait_lb_up(replicas: int, timeout: float = 30) -> bool:
|
||||
"""Все реплики UP в LB: `--wait` compose ждёт только healthcheck
|
||||
контейнера, а слот вернувшейся реплики включается после проверок LB."""
|
||||
deadline = time.monotonic() + timeout
|
||||
while time.monotonic() < deadline:
|
||||
up = [row for row in lb_rows() if row.get("status") == "UP"]
|
||||
if len(up) >= replicas:
|
||||
return True
|
||||
time.sleep(0.5)
|
||||
return False
|
||||
|
||||
|
||||
def ps() -> str:
|
||||
return compose(
|
||||
"ps", "-a", "--format", "table {{.Name}}\t{{.ID}}\t{{.State}}\t{{.Status}}"
|
||||
).stdout
|
||||
|
||||
|
||||
def failover_each(args: argparse.Namespace) -> int:
|
||||
done: set[str] = set()
|
||||
log = Path(tempfile.mkdtemp(prefix="stop-one-")) / "stop-one.log"
|
||||
env = {**os.environ, "STOP_ONE_LOG": str(log), "STOP_MODE": args.mode}
|
||||
attempt = 0
|
||||
while len(done) < args.replicas and attempt < args.replicas * 4:
|
||||
attempt += 1
|
||||
replicas = compose(
|
||||
"up",
|
||||
"-d",
|
||||
"--no-deps",
|
||||
"--no-recreate",
|
||||
"--wait",
|
||||
"--scale",
|
||||
f"api={args.replicas}",
|
||||
"api",
|
||||
check=False,
|
||||
)
|
||||
if replicas.returncode != 0 or not wait_lb_up(args.replicas):
|
||||
print(replicas.stderr or "replicas are not UP in the LB")
|
||||
return 2
|
||||
# сдвиг очереди round-robin: тест останавливает реплику, ответившую
|
||||
# на его первую запись; с каждой попыткой сдвиг на запрос больше
|
||||
for _ in range(attempt - 1):
|
||||
call(args.base_url, "GET", "/health/live")
|
||||
print(f"=== attempt {attempt}: docker compose ps before\n{ps()}", flush=True)
|
||||
before = log.read_text().splitlines() if log.exists() else []
|
||||
test = subprocess.run(
|
||||
[
|
||||
sys.executable,
|
||||
"tests/resilience.py",
|
||||
"--mode",
|
||||
"failover",
|
||||
"--action-script",
|
||||
"scripts/stop-one.sh",
|
||||
"--base-url",
|
||||
args.base_url,
|
||||
],
|
||||
cwd=ROOT,
|
||||
env=env,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
)
|
||||
print(
|
||||
"$ make test-failover ACTION_SCRIPT=scripts/stop-one.sh "
|
||||
f"(STOP_MODE={args.mode})"
|
||||
)
|
||||
print(test.stdout.strip())
|
||||
lines = log.read_text().splitlines() if log.exists() else []
|
||||
new = lines[len(before) :]
|
||||
print("STOP_ONE_LOG: " + (new[-1] if new else "(no line)"))
|
||||
print(f"docker compose ps after\n{ps()}", flush=True)
|
||||
if test.returncode != 0:
|
||||
return test.returncode
|
||||
stopped = new[-1].split("container=")[1].split()[0][:12] if new else ""
|
||||
if stopped:
|
||||
done.add(stopped)
|
||||
compose(
|
||||
"up",
|
||||
"-d",
|
||||
"--no-deps",
|
||||
"--no-recreate",
|
||||
"--wait",
|
||||
"--scale",
|
||||
f"api={args.replicas}",
|
||||
"api",
|
||||
)
|
||||
print(f"replicas stopped one by one: {sorted(done)}; all restored:\n{ps()}")
|
||||
return 0 if len(done) >= args.replicas else 1
|
||||
|
||||
|
||||
# --- вложение и отказ принявшей реплики ---------------------------------------
|
||||
|
||||
|
||||
def wait_status(
|
||||
base: str, actor: dict, attachment_id: str, wanted: tuple, timeout: float = 60
|
||||
) -> dict:
|
||||
"""Опрос метаданных, пока статус не войдёт в `wanted`."""
|
||||
deadline = time.monotonic() + timeout
|
||||
while True:
|
||||
status, _, data = call(
|
||||
base, "GET", f"/api/v1/attachments/{attachment_id}", headers=bearer(actor)
|
||||
)
|
||||
if status == 200:
|
||||
meta = json.loads(data)
|
||||
if meta["status"] in wanted:
|
||||
return meta
|
||||
if time.monotonic() > deadline:
|
||||
raise SystemExit(f"attachment did not reach {wanted} in {timeout:.0f} s")
|
||||
time.sleep(0.3)
|
||||
|
||||
|
||||
def restore_replicas(replicas: int) -> None:
|
||||
compose(
|
||||
"up",
|
||||
"-d",
|
||||
"--no-deps",
|
||||
"--no-recreate",
|
||||
"--wait",
|
||||
"--scale",
|
||||
f"api={replicas}",
|
||||
"api",
|
||||
)
|
||||
wait_lb_up(replicas)
|
||||
|
||||
|
||||
def attachment_failover(args: argparse.Namespace) -> int:
|
||||
base = args.base_url
|
||||
restore_replicas(args.replicas)
|
||||
alice, bob, eve = (register(base, label) for label in ("alice", "bob", "eve"))
|
||||
status, _, data = call(
|
||||
base,
|
||||
"POST",
|
||||
"/api/v1/chats",
|
||||
headers=bearer(alice),
|
||||
body={"title": "Отказ реплики", "member_ids": [alice["id"], bob["id"]]},
|
||||
)
|
||||
if status != 201:
|
||||
raise SystemExit(f"create chat -> {status}")
|
||||
chat = json.loads(data)["id"]
|
||||
content = FIXTURE.read_bytes()
|
||||
expected = hashlib.sha256(content).hexdigest()
|
||||
status, headers, data = call(
|
||||
base,
|
||||
"POST",
|
||||
f"/api/v1/chats/{chat}/attachments?filename=sample.png",
|
||||
headers={**bearer(alice), "Content-Type": "image/png"},
|
||||
raw=content,
|
||||
)
|
||||
if status != 202:
|
||||
raise SystemExit(f"upload -> {status} {data[:200]!r}")
|
||||
attachment = json.loads(data)
|
||||
uploader = instance_of(headers)
|
||||
print(
|
||||
f"upload: 202 {attachment['status']} X-Instance-Id={uploader} "
|
||||
f"size={attachment['size_bytes']} sha256={attachment['sha256'][:16]}… "
|
||||
f"(fixture {expected[:16]}…)"
|
||||
)
|
||||
ready = wait_status(base, alice, attachment["id"], ("ready", "failed"))
|
||||
print(f"processed: {ready['status']} variants={len(ready['variants'])}")
|
||||
status, headers, data = call(
|
||||
base,
|
||||
"POST",
|
||||
f"/api/v1/chats/{chat}/messages",
|
||||
headers={**bearer(alice), "Idempotency-Key": uuid.uuid4().hex},
|
||||
body={"text": "", "attachment_ids": [attachment["id"]]},
|
||||
)
|
||||
print(
|
||||
f"message with attachment: {status} attachment_ids="
|
||||
f"{json.loads(data).get('attachment_ids') if status == 201 else '-'}"
|
||||
)
|
||||
log = Path(tempfile.mkdtemp(prefix="stop-one-")) / "stop-one.log"
|
||||
print(f"=== docker compose ps before\n{ps()}", flush=True)
|
||||
stop = subprocess.run(
|
||||
["scripts/stop-one.sh"],
|
||||
cwd=ROOT,
|
||||
env={
|
||||
**os.environ,
|
||||
"TARGET_INSTANCE_ID": uploader,
|
||||
"STOP_MODE": args.mode,
|
||||
"STOP_ONE_LOG": str(log),
|
||||
},
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
)
|
||||
print(f"$ TARGET_INSTANCE_ID={uploader} STOP_MODE={args.mode} scripts/stop-one.sh")
|
||||
stopped = log.read_text().strip() if log.exists() else "-"
|
||||
print(f"exit {stop.returncode}; STOP_ONE_LOG: {stopped}")
|
||||
if stop.returncode != 0:
|
||||
print(stop.stderr.strip())
|
||||
return 1
|
||||
# LB исключает реплику за ≤ 2,5 с; ответы ещё могут прийти от неё —
|
||||
# ждём ответа другой реплики не дольше 10 с
|
||||
deadline = time.monotonic() + 10
|
||||
started = time.monotonic()
|
||||
result = None
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
status, headers, data = call(
|
||||
base,
|
||||
"GET",
|
||||
f"/api/v1/attachments/{attachment['id']}/content",
|
||||
headers=bearer(bob),
|
||||
)
|
||||
except OSError:
|
||||
status, headers, data = 0, {}, b""
|
||||
if status == 200 and instance_of(headers) != uploader:
|
||||
result = (status, headers, data)
|
||||
break
|
||||
time.sleep(0.2)
|
||||
ok = result is not None
|
||||
if result is not None:
|
||||
status, headers, data = result
|
||||
digest = hashlib.sha256(data).hexdigest()
|
||||
ok = digest == expected and instance_of(headers) != uploader
|
||||
kind = headers.get("content-type") or headers.get("Content-Type")
|
||||
print(
|
||||
f"download by bob after stop: {status} "
|
||||
f"X-Instance-Id={instance_of(headers)} "
|
||||
f"after {time.monotonic() - started:.2f}s, bytes={len(data)}, "
|
||||
f"sha256 {'== fixture' if digest == expected else '!= fixture'}, "
|
||||
f"content-type={kind}"
|
||||
)
|
||||
else:
|
||||
print("download by bob after stop: no 200 from another replica in 10 s")
|
||||
for suffix in ("", "/content"):
|
||||
status, _, _ = call(
|
||||
base,
|
||||
"GET",
|
||||
f"/api/v1/attachments/{attachment['id']}{suffix}",
|
||||
headers=bearer(eve),
|
||||
)
|
||||
print(f"outsider GET /attachments/{{id}}{suffix}: {status}")
|
||||
ok = ok and status == 404
|
||||
print(f"=== docker compose ps after\n{ps()}", flush=True)
|
||||
restore_replicas(args.replicas)
|
||||
print(f"replicas restored:\n{ps()}")
|
||||
print("PASS" if ok else "FAIL")
|
||||
return 0 if ok else 1
|
||||
|
||||
|
||||
# --- лаба 5: сценарий защиты и состояние pipeline ------------------------------
|
||||
|
||||
|
||||
def started_at(service: str) -> str:
|
||||
ids = compose("ps", "-q", service, check=False).stdout.split()
|
||||
if not ids:
|
||||
return f"{service}: not running"
|
||||
done = subprocess.run(
|
||||
["docker", "inspect", "-f", "{{.Name}} {{.State.StartedAt}}", *ids],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
)
|
||||
return "; ".join(line.strip().lstrip("/") for line in done.stdout.splitlines())
|
||||
|
||||
|
||||
def jpeg_size(data: bytes) -> tuple:
|
||||
"""Размеры JPEG по маркеру SOF (как tests/image_probe.py)."""
|
||||
i = 2
|
||||
while i + 9 < len(data):
|
||||
if data[i] != 0xFF:
|
||||
i += 1
|
||||
continue
|
||||
marker = data[i + 1]
|
||||
length = int.from_bytes(data[i + 2 : i + 4], "big")
|
||||
if marker in (0xC0, 0xC1, 0xC2):
|
||||
height = int.from_bytes(data[i + 5 : i + 7], "big")
|
||||
width = int.from_bytes(data[i + 7 : i + 9], "big")
|
||||
return width, height
|
||||
i += 2 + length
|
||||
return (0, 0)
|
||||
|
||||
|
||||
def defense(args: argparse.Namespace) -> int:
|
||||
base = args.base_url
|
||||
services = ("api", "postgres", "valkey", "s3", "rabbitmq", "worker", "relay")
|
||||
print("$ docker compose stop worker")
|
||||
compose("stop", "worker")
|
||||
ok = True
|
||||
try:
|
||||
alice, bob = (register(base, label) for label in ("alice", "bob"))
|
||||
status, _, data = call(
|
||||
base,
|
||||
"POST",
|
||||
"/api/v1/chats",
|
||||
headers=bearer(alice),
|
||||
body={"title": "Защита", "member_ids": [alice["id"], bob["id"]]},
|
||||
)
|
||||
chat = json.loads(data)["id"]
|
||||
content = FIXTURE.read_bytes()
|
||||
status, _, data = call(
|
||||
base,
|
||||
"POST",
|
||||
f"/api/v1/chats/{chat}/attachments?filename=sample.png",
|
||||
headers={**bearer(alice), "Content-Type": "image/png"},
|
||||
raw=content,
|
||||
)
|
||||
attachment = json.loads(data)
|
||||
print(f"worker stopped; upload -> {status} {attachment.get('status')}")
|
||||
print(f"attachment_id={attachment.get('id')}")
|
||||
ok = ok and status == 202
|
||||
status, _, data = call(
|
||||
base,
|
||||
"POST",
|
||||
f"/api/v1/chats/{chat}/messages",
|
||||
headers={**bearer(alice), "Idempotency-Key": uuid.uuid4().hex},
|
||||
body={"text": "", "attachment_ids": [attachment["id"]]},
|
||||
)
|
||||
print(f"message with queued attachment -> {status}")
|
||||
ok = ok and status == 201
|
||||
status, _, _ = call(
|
||||
base,
|
||||
"GET",
|
||||
f"/api/v1/attachments/{attachment['id']}/content",
|
||||
headers=bearer(bob),
|
||||
)
|
||||
print(f"content while worker is stopped -> {status}")
|
||||
ok = ok and status == 409
|
||||
status, _, _ = call(
|
||||
base,
|
||||
"POST",
|
||||
f"/api/v1/chats/{chat}/messages",
|
||||
headers={**bearer(bob), "Idempotency-Key": uuid.uuid4().hex},
|
||||
body={"text": "текст без вложения"},
|
||||
)
|
||||
print(f"plain text message meanwhile -> {status}")
|
||||
ok = ok and status == 201
|
||||
print("StartedAt before restart:")
|
||||
for service in services:
|
||||
print(" " + started_at(service))
|
||||
restart = subprocess.run(
|
||||
["scripts/restart.sh"],
|
||||
cwd=ROOT,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
)
|
||||
print(f"$ scripts/restart.sh -> exit {restart.returncode}")
|
||||
ok = ok and restart.returncode == 0
|
||||
deadline = time.monotonic() + 90
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
if call(base, "GET", "/health/ready")[0] == 200:
|
||||
break
|
||||
except OSError:
|
||||
pass
|
||||
time.sleep(0.5)
|
||||
meta = wait_status(base, bob, attachment["id"], ("queued", "processing"), 30)
|
||||
print(f"after restart, worker still stopped: {meta['status']}")
|
||||
finally:
|
||||
print("$ docker compose start worker")
|
||||
compose("up", "-d", "--no-deps", "worker")
|
||||
meta = wait_status(base, bob, attachment["id"], ("ready", "failed"))
|
||||
print(f"after worker start: {meta['status']} error_code={meta['error_code']}")
|
||||
ok = ok and meta["status"] == "ready"
|
||||
status, _, data = call(
|
||||
base,
|
||||
"GET",
|
||||
f"/api/v1/attachments/{attachment['id']}/content",
|
||||
headers=bearer(bob),
|
||||
)
|
||||
same = hashlib.sha256(data).hexdigest() == hashlib.sha256(content).hexdigest()
|
||||
print(f"original -> {status}, sha256 {'== fixture' if same else '!= fixture'}")
|
||||
ok = ok and status == 200 and same
|
||||
status, _, data = call(
|
||||
base,
|
||||
"GET",
|
||||
f"/api/v1/attachments/{attachment['id']}/content?variant=thumbnail",
|
||||
headers=bearer(bob),
|
||||
)
|
||||
print(f"thumbnail -> {status}, JPEG {jpeg_size(data)}")
|
||||
ok = ok and status == 200 and jpeg_size(data) == (256, 160)
|
||||
print("StartedAt after:")
|
||||
for service in services:
|
||||
print(" " + started_at(service))
|
||||
print("PASS" if ok else "FAIL")
|
||||
return 0 if ok else 1
|
||||
|
||||
|
||||
PIPELINE_SQL = (
|
||||
"SELECT state, count(*), "
|
||||
"coalesce(round(extract(epoch FROM now() - min(created_at))::numeric, 1), 0) "
|
||||
"FROM attachment_jobs GROUP BY state ORDER BY state"
|
||||
)
|
||||
|
||||
|
||||
def pipeline(_args: argparse.Namespace) -> int:
|
||||
db = _env_value("POSTGRES_DB", "messenger")
|
||||
user = _env_value("POSTGRES_USER", "messenger")
|
||||
done = compose(
|
||||
"exec",
|
||||
"-T",
|
||||
"postgres",
|
||||
"psql",
|
||||
"-U",
|
||||
user,
|
||||
"-d",
|
||||
db,
|
||||
"-At",
|
||||
"-F",
|
||||
" ",
|
||||
"-c",
|
||||
PIPELINE_SQL,
|
||||
check=False,
|
||||
)
|
||||
print("state count oldest_s")
|
||||
print(done.stdout.strip() or done.stderr.strip())
|
||||
queues = compose(
|
||||
"exec",
|
||||
"-T",
|
||||
"rabbitmq",
|
||||
"rabbitmqctl",
|
||||
"-q",
|
||||
"list_queues",
|
||||
"name",
|
||||
"messages_ready",
|
||||
"messages_unacknowledged",
|
||||
"consumers",
|
||||
check=False,
|
||||
)
|
||||
print("queue ready unacked consumers")
|
||||
print(queues.stdout.strip() or queues.stderr.strip())
|
||||
return done.returncode
|
||||
|
||||
|
||||
# --- приватность bucket вне API ------------------------------------------------
|
||||
|
||||
|
||||
def _env_value(name: str, default: str) -> str:
|
||||
env_file = os.environ.get("COMPOSE_ENV_FILES") or str(ROOT / ".env")
|
||||
path = Path(env_file.split(",")[0])
|
||||
if path.exists():
|
||||
for key, value in _assignments(path.read_text(encoding="utf-8")):
|
||||
if key == name and value:
|
||||
return value
|
||||
return os.environ.get(name) or default
|
||||
|
||||
|
||||
def _http_status(url: str, method: str = "GET", data: bytes | None = None) -> str:
|
||||
request = urllib.request.Request(url, data=data, method=method)
|
||||
try:
|
||||
with urllib.request.urlopen(request, timeout=5) as response:
|
||||
return str(response.status)
|
||||
except urllib.error.HTTPError as error:
|
||||
return str(error.code)
|
||||
except OSError as error:
|
||||
return type(error).__name__
|
||||
|
||||
|
||||
def _network_status(path: str, method: str = "GET") -> str:
|
||||
"""Анонимный запрос к API S3 из сети стенда (контейнер LB, bash /dev/tcp)."""
|
||||
request = (
|
||||
f"{method} {path} HTTP/1.0\\r\\nHost: s3\\r\\nContent-Length: 0\\r\\n\\r\\n"
|
||||
)
|
||||
script = f"exec 3<>/dev/tcp/s3/8333 && printf '{request}' >&3 && head -n 1 <&3"
|
||||
done = compose("exec", "-T", "haproxy", "bash", "-c", script, check=False)
|
||||
match = re.search(r"HTTP/1\.[01] (\d{3})", done.stdout)
|
||||
return match.group(1) if match else "no answer"
|
||||
|
||||
|
||||
def _port_open(port: str) -> bool:
|
||||
script = f"timeout 2 bash -c 'exec 3<>/dev/tcp/s3/{port}'"
|
||||
return (
|
||||
compose("exec", "-T", "haproxy", "bash", "-c", script, check=False).returncode
|
||||
== 0
|
||||
)
|
||||
|
||||
|
||||
def s3_check(_args: argparse.Namespace) -> int:
|
||||
rows: list[tuple[str, str, str, str]] = []
|
||||
print("$ docker compose run --rm --no-deps s3-bootstrap check")
|
||||
check = compose("run", "--rm", "--no-deps", "s3-bootstrap", "check", check=False)
|
||||
for line in check.stdout.splitlines():
|
||||
parts = line.split("\t")
|
||||
if line.startswith("s3-bootstrap: check") and len(parts) == 5:
|
||||
rows.append(("bootstrap (loopback s3)", parts[1], parts[2], parts[3]))
|
||||
elif line.strip():
|
||||
print(line)
|
||||
if check.returncode != 0:
|
||||
print(check.stderr.strip())
|
||||
bucket = _env_value("S3_BUCKET", "messenger-attachments")
|
||||
host = f"http://127.0.0.1:{_env_value('S3_HOST_PORT', '8334')}"
|
||||
probe = f"/{bucket}/.s3-check/probe"
|
||||
try:
|
||||
for name, url, method in (
|
||||
("anonymous get object", host + probe, "GET"),
|
||||
("anonymous head object", host + probe, "HEAD"),
|
||||
("anonymous list objects", f"{host}/{bucket}?list-type=2", "GET"),
|
||||
("anonymous list buckets", host + "/", "GET"),
|
||||
):
|
||||
rows.append(("host 127.0.0.1", name, "403", _http_status(url, method)))
|
||||
rows.append(
|
||||
(
|
||||
"host 127.0.0.1",
|
||||
"anonymous put object",
|
||||
"403",
|
||||
_http_status(f"{host}/{bucket}/.s3-check/anonymous", "PUT", b"x"),
|
||||
)
|
||||
)
|
||||
for name, path, method in (
|
||||
("anonymous get object", probe, "GET"),
|
||||
("anonymous list objects", f"/{bucket}?list-type=2", "GET"),
|
||||
("anonymous put object", f"/{bucket}/.s3-check/anonymous", "PUT"),
|
||||
):
|
||||
rows.append(
|
||||
("stand network (haproxy)", name, "403", _network_status(path, method))
|
||||
)
|
||||
listening = compose(
|
||||
"exec", "-T", "s3", "sh", "-c", "netstat -tln", check=False
|
||||
).stdout
|
||||
ports = sorted(
|
||||
{
|
||||
m.group(1)
|
||||
for m in re.finditer(r"\s\S+:(\d+)\s+\S+:\*?\S*\s+LISTEN", listening)
|
||||
},
|
||||
key=int,
|
||||
)
|
||||
for port in ports:
|
||||
expected = "open" if port in S3_NETWORK_PORTS else "closed"
|
||||
got = "open" if _port_open(port) else "closed"
|
||||
rows.append(("stand network (haproxy)", f"s3 port {port}", expected, got))
|
||||
finally:
|
||||
cleanup = compose(
|
||||
"run", "--rm", "--no-deps", "s3-bootstrap", "cleanup", check=False
|
||||
)
|
||||
print(
|
||||
"$ docker compose run --rm --no-deps s3-bootstrap cleanup -> exit",
|
||||
cleanup.returncode,
|
||||
)
|
||||
failed = 0
|
||||
print(f"{'where':<26} {'check':<46} {'expected':>8} {'got':>8} verdict")
|
||||
for where, name, expected, got in rows:
|
||||
verdict = "ok" if expected == got else "FAIL"
|
||||
failed += verdict != "ok"
|
||||
print(f"{where:<26} {name:<46} {expected:>8} {got:>8} {verdict}")
|
||||
print(f"checks: {len(rows)}, failed: {failed}, bootstrap exit {check.returncode}")
|
||||
return 1 if failed or check.returncode != 0 else 0
|
||||
|
||||
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
||||
commands = parser.add_subparsers(dest="command", required=True)
|
||||
commands.add_parser("env")
|
||||
one = commands.add_parser("instances")
|
||||
one.add_argument("--base-url", default="http://localhost:8080")
|
||||
one.add_argument("--requests", type=int, default=12)
|
||||
commands.add_parser("lb-stats")
|
||||
each = commands.add_parser("failover-each")
|
||||
each.add_argument("--base-url", default="http://localhost:8080")
|
||||
each.add_argument("--replicas", type=int, default=2)
|
||||
each.add_argument("--mode", choices=("stop", "kill"), default="stop")
|
||||
attachment = commands.add_parser("attachment-failover")
|
||||
attachment.add_argument("--base-url", default="http://localhost:8080")
|
||||
attachment.add_argument("--replicas", type=int, default=2)
|
||||
attachment.add_argument("--mode", choices=("stop", "kill"), default="stop")
|
||||
commands.add_parser("s3-check")
|
||||
scenario = commands.add_parser("defense")
|
||||
scenario.add_argument("--base-url", default="http://localhost:8080")
|
||||
commands.add_parser("pipeline")
|
||||
args = parser.parse_args()
|
||||
handler = {
|
||||
"env": env,
|
||||
"instances": instances,
|
||||
"lb-stats": lb_stats,
|
||||
"failover-each": failover_each,
|
||||
"attachment-failover": attachment_failover,
|
||||
"s3-check": s3_check,
|
||||
"defense": defense,
|
||||
"pipeline": pipeline,
|
||||
}
|
||||
return handler[args.command](args)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user