|
|
@@ -1,17 +1,26 @@
|
|
|
-"""Configuration loader for SpoolBuddy daemon."""
|
|
|
+"""Configuration loader for SpoolBuddy daemon.
|
|
|
+
|
|
|
+All configuration is via environment variables. The systemd service file
|
|
|
+or a shell wrapper sets these before launching the daemon.
|
|
|
+
|
|
|
+Required:
|
|
|
+ SPOOLBUDDY_BACKEND_URL — Bambuddy server URL (e.g. http://192.168.1.100:5000)
|
|
|
+ SPOOLBUDDY_API_KEY — API key created in Bambuddy Settings → API Keys
|
|
|
+
|
|
|
+Optional:
|
|
|
+ SPOOLBUDDY_DEVICE_ID — Unique device identifier (default: derived from MAC)
|
|
|
+ SPOOLBUDDY_HOSTNAME — Display name (default: system hostname)
|
|
|
+"""
|
|
|
|
|
|
import os
|
|
|
+import socket
|
|
|
from dataclasses import dataclass
|
|
|
from pathlib import Path
|
|
|
|
|
|
-import yaml
|
|
|
-
|
|
|
-CONFIG_PATH = Path(os.environ.get("SPOOLBUDDY_CONFIG", "/etc/spoolbuddy/config.yaml"))
|
|
|
-
|
|
|
|
|
|
@dataclass
|
|
|
class Config:
|
|
|
- backend_url: str = "http://localhost:5000"
|
|
|
+ backend_url: str = ""
|
|
|
api_key: str = ""
|
|
|
device_id: str = ""
|
|
|
hostname: str = ""
|
|
|
@@ -30,25 +39,15 @@ class Config:
|
|
|
def load(cls) -> "Config":
|
|
|
cfg = cls()
|
|
|
|
|
|
- # Load from YAML if exists
|
|
|
- if CONFIG_PATH.exists():
|
|
|
- with open(CONFIG_PATH) as f:
|
|
|
- data = yaml.safe_load(f) or {}
|
|
|
- for key, val in data.items():
|
|
|
- if hasattr(cfg, key):
|
|
|
- setattr(cfg, key, val)
|
|
|
-
|
|
|
- # Environment overrides
|
|
|
- env_map = {
|
|
|
- "SPOOLBUDDY_BACKEND_URL": "backend_url",
|
|
|
- "SPOOLBUDDY_API_KEY": "api_key",
|
|
|
- "SPOOLBUDDY_DEVICE_ID": "device_id",
|
|
|
- "SPOOLBUDDY_HOSTNAME": "hostname",
|
|
|
- }
|
|
|
- for env_key, attr in env_map.items():
|
|
|
- val = os.environ.get(env_key)
|
|
|
- if val:
|
|
|
- setattr(cfg, attr, val)
|
|
|
+ cfg.backend_url = os.environ.get("SPOOLBUDDY_BACKEND_URL", "")
|
|
|
+ cfg.api_key = os.environ.get("SPOOLBUDDY_API_KEY", "")
|
|
|
+ cfg.device_id = os.environ.get("SPOOLBUDDY_DEVICE_ID", "")
|
|
|
+ cfg.hostname = os.environ.get("SPOOLBUDDY_HOSTNAME", "")
|
|
|
+
|
|
|
+ if not cfg.backend_url:
|
|
|
+ raise RuntimeError("SPOOLBUDDY_BACKEND_URL is required (e.g. http://192.168.1.100:5000)")
|
|
|
+ if not cfg.api_key:
|
|
|
+ raise RuntimeError("SPOOLBUDDY_API_KEY is required (create one in Bambuddy Settings → API Keys)")
|
|
|
|
|
|
# Default device_id from MAC address
|
|
|
if not cfg.device_id:
|
|
|
@@ -56,8 +55,6 @@ class Config:
|
|
|
|
|
|
# Default hostname from system
|
|
|
if not cfg.hostname:
|
|
|
- import socket
|
|
|
-
|
|
|
cfg.hostname = socket.gethostname()
|
|
|
|
|
|
return cfg
|