my-homelab-configs/scripts/doctor-focus

135 lines
6.5 KiB
Bash

#!/usr/bin/env bash
# Sourced by Jeannie; all probes are read-only.
doctor_focus_usage() {
cat <<'EOF'
Usage: ./jeannie doctor {oci|rpi|opi5|debian|cluster} [ITEM] [--ai] [--json] [--details] [--all]
Diagnose one inventory area. Optionally select an exact check, Argo CD
application, Compose project/service, or unambiguous Compose service name
shown by status. There is no all-area doctor.
--ai Request an evidence-validated AI explanation (text only).
--no-local-rca Disable the AI explanation.
--json Return the structured diagnostic report.
--details Show supporting evidence.
--all Include healthy checks (default).
--item NAME Alternative to positional ITEM.
Examples:
./jeannie doctor rpi
./jeannie doctor cluster gitea-runner --ai
./jeannie remote doctor debian --item homelab-gitea/gitea
EOF
}
doctor_focus_https() {
curl -fsS -o /dev/null --connect-timeout 3 --max-time 8 "${LAB_PUBLIC_URL%/}/edge-health"
}
doctor_focus_application_evidence() {
local name="$1" snapshot
[[ "${name}" =~ ^[a-z0-9]([-a-z0-9.]*[a-z0-9])?$ ]] || { echo "Invalid application name" >&2; return 1; }
snapshot="$(kubectl --kubeconfig "${KUBECONFIG_PATH}" --request-timeout=10s -n argocd get application "${name}" -o json)" || return 1
printf '%s' "${snapshot}" | python3 "${REPO_ROOT}/scripts/doctor-application.py"
}
doctor_focus_compose_evidence() {
local scope="$1" check="$2" name host user key
name="${check##* (}"
name="${name%)}"
[[ "${name}" =~ ^[a-zA-Z0-9][a-zA-Z0-9_.-]*$ ]] || { echo "Invalid container name" >&2; return 1; }
local format='{{.State.Status}} restarts={{.RestartCount}} exit={{.State.ExitCode}} health={{if .State.Health}}{{.State.Health.Status}}{{else}}not-configured{{end}}'
if [[ "${scope}" == debian ]]; then
timeout 10 sudo -n docker inspect --format "${format}" "${name}"
return
fi
case "${scope}" in
rpi) host="${LAB_RPI_HOST:-}"; user="${LAB_RPI_USER:-}"; key="${LAB_RPI_SSH_KEY_PATH:-${HOME}/.ssh/id_ed25519}" ;;
opi5) host="${LAB_PIMOX_HOST:-}"; user="${LAB_PIMOX_USER:-}"; key="${LAB_PIMOX_SSH_KEY_PATH:-${HOME}/.ssh/id_ed25519}" ;;
oci) host="${LAB_EDGE_TAILSCALE_IP:-}"; user="${LAB_EDGE_USER:-}"; key="${LAB_EDGE_SSH_KEY_PATH:-${LAB_OCI_SSH_PRIVATE_KEY:-${HOME}/.ssh/id_ed25519}}" ;;
*) return 2 ;;
esac
[[ -n "${host}" && -n "${user}" ]] || { echo "Host or user is missing from inventory" >&2; return 1; }
timeout 10 ssh -n -i "${key}" -o BatchMode=yes -o ConnectTimeout=3 \
-o ConnectionAttempts=1 -o StrictHostKeyChecking=yes "${user}@${host}" \
"sudo -n docker inspect --format '${format}' '${name}'"
}
doctor_focus_extra() {
case "$1" in
oci)
if [[ -n "${LAB_PUBLIC_URL:-}" ]]; then
report_ui_check warn "OCI edge diagnostics" "Public HTTPS certificate and health" "curl -v --max-time 8 '${LAB_PUBLIC_URL%/}/edge-health'" "" "" doctor_focus_https
fi
;;
rpi)
report_ui_check warn "RPi4 diagnostics" "Docker root state" "./jeannie doctor-rpi --details" "" "" check_rpi_docker_root_state
report_ui_check warn "RPi4 diagnostics" "Tailscale IP" "./jeannie doctor-rpi --details" "" "" check_rpi_tailscale_ip
;;
debian)
report_ui_check warn "Debian diagnostics" "Gitea runner service" "systemctl status homelab-gitea-runner.service --no-pager" "" "" check_systemd_active homelab-gitea-runner
report_ui_check warn "Debian diagnostics" "Gitea SSH" "./jeannie doctor-gitea --details" "" "" check_gitea_ssh
;;
cluster)
report_ui_check warn "Kubernetes diagnostics" "Problem pods" "kubectl get pods -A -o wide" "" "" status_no_problem_pods
report_ui_check warn "Kubernetes diagnostics" "Recent deployments" "kubectl get deployments -A" "" "" status_recent_deployments
;;
opi5) ;;
esac
}
doctor_focus() {
local scope="${1:-}" item="" rc=0 selected area check
local -a flags=()
case "${scope}" in
-h|--help) doctor_focus_usage; return 0 ;;
oci|rpi|opi5|debian|cluster) shift ;;
*) doctor_focus_usage >&2; return 2 ;;
esac
while (($#)); do
case "$1" in
-h|--help) doctor_focus_usage; return 0 ;;
--item)
if (($# < 2)) || [[ -z "$2" || "$2" == --* || -n "${item}" ]]; then echo "Provide exactly one item name." >&2; return 2; fi
item="$2"; shift 2 ;;
--ai|--no-local-rca|--json|--details|--all) flags+=("$1"); shift ;;
-*) echo "Unknown doctor option: $1" >&2; return 2 ;;
*) if [[ -n "${item}" ]]; then echo "Provide exactly one item name." >&2; return 2; fi; item="$1"; shift ;;
esac
done
require_debian_server "doctor"
# These variables are consumed by the dynamically scoped report helpers.
# shellcheck disable=SC2034
local REPORT_UI_ONLY=all REPORT_UI_LOCAL_RCA=false REPORT_UI_PARALLEL=true REPORT_UI_STATUS_SUMMARY=false
report_ui_parse_args "${flags[@]}" || return 2
report_ui_begin
status_focus_collect "${scope}"
if [[ -z "${item}" ]]; then doctor_focus_extra "${scope}"; fi
report_ui_finish
if [[ -n "${item}" ]]; then
if ! selected="$(python3 "${REPO_ROOT}/scripts/doctor-filter.py" "${item}" <"${REPORT_UI_ROWS_FILE}")"; then
report_ui_cleanup
return 2
fi
printf '%s\n' "${selected}" >"${REPORT_UI_ROWS_FILE}"
REPORT_UI_FAILURES=0
IFS=$'\t' read -r _ area check _ <<<"${selected}"
[[ "${selected}" != fail$'\t'* ]] || REPORT_UI_FAILURES=1
if [[ "${area}" == "Kubernetes apps" ]]; then
# Application evidence is a separate read-only request for the selected item.
# shellcheck disable=SC2034
REPORT_UI_PARALLEL=false
report_ui_check warn "Kubernetes evidence" "${check} application evidence retrieved" "kubectl -n argocd get application ${check} -o yaml" "" "" doctor_focus_application_evidence "${check}"
elif [[ "${area}" == *" Compose" ]]; then
# shellcheck disable=SC2034
REPORT_UI_PARALLEL=false
report_ui_check warn "Compose evidence" "${check} container state retrieved" "docker ps -a --filter label=com.docker.compose.project" "" "" doctor_focus_compose_evidence "${scope}" "${check}"
fi
fi
((REPORT_UI_FAILURES == 0)) || rc=1
report_ui_render "Jeannie Doctor: ${scope}${item:+ / ${item}}"
report_ui_cleanup
return "${rc}"
}