forked from mtusi2026/mtusi2026lab4
ЛР 4: S3-хранилище (SeaweedFS) и вложения
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
import json
|
||||
import secrets
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
SECRETS = ROOT / "secrets"
|
||||
|
||||
|
||||
def write(name: str, value: str) -> None:
|
||||
with open(SECRETS / name, "w", encoding="utf-8", newline="") as file:
|
||||
file.write(value)
|
||||
(SECRETS / name).chmod(0o644) # контейнеры читают секрет не от root; папка secrets/ в .gitignore
|
||||
|
||||
|
||||
def bucket_name() -> str:
|
||||
"""S3_BUCKET из .env (если задан), иначе значение по умолчанию."""
|
||||
env = ROOT / ".env"
|
||||
if env.exists():
|
||||
for line in env.read_text(encoding="utf-8").splitlines():
|
||||
if line.strip().startswith("S3_BUCKET="):
|
||||
return line.split("=", 1)[1].strip()
|
||||
return "messenger-attachments"
|
||||
|
||||
|
||||
def s3_config(bucket: str) -> str:
|
||||
"""Конфиг ключей SeaweedFS: admin — только для s3-init, messenger-api — один bucket.
|
||||
|
||||
Права SeaweedFS: Read (GetObject/HeadObject), Write (PutObject/DeleteObject), List (ListObjects)
|
||||
с суффиксом :<bucket> действуют только в этом bucket. Admin (создание/удаление bucket и т.п.) у API нет.
|
||||
Identity "anonymous" не объявлена, поэтому запросы без подписи отклоняются.
|
||||
"""
|
||||
def read(name):
|
||||
return (SECRETS / name).read_text(encoding="utf-8").strip()
|
||||
config = {"identities": [
|
||||
{"name": "admin",
|
||||
"credentials": [{"accessKey": read("s3_root_user"), "secretKey": read("s3_root_password")}],
|
||||
"actions": ["Admin", "Read", "Write", "List", "Tagging"]},
|
||||
{"name": "messenger-api",
|
||||
"credentials": [{"accessKey": read("s3_access_key_id"), "secretKey": read("s3_secret_access_key")}],
|
||||
"actions": [f"Read:{bucket}", f"Write:{bucket}", f"List:{bucket}"]},
|
||||
]}
|
||||
return json.dumps(config, indent=2)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
"""Создаёт недостающие секреты. Уже существующие не меняет (иначе разошлись бы с данными в volumes)."""
|
||||
SECRETS.mkdir(exist_ok=True)
|
||||
created = []
|
||||
if not (SECRETS / "postgres_password").exists():
|
||||
db_password = secrets.token_hex(24)
|
||||
redis_password = secrets.token_hex(24)
|
||||
write("postgres_password", db_password)
|
||||
write("database_url", f"postgresql://messenger:{db_password}@postgres:5432/messenger")
|
||||
write("cache_url", f"redis://:{redis_password}@redis:6379/0")
|
||||
write("redis_auth", f"requirepass {redis_password}\n")
|
||||
created += ["postgres_password", "database_url", "cache_url", "redis_auth"]
|
||||
if not (SECRETS / "s3_root_password").exists():
|
||||
# admin-ключ S3: только для сервисов s3 (в конфиге) и s3-init
|
||||
write("s3_root_user", "root-" + secrets.token_hex(6))
|
||||
write("s3_root_password", secrets.token_hex(24))
|
||||
created += ["s3_root_user", "s3_root_password"]
|
||||
if not (SECRETS / "s3_secret_access_key").exists():
|
||||
# отдельный ключ API с политикой только на свой bucket (создаёт s3-init)
|
||||
write("s3_access_key_id", "api-" + secrets.token_hex(8))
|
||||
write("s3_secret_access_key", secrets.token_hex(24))
|
||||
created += ["s3_access_key_id", "s3_secret_access_key"]
|
||||
if not (SECRETS / "s3_config.json").exists():
|
||||
write("s3_config.json", s3_config(bucket_name()))
|
||||
created.append("s3_config.json")
|
||||
if created:
|
||||
print("Created in secrets/: " + ", ".join(created) + " (values are not printed).")
|
||||
else:
|
||||
print("secrets/ is complete, nothing changed.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,13 @@
|
||||
@echo off
|
||||
rem Restart hook for tests/resilience.py on Windows (same as scripts/restart.sh).
|
||||
rem Python on Windows cannot start .sh files directly (WinError 193), a .cmd file works.
|
||||
rem Restarts the stores first (PostgreSQL, Redis, SeaweedFS S3), then api and janitor, then the balancer. Volumes are kept.
|
||||
rem "restart" does not wait for healthchecks; SeaweedFS needs a few seconds to bring up S3, so wait explicitly
|
||||
rem ("up --wait --no-recreate" keeps the same containers and only waits until they are healthy).
|
||||
cd /d "%~dp0.."
|
||||
docker compose restart postgres redis s3 || exit /b 1
|
||||
docker compose up -d --wait --no-recreate --no-build --no-deps postgres redis s3 || exit /b 1
|
||||
docker compose restart api janitor || exit /b 1
|
||||
docker compose restart lb || exit /b 1
|
||||
docker compose up -d --wait --no-recreate --no-build --no-deps api lb || exit /b 1
|
||||
exit /b 0
|
||||
@@ -0,0 +1,13 @@
|
||||
#!/bin/sh
|
||||
# Restart hook for tests/resilience.py --mode persistence (Linux/macOS).
|
||||
# Restarts the stores first (PostgreSQL, Redis, SeaweedFS S3), then all api replicas and janitor, then the balancer.
|
||||
# Volumes are kept (no down -v), nothing is re-seeded.
|
||||
# "restart" does not wait for healthchecks; SeaweedFS needs a few seconds to bring up S3, so wait explicitly
|
||||
# ("up --wait --no-recreate" keeps the same containers and only waits until they are healthy).
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
docker compose restart postgres redis s3
|
||||
docker compose up -d --wait --no-recreate --no-build --no-deps postgres redis s3
|
||||
docker compose restart api janitor
|
||||
docker compose restart lb
|
||||
docker compose up -d --wait --no-recreate --no-build --no-deps api lb
|
||||
@@ -0,0 +1,6 @@
|
||||
@echo off
|
||||
rem Hook for tests/resilience.py --mode failover on Windows (same as scripts/stop-one.sh).
|
||||
rem Python on Windows cannot start .sh files directly (WinError 193), a .cmd file works.
|
||||
rem Stops only the api replica from TARGET_INSTANCE_ID, the logic is in scripts\stop_one.py.
|
||||
python "%~dp0stop_one.py"
|
||||
exit /b %ERRORLEVEL%
|
||||
@@ -0,0 +1,5 @@
|
||||
#!/bin/sh
|
||||
# Hook for tests/resilience.py --mode failover (Linux/macOS): stops the api replica from TARGET_INSTANCE_ID.
|
||||
# Logic is in scripts/stop_one.py, Windows uses scripts/stop-one.cmd.
|
||||
set -eu
|
||||
exec python3 "$(dirname "$0")/stop_one.py"
|
||||
@@ -0,0 +1,29 @@
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
STOP_TIMEOUT_SECONDS = 8
|
||||
|
||||
|
||||
def main() -> int:
|
||||
target = os.environ.get("TARGET_INSTANCE_ID", "").strip().lower()
|
||||
if not re.fullmatch(r"[0-9a-f]{12,64}", target):
|
||||
print("TARGET_INSTANCE_ID must be a container id (X-Instance-Id)", file=sys.stderr)
|
||||
return 2
|
||||
running = subprocess.run(["docker", "compose", "ps", "-q", "api"], cwd=ROOT,
|
||||
capture_output=True, text=True, check=True).stdout.split()
|
||||
matches = [container for container in running if container.startswith(target)]
|
||||
if len(matches) != 1:
|
||||
print(f"no running api container with id {target}", file=sys.stderr)
|
||||
return 1
|
||||
subprocess.run(["docker", "stop", "--time", str(STOP_TIMEOUT_SECONDS), matches[0]],
|
||||
cwd=ROOT, check=True, stdout=subprocess.DEVNULL)
|
||||
print(f"stopped api container {matches[0][:12]}")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user