Files
mtusi2026lab2/scripts/bootstrap.py
T
mayorovasonya 400e62ffdc
Application / application (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
ЛР 2 — БВТ2301 — Майорова — на 5
2026-10-23 11:13:22 +03:00

29 lines
1.1 KiB
Python

#!/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.')