From 5aae4ef0d6c49c565abae849702a69a5db5194f8 Mon Sep 17 00:00:00 2001 From: kiskaadee Date: Sun, 13 Sep 2026 03:14:15 -0500 Subject: [PATCH] feat(gitops): add serialized asynchronous deployment --- scripts/gitops_dispatcher.py | 107 ++++++++++++++++++++++++- tests/security/test_gitops_security.py | 81 +++++++++++++++++++ 2 files changed, 186 insertions(+), 2 deletions(-) diff --git a/scripts/gitops_dispatcher.py b/scripts/gitops_dispatcher.py index 160ea46..21f13ce 100755 --- a/scripts/gitops_dispatcher.py +++ b/scripts/gitops_dispatcher.py @@ -7,6 +7,9 @@ in ~/Sites (via app.yaml) or data vaults (like ~/Brain), and executes the declar deployment actions with zero Core restarts. """ +import contextlib +import datetime +import fcntl import hashlib import hmac import json @@ -21,6 +24,7 @@ SITES_DIR = Path(os.environ.get("SITES_DIR", os.path.expanduser("~/Sites"))) BRAIN_DIR = Path(os.environ.get("BRAIN_DIR", os.path.expanduser("~/Brain"))) CONFIG_DIR = Path(os.environ.get("HOMELAB_CONFIG_DIR", os.path.expanduser("~/.config/homelab"))) DEFAULT_SECRET_PATH = Path(os.environ.get("GITOPS_SECRET_FILE", "/run/secrets/gitops/webhook_secret")) +STATE_DIR = Path(os.environ.get("GITOPS_STATE_DIR", Path.home() / ".local/state/homelab/gitops")) logging.basicConfig( level=logging.INFO, @@ -479,6 +483,93 @@ def admit_deployment( return True, target_dir, deployment_config, pushed_branch, f"Admitted for deployment on branch '{pushed_branch}'." +def get_target_lock(target_name: str) -> Path: + STATE_DIR.mkdir(parents=True, exist_ok=True) + return STATE_DIR / f"{target_name}.lock" + + +def get_pending_file(target_name: str) -> Path: + STATE_DIR.mkdir(parents=True, exist_ok=True) + return STATE_DIR / f"{target_name}.pending.json" + + +def enqueue_deployment(target_dir: Path, deployment_config: dict, branch: str) -> Path: + """ + Atomically enqueue a deployment request. + Supersedes any existing pending request for this target. + """ + target_name = target_dir.name + pending_file = get_pending_file(target_name) + temp_file = STATE_DIR / f"{target_name}.pending.tmp.{os.getpid()}" + + data = { + "target_dir": str(target_dir), + "deployment_config": deployment_config, + "branch": branch, + "queued_at": datetime.datetime.now(datetime.timezone.utc).isoformat(), + } + + temp_file.write_text(json.dumps(data), encoding="utf-8") + temp_file.replace(pending_file) + logger.info(f"📥 Enqueued deployment for '{target_name}' (branch: {branch}, superseding previous pending).") + return pending_file + + +def run_target_worker(target_name: str) -> bool: + """ + Serialized worker for a specific repository. + Acquires exclusive flock and processes pending deployments until queue is empty. + """ + lock_file_path = get_target_lock(target_name) + pending_file_path = get_pending_file(target_name) + + with open(lock_file_path, "w", encoding="utf-8") as lock_fd: + try: + fcntl.flock(lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB) + except (BlockingIOError, OSError): + logger.info(f"Deployment worker for '{target_name}' is already active. Pending update will be picked up.") + return True + + overall_success = True + try: + while True: + if not pending_file_path.is_file(): + break + + try: + content = pending_file_path.read_text(encoding="utf-8") + pending_file_path.unlink(missing_ok=True) + item = json.loads(content) + except (OSError, json.JSONDecodeError) as e: + logger.error(f"Failed to read pending deployment file: {e}") + break + + target_dir = Path(item["target_dir"]) + deployment_config = item["deployment_config"] + branch = item["branch"] + + success = execute_deployment(target_dir, deployment_config, branch) + if not success: + overall_success = False + + return overall_success + finally: + with contextlib.suppress(OSError): + fcntl.flock(lock_fd, fcntl.LOCK_UN) + + +def spawn_worker_async(target_name: str): + """Spawn a detached worker process to execute deployments asynchronously.""" + dispatcher_script = Path(__file__).resolve() + cmd = [sys.executable, str(dispatcher_script), "--worker", target_name] + subprocess.Popen( + cmd, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + start_new_session=True + ) + + def main(): payload_raw = "" repo_name = "" @@ -486,6 +577,16 @@ def main(): signature = None event_type = None + if "--worker" in sys.argv: + w_idx = sys.argv.index("--worker") + if w_idx + 1 < len(sys.argv): + worker_target = sys.argv[w_idx + 1] + success = run_target_worker(worker_target) + sys.exit(0 if success else 1) + else: + logger.error("--worker requires a repository/target name.") + sys.exit(1) + if "--signature" in sys.argv: sig_idx = sys.argv.index("--signature") if sig_idx + 1 < len(sys.argv): @@ -555,8 +656,10 @@ def main(): sys.exit(0 if "Branch mismatch" in reason else 1) logger.info(f"✅ Webhook admission passed: {reason}") - success = execute_deployment(target_dir, deployment_config, branch) - sys.exit(0 if success else 1) + enqueue_deployment(target_dir, deployment_config, branch) + spawn_worker_async(target_dir.name) + logger.info(f"🚀 Asynchronous deployment scheduled for '{target_dir.name}'. Returning HTTP 200.") + sys.exit(0) if __name__ == "__main__": diff --git a/tests/security/test_gitops_security.py b/tests/security/test_gitops_security.py index 1daf46b..96c766e 100644 --- a/tests/security/test_gitops_security.py +++ b/tests/security/test_gitops_security.py @@ -2,6 +2,7 @@ Security invariant tests for homelab GitOps execution and admission. """ +import json import sys from pathlib import Path from unittest.mock import MagicMock, patch @@ -360,4 +361,84 @@ def test_admission_admits_valid_push(tmp_path: Path, monkeypatch): assert config["actions"] == ["git_pull", "compose_up"] +def test_enqueue_supersedes_pending_revision(tmp_path: Path, monkeypatch): + """Ensure newer pending deployment overwrites older pending deployment.""" + state_dir = tmp_path / "state" + monkeypatch.setattr(gitops_dispatcher, "STATE_DIR", state_dir) + + target_dir = tmp_path / "app" + target_dir.mkdir() + + # Enqueue revision A + gitops_dispatcher.enqueue_deployment(target_dir, {"branch": "main", "rev": "A"}, "main") + pending_file = gitops_dispatcher.get_pending_file("app") + assert json.loads(pending_file.read_text())["deployment_config"]["rev"] == "A" + + # Enqueue revision B (supersedes A) + gitops_dispatcher.enqueue_deployment(target_dir, {"branch": "main", "rev": "B"}, "main") + assert json.loads(pending_file.read_text())["deployment_config"]["rev"] == "B" + + # Enqueue revision C (supersedes B) + gitops_dispatcher.enqueue_deployment(target_dir, {"branch": "main", "rev": "C"}, "main") + assert json.loads(pending_file.read_text())["deployment_config"]["rev"] == "C" + + +def test_serialized_worker_race_superseding(tmp_path: Path, monkeypatch): + """ + Simulate race condition: + A starts + B arrives (enqueued) + C arrives (supersedes B) + A finishes + Verify: B is skipped, C is deployed. + """ + import threading + + state_dir = tmp_path / "state" + monkeypatch.setattr(gitops_dispatcher, "STATE_DIR", state_dir) + + target_dir = tmp_path / "app" + target_dir.mkdir() + + executed_revisions = [] + a_started_event = threading.Event() + b_and_c_enqueued_event = threading.Event() + + def mock_execute(target, config, branch): + rev = config.get("rev", "unknown") + executed_revisions.append(rev) + if rev == "A": + a_started_event.set() + # Wait until B and C are enqueued + b_and_c_enqueued_event.wait(timeout=2.0) + return True + + monkeypatch.setattr(gitops_dispatcher, "execute_deployment", mock_execute) + + # 1. Enqueue A + gitops_dispatcher.enqueue_deployment(target_dir, {"branch": "main", "rev": "A"}, "main") + + # Start worker in thread (takes A) + worker_thread = threading.Thread(target=gitops_dispatcher.run_target_worker, args=("app",)) + worker_thread.start() + + # Wait for A to start executing + assert a_started_event.wait(timeout=2.0) is True + + # 2. B arrives while A is running + gitops_dispatcher.enqueue_deployment(target_dir, {"branch": "main", "rev": "B"}, "main") + + # 3. C arrives while A is running (supersedes B) + gitops_dispatcher.enqueue_deployment(target_dir, {"branch": "main", "rev": "C"}, "main") + + # Let A complete + b_and_c_enqueued_event.set() + + worker_thread.join(timeout=3.0) + + # Verification: A was executed, B was superseded/skipped, C was executed! + assert executed_revisions == ["A", "C"], f"Expected ['A', 'C'] but got {executed_revisions}" + + +