Реализовать ЛР 4 на 3: приватное S3 и вложения — Гавриленко БПИ2303

This commit is contained in:
2026-11-13 01:54:11 +03:00
parent ec540a1a72
commit 53ea5b2faf
34 changed files with 1377 additions and 35 deletions
+5
View File
@@ -0,0 +1,5 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
docker compose stop lb api db s3
docker compose up -d --wait
+35
View File
@@ -0,0 +1,35 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
python3 - <<'PY'
import os
import json
import secrets
from pathlib import Path
os.umask(0o077)
root = Path('secrets')
root.mkdir(exist_ok=True)
Path('data/s3').mkdir(parents=True, exist_ok=True)
password_file = root / 'db_password'
if not password_file.exists():
password_file.write_text(secrets.token_hex(32))
password = password_file.read_text().strip()
url = root / 'database_url'
if not url.exists():
from urllib.parse import quote
url.write_text('postgresql://messenger:' + quote(password, safe='') + '@db:5432/messenger')
# Private parent directory protects files on the host; API UID must read its mount.
url.chmod(0o644)
for name in ('s3_access_key_id', 's3_secret_access_key'):
target = root / name
if not target.exists():
target.write_text(secrets.token_hex(24))
target.chmod(0o644)
config = root / 's3_config.json'
if not config.exists():
config.write_text(json.dumps({'identities': [{'name': 'lab-admin', 'credentials': [{'accessKey': (root / 's3_access_key_id').read_text().strip(), 'secretKey': (root / 's3_secret_access_key').read_text().strip()}], 'actions': ['Admin', 'Read', 'Write', 'List', 'Tagging']}]}))
if not Path('.env').exists():
Path('.env').write_text('HOST_PORT=8084\nSESSION_TTL_SECONDS=3600\n')
print('Local secrets and .env ready (existing files preserved)')
PY
+13
View File
@@ -0,0 +1,13 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
: "${TARGET_INSTANCE_ID:?Set TARGET_INSTANCE_ID from the API response}"
for id in $(docker compose ps -q api); do
hostname=$(docker inspect --format '{{.Config.Hostname}}' "$id")
if [ "$hostname" = "$TARGET_INSTANCE_ID" ]; then
docker stop --time 5 "$id"
exit 0
fi
done
echo 'Target is not a running API replica in this Compose project' >&2
exit 1