forked from mtusi2026/mtusi2026lab2
lab 2 realization: PostgreSQL, Valkey, track 2
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
"""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.')
|
||||
Reference in New Issue
Block a user