Files
mtusi2026lab5/scripts/init-local.py
T

27 lines
1.2 KiB
Python

"""Generate local credentials once; never overwrite an existing secret."""
from pathlib import Path
import secrets
root = Path(__file__).resolve().parents[1]
directory = root / 'secrets'
directory.mkdir(mode=0o700, exist_ok=True)
values = {'broker_password': secrets.token_hex(24), 'postgres_password': secrets.token_hex(24), 's3_root_user': 'lab5root', 's3_root_password': secrets.token_hex(24), 's3_access_key_id': 'lab5api', 's3_secret_access_key': secrets.token_hex(24)}
for name, value in values.items():
path = directory / name
if not path.exists():
path.write_text(value + '\n')
path.chmod(0o600)
path = directory / 'database_url'
if not path.exists():
password = (directory / 'postgres_password').read_text().strip()
path.write_text(f'postgresql://messenger:{password}@postgres:5432/messenger\n')
path.chmod(0o600)
path = directory / 'broker_url'
if not path.exists():
password = (directory / 'broker_password').read_text().strip()
path.write_text(f'amqp://messenger:{password}@broker:5672/%2F\n')
path.chmod(0o600)
path = root / '.env'
if not path.exists():
path.write_text('COMPOSE_PROJECT_NAME=mtusi2026lab5\nPUBLIC_PORT=8080\n')
print('Local configuration ready (secret values suppressed)')