Some checks failed
Test / test (push) Has been cancelled
Co-authored-by: Cursor <cursoragent@cursor.com>
110 lines
2.6 KiB
Bash
Executable File
110 lines
2.6 KiB
Bash
Executable File
#!/bin/bash
|
|
source ~/.bashrc
|
|
# Fix VM Creation - Delete failed VMs and recreate them properly
|
|
|
|
set -e
|
|
|
|
# Colors
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
NC='\033[0m'
|
|
|
|
log_info() {
|
|
echo -e "${GREEN}[INFO]${NC} $1"
|
|
}
|
|
|
|
log_warn() {
|
|
echo -e "${YELLOW}[WARN]${NC} $1"
|
|
}
|
|
|
|
log_error() {
|
|
echo -e "${RED}[ERROR]${NC} $1"
|
|
}
|
|
|
|
# Load environment variables
|
|
if [ -f .env ]; then
|
|
set -a
|
|
source <(grep -v '^#' .env | grep -v '^$' | sed 's/#.*$//' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//' | grep '=')
|
|
set +a
|
|
fi
|
|
|
|
PVE_USERNAME="${PVE_USERNAME:-root@pam}"
|
|
PVE_PASSWORD="${PVE_ROOT_PASS:-}"
|
|
PROXMOX_HOST="${1:-192.168.1.206}"
|
|
PROXMOX_URL="https://${PROXMOX_HOST}:8006"
|
|
PROXMOX_NODE="${2:-pve}"
|
|
|
|
# Get authentication ticket
|
|
get_ticket() {
|
|
local response=$(curl -k -s -d "username=$PVE_USERNAME&password=$PVE_PASSWORD" \
|
|
"$PROXMOX_URL/api2/json/access/ticket")
|
|
|
|
local ticket=$(echo "$response" | grep -o '"ticket":"[^"]*' | cut -d'"' -f4)
|
|
local csrf=$(echo "$response" | grep -o '"CSRFPreventionToken":"[^"]*' | cut -d'"' -f4)
|
|
|
|
if [ -z "$ticket" ] || [ -z "$csrf" ]; then
|
|
log_error "Failed to authenticate with Proxmox"
|
|
return 1
|
|
fi
|
|
|
|
echo "$ticket|$csrf"
|
|
}
|
|
|
|
# Delete VM
|
|
delete_vm() {
|
|
local auth=$1
|
|
local vmid=$2
|
|
local ticket=$(echo "$auth" | cut -d'|' -f1)
|
|
local csrf=$(echo "$auth" | cut -d'|' -f2)
|
|
|
|
log_warn "Deleting VM $vmid..."
|
|
|
|
# Stop VM first if running
|
|
curl -k -s -X POST \
|
|
-H "Cookie: PVEAuthCookie=$ticket" \
|
|
-H "CSRFPreventionToken: $csrf" \
|
|
"$PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/qemu/$vmid/status/stop" > /dev/null 2>&1
|
|
|
|
sleep 2
|
|
|
|
# Delete VM
|
|
local delete_response=$(curl -k -s -X DELETE \
|
|
-H "Cookie: PVEAuthCookie=$ticket" \
|
|
-H "CSRFPreventionToken: $csrf" \
|
|
"$PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/qemu/$vmid" 2>&1)
|
|
|
|
if echo "$delete_response" | grep -q '"errors"'; then
|
|
log_error "Failed to delete VM $vmid: $delete_response"
|
|
return 1
|
|
fi
|
|
|
|
log_info "✓ VM $vmid deleted"
|
|
return 0
|
|
}
|
|
|
|
main() {
|
|
echo "========================================="
|
|
echo "Fix VM Creation - Cleanup and Recreate"
|
|
echo "========================================="
|
|
echo ""
|
|
|
|
# Authenticate
|
|
auth=$(get_ticket)
|
|
if [ $? -ne 0 ]; then
|
|
exit 1
|
|
fi
|
|
|
|
# Delete failed VMs (100-103)
|
|
for vmid in 100 101 102 103; do
|
|
delete_vm "$auth" "$vmid" || log_warn "Could not delete VM $vmid (may not exist)"
|
|
done
|
|
|
|
echo ""
|
|
log_info "Cleanup complete. Now run: ./scripts/create-vms-from-iso.sh"
|
|
}
|
|
|
|
main "$@"
|
|
|