Files
mtusi2026lab2-fork/scripts/bootstrap.py
T

30 lines
1.2 KiB
Python

"""Create local secrets once. Never print values or overwrite existing secrets."""
from pathlib import Path
import secrets
import shutil
root = Path(__file__).resolve().parents[1]
folder = root / 'secrets'
folder.mkdir(exist_ok=True)
folder.chmod(0o700)
def put(name, value):
path = folder / name
if not path.exists():
with path.open('x', encoding='utf-8', newline='\n') as f:
f.write(value + '\n')
# Parent directory is private; Compose mounts individual files for non-root services.
path.chmod(0o644)
return path.read_text(encoding='utf-8').strip()
pg = put('postgres_password', secrets.token_hex(32))
vk = put('valkey_password', secrets.token_hex(32))
put('database_url', f'postgres://messenger:{pg}@db:5432/messenger?sslmode=disable')
put('cache_url', f'redis://:{vk}@valkey:6379/0')
put('cursor_secret', secrets.token_hex(32))
put('valkey.conf', '\n'.join(['bind 0.0.0.0', 'protected-mode yes', f'requirepass {vk}',
'dir /data', 'appendonly yes', 'appendfsync always', 'maxmemory 128mb',
'maxmemory-policy noeviction', 'save 60 1000']))
if not (root / '.env').exists():
shutil.copyfile(root / '.env.example', root / '.env')
print('Local .env and secrets ready; existing values preserved.')