level 4, changed to HAProxy
This commit is contained in:
+44
-15
@@ -2,6 +2,7 @@ import os
|
||||
import socket
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from urllib.parse import quote
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -11,39 +12,67 @@ class Settings:
|
||||
env: str
|
||||
log_level: str
|
||||
shutdown_timeout: int
|
||||
drain_seconds: float
|
||||
keepalive_timeout: int
|
||||
session_ttl_seconds: int
|
||||
idempotency_ttl_seconds: int
|
||||
database_url: str
|
||||
instance_id: str
|
||||
app_version: str
|
||||
|
||||
|
||||
def _secret(file_var: str, value_var: str) -> str:
|
||||
"""A *_FILE variable wins. The secret value is not logged."""
|
||||
file_name = os.getenv(file_var, '').strip()
|
||||
if file_name:
|
||||
try:
|
||||
value = Path(file_name).read_text(encoding='utf-8').strip()
|
||||
except OSError:
|
||||
raise RuntimeError('%s could not be read' % file_var) from None
|
||||
if not value:
|
||||
raise RuntimeError('%s is empty' % file_var)
|
||||
return value
|
||||
value = os.getenv(value_var, '').strip()
|
||||
def _read_secret_file(file_var: str, file_name: str) -> str:
|
||||
try:
|
||||
value = Path(file_name).read_text(encoding='utf-8').strip()
|
||||
except OSError:
|
||||
raise RuntimeError('%s could not be read' % file_var) from None
|
||||
if not value:
|
||||
raise RuntimeError('%s or %s is required' % (value_var, file_var))
|
||||
raise RuntimeError('%s is empty' % file_var)
|
||||
return value
|
||||
|
||||
|
||||
def _database_url() -> str:
|
||||
"""DATABASE_URL_FILE, then DATABASE_URL, then DB_* parts with the password from DB_PASSWORD_FILE.
|
||||
|
||||
Secret values are never logged.
|
||||
"""
|
||||
file_name = os.getenv('DATABASE_URL_FILE', '').strip()
|
||||
if file_name:
|
||||
return _read_secret_file('DATABASE_URL_FILE', file_name)
|
||||
value = os.getenv('DATABASE_URL', '').strip()
|
||||
if value:
|
||||
return value
|
||||
password_file = os.getenv('DB_PASSWORD_FILE', '').strip()
|
||||
if not password_file:
|
||||
raise RuntimeError('DATABASE_URL, DATABASE_URL_FILE or DB_PASSWORD_FILE is required')
|
||||
password = _read_secret_file('DB_PASSWORD_FILE', password_file)
|
||||
parts = {name: os.getenv(name, '').strip() for name in ('DB_HOST', 'DB_USER', 'DB_NAME')}
|
||||
missing = [name for name, part in parts.items() if not part]
|
||||
if missing:
|
||||
raise RuntimeError('%s required with DB_PASSWORD_FILE' % ', '.join(missing))
|
||||
return 'postgresql://%s:%s@%s:%s/%s' % (
|
||||
quote(parts['DB_USER'], safe=''),
|
||||
quote(password, safe=''),
|
||||
parts['DB_HOST'],
|
||||
int(os.getenv('DB_PORT', '5432')),
|
||||
quote(parts['DB_NAME'], safe=''),
|
||||
)
|
||||
|
||||
|
||||
def load_settings() -> Settings:
|
||||
return Settings(
|
||||
host=os.getenv('APP_HOST', '0.0.0.0'),
|
||||
port=int(os.getenv('APP_PORT', '8080')),
|
||||
env=os.getenv('APP_ENV', 'lab'),
|
||||
log_level=os.getenv('LOG_LEVEL', 'info').lower(),
|
||||
shutdown_timeout=int(os.getenv('SHUTDOWN_TIMEOUT_SECONDS', '5')),
|
||||
shutdown_timeout=int(os.getenv('SHUTDOWN_TIMEOUT_SECONDS', '3')),
|
||||
drain_seconds=float(os.getenv('DRAIN_SECONDS', '4')),
|
||||
# Longer than HAProxy's idle-connection purge (pool-purge-delay 5 s), so the LB usually closes them first.
|
||||
keepalive_timeout=int(os.getenv('KEEPALIVE_TIMEOUT_SECONDS', '10')),
|
||||
session_ttl_seconds=int(os.getenv('SESSION_TTL_SECONDS', '3600')),
|
||||
idempotency_ttl_seconds=int(os.getenv('IDEMPOTENCY_TTL_SECONDS', '86400')),
|
||||
database_url=_secret('DATABASE_URL_FILE', 'DATABASE_URL'),
|
||||
database_url=_database_url(),
|
||||
# Docker sets the hostname to the short container ID, which stop-one.sh maps back to a container.
|
||||
instance_id=os.getenv('INSTANCE_ID', '').strip() or socket.gethostname(),
|
||||
app_version=os.getenv('APP_VERSION', 'dev').strip() or 'dev',
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user