Добавить Dockerfile и Compose-стенд с secrets и именованным volume
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
"""Create local secrets once. Never overwrite existing credentials."""
|
||||
from pathlib import Path
|
||||
import secrets
|
||||
import shutil
|
||||
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
directory = root / 'secrets'
|
||||
directory.mkdir(mode=0o700, exist_ok=True)
|
||||
password_file = directory / 'postgres_password'
|
||||
url_file = directory / 'database_url'
|
||||
if password_file.exists() != url_file.exists():
|
||||
raise SystemExit('Incomplete secrets: restore the missing file; do not regenerate credentials for an existing volume.')
|
||||
if not password_file.exists():
|
||||
password = secrets.token_hex(32)
|
||||
for path, value in [(password_file, password),
|
||||
(url_file, 'postgresql://messenger:' + password + '@db:5432/messenger')]:
|
||||
with path.open('x', encoding='utf-8') as f:
|
||||
f.write(value + '\n')
|
||||
# Compose mounts secret files individually; PostgreSQL runs as a distinct UID.
|
||||
# The enclosing host directory is 0700, while this mounted file must be readable.
|
||||
path.chmod(0o644 if path == password_file else 0o600)
|
||||
if not (root / '.env').exists():
|
||||
shutil.copyfile(root / '.env.example', root / '.env')
|
||||
print('Local configuration ready; existing credentials preserved.')
|
||||
Reference in New Issue
Block a user