#!/bin/bash
# Aggressively force delete all VMs

PROXMOX_PASS="L@kers2010"
SITE1="192.168.11.10"
SITE2="192.168.11.11"

GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

log() {
    echo -e "${BLUE}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $*"
}

log_success() {
    echo -e "${GREEN}[$(date +'%Y-%m-%d %H:%M:%S')] ✅${NC} $*"
}

log_warning() {
    echo -e "${YELLOW}[$(date +'%Y-%m-%d %H:%M:%S')] ⚠️${NC} $*"
}

force_delete_host() {
    local host=$1
    local site_name=$2
    
    log "=========================================="
    log "Force deleting all VMs on ${site_name} (${host})"
    log "=========================================="
    
    # Kill all qm processes
    log "Killing all qm processes..."
    sshpass -p "${PROXMOX_PASS}" ssh -o StrictHostKeyChecking=no root@${host} "pkill -9 -f 'qm '"
    sleep 2
    
    # Remove all lock files
    log "Removing all lock files..."
    sshpass -p "${PROXMOX_PASS}" ssh -o StrictHostKeyChecking=no root@${host} "rm -rf /var/lock/qemu-server/lock-*.conf"
    sleep 1
    
    # Get VM list
    log "Getting VM list..."
    local vm_list
    vm_list=$(sshpass -p "${PROXMOX_PASS}" ssh -o StrictHostKeyChecking=no root@${host} "qm list 2>/dev/null | tail -n +2 | awk '{print \$1}'")
    
    if [ -z "${vm_list}" ]; then
        log_warning "No VMs found"
        return 0
    fi
    
    # Force delete each VM
    for vmid in $vm_list; do
        log "Force deleting VM ${vmid}..."
        
        # Remove lock file
        sshpass -p "${PROXMOX_PASS}" ssh -o StrictHostKeyChecking=no root@${host} "rm -f /var/lock/qemu-server/lock-${vmid}.conf" 2>/dev/null
        
        # Stop VM forcefully
        sshpass -p "${PROXMOX_PASS}" ssh -o StrictHostKeyChecking=no root@${host} "qm stop ${vmid} --skiplock" 2>/dev/null || true
        sleep 1
        
        # Delete VM config
        sshpass -p "${PROXMOX_PASS}" ssh -o StrictHostKeyChecking=no root@${host} "rm -f /etc/pve/qemu-server/${vmid}.conf" 2>/dev/null
        
        # Delete VM with purge and skip lock
        sshpass -p "${PROXMOX_PASS}" ssh -o StrictHostKeyChecking=no root@${host} "qm destroy ${vmid} --purge --skiplock" 2>&1 | grep -v "trying to acquire lock" || true
        
        # If still exists, remove config and disks manually
        sleep 2
        local still_exists
        still_exists=$(sshpass -p "${PROXMOX_PASS}" ssh -o StrictHostKeyChecking=no root@${host} "qm list 2>/dev/null | grep -c \"^[[:space:]]*${vmid}\" || echo 0")
        
        if [ "${still_exists}" -gt 0 ]; then
            log_warning "VM ${vmid} still exists, removing manually..."
            
            # Remove config
            sshpass -p "${PROXMOX_PASS}" ssh -o StrictHostKeyChecking=no root@${host} "rm -f /etc/pve/qemu-server/${vmid}.conf" 2>/dev/null
            
            # Remove lock
            sshpass -p "${PROXMOX_PASS}" ssh -o StrictHostKeyChecking=no root@${host} "rm -f /var/lock/qemu-server/lock-${vmid}.conf" 2>/dev/null
            
            # Find and remove disks
            sshpass -p "${PROXMOX_PASS}" ssh -o StrictHostKeyChecking=no root@${host} "lvs | grep vm-${vmid} | awk '{print \$1}' | xargs -r lvremove -f" 2>/dev/null || true
        else
            log_success "VM ${vmid} deleted"
        fi
    done
    
    log_success "Completed force deletion on ${site_name}"
}

main() {
    log "=========================================="
    log "Aggressive Force Delete All VMs"
    log "=========================================="
    log ""
    log_warning "This will forcefully delete ALL VMs on both hosts"
    log ""
    
    # Force delete Site 1
    force_delete_host "${SITE1}" "Site 1 (ml110-01)"
    
    echo ""
    
    # Force delete Site 2
    force_delete_host "${SITE2}" "Site 2 (r630-01)"
    
    echo ""
    log "=========================================="
    log "Verifying deletion..."
    echo ""
    
    log "Site 1 VMs remaining:"
    sshpass -p "${PROXMOX_PASS}" ssh -o StrictHostKeyChecking=no root@${SITE1} "qm list"
    
    echo ""
    log "Site 2 VMs remaining:"
    sshpass -p "${PROXMOX_PASS}" ssh -o StrictHostKeyChecking=no root@${SITE2} "qm list"
    
    echo ""
    log "=========================================="
    log_success "Force deletion completed!"
}

main "$@"

