#!/usr/bin/env bash # Live Proxmox guest inventory + drift vs config/ip-addresses.conf. # Usage: bash scripts/it-ops/export-live-inventory-and-drift.sh # Requires: SSH key root@SEED, python3 locally and on PVE. set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" # shellcheck source=/dev/null source "${PROJECT_ROOT}/config/ip-addresses.conf" 2>/dev/null || true SEED="${SEED_HOST:-${PROXMOX_HOST_R630_01:-192.168.11.11}}" OUT_DIR="${OUT_DIR:-${PROJECT_ROOT}/reports/status}" TS="$(date +%Y%m%d_%H%M%S)" TMP="${TMPDIR:-/tmp}/live_inv_${TS}.json" PY="${SCRIPT_DIR}/lib/collect_inventory_remote.py" mkdir -p "$OUT_DIR" stub_unreachable() { python3 - <<'PY' import json from datetime import datetime, timezone print(json.dumps({ "collected_at": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"), "error": "seed_unreachable", "guests": [], }, indent=2)) PY } if ! ping -c1 -W2 "$SEED" >/dev/null 2>&1; then stub_unreachable >"$TMP" else if ! ssh -o BatchMode=yes -o ConnectTimeout=15 -o StrictHostKeyChecking=no \ "root@${SEED}" "python3 -" <"$PY" >"$TMP" 2>/dev/null; then stub_unreachable >"$TMP" fi fi set +e python3 "${SCRIPT_DIR}/compute_ipam_drift.py" --live "$TMP" \ --ip-conf "${PROJECT_ROOT}/config/ip-addresses.conf" \ --all-vmids-md "${PROJECT_ROOT}/docs/04-configuration/ALL_VMIDS_ENDPOINTS.md" \ --out-dir "$OUT_DIR" DRIFT_RC=$? set -e cp -f "$OUT_DIR/live_inventory.json" "${OUT_DIR}/live_inventory_${TS}.json" 2>/dev/null || true cp -f "$OUT_DIR/drift.json" "${OUT_DIR}/drift_${TS}.json" 2>/dev/null || true rm -f "$TMP" echo "Latest: ${OUT_DIR}/live_inventory.json , ${OUT_DIR}/drift.json" exit "${DRIFT_RC}"