diff --git a/bootstrap/edge/main.tf b/bootstrap/edge/main.tf index eea1bbd..68f25a3 100644 --- a/bootstrap/edge/main.tf +++ b/bootstrap/edge/main.tf @@ -235,7 +235,6 @@ install_missing_packages() { edge_stage packages install_missing_packages ca-certificates curl openssl prometheus-node-exporter -sudo systemctl enable --now prometheus-node-exporter edge_stage tailscale if ! command -v tailscale >/dev/null 2>&1; then @@ -430,17 +429,40 @@ cleanup_legacy_edge_containers() { done } -edge_stage compose +run_compose_step() { + step="$1" + shift + edge_stage "compose-$step" + command_log="$(mktemp)" + if "$@" >"$command_log" 2>&1; then + sudo install -m 0600 "$command_log" "$install_dir/last-compose.log" + rm -f "$command_log" + else + compose_rc=$? + sudo install -m 0600 "$command_log" "$install_dir/last-compose.log" || true + rm -f "$command_log" + printf 'Compose step %s failed (exit=%s); see %s/last-compose.log\\n' "$step" "$compose_rc" "$install_dir" >&2 + exit "$compose_rc" + fi +} + +edge_stage compose-cleanup cd "$install_dir" cleanup_legacy_edge_containers -sudo docker compose pull -sudo docker compose up -d --remove-orphans +run_compose_step validate sudo docker compose config --quiet +run_compose_step pull sudo docker compose pull +# Port 9100 belongs to the host exporter. --remove-orphans retires the old +# Compose exporter before enabling the host service, without deleting volumes. +run_compose_step up sudo docker compose up -d --remove-orphans # On a freshly installed Docker host, Compose can create dependency-chain # containers without starting them while the daemon is still settling. Start # any created containers explicitly before running the edge health checks. -sudo docker compose start || true +run_compose_step start sudo docker compose start sudo docker compose ps +edge_stage node-exporter +sudo systemctl enable --now prometheus-node-exporter + edge_stage certificates if [ "$enable_letsencrypt" = "true" ]; then email_args="--register-unsafely-without-email" diff --git a/bootstrap/edge/templates/docker-compose.yml.tftpl b/bootstrap/edge/templates/docker-compose.yml.tftpl index b044e84..73189e5 100644 --- a/bootstrap/edge/templates/docker-compose.yml.tftpl +++ b/bootstrap/edge/templates/docker-compose.yml.tftpl @@ -55,21 +55,6 @@ services: - squid_cache:/var/spool/squid - squid_logs:/var/log/squid - node-exporter: - image: prom/node-exporter:latest - restart: unless-stopped - pid: host - ports: - - "0.0.0.0:9100:9100/tcp" - command: - - --path.procfs=/host/proc - - --path.sysfs=/host/sys - - --path.rootfs=/rootfs - volumes: - - /proc:/host/proc:ro - - /sys:/host/sys:ro - - /:/rootfs:ro - cadvisor: image: gcr.io/cadvisor/cadvisor:v0.49.1 restart: unless-stopped diff --git a/bootstrap/oci/README.md b/bootstrap/oci/README.md index 34f2663..0883892 100644 --- a/bootstrap/oci/README.md +++ b/bootstrap/oci/README.md @@ -63,6 +63,13 @@ SSH uses a single bounded authentication check and preserves known host keys. Routine applies no longer restart caches; use `./jeannie edge-cache-flush` when a cache reset is intended. +Use `./jeannie remote edge diagnose` to inspect a failed deployment through +the API. It reports the bootstrap substep, the last Compose command output, +container startup errors, port listeners, and disk usage without printing +rendered configuration or container environments. Edge host metrics use the +systemd `prometheus-node-exporter` on port 9100; Compose only runs cAdvisor +on 9101, avoiding duplicate exporters trying to bind the same host port. + Prepare the config with `./jeannie oci-auth init`. The command creates the private key locally, prints the public key for the one-time OCI user upload, and writes the config with restrictive permissions. It never sends the private diff --git a/jeannie b/jeannie index f1725ab..b5445a8 100755 --- a/jeannie +++ b/jeannie @@ -139,6 +139,7 @@ Observability And GitOps oci {status|doctor|bastion ssh [--print-command]|cleanup} Inspect OCI resources, open Bastion SSH, or clean up infrastructure. edge-cache-flush Clear Nginx, Varnish, and Squid edge caches. + edge diagnose Inspect edge startup errors and port listeners. promote {plan|validate|rollback APP} Run canary promotion gates and rollback plan. cert-check Check public DNS, TLS, and edge URL health. backup-status Check backup and restore-drill freshness. @@ -306,6 +307,13 @@ case "${1:-}" in echo "Log: ${JEANNIE_LOG_FILE}" ;; edge) + if [[ "${2:-}" == "diagnose" ]]; then + edge_diagnose + exit "$?" + elif [[ -n "${2:-}" && "${2}" != "apply" ]]; then + echo "Usage: ./jeannie edge [apply|diagnose]" >&2 + exit 2 + fi record_change_journal "edge" "$@" require_debian_server "edge" jeannie_log_start "edge" diff --git a/lib/jeannie/pipeline.sh b/lib/jeannie/pipeline.sh index 8657f29..90e9b09 100644 --- a/lib/jeannie/pipeline.sh +++ b/lib/jeannie/pipeline.sh @@ -8285,11 +8285,24 @@ edge_remote_failure_report() { if [[ -z "${edge_host}" ]]; then return 0 fi + local install_dir="${TF_VAR_edge_install_dir:-${LAB_EDGE_INSTALL_DIR:-/opt/homelab-edge}}" + if [[ "${install_dir}" == *"'"* ]]; then + echo "Invalid edge install directory." >&2 + return 1 + fi edge_ssh_args - echo "Remote edge bootstrap stage report:" + echo "Remote edge bootstrap diagnostics:" ssh "${EDGE_SSH_ARGS[@]}" "${edge_user}@${edge_host}" \ - "sudo cat /tmp/homelab-edge-bootstrap-stage.log 2>/dev/null || true" \ - 2>/dev/null || echo "Unable to retrieve the remote edge stage report." >&2 + "bash -s -- '${install_dir}'" <"${REPO_ROOT}/scripts/edge-diagnostics" || + { echo "Unable to retrieve the remote edge diagnostics." >&2; return 1; } +} + +edge_diagnose() { + require_debian_server "edge diagnose" + oci_foundation_load_config || return 1 + refresh_oci_edge_endpoint || return 1 + edge_require_ssh_transport || return 1 + edge_remote_failure_report } edge_flush_caches() { diff --git a/scripts/edge-diagnostics b/scripts/edge-diagnostics new file mode 100644 index 0000000..9672cdf --- /dev/null +++ b/scripts/edge-diagnostics @@ -0,0 +1,30 @@ +#!/usr/bin/env bash +# Read-only edge diagnosis. No rendered config, environment, or service logs. +set -uo pipefail + +install_dir="${1:-/opt/homelab-edge}" +printf 'bootstrap-stage:\n' +sudo -n cat /tmp/homelab-edge-bootstrap-stage.log 2>/dev/null || true +printf '\nlast-compose-command:\n' +sudo -n tail -n 100 "${install_dir}/last-compose.log" 2>/dev/null || true +printf '\nhost-node-exporter:\n' +sudo -n systemctl is-active prometheus-node-exporter 2>/dev/null || true +printf '\nport-listeners:\n' +sudo -n ss -ltnp 2>/dev/null | + awk 'NR == 1 || $4 ~ /:(80|443|9100|9101)$/' || true +printf '\ndisk:\n' +df -hP "${install_dir}" 2>/dev/null || true +if [[ ! -d "${install_dir}" ]]; then + echo "Edge install directory does not exist." + exit 0 +fi +cd "${install_dir}" || exit 1 +printf '\ncompose-state:\n' +sudo -n docker compose ps --all 2>&1 || true +container_ids="$(sudo -n docker compose ps --all --quiet 2>/dev/null || true)" +printf '\ncontainer-startup-errors:\n' +for container_id in ${container_ids}; do + sudo -n docker inspect --format \ + '{{.Name}} status={{.State.Status}} exit={{.State.ExitCode}} error={{json .State.Error}}' \ + "${container_id}" 2>/dev/null || true +done