Files
proxmox/scripts/ensure-npmplus-vm-operational.sh

134 lines
5.6 KiB
Bash
Raw Permalink Normal View History

#!/usr/bin/env bash
# Ensure the VM that hosts NPM (NPM_URL, e.g. https://${IP_NPMPLUS}:81) is running and reachable.
# VMID 10233 (npmplus) on r630-01 (${PROXMOX_HOST_R630_01:-192.168.11.11}); IP ${IP_NPMPLUS:-${IP_NPMPLUS:-192.168.11.167}} (eth1).
# Run from repo root. Uses .env for NPM_URL, NPM_HOST, PROXMOX_HOST. Optionally starts container if stopped (requires SSH to Proxmox).
set -euo pipefail
# Load IP configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
source "${PROJECT_ROOT}/config/ip-addresses.conf" 2>/dev/null || true
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
cd "$PROJECT_ROOT"
# Load .env (set +u so values with $ in them don't trigger unbound variable)
if [ -f .env ]; then
set +u
set -a
# shellcheck source=/dev/null
source .env 2>/dev/null || true
set +a
set -u
fi
# NPM container: VMID 10233 on r630-01 (see docs/04-configuration/DNS_NPMPLUS_VM_STREAMLINED_TABLE.md)
NPMPLUS_VMID="${NPMPLUS_VMID:-${NPM_VMID:-10233}}"
PROXMOX_HOST="${PROXMOX_HOST:-192.168.11.11}"
# NPM URL (e.g. https://${IP_NPMPLUS}:81) or derive from NPM_HOST
NPM_URL="${NPM_URL:-https://${IP_NPMPLUS}:81}"
if [[ "$NPM_URL" =~ ^https?://([^:/]+)(:([0-9]+))? ]]; then
NPM_HOST="${BASH_REMATCH[1]}"
NPM_PORT="${BASH_REMATCH[3]:-81}"
else
NPM_HOST="${NPM_HOST:-${IP_NPMPLUS:-192.168.11.167}}"
NPM_PORT="${NPM_PORT:-81}"
fi
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_ok() { echo -e "${GREEN}[✓]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[⚠]${NC} $1"; }
log_err() { echo -e "${RED}[✗]${NC} $1"; }
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "NPMplus VM operational check (VMID $NPMPLUS_VMID @ $NPM_HOST)"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
FAIL=0
# 1. Optional: check container status on Proxmox and start if stopped
if command -v ssh >/dev/null 2>&1; then
log_info "Checking container status on $PROXMOX_HOST (VMID $NPMPLUS_VMID)..."
STATUS=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=accept-new root@"$PROXMOX_HOST" "pct status $NPMPLUS_VMID 2>/dev/null" | awk '/status:/ {print $2}' || echo "unknown")
if [ "$STATUS" = "running" ]; then
log_ok "Container $NPMPLUS_VMID is running on $PROXMOX_HOST"
elif [ "$STATUS" = "stopped" ]; then
log_warn "Container $NPMPLUS_VMID is stopped. Starting..."
if timeout 60 ssh -o ConnectTimeout=10 root@"$PROXMOX_HOST" "pct start $NPMPLUS_VMID" 2>/dev/null; then
log_ok "Started container $NPMPLUS_VMID. Waiting 10s for services..."
sleep 10
else
log_err "Failed to start container $NPMPLUS_VMID. Start manually: ssh root@$PROXMOX_HOST 'pct start $NPMPLUS_VMID'"
FAIL=1
fi
else
log_warn "Could not get status for VMID $NPMPLUS_VMID (SSH to $PROXMOX_HOST failed or container missing). Continuing with HTTP checks..."
fi
else
log_info "SSH not available; skipping Proxmox container check. Proceeding with HTTP checks..."
fi
echo ""
# 2. HTTP/HTTPS reachability: 80, 81, 443
log_info "Checking NPM services on $NPM_HOST..."
for PORT in 80 81 443; do
if [ "$PORT" = "443" ]; then
SCHEME="https"
else
SCHEME="http"
fi
URL="${SCHEME}://${NPM_HOST}:${PORT}"
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" -k --connect-timeout 5 --max-time 8 "$URL" 2>/dev/null || echo "000")
if [ "$HTTP_CODE" = "000" ]; then
log_err "$URL unreachable (connection failed)"
FAIL=1
elif [ "$HTTP_CODE" -ge 200 ] && [ "$HTTP_CODE" -lt 500 ]; then
log_ok "$URL HTTP $HTTP_CODE"
else
log_warn "$URL HTTP $HTTP_CODE"
fi
done
echo ""
# 3. Optional: NPM API login (confirms NPM app is responding)
if [ -n "${NPM_PASSWORD:-}" ]; then
log_info "Checking NPM API (login)..."
TOKEN_RESPONSE=$(curl -s -k -X POST "https://${NPM_HOST}:81/api/tokens" \
-H "Content-Type: application/json" \
-d "{\"identity\":\"${NPM_EMAIL:-admin@example.org}\",\"secret\":\"$NPM_PASSWORD\"}" \
--connect-timeout 5 --max-time 10 2>/dev/null || echo "{}")
if echo "$TOKEN_RESPONSE" | grep -q '"token"'; then
log_ok "NPM API login successful"
else
log_warn "NPM API login failed or skipped (check NPM_EMAIL/NPM_PASSWORD in .env)"
fi
else
log_info "NPM_PASSWORD not set; skipping API login check"
fi
echo ""
if [ $FAIL -eq 0 ]; then
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
log_ok "NPMplus VM is operational. NPM_URL=$NPM_URL"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
else
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
log_err "Some checks failed. Ensure VMID $NPMPLUS_VMID is running on $PROXMOX_HOST and ports 80/81/443 are reachable at $NPM_HOST"
echo " Start container: ssh root@$PROXMOX_HOST 'pct start $NPMPLUS_VMID'"
echo " See: docs/04-configuration/DNS_NPMPLUS_VM_STREAMLINED_TABLE.md"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
exit 1
fi