Files
mtusi2026lab5/scripts/bootstrap.py
T

27 lines
1.1 KiB
Python

"""Create local synthetic secrets once; never overwrite existing credentials."""
import secrets
from pathlib import Path
root = Path(__file__).resolve().parents[1]
directory = root / 'secrets'
directory.mkdir(exist_ok=True)
password_file = directory / 'db_password'
if not password_file.exists():
with password_file.open('x', encoding='utf-8') as file:
file.write(secrets.token_urlsafe(32))
password = password_file.read_text(encoding='utf-8').strip()
url_file = directory / 'database_url'
if not url_file.exists():
with url_file.open('x', encoding='utf-8') as file:
file.write('postgresql://messenger:' + password + '@db:5432/messenger')
env = root / '.env'
if not env.exists():
with env.open('x', encoding='utf-8') as file:
file.write('SESSION_TTL_SECONDS=3600\n')
print('Local configuration ready; credentials are not printed.')
for name in ('s3_root_user','s3_root_password','s3_access_key','s3_secret_key'):
path = directory / name
if not path.exists():
with path.open('x', encoding='utf-8') as file:
file.write(secrets.token_hex(16))