#!/bin/bash
# install-guest-agent-via-proxmox-console.sh
# Instructions for installing guest agent via Proxmox console

set -euo pipefail

PROXMOX_1_HOST="192.168.11.10"
PROXMOX_2_HOST="192.168.11.11"
PROXMOX_PASS="L@kers2010"

SITE1_VMS="136 139 141 142 145 146 150 151"
SITE2_VMS="101 104 137 138 144 148"

echo "=========================================="
echo "Guest Agent Installation via Proxmox Console"
echo "=========================================="
echo ""
echo "Since VMs are not accessible via SSH, use Proxmox Web UI console:"
echo ""
echo "METHOD 1: Proxmox Web UI Console (Recommended)"
echo "-----------------------------------------------"
echo "1. Open Proxmox Web UI: https://192.168.11.10:8006 or https://192.168.11.11:8006"
echo "2. For each VM, click on the VM → Console"
echo "3. Login as 'admin' user"
echo "4. Run these commands:"
echo ""
echo "   sudo apt-get update"
echo "   sudo apt-get install -y qemu-guest-agent"
echo "   sudo systemctl enable qemu-guest-agent"
echo "   sudo systemctl start qemu-guest-agent"
echo "   sudo systemctl status qemu-guest-agent"
echo ""
echo "METHOD 2: Check if Package Already Installed"
echo "-----------------------------------------------"
echo "Checking if qemu-guest-agent package is already installed..."
echo ""

check_package_installed() {
    local host=$1
    local vmid=$2
    local vmname=$3
    
    echo -n "VMID $vmid ($vmname): "
    
    # Try to check via guest agent (if it becomes available)
    result=$(sshpass -p "$PROXMOX_PASS" ssh -o StrictHostKeyChecking=no root@$host \
        "qm guest exec $vmid -- 'dpkg -l | grep qemu-guest-agent' 2>&1" 2>/dev/null || echo "agent_not_running")
    
    if echo "$result" | grep -q "qemu-guest-agent"; then
        echo "✅ Package installed (but service may not be running)"
        return 0
    elif echo "$result" | grep -q "agent_not_running"; then
        echo "⚠️  Cannot check (guest agent not running - chicken-and-egg)"
        return 1
    else
        echo "❌ Package not found or cannot verify"
        return 1
    fi
}

echo "Site 1 (ml110-01):"
for vmid in $SITE1_VMS; do
    name=$(sshpass -p "$PROXMOX_PASS" ssh -o StrictHostKeyChecking=no root@$PROXMOX_1_HOST \
        "qm config $vmid | grep '^name:' | cut -d' ' -f2" || echo "unknown")
    check_package_installed "$PROXMOX_1_HOST" "$vmid" "$name"
done

echo ""
echo "Site 2 (r630-01):"
for vmid in $SITE2_VMS; do
    name=$(sshpass -p "$PROXMOX_PASS" ssh -o StrictHostKeyChecking=no root@$PROXMOX_2_HOST \
        "qm config $vmid | grep '^name:' | cut -d' ' -f2" || echo "unknown")
    check_package_installed "$PROXMOX_2_HOST" "$vmid" "$name"
done

echo ""
echo "=========================================="
echo "Installation Instructions"
echo "=========================================="
echo ""
echo "Since we cannot verify package installation without guest agent running,"
echo "and VMs are not accessible via SSH, please use Proxmox Web UI Console:"
echo ""
echo "1. Access Proxmox Web UI"
echo "2. Open each VM's console"
echo "3. Login and install/start guest agent"
echo ""
echo "OR wait for cloud-init to complete (may take 5-10 minutes after VM boot)"
echo ""

