from pathlib import Path import secrets import json root = Path(__file__).resolve().parents[1] directory = root / 'secrets' directory.mkdir(exist_ok=True) password_file = directory / 'db_password' if not password_file.exists(): password_file.write_text(secrets.token_hex(32), encoding='utf-8') url_file = directory / 'database_url' if not url_file.exists(): url_file.write_text('postgresql://messenger:' + password_file.read_text().strip() + '@postgres:5432/messenger', encoding='utf-8') environment = root / '.env' if not environment.exists(): environment.write_text('API_REPLICAS=2\nAPI_CPUS=0.5\nAPI_MEMORY=512m\nDB_POOL_SIZE=4\nSESSION_TTL_SECONDS=3600\n', encoding='utf-8') print('Local configuration prepared; existing credentials preserved.') for name, value in { 'minio_root_user': 'root-' + secrets.token_hex(8), 'minio_root_password': secrets.token_hex(32), 's3_access_key_id': 'api-' + secrets.token_hex(8), 's3_secret_access_key': secrets.token_hex(32), }.items(): target = directory / name if not target.exists(): target.write_text(value, encoding='utf-8') identities = [] for name, key, password, actions in ( ('bootstrap', 'minio_root_user', 'minio_root_password', ['Admin', 'Read', 'Write', 'List']), ('api', 's3_access_key_id', 's3_secret_access_key', ['Read:messenger-attachments', 'Write:messenger-attachments', 'List:messenger-attachments']), ): identities.append({'name': name, 'credentials': [{'accessKey': (directory / key).read_text().strip(), 'secretKey': (directory / password).read_text().strip()}], 'actions': actions}) (directory / 's3_config').write_text(json.dumps({'identities': identities}), encoding='utf-8')