#!/bin/bash
# Automated cleanup script for VM 100 on Proxmox node
# Run this script on the Proxmox node: root@ml110-01

VMID=100

echo "=== Automated Cleanup for VM $VMID ==="
echo ""

# 1. Kill all stuck processes
echo "1. Killing stuck processes..."
pkill -9 -f "task.*$VMID" 2>/dev/null && echo "   ✅ Killed task processes" || echo "   ℹ️  No task processes found"
pkill -9 -f "qm.*$VMID" 2>/dev/null && echo "   ✅ Killed qm processes" || echo "   ℹ️  No qm processes found"
pkill -9 -f "qemu.*$VMID" 2>/dev/null && echo "   ✅ Killed qemu processes" || echo "   ℹ️  No qemu processes found"

# Wait for processes to die
sleep 3

# 2. Remove lock file
echo ""
echo "2. Removing lock file..."
if [ -f "/var/lock/qemu-server/lock-$VMID.conf" ]; then
    rm -f "/var/lock/qemu-server/lock-$VMID.conf"
    if [ ! -f "/var/lock/qemu-server/lock-$VMID.conf" ]; then
        echo "   ✅ Lock file removed"
    else
        echo "   ⚠️  Failed to remove lock file"
        exit 1
    fi
else
    echo "   ℹ️  Lock file already removed"
fi

# 3. Verify cleanup
echo ""
echo "3. Verifying cleanup..."
REMAINING_PROCS=$(ps aux | grep -E "task.*$VMID|qm.*$VMID|qemu.*$VMID" | grep -v grep)
if [ -z "$REMAINING_PROCS" ]; then
    echo "   ✅ No processes remaining"
else
    echo "   ⚠️  Some processes still running:"
    echo "$REMAINING_PROCS" | while read line; do
        echo "      $line"
    done
fi

LOCK_EXISTS=$(ls -la /var/lock/qemu-server/lock-$VMID.conf 2>&1)
if echo "$LOCK_EXISTS" | grep -q "No such file"; then
    echo "   ✅ Lock file confirmed removed"
else
    echo "   ⚠️  Lock file still exists:"
    echo "      $LOCK_EXISTS"
fi

# 4. Attempt unlock
echo ""
echo "4. Attempting unlock..."
if qm unlock $VMID 2>&1; then
    echo "   ✅ VM unlocked successfully"
else
    UNLOCK_RESULT=$?
    echo "   ⚠️  Unlock returned exit code: $UNLOCK_RESULT"
    echo "   (This may be normal if lock was already cleared)"
fi

# 5. Final status
echo ""
echo "5. Final VM status..."
qm status $VMID 2>&1 | head -5

echo ""
echo "=== Cleanup Complete ==="
echo ""
echo "Next steps:"
echo "1. Monitor from Kubernetes: kubectl get proxmoxvm basic-vm-001 -w"
echo "2. Provider will automatically retry within 1 minute"
echo "3. VM should complete configuration and boot"

