forked from mtusi2026/mtusi2026lab2
ЛР 2 — БВТ2303 — Брезгунова — на 5
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Generate private LOCAL configuration once; never display credentials."""
|
||||
from pathlib import Path
|
||||
import secrets
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
def main():
|
||||
env = ROOT / '.env'
|
||||
folder = ROOT / 'secrets'
|
||||
folder.mkdir(exist_ok=True)
|
||||
if not env.exists():
|
||||
password = secrets.token_hex(24)
|
||||
env.write_text('POSTGRES_PASSWORD='+password+'\nSESSION_TTL_SECONDS=3600\nHOST_PORT=8080\n', encoding='utf-8')
|
||||
settings = dict(line.split('=', 1) for line in env.read_text(encoding='utf-8').splitlines() if line and not line.startswith('#') and '=' in line)
|
||||
password = settings.get('POSTGRES_PASSWORD')
|
||||
if not password or password == 'GENERATED_BY_BOOTSTRAP':
|
||||
raise SystemExit('Use a local generated POSTGRES_PASSWORD in .env or remove an unused example .env before bootstrap.')
|
||||
from urllib.parse import quote
|
||||
url = 'postgres://messenger:'+quote(password, safe='')+'@postgres:5432/messenger?sslmode=disable&connect_timeout=2&statement_timeout=2000&lock_timeout=1000'
|
||||
(folder / 'database_url').write_text(url+'\n', encoding='utf-8')
|
||||
(folder / 'cache_url').write_text('redis://redis:6379/0\n', encoding='utf-8')
|
||||
# File-backed Compose secrets keep host permissions on Linux. The private
|
||||
# directory protects them on the host; 0644 lets the unprivileged API read
|
||||
# individual bind-mounted files inside its container.
|
||||
folder.chmod(0o700)
|
||||
env.chmod(0o600)
|
||||
for path in [folder / 'database_url', folder / 'cache_url']:
|
||||
path.chmod(0o644)
|
||||
print('Local configuration ready. Credentials are kept in ignored .env and secrets/.')
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,3 @@
|
||||
@echo off
|
||||
cd /d "%~dp0.."
|
||||
docker compose --env-file .env restart postgres redis api
|
||||
Executable
+4
@@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
docker compose --env-file .env restart postgres redis api
|
||||
@@ -0,0 +1,20 @@
|
||||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
import time
|
||||
from urllib.request import urlopen
|
||||
|
||||
p = argparse.ArgumentParser()
|
||||
p.add_argument('--base-url', default='http://localhost:8080')
|
||||
args = p.parse_args()
|
||||
deadline = time.monotonic()+90
|
||||
while True:
|
||||
try:
|
||||
with urlopen(args.base_url+'/health/ready', timeout=3) as response:
|
||||
if response.status == 200:
|
||||
print('API ready')
|
||||
break
|
||||
except OSError:
|
||||
pass
|
||||
if time.monotonic() >= deadline:
|
||||
raise SystemExit('API did not become ready in 90 seconds')
|
||||
time.sleep(0.5)
|
||||
Reference in New Issue
Block a user