From 4850c31335d098d29a79bdc8dd36a5ef987d843c Mon Sep 17 00:00:00 2001 From: "juvenal.diaz" Date: Fri, 30 Oct 2026 09:47:32 -0600 Subject: [PATCH] Render valid self-heal working directory and verify units before install --- infra/jeannie-eventd/README.md | 3 +++ lib/jeannie/commands.sh | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/infra/jeannie-eventd/README.md b/infra/jeannie-eventd/README.md index 3075b86..78aceb5 100644 --- a/infra/jeannie-eventd/README.md +++ b/infra/jeannie-eventd/README.md @@ -10,6 +10,9 @@ The installer records the selected profile, explicit kubeconfig, and current checkout path in the systemd unit. It restarts the webhook so new source and environment settings take effect. Without `--apply`, installation defaults to dry-run mode unless `LAB_SELF_HEAL_APPLY=true` is explicitly supplied. +The rendered file is staged with its `.service` filename and checked using +`systemd-analyze verify --man=no` on Debian before replacing the installed unit. +An invalid rendered unit therefore leaves the existing unit file untouched. `HomelabStaleResidualPods`, `KubePodEvicted`, and `KubePodContainerStatusUnknown` select the guarded stale-pod cleanup directly. diff --git a/lib/jeannie/commands.sh b/lib/jeannie/commands.sh index d24d3ba..f48bb95 100644 --- a/lib/jeannie/commands.sh +++ b/lib/jeannie/commands.sh @@ -3615,6 +3615,7 @@ self_heal() { if [ "${2:-}" = "install-service" ]; then local apply_mode local service_file + local service_stage apply_mode="${LAB_SELF_HEAL_APPLY:-false}" if [ "${3:-}" = "--apply" ]; then @@ -3628,7 +3629,8 @@ self_heal() { echo "Self-heal kubeconfig is not readable: ${KUBECONFIG}" >&2 return 1 fi - service_file="$(mktemp)" + service_stage="$(mktemp -d "${TMPDIR:-/tmp}/jeannie-eventd-unit.XXXXXXXX")" + service_file="${service_stage}/jeannie-eventd.service" if ! python3 - "${REPO_ROOT}/infra/jeannie-eventd/jeannie-eventd.service" \ "${REPO_ROOT}" "${LAB_CLUSTER_PROFILE}" "${KUBECONFIG}" "${apply_mode}" >"${service_file}" <<'PY' from pathlib import Path @@ -3642,7 +3644,9 @@ def quote_unit(value): return '"' + value.replace("\\", "\\\\").replace('"', '\\"').replace("%", "%%") + '"' replacements = { - "WorkingDirectory": quote_unit(repo), + # WorkingDirectory takes a literal absolute path, unlike the shell-word + # parsing used by ExecStart and Environment. Quotes become path characters. + "WorkingDirectory": repo.replace("%", "%%"), "ExecStart": quote_unit(repo + "/scripts/jeannie-eventd") + " serve", "Environment=LAB_CLUSTER_PROFILE": quote_unit("LAB_CLUSTER_PROFILE=" + profile), "Environment=KUBECONFIG": quote_unit("KUBECONFIG=" + kubeconfig), @@ -3659,11 +3663,19 @@ for line in Path(template).read_text().splitlines(): PY then rm -f "${service_file}" + rmdir "${service_stage}" echo "Self-heal service rendering failed; existing service preserved." >&2 return 1 fi + if ! sudo systemd-analyze verify --man=no "${service_file}"; then + rm -f "${service_file}" + rmdir "${service_stage}" + echo "Self-heal unit validation failed; existing service preserved." >&2 + return 1 + fi sudo install -m 0644 "${service_file}" /etc/systemd/system/jeannie-eventd.service rm -f "${service_file}" + rmdir "${service_stage}" sudo systemctl daemon-reload sudo systemctl enable jeannie-eventd sudo systemctl restart jeannie-eventd