my-homelab-configs/scripts/heal

789 lines
27 KiB
Python
Executable File

#!/usr/bin/env python3
"""Plan and apply guarded self-healing actions from Jeannie status findings."""
from __future__ import annotations
import argparse
import json
import os
import re
import subprocess
import sys
import time
from dataclasses import dataclass
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent))
from cluster_runtime_context import resolve, environment
REPO_ROOT = Path(__file__).resolve().parents[1]
CLUSTER = resolve()
os.environ.update(environment(CLUSTER["profile"]))
DEFAULT_KUBECONFIG = Path(CLUSTER["kubeconfig"])
STATE_DIR = Path(
os.environ.get(
"HOMELAB_STATE_DIR",
Path(os.environ.get("XDG_DATA_HOME", Path.home() / ".local/share")) / "homelab",
)
)
HEAL_STATE_FILE = Path(
os.environ.get("LAB_HEAL_STATE_FILE", Path(CLUSTER["root"]) / "heal-state.json")
)
HEAL_COOLDOWN_SECONDS = int(os.environ.get("LAB_HEAL_COOLDOWN_SECONDS", "1800"))
@dataclass(frozen=True)
class HealRule:
check: str
command: str
risk: str
reason: str
auto: bool
priority: int
RULES = [
HealRule(
"Kubernetes API",
"./jeannie start-cluster",
"low",
"API is down; start-cluster starts the selected runtime without destroying state.",
True,
100,
),
HealRule(
"Pimox workers running",
"./jeannie start-cluster",
"low",
"Worker VMs are expected cluster capacity and start-cluster is idempotent.",
True,
95,
),
HealRule(
"Kubernetes nodes Ready",
"./jeannie start-cluster",
"low",
"NotReady worker nodes commonly recover by starting the saved cluster runtime.",
True,
90,
),
HealRule(
"Pi-hole DNS",
"./jeannie rpi-services",
"medium",
"Reapplies RPi DNS services; safe for the lab but can briefly disrupt DNS.",
True,
80,
),
HealRule(
"RPi Docker root state",
"./jeannie rpi-services",
"medium",
"May repair service runtime but should be reviewed if storage is failing.",
True,
78,
),
HealRule(
"Uptime Kuma HTTP",
"./jeannie rpi-services",
"medium",
"Reapplies RPi service Compose stack.",
True,
70,
),
HealRule(
"Gitea container",
"./jeannie deploy-gitea",
"medium",
"Reapplies the Debian-hosted Gitea Compose service.",
True,
75,
),
HealRule(
"Gitea local HTTP",
"./jeannie deploy-gitea",
"medium",
"Local Gitea HTTP is down; redeploying Compose is usually safe but still mutates Git service runtime.",
True,
74,
),
HealRule(
"Traefik deployment",
"./jeannie promote validate",
"diagnostic",
"Validate gates first; deployment rollout failures are usually downstream of cluster/node health.",
False,
65,
),
HealRule(
"Website deployment",
"./jeannie promote validate",
"diagnostic",
"Validate gates first; deployment rollout failures are usually downstream of cluster/node health.",
False,
64,
),
HealRule(
"Traefik LoadBalancer HTTP",
"./jeannie doctor-edge",
"diagnostic",
"Edge/LB failures need diagnosis after cluster nodes are healthy.",
False,
60,
),
HealRule(
"Website public URL",
"./jeannie doctor-edge",
"diagnostic",
"Public URL failures need edge and cluster diagnosis after local services recover.",
False,
58,
),
HealRule(
"Gitea public route",
"./jeannie doctor-gitea",
"diagnostic",
"Public Gitea route needs Gitea and edge diagnosis.",
False,
55,
),
HealRule(
"No problem pods",
"./jeannie heal cleanup-stale-pods --yes",
"low",
"Only deletes stale Evicted/ContainerStatusUnknown pods; live pods and PVCs are untouched.",
True,
88,
),
HealRule(
"Recent deployments healthy",
"./jeannie explain status",
"diagnostic",
"Deployment drift can be a symptom of node health, image pulls, or scheduling.",
False,
45,
),
HealRule(
"Pod restart pressure",
"./jeannie explain status",
"diagnostic",
"Restart pressure needs workload-specific diagnosis before mutation.",
False,
40,
),
HealRule(
"Traefik 5xx/404 signals",
"./jeannie doctor-edge",
"diagnostic",
"Use edge logs and route checks before changing config.",
False,
35,
),
]
def load_status(path: Path | None) -> dict[str, object]:
if path:
return json.loads(path.read_text(encoding="utf-8"))
process = subprocess.run(
[str(REPO_ROOT / "jeannie"), "status", "--deep", "--json"],
cwd=REPO_ROOT,
capture_output=True,
text=True,
check=False,
)
if not process.stdout.strip():
print(process.stderr.strip() or "status produced no JSON", file=sys.stderr)
raise SystemExit(1)
return json.loads(process.stdout)
def status_rows(status: dict[str, object]) -> list[dict[str, str]]:
rows = status.get("rows", [])
if not isinstance(rows, list):
return []
return [
row
for row in rows
if isinstance(row, dict) and row.get("status") in {"fail", "warn"}
]
def rule_for(row: dict[str, str]) -> HealRule | None:
check = str(row.get("check", ""))
if CLUSTER["runtime"] == "k3s" and ("Pimox" in check or "VM" in check):
return None
check = {"API ready": "Kubernetes API", "API": "Kubernetes API",
"Problem pods": "No problem pods",
"Nodes Ready": "Kubernetes nodes Ready", "Control-plane runtime": "Kubernetes API",
"Workers running": "Pimox workers running", "Worker VMs running": "Pimox workers running",
"Control-plane VM running": "Kubernetes API"}.get(check, check)
for rule in RULES:
if rule.check == check:
return rule
return None
def pimox_worker_index(row: dict[str, str]) -> int | None:
text = " ".join(str(row.get(key, "")) for key in ("summary", "detail", "check"))
match = re.search(r"\bpimox-worker-(\d{1,2})\b", text)
if not match:
return None
return int(match.group(1))
def command_for(row: dict[str, str], rule: HealRule) -> tuple[str, str, int]:
if rule.check == "Kubernetes nodes Ready":
index = pimox_worker_index(row)
if index is not None and CLUSTER["runtime"] != "k3s":
return (
f"./jeannie workers restart {index}",
f"Restart Pimox VM for pimox-worker-{index:02d}; a running VM with a NotReady kubelet can be wedged.",
98,
)
if rule.check == "No problem pods":
text = " ".join(str(row.get(key, "")) for key in ("summary", "detail", "check"))
if not re.search(
r"\b(Evicted|ContainerStatusUnknown|NodeLost|Unknown|Error)\b", text
):
return (
"./jeannie explain status",
"Problem pods are not stale residual pod records; keep this diagnostic-only.",
50,
)
return rule.command, rule.reason, rule.priority
def build_plan(status: dict[str, object]) -> list[dict[str, object]]:
if status.get("mode") == "fast":
raise SystemExit("Heal refused: fast status omits diagnostic evidence; collect status --deep --json")
report_cluster = status.get("cluster") or {}
if (report_cluster.get("profile") != CLUSTER["profile"] or report_cluster.get("kubeconfig") != CLUSTER["kubeconfig"]
or not CLUSTER["uid"] or report_cluster.get("uid") != CLUSTER["uid"]):
raise SystemExit("Heal refused: status report cluster context is missing or differs from the selected profile; collect a fresh status report")
planned: list[dict[str, object]] = []
seen_commands: set[str] = set()
blocked = recent_failed_commands()
for row in status_rows(status):
rule = rule_for(row)
if rule is None:
planned.append(
{
"check": row.get("check", ""),
"area": row.get("area", ""),
"status": row.get("status", ""),
"command": "./jeannie explain status",
"risk": "diagnostic",
"auto": False,
"priority": 1,
"reason": "No specific heal rule exists yet.",
"summary": row.get("summary", ""),
}
)
continue
command, reason, priority = command_for(row, rule)
auto = rule.auto
risk = rule.risk
if rule.check == "No problem pods" and command == "./jeannie explain status":
auto = False
risk = "diagnostic"
if command in blocked:
failed_finding = blocked[command]
auto = False
risk = "diagnostic"
command = "./jeannie doctor-cluster --details"
reason = f"Previous heal action did not clear this finding recently; escalate to diagnostics before retrying. Last failure: {failed_finding}"
priority = max(priority - 20, 1)
if command in seen_commands and auto:
continue
seen_commands.add(command)
planned.append(
{
"check": row.get("check", ""),
"area": row.get("area", ""),
"status": row.get("status", ""),
"command": command,
"risk": risk,
"auto": auto,
"priority": priority,
"reason": reason,
"summary": row.get("summary", ""),
}
)
return planned
def impact_sort_key(item: dict[str, object]) -> tuple[int, int, str]:
status_score = 10 if item.get("status") == "fail" else 0
return (
int(item.get("priority") or 0) + status_score,
1 if item.get("auto") else 0,
str(item.get("check") or ""),
)
def highest_impact(
plan: list[dict[str, object]], *, auto_only: bool
) -> dict[str, object] | None:
candidates = (
[item for item in plan if item.get("auto")] if auto_only else list(plan)
)
if not candidates:
return None
return max(candidates, key=impact_sort_key)
def print_plan(plan: list[dict[str, object]], ai: bool) -> None:
target = highest_impact(plan, auto_only=True)
top_overall = highest_impact(plan, auto_only=False)
deferred = [
item
for item in sorted(plan, key=impact_sort_key, reverse=True)
if item is not target
]
print("Jeannie Heal Plan")
print("=================")
print("mode: one-at-a-time")
print(f"profile={CLUSTER['profile']} runtime={CLUSTER['runtime']} kubeconfig={DEFAULT_KUBECONFIG}")
print(f"findings={len(plan)}")
print()
if target:
print("Next heal target:")
print(f" command: {target['command']}")
print(f" finding: {target['area']} / {target['check']} ({target['status']})")
print(f" impact: {target['priority']}")
print(f" why: {target['reason']}")
else:
print("Next heal target: none")
if top_overall:
print(
f"highest finding is diagnostic-only: {top_overall['area']} / {top_overall['check']}"
)
print(f"next diagnostic command: {top_overall['command']}")
print()
if deferred:
print("Deferred until next status/heal cycle:")
for item in deferred[:8]:
auto_marker = "auto" if item.get("auto") else "diagnostic"
print(
f" - {item['area']} / {item['check']} ({auto_marker}, impact={item['priority']})"
)
if ai:
print_ai_context([target] if target else ([top_overall] if top_overall else []))
def print_ai_context(plan: list[dict[str, object]]) -> None:
query = " ".join(str(item["check"]) for item in plan[:5])
if not query:
return
process = subprocess.run(
[
str(REPO_ROOT / "scripts" / "query-homelab-ai-index"),
"--citations",
"--limit",
"3",
query,
],
cwd=REPO_ROOT,
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
check=False,
)
if process.returncode == 0 and process.stdout.strip():
print()
print("AI/RAG Context")
print("--------------")
print(process.stdout.strip())
def apply_plan(plan: list[dict[str, object]], yes: bool) -> int:
target = highest_impact(plan, auto_only=True)
if not target:
print("No auto-eligible heal actions.")
return 0
print("Jeannie Heal Apply")
print("==================")
command = str(target["command"])
sandbox = subprocess.run(
[str(REPO_ROOT / "scripts" / "agent-sandbox"), "check", command, "--json"],
cwd=REPO_ROOT,
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
check=False,
)
try:
decision = json.loads(sandbox.stdout).get("decision")
except json.JSONDecodeError:
decision = "approval"
if decision != "allow":
print(f"skip: {command} blocked by agent sandbox decision={decision}")
return 1
print(f"target: {target['area']} / {target['check']}")
if not yes:
print(f"would run: {command}")
else:
if not cluster_identity_ok(startup=command == "./jeannie start-cluster"):
return 1
print(f"run: {command}")
process = subprocess.run(command.split(), cwd=REPO_ROOT, text=True, check=False)
if process.returncode != 0:
record_failed_action(command, target)
return process.returncode
if verify_healed(target):
print(f"healed: {target['area']} / {target['check']}")
clear_failed_action(command)
else:
print(f"still failing: {target['area']} / {target['check']}")
print("Run ./jeannie status --details for the current evidence.")
record_failed_action(command, target)
return 1
if not yes:
print()
print("Dry run only. Re-run with: ./jeannie heal apply --yes")
print(
"After it runs, use ./jeannie status again before healing the next finding."
)
else:
print("Run ./jeannie status again before healing the next finding.")
return 0
def controller_owner(metadata: dict[str, object]) -> dict[str, str] | None:
owner_refs = metadata.get("ownerReferences") or []
if not isinstance(owner_refs, list):
return None
for owner in owner_refs:
if not isinstance(owner, dict):
continue
if owner.get("controller") is True:
return {
"kind": str(owner.get("kind") or ""),
"name": str(owner.get("name") or ""),
}
return None
def safe_controller_owned(owner: dict[str, str] | None) -> bool:
return bool(owner and owner.get("kind") in {"DaemonSet", "Job", "ReplicaSet", "StatefulSet"})
def stale_reason(status: dict[str, object]) -> str:
phase = str(status.get("phase", ""))
reason = str(status.get("reason", ""))
reasons = {reason} if reason else set()
for container_status in status.get("containerStatuses", []) or []:
if not isinstance(container_status, dict):
continue
state = container_status.get("state", {})
if not isinstance(state, dict):
continue
waiting = state.get("waiting") or {}
terminated = state.get("terminated") or {}
if isinstance(waiting, dict) and waiting.get("reason"):
reasons.add(str(waiting.get("reason")))
if isinstance(terminated, dict) and terminated.get("reason"):
reasons.add(str(terminated.get("reason")))
if reason in {"Evicted", "NodeLost"}:
return reason
if phase == "Unknown":
return "Unknown"
if "ContainerStatusUnknown" in reasons:
return "ContainerStatusUnknown"
if phase == "Failed" and reasons & {"Error", "NodeLost", "ContainerStatusUnknown"}:
return ",".join(sorted(reasons & {"Error", "NodeLost", "ContainerStatusUnknown"}))
return ""
def stale_pods(timeout=30) -> tuple[list[dict[str, str]], list[dict[str, str]]]:
try:
process = subprocess.run(
["kubectl", "--kubeconfig", str(DEFAULT_KUBECONFIG), "get", "pods", "-A", "-o", "json"],
cwd=REPO_ROOT,
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=False,
timeout=timeout,
)
except (OSError, subprocess.TimeoutExpired) as error:
print(f"Cannot read pods: {error}", file=sys.stderr)
raise SystemExit(1)
if process.returncode != 0:
print(process.stderr.strip() or "kubectl get pods failed", file=sys.stderr)
raise SystemExit(process.returncode)
document = json.loads(process.stdout)
pods: list[dict[str, str]] = []
skipped: list[dict[str, str]] = []
for item in document.get("items", []):
metadata = item.get("metadata", {})
status = item.get("status", {})
if not isinstance(metadata, dict) or not isinstance(status, dict):
continue
annotations = metadata.get("annotations") or {}
if isinstance(annotations, dict) and annotations.get("kubernetes.io/config.mirror"):
continue
namespace = str(metadata.get("namespace", "default"))
name = str(metadata.get("name", ""))
phase = str(status.get("phase", ""))
reason = stale_reason(status)
if not reason:
continue
owner = controller_owner(metadata)
pod = {
"namespace": namespace,
"name": name,
"phase": phase,
"reason": reason,
"owner": f"{owner['kind']}/{owner['name']}" if owner else "none",
}
if safe_controller_owned(owner):
pods.append(pod)
else:
skipped.append(pod)
return pods, skipped
def cleanup_stale_pods(yes: bool) -> int:
pods, skipped = stale_pods()
print("Jeannie Stale Pod Cleanup")
print("=========================")
if not pods and not skipped:
print("No stale residual pods found.")
return 0
print(f"targets={len(pods)}")
for pod in pods:
print(
f" - {pod['namespace']}/{pod['name']} reason={pod['reason']} owner={pod['owner']}"
)
if skipped:
print(f"skipped={len(skipped)}")
for pod in skipped:
print(
f" - {pod['namespace']}/{pod['name']} reason={pod['reason']} owner={pod['owner']} unsafe-unmanaged"
)
if not yes:
print()
print("Dry run only. Re-run with: ./jeannie heal cleanup-stale-pods --yes")
return 0
for pod in pods:
if not cluster_identity_ok():
return 1
command = [
"kubectl",
"--kubeconfig",
str(DEFAULT_KUBECONFIG),
"-n",
pod["namespace"],
"delete",
"pod",
pod["name"],
"--ignore-not-found=true",
]
print("+ " + " ".join(command))
process = subprocess.run(command, cwd=REPO_ROOT, text=True, check=False)
if process.returncode != 0:
return process.returncode
return 0
def cluster_identity_ok(startup=False, timeout=30) -> bool:
try:
result = subprocess.run(
[sys.executable, str(REPO_ROOT / "scripts/cluster-context.py"), "check-startup" if startup else "check", "--profile", CLUSTER["profile"], "--kubeconfig", str(DEFAULT_KUBECONFIG)],
cwd=REPO_ROOT, text=True, stdout=subprocess.DEVNULL, check=False, timeout=timeout,
)
return result.returncode == 0
except (OSError, subprocess.TimeoutExpired):
return False
def load_heal_state() -> dict[str, object]:
if not HEAL_STATE_FILE.is_file():
return {"failed_actions": {}}
try:
document = json.loads(HEAL_STATE_FILE.read_text(encoding="utf-8"))
except (OSError, json.JSONDecodeError):
return {"failed_actions": {}}
if not isinstance(document, dict):
return {"failed_actions": {}}
if not isinstance(document.get("failed_actions"), dict):
document["failed_actions"] = {}
return document
def write_heal_state(document: dict[str, object]) -> None:
HEAL_STATE_FILE.parent.mkdir(parents=True, exist_ok=True)
HEAL_STATE_FILE.write_text(
json.dumps(document, indent=2, sort_keys=True) + "\n", encoding="utf-8"
)
def recent_failed_commands() -> dict[str, str]:
now = int(time.time())
document = load_heal_state()
failed = document.get("failed_actions", {})
blocked: dict[str, str] = {}
changed = False
for command, info in list(failed.items()):
if not isinstance(info, dict):
failed.pop(command, None)
changed = True
continue
failed_at = int(info.get("failed_at", 0) or 0)
if now - failed_at > HEAL_COOLDOWN_SECONDS:
failed.pop(command, None)
changed = True
continue
blocked[command] = str(info.get("finding") or "unknown")
if changed:
document["failed_actions"] = failed
write_heal_state(document)
return blocked
def record_failed_action(command: str, target: dict[str, object]) -> None:
document = load_heal_state()
failed = document.setdefault("failed_actions", {})
if isinstance(failed, dict):
failed[command] = {
"failed_at": int(time.time()),
"finding": f"{target.get('area')} / {target.get('check')}",
"summary": target.get("summary", ""),
}
write_heal_state(document)
print(f"recorded failed heal action cooldown: {command}")
def clear_failed_action(command: str) -> None:
document = load_heal_state()
failed = document.get("failed_actions", {})
if isinstance(failed, dict) and command in failed:
failed.pop(command, None)
write_heal_state(document)
def verify_healed(target: dict[str, object]) -> bool:
timeout = int(os.environ.get("LAB_HEAL_VERIFY_TIMEOUT_SECONDS", "180"))
interval = int(os.environ.get("LAB_HEAL_VERIFY_INTERVAL_SECONDS", "10"))
deadline = time.monotonic() + timeout
while True:
remaining = max(0.1, deadline - time.monotonic())
result = target_is_healed(target, timeout=min(30, remaining))
if result is None:
print("No focused verification exists for this action; manual investigation is required.")
return False
if result:
return True
if time.monotonic() >= deadline:
return False
remaining = int(deadline - time.monotonic())
print(f"waiting for heal verification ({max(remaining, 0)}s left)...")
time.sleep(min(interval, max(0, deadline - time.monotonic())))
def target_is_healed(target: dict[str, object], timeout=30) -> bool | None:
command = str(target.get("command") or "")
deadline = time.monotonic() + timeout
if (command == "./jeannie start-cluster" or command == "./jeannie heal cleanup-stale-pods --yes"
or re.fullmatch(r"\./jeannie workers restart \d+", command)):
if not cluster_identity_ok(timeout=max(0.1, deadline - time.monotonic())):
return False
remaining = max(0.1, deadline - time.monotonic())
if command == "./jeannie heal cleanup-stale-pods --yes":
try:
eligible, _ = stale_pods(timeout=remaining)
except (SystemExit, ValueError, TypeError):
return False
# Pending workloads are a separate scheduling issue and cannot be
# fixed by removing terminal residual pod records.
return not eligible
worker_match = re.fullmatch(r"\./jeannie workers restart (\d+)", command)
check = str(target.get("check") or "")
if worker_match:
return pimox_worker_ready(int(worker_match.group(1)), timeout=remaining)
checks = {
("./jeannie start-cluster", "Kubernetes API"): "api",
("./jeannie start-cluster", "API ready"): "api",
("./jeannie start-cluster", "API"): "api",
("./jeannie start-cluster", "Kubernetes nodes Ready"): "nodes",
("./jeannie start-cluster", "Nodes Ready"): "nodes",
("./jeannie start-cluster", "Control-plane runtime"): "runtime",
("./jeannie start-cluster", "Control-plane VM running"): "control-vm",
("./jeannie start-cluster", "Pimox workers running"): "worker-vms",
("./jeannie start-cluster", "Workers running"): "worker-vms",
("./jeannie start-cluster", "Worker VMs running"): "worker-vms",
("./jeannie rpi-services", "Pi-hole DNS"): "pihole",
("./jeannie rpi-services", "RPi Docker root state"): "rpi-storage",
("./jeannie rpi-services", "Uptime Kuma HTTP"): "uptime-kuma",
("./jeannie deploy-gitea", "Gitea container"): "gitea-container",
("./jeannie deploy-gitea", "Gitea local HTTP"): "gitea-http",
}
probe = checks.get((command, check))
if probe is None:
return None
try:
result = subprocess.run([str(REPO_ROOT / "jeannie"), "__heal-check", probe],
cwd=REPO_ROOT, text=True, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL, check=False, timeout=remaining)
except (OSError, subprocess.TimeoutExpired):
return False
return result.returncode == 0
def pimox_worker_ready(index: int, timeout=30) -> bool:
node_name = f"pimox-worker-{index:02d}"
try:
process = subprocess.run(
["kubectl", "--kubeconfig", str(DEFAULT_KUBECONFIG), "get", "node", node_name, "--no-headers"],
cwd=REPO_ROOT, text=True, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL,
check=False, timeout=timeout,
)
except (OSError, subprocess.TimeoutExpired):
return False
if process.returncode != 0:
return False
parts = process.stdout.split()
return len(parts) >= 2 and parts[1] == "Ready"
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
subparsers = parser.add_subparsers(dest="command", required=True)
plan_parser = subparsers.add_parser("plan")
plan_parser.add_argument("--status-json", type=Path)
plan_parser.add_argument("--ai", action="store_true")
apply_parser = subparsers.add_parser("apply")
apply_parser.add_argument("--status-json", type=Path)
apply_parser.add_argument("--yes", action="store_true")
apply_parser.add_argument("--ai", action="store_true")
cleanup_parser = subparsers.add_parser("cleanup-stale-pods")
cleanup_parser.add_argument("--yes", action="store_true")
args = parser.parse_args()
if args.command == "cleanup-stale-pods":
return cleanup_stale_pods(args.yes)
status = load_status(getattr(args, "status_json", None))
plan = build_plan(status)
if args.command == "plan":
print_plan(plan, args.ai)
return 0
if args.command == "apply":
print_plan(plan, args.ai)
print()
return apply_plan(plan, args.yes)
return 2
if __name__ == "__main__":
raise SystemExit(main())