#!/usr/bin/env python3 """Convert read-only Docker or Argo CD snapshots to status report rows.""" import json import sys def clean(value): return str(value).replace("\t", " ").replace("\r", " ").replace("\n", " ") def emit(status, name, detail): print("\t".join(map(clean, (status, name, detail)))) def compose(lines): count = 0 for line in lines: if not line.strip(): continue item = json.loads(line) labels = dict(part.split("=", 1) for part in item.get("Labels", "").split(",") if "=" in part) if labels.get("com.docker.compose.oneoff", "").lower() == "true": continue project = labels.get("com.docker.compose.project") service = labels.get("com.docker.compose.service") if not project or not service: continue state = item.get("State", "unknown").lower() description = item.get("Status", state) unhealthy = "(unhealthy)" in description.lower() or "(health: starting)" in description.lower() status = "ok" if state == "running" and not unhealthy else "warn" name = item.get("Names", service) emit(status, f"{project}/{service} ({name})", description) count += 1 if not count: emit("skip", "Compose services", "No Compose-managed containers found; absent services cannot be assessed") def runner_candidate(document): items = document.get("items") if not isinstance(items, list): raise ValueError("Invalid Kubernetes application list") for app in items: if (app.get("metadata") or {}).get("name") != "gitea-runner": continue status = app.get("status") or {} if ((status.get("health") or {}).get("status") != "Progressing" or (status.get("sync") or {}).get("status") != "Synced" or status.get("conditions")): return found = False for resource in status.get("resources") or []: is_runner = (resource.get("kind") == "Deployment" and resource.get("name") == "gitea-runner" and resource.get("namespace") == "gitea-runner") found = found or is_runner health = (resource.get("health") or {}).get("status") if resource.get("status") in ("OutOfSync", "Unknown"): return if health and health != "Healthy" and not (is_runner and health == "Progressing"): return revision = (status.get("sync") or {}).get("revision") if found and isinstance(revision, str) and revision: print(revision) return def applications(document, expected_runner=False): items = document.get("items") if not isinstance(items, list): raise ValueError("Invalid Kubernetes application list") if not items: emit("skip", "Argo CD applications", "No Argo CD applications found") for app in sorted(items, key=lambda item: item.get("metadata", {}).get("name", "")): metadata, status = app.get("metadata", {}), app.get("status", {}) name = metadata.get("name", "unknown") health = status.get("health", {}).get("status", "Unknown") sync = status.get("sync", {}).get("status", "Unknown") if name == "gitea-runner" and health == "Progressing" and sync == "Synced" and expected_runner: severity = "ok" elif health in ("Degraded", "Missing"): severity = "fail" elif health != "Healthy" or sync != "Synced": severity = "warn" else: severity = "ok" suffix = "; zero-replica Kubernetes runner is expected; Debian runner service is active" if ( name == "gitea-runner" and health == "Progressing" and sync == "Synced" and expected_runner) else "" emit(severity, name, f"health={health}, sync={sync}{suffix}") def main(): try: if len(sys.argv) not in (2, 3): raise ValueError("Expected compose or applications") if sys.argv[1] == "compose": compose(sys.stdin) elif sys.argv[1] == "applications": if len(sys.argv) == 3 and sys.argv[2] != "--expected-runner": raise ValueError("Unknown applications option") applications(json.load(sys.stdin), len(sys.argv) == 3) elif sys.argv[1] == "runner-candidate" and len(sys.argv) == 2: runner_candidate(json.load(sys.stdin)) else: raise ValueError("Unknown snapshot type") except (ValueError, TypeError, AttributeError) as exc: print(f"Cannot interpret status snapshot: {exc}", file=sys.stderr) return 1 return 0 if __name__ == "__main__": sys.exit(main())