Лабораторная 4: S3-хранилище и вложения

This commit is contained in:
Алиса Шурман
2026-11-12 22:27:32 +03:00
parent ec540a1a72
commit 7fbdef3a6a
25 changed files with 1447 additions and 23 deletions
+32
View File
@@ -0,0 +1,32 @@
"""Generate local-only synthetic credentials once; never overwrite them."""
import os
import secrets
from pathlib import Path
root = Path(__file__).resolve().parents[1]
os.umask(0o077)
(root / "secrets").mkdir(exist_ok=True)
p = root / "secrets/postgres_password"
if not p.exists():
p.write_text(secrets.token_hex(32))
values = {"database_url": "postgresql://messenger:" + p.read_text().strip() + "@postgres:5432/messenger", "cache_url": "redis://valkey:6379/0"}
for name, value in values.items():
dest = root / "secrets" / name
if not dest.exists():
dest.write_text(value)
# Docker Desktop bind-mounted secrets need read permission for container UID 10001.
for name in ["postgres_password", "database_url", "cache_url"]:
(root / "secrets" / name).chmod(0o644)
env = root / ".env"
if not env.exists():
env.write_text("SESSION_TTL_SECONDS=3600\nHOST_PORT=8084\n")
print("Local configuration ready (secrets not printed)")
for name, value in {
's3_root_user': 'admin-' + secrets.token_hex(8),
's3_root_password': secrets.token_hex(32),
's3_access_key_id': 'api-' + secrets.token_hex(8),
's3_secret_access_key': secrets.token_hex(32),
}.items():
dest = root / 'secrets' / name
if not dest.exists():
dest.write_text(value)
dest.chmod(0o644)
+4
View File
@@ -0,0 +1,4 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
docker compose restart postgres valkey s3 api1 api2 cleaner lb
+7
View File
@@ -0,0 +1,7 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
case "${TARGET_INSTANCE_ID:-}" in
api1|api2) docker compose stop "$TARGET_INSTANCE_ID" ;;
*) echo 'Unknown target instance' >&2; exit 1 ;;
esac
+40
View File
@@ -0,0 +1,40 @@
"""Run all checks and save actual outputs. Stop on the first failure."""
import os
from pathlib import Path
import subprocess
import sys
root=Path(__file__).resolve().parents[1]
os.chdir(root)
os.environ.setdefault('BASE_URL','http://localhost:8084')
(root/'evidence').mkdir(exist_ok=True)
def run(name, args, stdin=None):
result=subprocess.run(args,input=stdin,text=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
(root/'evidence'/name).write_text(result.stdout)
print(result.stdout,end='')
if result.returncode:
raise SystemExit(result.returncode)
run('check.txt',['make','check'])
run('unit.txt',[sys.executable,'student-tests/unit.py'])
run('smoke.txt',['make','test','BASE_URL='+os.environ['BASE_URL']])
run('student-tests.txt',[sys.executable,'student-tests/verify.py'])
subprocess.run(['docker','compose','stop','cleaner'],check=True)
try:
run('storage-checks.txt',['docker','compose','exec','-T','api1','python','-'],
(root/'student-tests/storage_checks.py').read_text())
finally:
subprocess.run(['docker','compose','start','cleaner'],check=True)
run('outage.txt',[sys.executable,'student-tests/outage.py'])
run('bootstrap-repeat.txt',['docker','compose','run','--rm','s3-init'])
run('containers-before.txt',['docker','compose','ps','--format','json'])
run('persistence.txt',['make','test-persistence','ACTION_SCRIPT=scripts/restart.sh','BASE_URL='+os.environ['BASE_URL']])
run('containers-after.txt',['docker','compose','ps','--format','json'])
try:
run('failover.txt',['make','test-failover','ACTION_SCRIPT=scripts/stop-one.sh','BASE_URL='+os.environ['BASE_URL']])
finally:
subprocess.run(['docker','compose','start','api1','api2'],check=True)
# Compose waits for the API's actual readiness after restoring the replica.
subprocess.run(['docker','compose','up','-d','--wait','api1','api2'],check=True)
run('memory.txt',[sys.executable,'student-tests/memory.py'])
print('All requested checks passed; actual results are in evidence/.')