"""Forced SSH command: accept only a fixed verb, UUID, revision and image digest.""" import fcntl import json import os from pathlib import Path import subprocess import sys from reconcile import attempt, validate_request def parse_command(value): parts = value.split() if len(parts) != 4 or parts[0] != "run": raise ValueError("Only the fixed run command is allowed") return validate_request({"schema_version": 1, "request_id": parts[1], "revision": parts[2], "image": parts[3]}) def run(root, request): state = root / "state/requests" state.mkdir(parents=True, mode=0o700, exist_ok=True) with (root / "state/agent.lock").open("a") as lock: try: fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB) except BlockingIOError: raise ValueError("Another job is active") for path in state.glob("*.json"): if json.loads(path.read_text()).get("status") == "attempted": raise ValueError("Unknown previous outcome requires investigation") return attempt(request, state, root / "compose.runtime.yaml", root / "app.env", private_log=True) def main(): os.umask(0o077) try: # Fixed location: the SSH caller cannot override the stack or environment. root = Path.home() / "containers/car-tracker" request = parse_command(os.environ.get("SSH_ORIGINAL_COMMAND", "")) return run(root, request) except (OSError, ValueError, KeyError, TypeError, subprocess.SubprocessError): print("Run refused or failed before completion. Inspect private host state/logs; do not automatically retry.") return 70 if __name__ == "__main__": sys.exit(main())