car-tracker/ops/ssh/host_run.py
Antoine Vandeplanque e57144c9b1
All checks were successful
CI / Lint, PostgreSQL tests, and package (push) Successful in 12s
CI / Build and publish tested image (push) Successful in 1m14s
run selected releases through restricted SSH instead of GitOps polling
2026-10-01 17:20:40 +02:00

49 lines
1.7 KiB
Python

"""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())