Выполнение ЛР5 на 5 (трек 2)

This commit is contained in:
Ilya
2026-11-11 19:09:08 +03:00
parent 276dbdad8a
commit 9de64e36b2
58 changed files with 5132 additions and 30 deletions
+60
View File
@@ -0,0 +1,60 @@
#!/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
# RabbitMQ: the default user is defined in a conf.d file mounted as a secret (no password in compose/env).
once rabbitmq_password gen
RMQ_PASS=$(cat secrets/rabbitmq_password)
{
printf 'default_user = messenger\n'
printf 'default_pass = %s\n' "$RMQ_PASS"
printf 'default_vhost = /\n'
printf 'loopback_users.guest = true\n'
printf 'vm_memory_high_watermark.absolute = 300MiB\n'
printf 'disk_free_limit.absolute = 256MB\n'
} > secrets/rabbitmq.conf
printf 'amqp://messenger:%s@rabbitmq:5672/\n' "$RMQ_PASS" > secrets/broker_url
chmod 644 secrets/*
echo "secrets ready in ./secrets (not tracked by git)"
+10
View File
@@ -0,0 +1,10 @@
#!/bin/sh
# Restart hook for tests/resilience.py: API, worker, relay and ALL stores (PostgreSQL, Valkey, SeaweedFS,
# RabbitMQ) are stopped and started again with the same named volumes. Nothing is removed or re-seeded.
set -eu
cd "$(dirname "$0")/.."
docker compose ps --format 'table {{.Name}}\t{{.Status}}'
docker compose stop lb api worker relay sweeper ops
docker compose stop valkey postgres s3 rabbitmq
docker compose up -d --no-build --wait api lb worker relay sweeper ops
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}}'
+5
View File
@@ -0,0 +1,5 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
docker compose up -d --no-build --wait worker
docker compose ps worker --format 'table {{.Name}}\t{{.Status}}'
+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
+6
View File
@@ -0,0 +1,6 @@
#!/bin/sh
# Stops every worker container (the API, relay and broker keep running). Volumes are untouched.
set -eu
cd "$(dirname "$0")/.."
docker compose stop worker
docker compose ps worker --all --format 'table {{.Name}}\t{{.Status}}'