Files
mtusi2026lab3/scripts/bootstrap.py
T

18 lines
790 B
Python

#!/usr/bin/env python3
"""Generate local synthetic credentials once; never print their values."""
from pathlib import Path
import secrets
root=Path(__file__).resolve().parents[1]
folder=root/'secrets'
folder.mkdir(mode=0o700,exist_ok=True)
folder.chmod(0o700)
password=folder/'db_password'
if not password.exists():
password.write_text(secrets.token_hex(32))
for name,value in {'database_url':'postgresql://messenger:'+password.read_text().strip()+'@postgres:5432/messenger','cache_url':'redis://redis:6379/0'}.items():
path=folder/name
if not path.exists(): path.write_text(value)
for path in folder.iterdir(): path.chmod(0o600)
if not (root/'.env').exists(): (root/'.env').write_text('API_REPLICAS=2\nSESSION_TTL_SECONDS=3600\n')
print('Local secrets ready (values hidden).')