left4me/l4d2host/spec.py
mwiegand 985df970f8
feat(l4d2-web): per-overlay server.cfg aliases — expose checkbox + auto-exec
Each linked overlay gets a checkbox on the blueprint detail page that opts
its server.cfg in as exec server_overlay_<id>. The web app builds the
spec with {path, alias} per overlay and prepends exec server_overlay_<id>
lines to the blueprint config in lowest-overlay-first order. The host
stages those copies in the overlayfs upper layer before mounting (avoids
copy-up writes against a sandbox-uid file). A live preview block above the
Config textarea shows what gets auto-executed.

Schema:
- alembic 0007: BlueprintOverlay.expose_server_cfg BOOLEAN

Spec contract:
- l4d2host OverlayRef(path, alias?). load_spec accepts both bare-string
  and {path, alias} entries.

Side effects folded in (same file in l4d2_facade):
- start_server auto-initializes; the manual Initialize step is no longer
  needed before Start.
- initialize_server no longer runs blueprint builders — builds happen on
  overlay save, not on every server Start.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-09 01:26:31 +02:00

41 lines
1.3 KiB
Python

from dataclasses import dataclass, field
from pathlib import Path
import yaml
@dataclass(slots=True)
class OverlayRef:
path: str
alias: str | None = None
@dataclass(slots=True)
class InstanceSpec:
port: int
overlays: list[OverlayRef] = field(default_factory=list)
arguments: list[str] = field(default_factory=list)
config: list[str] = field(default_factory=list)
def _parse_overlay(item) -> OverlayRef:
if isinstance(item, str):
return OverlayRef(path=item)
if isinstance(item, dict):
path = item.get("path")
if not isinstance(path, str) or not path:
raise ValueError(f"overlay entry missing 'path': {item!r}")
raw_alias = item.get("alias")
alias = str(raw_alias) if raw_alias not in (None, "") else None
return OverlayRef(path=path, alias=alias)
raise ValueError(f"unsupported overlay entry type: {type(item).__name__}")
def load_spec(path: Path) -> InstanceSpec:
raw = yaml.safe_load(path.read_text()) or {}
return InstanceSpec(
port=int(raw["port"]),
overlays=[_parse_overlay(item) for item in raw.get("overlays", [])],
arguments=[str(item) for item in raw.get("arguments", [])],
config=[str(item) for item in raw.get("config", [])],
)