#!/bin/bash
# Complete VM 100 deployment process
# This script guides through checking, fixing, and starting VM 100

set -e

VMID=100
VM_NAME="basic-vm-001"

echo "=========================================="
echo "VM 100 Deployment Completion Script"
echo "=========================================="
echo ""

# Step 1: Check current status from Kubernetes
echo "Step 1: Checking Kubernetes status..."
echo "--------------------------------------"
kubectl get proxmoxvm $VM_NAME -o wide 2>/dev/null || echo "VM not found in Kubernetes"
echo ""

# Step 2: Instructions for Proxmox node
echo "Step 2: Run these commands on Proxmox node (root@ml110-01)"
echo "--------------------------------------"
echo ""
echo "Copy and paste this block into your Proxmox SSH session:"
echo ""
echo "--- START PROXMOX COMMANDS ---"
echo "VMID=100"
echo ""
echo "# 1. Check VM status"
echo "qm status \$VMID"
echo ""
echo "# 2. Check full configuration"
echo "qm config \$VMID"
echo ""
echo "# 3. Check boot order"
echo "qm config \$VMID | grep '^boot:' || echo '⚠️  Boot order not set'"
echo ""
echo "# 4. Check disk configuration"
echo "qm config \$VMID | grep '^scsi0:' || echo '⚠️  Disk not configured'"
echo ""
echo "# 5. Check if disk exists"
echo "lvs | grep vm-\$VMID-disk || echo '⚠️  No disk found'"
echo ""
echo "# 6. Check cloud-init"
echo "qm config \$VMID | grep -E '^ide2:|^ciuser:' || echo '⚠️  Cloud-init not configured'"
echo ""
echo "# 7. Check network"
echo "qm config \$VMID | grep '^net0:' || echo '⚠️  Network not configured'"
echo ""
echo "# 8. Check guest agent"
echo "qm config \$VMID | grep '^agent:' || echo '⚠️  Guest agent not enabled'"
echo ""
echo "--- END PROXMOX COMMANDS ---"
echo ""

# Step 3: Fix commands (if needed)
echo "Step 3: Fix commands (run if issues found)"
echo "--------------------------------------"
echo ""
echo "If boot order missing:"
echo "  qm set $VMID --boot order=scsi0"
echo ""
echo "If guest agent missing:"
echo "  qm set $VMID --agent 1"
echo ""
echo "If cloud-init missing:"
echo "  qm set $VMID --ide2 local-lvm:cloudinit"
echo "  qm set $VMID --ciuser admin"
echo "  qm set $VMID --ipconfig0 ip=dhcp"
echo ""
echo "If disk not configured or blank, image import may have failed."
echo "Check if image exists:"
echo "  find /var/lib/vz/template/iso -name 'ubuntu-22.04-cloud.img'"
echo ""
echo ""

# Step 4: Start VM
echo "Step 4: Start VM (after verification)"
echo "--------------------------------------"
echo ""
echo "Once all checks pass, start the VM:"
echo "  qm start $VMID"
echo ""
echo ""

# Step 5: Monitoring commands
echo "Step 5: Monitoring commands"
echo "--------------------------------------"
echo ""
echo "From Kubernetes (run in another terminal):"
echo "  kubectl get proxmoxvm $VM_NAME -w"
echo ""
echo "From Proxmox node:"
echo "  watch -n 2 'qm status $VMID'"
echo ""
echo "Check provider logs:"
echo "  kubectl logs -n crossplane-system -l app=crossplane-provider-proxmox --tail=50 -f"
echo ""
echo ""

# Step 6: Verification after start
echo "Step 6: Verification after VM starts"
echo "--------------------------------------"
echo ""
echo "Once VM shows 'running' status:"
echo ""
echo "1. Get VM IP:"
echo "   kubectl get proxmoxvm $VM_NAME -o jsonpath='{.status.networkInterfaces[0].ipAddress}'"
echo ""
echo "2. Check cloud-init logs (once IP is available):"
echo "   ssh admin@<VM_IP> 'cat /var/log/cloud-init-output.log | tail -50'"
echo ""
echo "3. Verify services:"
echo "   ssh admin@<VM_IP> 'systemctl status qemu-guest-agent chrony unattended-upgrades'"
echo ""
echo ""

echo "=========================================="
echo "Next Actions:"
echo "=========================================="
echo "1. SSH to Proxmox node: root@ml110-01"
echo "2. Run the check commands from Step 2"
echo "3. Fix any issues found (Step 3)"
echo "4. Start the VM: qm start $VMID"
echo "5. Monitor from Kubernetes (Step 5)"
echo "6. Verify services once VM is running (Step 6)"
echo ""

