Files
mtusi2026lab3-fork/scripts/init_local.py
T
nothingredmouth 7d506a0587 Поднять балансировочный стенд из двух реплик за HAProxy
Dockerfile, compose (api1, api2, postgres, migrate, lb), haproxy.cfg с
round-robin и healthcheck на /health/ready, secrets с DATABASE_URL и
инициализация локальной БД (scripts/init_local.py).
2026-11-16 22:19:07 +00:00

25 lines
1.2 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)
if not (root / '.env').exists():
shutil.copyfile(root / '.env.example', root / '.env')
print('Local configuration ready; existing credentials preserved.')