feat(nixos,core): transition ~/Sites default to ~/Workloads with WORKLOADS_DIR env support (#33)
- Update nixos/modules/homeserver.nix default homelab.sitesDir to ~/Workloads and export both WORKLOADS_DIR and SITES_DIR - Add ~/Workloads and ~/Workloads/* to programs.git.config.safe.directory in nixos/configuration.nix while retaining ~/Sites/* for transition - Update all workload paths in config/deployments.yaml from ~/Sites/<app> to ~/Workloads/<app> - Update appctl_engine.py, appctl_daemon.py, and gitops_dispatcher.py to honor WORKLOADS_DIR -> SITES_DIR -> ~/Workloads and resolve both ~/Workloads/ and ~/Sites/ prefixes in deployments.yaml - Add TDD security and unit tests covering NixOS defaults, git safe.directory, tilde prefix resolution, and WORKLOADS_DIR env precedence
This commit is contained in:
1 parent
9ac44d0baa
commit
9d7114b547
9 files changed
+156
-22
No files matched your search
+10
-10
@@ -1,48 +1,48 @@
|
|||||||
# Core Authoritative Repository Identity Registry
|
# Core Authoritative Repository Identity Registry
|
||||||
# Governs admitted continuous deployment targets for GitOps webhook execution.
|
# Governs admitted continuous deployment targets for GitOps webhook execution.
|
||||||
# Owns: repository identity, canonical filesystem path, and aliases.
|
# Owns: repository identity, canonical filesystem path, and aliases.
|
||||||
# Enforces: strict path containment within ~/Sites.
|
# Enforces: strict path containment within ~/Workloads.
|
||||||
|
|
||||||
schemaVersion: "1.0"
|
schemaVersion: "1.0"
|
||||||
repositories:
|
repositories:
|
||||||
snappymail:
|
snappymail:
|
||||||
path: "~/Sites/snappymail"
|
path: "~/Workloads/snappymail"
|
||||||
aliases:
|
aliases:
|
||||||
- "homelab-snappymail"
|
- "homelab-snappymail"
|
||||||
- "mail"
|
- "mail"
|
||||||
- "webmail"
|
- "webmail"
|
||||||
gitea:
|
gitea:
|
||||||
path: "~/Sites/homelab-gitea"
|
path: "~/Workloads/homelab-gitea"
|
||||||
aliases:
|
aliases:
|
||||||
- "homelab-gitea"
|
- "homelab-gitea"
|
||||||
- "git"
|
- "git"
|
||||||
jellyfin:
|
jellyfin:
|
||||||
path: "~/Sites/homelab-jellyfin"
|
path: "~/Workloads/homelab-jellyfin"
|
||||||
aliases:
|
aliases:
|
||||||
- "homelab-jellyfin"
|
- "homelab-jellyfin"
|
||||||
- "media"
|
- "media"
|
||||||
courses:
|
courses:
|
||||||
path: "~/Sites/courses"
|
path: "~/Workloads/courses"
|
||||||
aliases:
|
aliases:
|
||||||
- "homelab-courses"
|
- "homelab-courses"
|
||||||
bdinvite:
|
bdinvite:
|
||||||
path: "~/Sites/homelab-bdinvite"
|
path: "~/Workloads/homelab-bdinvite"
|
||||||
aliases:
|
aliases:
|
||||||
- "homelab-bdinvite"
|
- "homelab-bdinvite"
|
||||||
- "birthday"
|
- "birthday"
|
||||||
landing:
|
landing:
|
||||||
path: "~/Sites/homelab-landing"
|
path: "~/Workloads/homelab-landing"
|
||||||
aliases:
|
aliases:
|
||||||
- "homelab-landing"
|
- "homelab-landing"
|
||||||
mermaid:
|
mermaid:
|
||||||
path: "~/Sites/homelab-mermaid"
|
path: "~/Workloads/homelab-mermaid"
|
||||||
aliases:
|
aliases:
|
||||||
- "homelab-mermaid"
|
- "homelab-mermaid"
|
||||||
excalidraw:
|
excalidraw:
|
||||||
path: "~/Sites/homelab-excalidraw"
|
path: "~/Workloads/homelab-excalidraw"
|
||||||
aliases:
|
aliases:
|
||||||
- "homelab-excalidraw"
|
- "homelab-excalidraw"
|
||||||
houston:
|
houston:
|
||||||
path: "~/Sites/homelab-houston"
|
path: "~/Workloads/homelab-houston"
|
||||||
aliases:
|
aliases:
|
||||||
- "homelab-houston"
|
- "homelab-houston"
|
||||||
@@ -23,6 +23,8 @@
|
|||||||
enable = true;
|
enable = true;
|
||||||
config.safe.directory = [
|
config.safe.directory = [
|
||||||
"/home/${config.homelab.operatorUser}/Core"
|
"/home/${config.homelab.operatorUser}/Core"
|
||||||
|
"/home/${config.homelab.operatorUser}/Workloads"
|
||||||
|
"/home/${config.homelab.operatorUser}/Workloads/*"
|
||||||
"/home/${config.homelab.operatorUser}/Sites/*"
|
"/home/${config.homelab.operatorUser}/Sites/*"
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -23,8 +23,8 @@
|
|||||||
};
|
};
|
||||||
sitesDir = lib.mkOption {
|
sitesDir = lib.mkOption {
|
||||||
type = lib.types.str;
|
type = lib.types.str;
|
||||||
default = "/home/${config.homelab.operatorUser}/Sites";
|
default = "/home/${config.homelab.operatorUser}/Workloads";
|
||||||
description = "Path to Sites directory";
|
description = "Path to Workloads directory (legacy option name: sitesDir)";
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -124,6 +124,8 @@
|
|||||||
GITOPS_APPCTL_TOKEN_FILE = config.sops.secrets."gitops/appctl_token".path;
|
GITOPS_APPCTL_TOKEN_FILE = config.sops.secrets."gitops/appctl_token".path;
|
||||||
DOCKER_CONFIG = "/home/${config.homelab.operatorUser}/.local/state/homelab/docker";
|
DOCKER_CONFIG = "/home/${config.homelab.operatorUser}/.local/state/homelab/docker";
|
||||||
HOMELAB_STATE_ROOT = "/home/${config.homelab.operatorUser}/var/lib/homelab";
|
HOMELAB_STATE_ROOT = "/home/${config.homelab.operatorUser}/var/lib/homelab";
|
||||||
|
WORKLOADS_DIR = config.homelab.sitesDir;
|
||||||
|
SITES_DIR = config.homelab.sitesDir;
|
||||||
};
|
};
|
||||||
|
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
@@ -198,6 +200,7 @@
|
|||||||
APPCTL_AUDIT_LOG = "/var/log/homelab/appctl/audit.jsonl";
|
APPCTL_AUDIT_LOG = "/var/log/homelab/appctl/audit.jsonl";
|
||||||
APPCTL_LOCK_DIR = "/var/lib/homelab/appctl/locks";
|
APPCTL_LOCK_DIR = "/var/lib/homelab/appctl/locks";
|
||||||
APPCTL_TOKENS_FILE = config.sops.secrets."appctl/tokens_json".path;
|
APPCTL_TOKENS_FILE = config.sops.secrets."appctl/tokens_json".path;
|
||||||
|
WORKLOADS_DIR = config.homelab.sitesDir;
|
||||||
SITES_DIR = config.homelab.sitesDir;
|
SITES_DIR = config.homelab.sitesDir;
|
||||||
CORE_DIR = config.homelab.coreDir;
|
CORE_DIR = config.homelab.coreDir;
|
||||||
HOMELAB_STATE_ROOT = "/home/${config.homelab.operatorUser}/var/lib/homelab";
|
HOMELAB_STATE_ROOT = "/home/${config.homelab.operatorUser}/var/lib/homelab";
|
||||||
|
|||||||
@@ -57,7 +57,12 @@ from pydantic import ValidationError
|
|||||||
logger = logging.getLogger("appctl_daemon")
|
logger = logging.getLogger("appctl_daemon")
|
||||||
|
|
||||||
CORE_DIR = Path(os.environ.get("CORE_DIR", Path(__file__).resolve().parent.parent))
|
CORE_DIR = Path(os.environ.get("CORE_DIR", Path(__file__).resolve().parent.parent))
|
||||||
SITES_DIR = Path(os.environ.get("SITES_DIR", os.path.expanduser("~/Sites")))
|
WORKLOADS_DIR = Path(
|
||||||
|
os.environ.get("WORKLOADS_DIR")
|
||||||
|
or os.environ.get("SITES_DIR")
|
||||||
|
or os.path.expanduser("~/Workloads")
|
||||||
|
)
|
||||||
|
SITES_DIR = WORKLOADS_DIR
|
||||||
DEPLOYMENTS_FILE = Path(
|
DEPLOYMENTS_FILE = Path(
|
||||||
os.environ.get("HOMELAB_DEPLOYMENTS_FILE", CORE_DIR / "config" / "deployments.yaml")
|
os.environ.get("HOMELAB_DEPLOYMENTS_FILE", CORE_DIR / "config" / "deployments.yaml")
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -37,12 +37,19 @@ CORE_DIR = os.environ.get(
|
|||||||
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
|
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
|
||||||
)
|
)
|
||||||
|
|
||||||
SITES_DIR = os.environ.get(
|
def _resolve_default_workloads_dir() -> str:
|
||||||
"SITES_DIR",
|
"""Resolve workloads root honoring WORKLOADS_DIR, SITES_DIR, sibling Workloads, or ~/Workloads."""
|
||||||
os.path.join(os.path.dirname(CORE_DIR), "Sites")
|
env_dir = os.environ.get("WORKLOADS_DIR") or os.environ.get("SITES_DIR")
|
||||||
if os.path.isdir(os.path.join(os.path.dirname(CORE_DIR), "Sites"))
|
if env_dir:
|
||||||
else os.path.expanduser("~/Sites"),
|
return env_dir
|
||||||
)
|
sibling_workloads = os.path.join(os.path.dirname(CORE_DIR), "Workloads")
|
||||||
|
if os.path.isdir(sibling_workloads):
|
||||||
|
return sibling_workloads
|
||||||
|
return os.path.expanduser("~/Workloads")
|
||||||
|
|
||||||
|
|
||||||
|
WORKLOADS_DIR = _resolve_default_workloads_dir()
|
||||||
|
SITES_DIR = WORKLOADS_DIR
|
||||||
|
|
||||||
HOMELAB_DOMAIN = (
|
HOMELAB_DOMAIN = (
|
||||||
os.environ.get("HOMELAB_DOMAIN")
|
os.environ.get("HOMELAB_DOMAIN")
|
||||||
|
|||||||
@@ -36,7 +36,12 @@ try:
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
yaml = None
|
yaml = None
|
||||||
|
|
||||||
SITES_DIR = Path(os.environ.get("SITES_DIR", os.path.expanduser("~/Sites")))
|
WORKLOADS_DIR = Path(
|
||||||
|
os.environ.get("WORKLOADS_DIR")
|
||||||
|
or os.environ.get("SITES_DIR")
|
||||||
|
or os.path.expanduser("~/Workloads")
|
||||||
|
)
|
||||||
|
SITES_DIR = WORKLOADS_DIR
|
||||||
CORE_DIR = Path(os.environ.get("CORE_DIR", Path(__file__).resolve().parent.parent))
|
CORE_DIR = Path(os.environ.get("CORE_DIR", Path(__file__).resolve().parent.parent))
|
||||||
CONFIG_DIR = Path(
|
CONFIG_DIR = Path(
|
||||||
os.environ.get("HOMELAB_CONFIG_DIR", os.path.expanduser("~/.config/homelab"))
|
os.environ.get("HOMELAB_CONFIG_DIR", os.path.expanduser("~/.config/homelab"))
|
||||||
@@ -287,7 +292,10 @@ def get_trusted_repository_mapping() -> dict[str, Path]:
|
|||||||
raw_path = entry.get("path")
|
raw_path = entry.get("path")
|
||||||
if not raw_path:
|
if not raw_path:
|
||||||
continue
|
continue
|
||||||
if raw_path.startswith("~/Sites/") or raw_path == "~/Sites":
|
if raw_path.startswith("~/Workloads/") or raw_path == "~/Workloads":
|
||||||
|
sub = raw_path[len("~/Workloads") :].lstrip("/")
|
||||||
|
p = (SITES_DIR / sub).resolve() if sub else SITES_DIR.resolve()
|
||||||
|
elif raw_path.startswith("~/Sites/") or raw_path == "~/Sites":
|
||||||
sub = raw_path[len("~/Sites") :].lstrip("/")
|
sub = raw_path[len("~/Sites") :].lstrip("/")
|
||||||
p = (SITES_DIR / sub).resolve() if sub else SITES_DIR.resolve()
|
p = (SITES_DIR / sub).resolve() if sub else SITES_DIR.resolve()
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -228,6 +228,34 @@ def test_missing_or_corrupt_registry_fails_closed(tmp_path: Path, monkeypatch):
|
|||||||
|
|
||||||
# Missing registry must fail closed: empty mapping, no resolution
|
# Missing registry must fail closed: empty mapping, no resolution
|
||||||
assert gitops_dispatcher.get_trusted_repository_mapping() == {}
|
assert gitops_dispatcher.get_trusted_repository_mapping() == {}
|
||||||
|
|
||||||
|
|
||||||
|
def test_registry_resolves_workloads_and_sites_tilde_prefixes(
|
||||||
|
tmp_path: Path, monkeypatch
|
||||||
|
):
|
||||||
|
"""Ensure ~/Workloads/<app> and legacy ~/Sites/<app> paths resolve cleanly inside SITES_DIR."""
|
||||||
|
workloads_dir = tmp_path / "Workloads"
|
||||||
|
app_a = workloads_dir / "snappymail"
|
||||||
|
app_b = workloads_dir / "legacy-app"
|
||||||
|
app_a.mkdir(parents=True)
|
||||||
|
app_b.mkdir(parents=True)
|
||||||
|
|
||||||
|
registry = tmp_path / "deployments.yaml"
|
||||||
|
registry.write_text(
|
||||||
|
'schemaVersion: "1.0"\n'
|
||||||
|
"repositories:\n"
|
||||||
|
" snappymail:\n"
|
||||||
|
' path: "~/Workloads/snappymail"\n'
|
||||||
|
" legacy:\n"
|
||||||
|
' path: "~/Sites/legacy-app"\n',
|
||||||
|
encoding="utf-8",
|
||||||
|
)
|
||||||
|
|
||||||
|
monkeypatch.setattr(gitops_dispatcher, "SITES_DIR", workloads_dir)
|
||||||
|
monkeypatch.setattr(gitops_dispatcher, "DEPLOYMENTS_FILE", registry)
|
||||||
|
|
||||||
|
assert gitops_dispatcher.resolve_repository("snappymail") == app_a.resolve()
|
||||||
|
assert gitops_dispatcher.resolve_repository("legacy") == app_b.resolve()
|
||||||
assert gitops_dispatcher.resolve_repository("valid") is None
|
assert gitops_dispatcher.resolve_repository("valid") is None
|
||||||
|
|
||||||
# Corrupt YAML registry must also fail closed
|
# Corrupt YAML registry must also fail closed
|
||||||
|
|||||||
@@ -485,12 +485,18 @@ def test_nixos_switch_alias_uses_path_and_impure():
|
|||||||
|
|
||||||
|
|
||||||
def test_nixos_safe_directory_includes_core_and_sites():
|
def test_nixos_safe_directory_includes_core_and_sites():
|
||||||
"""Ensure programs.git.config.safe.directory includes Core and Sites to prevent dubious ownership errors."""
|
"""Ensure programs.git.config.safe.directory includes Core, Workloads, and Sites to prevent dubious ownership errors."""
|
||||||
config_nix = CORE_ROOT / "nixos" / "configuration.nix"
|
config_nix = CORE_ROOT / "nixos" / "configuration.nix"
|
||||||
content = config_nix.read_text(encoding="utf-8")
|
content = config_nix.read_text(encoding="utf-8")
|
||||||
assert '"/home/${config.homelab.operatorUser}/Core"' in content, (
|
assert '"/home/${config.homelab.operatorUser}/Core"' in content, (
|
||||||
"nixos/configuration.nix must include Core in git safe.directory"
|
"nixos/configuration.nix must include Core in git safe.directory"
|
||||||
)
|
)
|
||||||
|
assert '"/home/${config.homelab.operatorUser}/Workloads"' in content, (
|
||||||
|
"nixos/configuration.nix must include Workloads in git safe.directory"
|
||||||
|
)
|
||||||
|
assert '"/home/${config.homelab.operatorUser}/Workloads/*"' in content, (
|
||||||
|
"nixos/configuration.nix must include Workloads/* in git safe.directory"
|
||||||
|
)
|
||||||
assert '"/home/${config.homelab.operatorUser}/Sites/*"' in content, (
|
assert '"/home/${config.homelab.operatorUser}/Sites/*"' in content, (
|
||||||
"nixos/configuration.nix must include Sites/* in git safe.directory"
|
"nixos/configuration.nix must include Sites/* in git safe.directory"
|
||||||
)
|
)
|
||||||
@@ -521,9 +527,31 @@ def test_nixos_safe_directory_includes_core_and_sites():
|
|||||||
assert "/home/operator/Core" in safe_dirs, (
|
assert "/home/operator/Core" in safe_dirs, (
|
||||||
f"Expected '/home/operator/Core' in git safe.directory, got {safe_dirs}"
|
f"Expected '/home/operator/Core' in git safe.directory, got {safe_dirs}"
|
||||||
)
|
)
|
||||||
|
assert "/home/operator/Workloads" in safe_dirs, (
|
||||||
|
f"Expected '/home/operator/Workloads' in git safe.directory, got {safe_dirs}"
|
||||||
|
)
|
||||||
|
assert "/home/operator/Workloads/*" in safe_dirs, (
|
||||||
|
f"Expected '/home/operator/Workloads/*' in git safe.directory, got {safe_dirs}"
|
||||||
|
)
|
||||||
assert "/home/operator/Sites/*" in safe_dirs, (
|
assert "/home/operator/Sites/*" in safe_dirs, (
|
||||||
f"Expected '/home/operator/Sites/*' in git safe.directory, got {safe_dirs}"
|
f"Expected '/home/operator/Sites/*' in git safe.directory, got {safe_dirs}"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_nixos_homeserver_defaults_to_workloads_dir():
|
||||||
|
"""Ensure homeserver.nix defaults sitesDir to ~/Workloads and exports WORKLOADS_DIR + SITES_DIR (#33)."""
|
||||||
|
homeserver_nix = CORE_ROOT / "nixos" / "modules" / "homeserver.nix"
|
||||||
|
content = homeserver_nix.read_text(encoding="utf-8")
|
||||||
|
|
||||||
|
assert 'default = "/home/${config.homelab.operatorUser}/Workloads";' in content, (
|
||||||
|
"homelab.sitesDir must default to /home/${config.homelab.operatorUser}/Workloads"
|
||||||
|
)
|
||||||
|
assert "WORKLOADS_DIR = config.homelab.sitesDir;" in content, (
|
||||||
|
"homeserver.nix must export WORKLOADS_DIR"
|
||||||
|
)
|
||||||
|
assert "SITES_DIR = config.homelab.sitesDir;" in content, (
|
||||||
|
"homeserver.nix must export SITES_DIR for backward compatibility"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -554,3 +554,56 @@ def test_cmd_list_flags_manifest_error(
|
|||||||
engine.cmd_list([])
|
engine.cmd_list([])
|
||||||
captured = capsys.readouterr()
|
captured = capsys.readouterr()
|
||||||
assert "Manifest Error" in captured.out
|
assert "Manifest Error" in captured.out
|
||||||
|
|
||||||
|
|
||||||
|
def test_workloads_dir_env_precedence_and_fallback(
|
||||||
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||||
|
):
|
||||||
|
"""Ensure WORKLOADS_DIR > SITES_DIR > ~/Workloads fallback across core modules (#33)."""
|
||||||
|
import importlib
|
||||||
|
|
||||||
|
import appctl_daemon
|
||||||
|
import gitops_dispatcher
|
||||||
|
|
||||||
|
workloads_override = tmp_path / "custom-workloads"
|
||||||
|
sites_override = tmp_path / "legacy-sites"
|
||||||
|
fake_home = tmp_path / "home"
|
||||||
|
fake_core = tmp_path / "repo" / "core"
|
||||||
|
fake_core.mkdir(parents=True)
|
||||||
|
|
||||||
|
try:
|
||||||
|
# 1. WORKLOADS_DIR takes precedence over SITES_DIR
|
||||||
|
monkeypatch.setenv("CORE_DIR", str(fake_core))
|
||||||
|
monkeypatch.setenv("WORKLOADS_DIR", str(workloads_override))
|
||||||
|
monkeypatch.setenv("SITES_DIR", str(sites_override))
|
||||||
|
importlib.reload(engine)
|
||||||
|
importlib.reload(appctl_daemon)
|
||||||
|
importlib.reload(gitops_dispatcher)
|
||||||
|
assert engine.SITES_DIR == str(workloads_override)
|
||||||
|
assert appctl_daemon.SITES_DIR == workloads_override
|
||||||
|
assert gitops_dispatcher.SITES_DIR == workloads_override
|
||||||
|
|
||||||
|
# 2. SITES_DIR acts as fallback when WORKLOADS_DIR is unset
|
||||||
|
monkeypatch.delenv("WORKLOADS_DIR", raising=False)
|
||||||
|
importlib.reload(engine)
|
||||||
|
importlib.reload(appctl_daemon)
|
||||||
|
importlib.reload(gitops_dispatcher)
|
||||||
|
assert engine.SITES_DIR == str(sites_override)
|
||||||
|
assert appctl_daemon.SITES_DIR == sites_override
|
||||||
|
assert gitops_dispatcher.SITES_DIR == sites_override
|
||||||
|
|
||||||
|
# 3. Default resolves to ~/Workloads when neither env var is set
|
||||||
|
monkeypatch.delenv("SITES_DIR", raising=False)
|
||||||
|
monkeypatch.setenv("HOME", str(fake_home))
|
||||||
|
importlib.reload(engine)
|
||||||
|
importlib.reload(appctl_daemon)
|
||||||
|
importlib.reload(gitops_dispatcher)
|
||||||
|
expected_default = fake_home / "Workloads"
|
||||||
|
assert engine.SITES_DIR == str(expected_default)
|
||||||
|
assert appctl_daemon.SITES_DIR == expected_default
|
||||||
|
assert gitops_dispatcher.SITES_DIR == expected_default
|
||||||
|
finally:
|
||||||
|
monkeypatch.undo()
|
||||||
|
importlib.reload(engine)
|
||||||
|
importlib.reload(appctl_daemon)
|
||||||
|
importlib.reload(gitops_dispatcher)
|
||||||
Reference in new issue
Block a user