import json import logging import os def env_true(value): return value.lower() in ('yes', 'y', '1', 'on', 'true', 't') def env_false(value): return value.lower() in ('no', 'n', '0', 'off', 'false', 'f') PROXMOX_READONLY = env_true(os.environ.get('PROXMOX_READONLY', '0')) MIKROTIK_READONLY = env_true(os.environ.get('MIKROTIK_READONLY', '0')) class ProxmoxConfig: HOST = os.environ.get('PROXMOX_HOST') USER = os.environ.get('PROXMOX_USER') PASS = os.environ.get('PROXMOX_PASS') NODE = os.environ.get('PROXMOX_NODE') READONLY = PROXMOX_READONLY MAX_MEM = int(os.environ.get('PROXMOX_MAX_MEM', 8192)) MAX_DISK = int(os.environ.get('PROXMOX_MAX_DISK', 100)) MAX_CORES = int(os.environ.get('PROXMOX_MAX_CORES', 8)) CREATE_LXC_TIMEOUT = int(os.environ.get('PROXMOX_CREATE_LXC_TIMEOUT', 600)) DEFAULT_STORAGE = os.environ.get('PROXMOX_DEFAULT_STORAGE', 'local') class MikrotikConfig: HOST = os.environ.get('MIKROTIK_HOST') USER = os.environ.get('MIKROTIK_USER') PASS = os.environ.get('MIKROTIK_PASS') IP_8 = os.environ.get('MIKROTIK_IP_8', '192,172') READONLY = MIKROTIK_READONLY ROOT_NETWORKS = os.environ.get('MIKROTIK_ROOT_NETWORKS', '192.168.1.0,192.168.107.0').split(',') class DatabaseConfig: HOST = os.environ.get('DATABASE_HOST', 'localhost') or 'localhost' ENGINE = os.environ.get('DATABASE_ENGINE', 'sqlite3') or 'sqlite3' USER = os.environ.get('DATABASE_USER', '') or '' PASSWORD = os.environ.get('DATABASE_PASSWORD', '') or '' NAME = os.environ.get('DATABASE_NAME', '') PORT = os.environ.get('DATABASE_PORT', 5432) or 5432 class AuthLDAPConfig: HOST = os.environ.get('AUTH_LDAP_HOST') BIND_DN = os.environ.get('AUTH_LDAP_BIND_DN') BIND_PASSWORD = os.environ.get('AUTH_LDAP_BIND_PASSWORD') USER_BASE = os.environ.get('AUTH_LDAP_USER_BASE') USER_FILTER = os.environ.get('AUTH_LDAP_USER_FILTER') GROUP_SEARCH_BASE = os.environ.get('AUTH_LDAP_GROUP_SEARCH_BASE') GROUP_SEARCH_FILTER = os.environ.get('AUTH_LDAP_GROUP_SEARCH_FILTER') USER_ATTR_MAP = json.loads(os.environ.get('AUTH_LDAP_USER_ATTR_MAP')) USER_FLAGS_BY_GROUP = json.loads(os.environ.get('AUTH_LDAP_USER_FLAGS_BY_GROUP')) FIND_GROUP_PERMS = os.environ.get('AUTH_LDAP_FIND_GROUP_PERMS') CACHE_GROUPS = os.environ.get('AUTH_LDAP_CACHE_GROUPS') GROUP_CACHE_TIMEOUT = os.environ.get('AUTH_LDAP_GROUP_CACHE_TIMEOUT') _missing = [] for cfg in ['Proxmox', 'Mikrotik', 'Database', ('AuthLDAP', 'AUTH_LDAP')]: if isinstance(cfg, tuple): cfg, mapname = cfg else: mapname = cfg.upper() cls = globals()[cfg + 'Config'] for k, v in cls.__dict__.items(): if k.startswith(mapname + '_'): if v is None: _missing.append(f'{cfg}.{k}') if _missing: raise Exception(f'Missing environment variables: \n{"\n".join(_missing)}\n') logging.debug(f'ProxmoxConfig: {ProxmoxConfig.__dict__}') logging.debug(f'MikrotikConfig: {MikrotikConfig.__dict__}') logging.debug(f'DatabaseConfig: {DatabaseConfig.__dict__}') logging.debug(f'AuthLDAPConfig: {AuthLDAPConfig.__dict__}')