forked from mtusi2026/mtusi2026lab3
23 lines
934 B
Python
23 lines
934 B
Python
#!/usr/bin/env python3
|
|
"""Generate local secrets once. Never overwrite a live installation's secrets."""
|
|
from pathlib import Path
|
|
import secrets
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
dest = ROOT / 'secrets'
|
|
dest.mkdir(mode=0o700, exist_ok=True)
|
|
password_path = dest / 'db_password'
|
|
if not password_path.exists():
|
|
password_path.write_text(secrets.token_hex(32), encoding='utf-8')
|
|
password = password_path.read_text(encoding='utf-8').strip()
|
|
values = {'database_url': 'postgresql://messenger:' + password + '@db:5432/messenger',
|
|
'cursor_key': secrets.token_hex(32)}
|
|
for name, value in values.items():
|
|
path = dest / name
|
|
if not path.exists():
|
|
path.write_text(value, encoding='utf-8')
|
|
env = ROOT / '.env'
|
|
if not env.exists():
|
|
env.write_text((ROOT / '.env.example').read_text(encoding='utf-8'), encoding='utf-8')
|
|
print('Local secrets and .env ready (existing files preserved; values not printed).')
|