Files
Dockhand_Backup/app/settings.py
2026-07-13 13:12:49 +02:00

74 lines
2.9 KiB
Python

"""Environment-based settings (secrets & connection info).
These come from the container environment and are NOT stored in the JSON "DB".
Mutable, user-editable state lives in <DATA_DIR>/config.json (see store.py).
"""
import os
_APP_DIR = os.path.dirname(os.path.abspath(__file__))
_ROOT = os.path.dirname(_APP_DIR)
# Load a .env at the project root if present (handy for local dev). Real
# container env vars always take precedence (override=False).
try:
from dotenv import load_dotenv
load_dotenv(os.path.join(_ROOT, ".env"), override=False)
except ImportError:
pass
def _bool(name: str, default: bool) -> bool:
val = os.environ.get(name)
if val is None:
return default
return val.strip().lower() in ("1", "true", "yes", "on")
# --- Dev mode ---
# When on, the app knows it is NOT inside Docker. The host bind paths reported
# by Dockhand won't exist locally, so the backup engine SIMULATES tar/up/down
# (writes a tiny placeholder .tar, never really stops your stacks) and uses
# local ./data and ./backup folders. Auto-detected via Docker's /.dockerenv.
DEV_MODE = _bool("DEV_MODE", not os.path.exists("/.dockerenv"))
# --- Flask ---
SECRET_KEY = os.environ.get("FLASK_SECRET_KEY", "change-me-in-production")
# Default timezone for display, filenames and cron. The container itself may
# run in UTC; the user-facing timezone is configurable (env is just the
# default — the JSON config can override it at runtime).
DEFAULT_TZ = os.environ.get("TZ", "UTC")
# --- Dockhand connection (daemon -> Dockhand) ---
DOCKHAND_URL = os.environ.get("DOCKHAND_URL", "").rstrip("/")
DOCKHAND_USER = os.environ.get("DOCKHAND_USER", "")
DOCKHAND_PASS = os.environ.get("DOCKHAND_PASS", "")
DOCKHAND_PROVIDER = os.environ.get("DOCKHAND_PROVIDER", "local")
DOCKHAND_ENV_ID = os.environ.get("DOCKHAND_ENV_ID", "1")
DOCKHAND_VERIFY_TLS = _bool("DOCKHAND_VERIFY_TLS", True)
def dockhand_configured() -> bool:
"""Real Dockhand client is used only when URL + credentials are present."""
return bool(DOCKHAND_URL and DOCKHAND_USER and DOCKHAND_PASS)
# --- Paths ---
# In Docker these are absolute mount points; in dev they fall back to local
# folders at the project root so nothing needs mounting.
_default_backup = os.path.join(_ROOT, "backup") if DEV_MODE else "/backup"
_default_data = os.path.join(_ROOT, "data") if DEV_MODE else "/data"
BACKUP_ROOT = os.environ.get("BACKUP_ROOT", _default_backup)
DATA_DIR = os.environ.get("DATA_DIR", _default_data)
CONFIG_PATH = os.path.join(DATA_DIR, "config.json")
# --- OIDC (Authentik) for the web UI ---
OIDC_ISSUER = os.environ.get("OIDC_ISSUER", "")
OIDC_CLIENT_ID = os.environ.get("OIDC_CLIENT_ID", "")
OIDC_CLIENT_SECRET = os.environ.get("OIDC_CLIENT_SECRET", "")
OIDC_REDIRECT_URI = os.environ.get("OIDC_REDIRECT_URI", "")
def oidc_enabled() -> bool:
"""OIDC login is active only when fully configured; otherwise UI is open."""
return bool(OIDC_ISSUER and OIDC_CLIENT_ID and OIDC_CLIENT_SECRET)