выполнение ЛР4 на 4

- S3: SeaweedFS 4.51 (MinIO-образы недоступны), приватный bucket,
  отдельные ограниченные ключи API, служебные порты закрыты;
  идемпотентный s3-init с проверкой приватности и прогревом записи
- вложения: потоковая загрузка PNG/JPEG, лимиты 10 MiB / 40 Мпикс,
  тип по содержимому, проверка filename, скачивание только участникам
- согласованность PG/S3: pending -> PUT -> ready, компенсация, sweeper
  с grace и dry-run; attachment_ids в сообщениях, FK по чату
- back-pressure на S3 PUT после найденного OOM SeaweedFS
- тесты student-tests, evidence, REPORT.md, DEMO.md
This commit is contained in:
Ilya
2026-10-28 18:06:07 +03:00
parent ec540a1a72
commit a97c35fc12
60 changed files with 4369 additions and 17 deletions
+47
View File
@@ -0,0 +1,47 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
mkdir -p secrets
gen() { python3 -c 'import secrets; print(secrets.token_hex(24))'; }
keyid() { python3 -c 'import secrets,string; a=string.ascii_uppercase+string.digits; print("".join(secrets.choice(a) for _ in range(20)))'; }
once() { [ -f "secrets/$1" ] || { "$2" > "secrets/$1"; echo "created secrets/$1"; }; }
once postgres_password gen
PG_PASS=$(cat secrets/postgres_password)
printf 'postgresql://messenger:%s@postgres:5432/messenger\n' "$PG_PASS" > secrets/database_url
if [ ! -f secrets/valkey.conf ]; then
{
printf 'requirepass %s\n' "$(gen)"
printf 'save ""\nappendonly no\n'
printf 'maxmemory 64mb\nmaxmemory-policy volatile-lru\n'
} > secrets/valkey.conf
echo "created secrets/valkey.conf"
fi
VK_PASS=$(sed -n 's/^requirepass //p' secrets/valkey.conf)
printf 'redis://:%s@valkey:6379/0\n' "$VK_PASS" > secrets/cache_url
# S3 (SeaweedFS): admin identity only for bucket bootstrap; API identity limited to Read/Write/List of one bucket.
BUCKET=${S3_BUCKET:-messenger-attachments}
once s3_admin_access_key_id keyid
once s3_admin_secret_access_key gen
once s3_access_key_id keyid
once s3_secret_access_key gen
python3 - "$BUCKET" > secrets/seaweedfs_s3.json <<'PY'
import json, sys
r = lambda n: open("secrets/" + n).read().strip()
b = sys.argv[1]
print(json.dumps({"identities": [
{"name": "bootstrap-admin",
"credentials": [{"accessKey": r("s3_admin_access_key_id"), "secretKey": r("s3_admin_secret_access_key")}],
"actions": ["Admin", "Read", "List", "Write"]},
{"name": "messenger-api",
"credentials": [{"accessKey": r("s3_access_key_id"), "secretKey": r("s3_secret_access_key")}],
"actions": ["Read:" + b, "Write:" + b, "List:" + b]},
]}, indent=1))
PY
chmod 644 secrets/*
echo "secrets ready in ./secrets (not tracked by git)"
+8
View File
@@ -0,0 +1,8 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
docker compose ps --format 'table {{.Name}}\t{{.Status}}'
docker compose stop lb api sweeper
docker compose stop valkey postgres s3
docker compose up -d --no-build --wait api lb sweeper
docker compose ps --format 'table {{.Name}}\t{{.Status}}'
+5
View File
@@ -0,0 +1,5 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
docker compose up -d --no-build --no-recreate --wait api
docker compose ps api --format 'table {{.Name}}\t{{.Status}}'
+31
View File
@@ -0,0 +1,31 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
N=$(docker compose ps -q api | wc -l | tr -d ' ')
[ "$N" -ge 1 ] || { echo "api is not running" >&2; exit 1; }
OLD=$(docker compose ps -q api)
docker compose build api
docker compose run --rm --no-deps migrate
wait_healthy() {
i=0
until [ "$(docker inspect -f '{{.State.Health.Status}}' "$1")" = healthy ]; do
i=$((i + 1)); [ "$i" -le 60 ] || { echo "replica $1 not healthy" >&2; exit 1; }
sleep 1
done
}
for old in $OLD; do
before=$(docker compose ps -q api | sort)
docker compose up -d --no-deps --no-recreate --scale api=$((N + 1)) api >/dev/null 2>&1
new=$(docker compose ps -q api | sort | grep -vxF "$before" || true)
[ -n "$new" ] || { echo "no new replica started" >&2; exit 1; }
wait_healthy "$new"
sleep 2
printf '%s new %s healthy; stopping old %s\n' "$(date -u +%H:%M:%S)" \
"$(docker inspect -f '{{.Name}}' "$new")" "$(docker inspect -f '{{.Name}}' "$old")"
docker stop --time 8 "$old" >/dev/null
docker rm "$old" >/dev/null
done
docker compose ps api --format 'table {{.Name}}\t{{.Image}}\t{{.Status}}'
+9
View File
@@ -0,0 +1,9 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
name=${1:?usage: stop-container.sh <project>-api-N}
case "$name" in *-api-*) ;; *) echo "refusing: $name is not an API replica" >&2; exit 2;; esac
alias=$(docker exec "$name" cat /tmp/instance-id 2>/dev/null || echo "?")
docker stop --time 8 "$name" >/dev/null
printf '%s stopped %s instance=%s state=%s\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "$name" "$alias" \
"$(docker inspect -f '{{.State.Status}}' "$name")" | tee -a evidence/stop-one.txt
+19
View File
@@ -0,0 +1,19 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
log=evidence/stop-one.txt
for cid in $(docker compose ps -q api); do
alias=$(docker exec "$cid" cat /tmp/instance-id 2>/dev/null || true)
if [ "$alias" = "$TARGET_INSTANCE_ID" ]; then
name=$(docker inspect -f '{{.Name}}' "$cid" | sed 's#^/##')
docker stop --time 8 "$cid" >/dev/null
state=$(docker inspect -f '{{.State.Status}}' "$cid")
printf '%s stopped %s (%s) instance=%s state=%s\n' \
"$(date -u +%Y-%m-%dT%H:%M:%SZ)" "$name" "$(printf %.12s "$cid")" "$alias" "$state" | tee -a "$log"
exit 0
fi
done
echo "no running api container reports instance id $TARGET_INSTANCE_ID" >&2
exit 1