#!/usr/bin/env python3 """Create local lab credentials once; never print values or overwrite an existing setup.""" from pathlib import Path import secrets root = Path(__file__).resolve().parents[1] directory = root / 'secrets' directory.mkdir(mode=0o700, exist_ok=True) password_path = directory / 'postgres_password' if not password_path.exists(): password_path.write_text(secrets.token_hex(24)) password_path.chmod(0o600) password = password_path.read_text().strip() for name, value in { 'database_url': 'postgresql://messenger:' + password + '@postgres:5432/messenger', 'cache_url': 'redis://redis:6379/0', }.items(): path = directory / name if not path.exists(): path.write_text(value) # Docker Compose bind-mounted secrets must be readable by UID 10001. # The containing host directory is accessible only to its owner. path.chmod(0o644) env = root / '.env' if not env.exists(): env.write_text((root / '.env.example').read_text()) env.chmod(0o600) print('Local configuration ready; secrets were not printed.')