ЛР 3 - БВТ2304 - Гончаревская.Е.А. - на 3

This commit is contained in:
Katarhi
2026-11-02 19:12:26 +03:00
parent 0ab3a341aa
commit 486afb97bc
10 changed files with 1102 additions and 0 deletions
+38
View File
@@ -0,0 +1,38 @@
#!/usr/bin/env python3
import argparse
import os
import secrets
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
def main():
parser = argparse.ArgumentParser(description="Create local Docker Compose secret files.")
parser.add_argument("--force", action="store_true", help="replace existing local secret files")
args = parser.parse_args()
directory = ROOT / "secrets"
directory.mkdir(mode=0o700, exist_ok=True)
files = [directory / "postgres_password", directory / "database_url", directory / "redis_url"]
if not args.force and any(path.exists() for path in files):
raise SystemExit("Secret files already exist. Use --force only if you intend to rotate credentials.")
password = secrets.token_hex(24)
values = {
"postgres_password": password + "\n",
"database_url": f"postgresql://messenger:{password}@postgres:5432/messenger?connect_timeout=5\n",
"redis_url": "redis://valkey:6379/0\n",
}
for name, value in values.items():
path = directory / name
path.write_text(value, encoding="utf-8")
try:
os.chmod(path, 0o600)
except OSError:
pass
print("Local secret files created. Values were not printed.")
if __name__ == "__main__":
main()