Files
mtusi2026lab2/scripts/bootstrap.py
T

25 lines
1016 B
Python

#!/usr/bin/env python3
"""Create local synthetic credentials once; never print them or replace existing files."""
from pathlib import Path
import secrets
root = Path(__file__).resolve().parents[1]
folder = root / 'secrets'
folder.mkdir(exist_ok=True)
password_file = folder / 'db_password'
if not password_file.exists():
password_file.write_text(secrets.token_hex(24), encoding='utf-8')
password = password_file.read_text(encoding='utf-8').strip()
values = {
'database_url': 'postgresql://messenger:' + password + '@db:5432/messenger',
'cache_url': 'redis://redis:6379/0',
'cursor_key': secrets.token_hex(32),
}
for name, value in values.items():
target = folder / name
if not target.exists():
target.write_text(value, encoding='utf-8')
if not (root / '.env').exists():
(root / '.env').write_text('APP_HOST=0.0.0.0\nAPP_PORT=8080\nAPP_ENV=lab\nSESSION_TTL_SECONDS=3600\nSESSION_STORE=redis\n', encoding='utf-8')
print('Local .env and secrets ready (existing values preserved).')