Files
mtusi2026lab4-fork/scripts/init_local.py
T
nothingredmouth e594df18f9 Поднять стенд из двух реплик за HAProxy с приватным MinIO S3
Dockerfile (python:3.11, waitress), compose: api1/api2 + postgres +
migrate + s3 (pinned minio, без опубликованных портов) + s3-bootstrap +
lb (haproxy, round-robin, healthcheck на /health/ready). Секреты DATABASE_URL
и S3 — через Docker secrets, локальная инициализация — scripts/init_local.py.
2026-11-17 03:37:34 +00:00

30 lines
1.4 KiB
Python

"""Create local secrets once. Never overwrite existing credentials."""
from pathlib import Path
import secrets
import shutil
root = Path(__file__).resolve().parents[1]
directory = root / 'secrets'
directory.mkdir(mode=0o700, exist_ok=True)
password_file = directory / 'postgres_password'
url_file = directory / 'database_url'
if password_file.exists() != url_file.exists():
raise SystemExit('Incomplete secrets: restore the missing file; do not regenerate credentials for an existing volume.')
if not password_file.exists():
password = secrets.token_hex(32)
for path, value in [(password_file, password),
(url_file, 'postgresql://messenger:' + password + '@db:5432/messenger')]:
with path.open('x', encoding='utf-8') as f:
f.write(value + '\n')
# Compose mounts secret files individually; PostgreSQL runs as a distinct UID.
# The enclosing host directory is 0700, while this mounted file must be readable.
path.chmod(0o644 if path == password_file else 0o600)
s3_file = directory / 's3_password'
if not s3_file.exists():
with s3_file.open('x', encoding='utf-8') as f:
f.write(secrets.token_hex(32) + '\n')
s3_file.chmod(0o644)
if not (root / '.env').exists():
shutil.copyfile(root / '.env.example', root / '.env')
print('Local configuration ready; existing credentials preserved.')