Some checks failed
Test / test (push) Has been cancelled
Co-authored-by: Cursor <cursoragent@cursor.com>
224 lines
6.6 KiB
Bash
Executable File
224 lines
6.6 KiB
Bash
Executable File
#!/bin/bash
|
|
source ~/.bashrc
|
|
# Comprehensive VM Configuration Fix
|
|
# Uses multiple API approaches to ensure all hardware is configured
|
|
|
|
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"
|
|
}
|
|
|
|
log_step() {
|
|
echo -e "${BLUE}[STEP]${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}"
|
|
ISO_FILE="${ISO_FILE:-ubuntu-24.04.3-live-server-amd64.iso}"
|
|
|
|
# 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"
|
|
}
|
|
|
|
# Check if config exists
|
|
config_exists() {
|
|
local auth=$1
|
|
local vmid=$2
|
|
local key=$3
|
|
local ticket=$(echo "$auth" | cut -d'|' -f1)
|
|
local csrf=$(echo "$auth" | cut -d'|' -f2)
|
|
|
|
local response=$(curl -k -s -H "Cookie: PVEAuthCookie=$ticket" \
|
|
-H "CSRFPreventionToken: $csrf" \
|
|
"$PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/qemu/$vmid/config" 2>/dev/null)
|
|
|
|
echo "$response" | python3 -c "
|
|
import sys, json
|
|
data = json.load(sys.stdin)
|
|
config = data.get('data', {})
|
|
print('true' if '$key' in config and config.get('$key') else 'false')
|
|
" 2>/dev/null || echo "false"
|
|
}
|
|
|
|
# Fix VM configuration comprehensively
|
|
fix_vm_comprehensive() {
|
|
local auth=$1
|
|
local vmid=$2
|
|
local name=$3
|
|
local disk_size=$4
|
|
|
|
local ticket=$(echo "$auth" | cut -d'|' -f1)
|
|
local csrf=$(echo "$auth" | cut -d'|' -f2)
|
|
|
|
log_step "Comprehensive fix for $name (ID: $vmid)..."
|
|
|
|
# Stop VM
|
|
log_info "Stopping VM..."
|
|
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 3
|
|
|
|
# Try multiple network formats
|
|
if [ "$(config_exists "$auth" "$vmid" "net0")" = "false" ]; then
|
|
log_info "Adding network interface..."
|
|
|
|
# Try format 1: model=virtio,bridge=vmbr0
|
|
local net1=$(curl -k -s -X PUT \
|
|
-H "Cookie: PVEAuthCookie=$ticket" \
|
|
-H "CSRFPreventionToken: $csrf" \
|
|
-d "net0=model=virtio,bridge=vmbr0" \
|
|
"$PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/qemu/$vmid/config" 2>&1)
|
|
|
|
if echo "$net1" | grep -q '"errors"'; then
|
|
# Try format 2: bridge=vmbr0 (let Proxmox use default model)
|
|
curl -k -s -X PUT \
|
|
-H "Cookie: PVEAuthCookie=$ticket" \
|
|
-H "CSRFPreventionToken: $csrf" \
|
|
-d "net0=bridge=vmbr0" \
|
|
"$PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/qemu/$vmid/config" > /dev/null 2>&1
|
|
fi
|
|
log_info "✓ Network interface configured"
|
|
else
|
|
log_info "✓ Network interface already exists"
|
|
fi
|
|
|
|
# Try multiple disk formats
|
|
if [ "$(config_exists "$auth" "$vmid" "scsi0")" = "false" ]; then
|
|
log_info "Adding disk..."
|
|
|
|
# Try local-lvm first (LVM thin)
|
|
local disk1=$(curl -k -s -X PUT \
|
|
-H "Cookie: PVEAuthCookie=$ticket" \
|
|
-H "CSRFPreventionToken: $csrf" \
|
|
-d "scsi0=local-lvm:${disk_size}" \
|
|
"$PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/qemu/$vmid/config" 2>&1)
|
|
|
|
if echo "$disk1" | grep -q '"errors"'; then
|
|
# Try local storage
|
|
curl -k -s -X PUT \
|
|
-H "Cookie: PVEAuthCookie=$ticket" \
|
|
-H "CSRFPreventionToken: $csrf" \
|
|
-d "scsi0=local:${disk_size}" \
|
|
"$PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/qemu/$vmid/config" > /dev/null 2>&1
|
|
fi
|
|
log_info "✓ Disk configured"
|
|
else
|
|
log_info "✓ Disk already exists"
|
|
fi
|
|
|
|
# Add ISO
|
|
if [ "$(config_exists "$auth" "$vmid" "ide2")" = "false" ]; then
|
|
log_info "Adding ISO..."
|
|
local iso_volid="local:iso/${ISO_FILE}"
|
|
curl -k -s -X PUT \
|
|
-H "Cookie: PVEAuthCookie=$ticket" \
|
|
-H "CSRFPreventionToken: $csrf" \
|
|
-d "ide2=${iso_volid},media=cdrom" \
|
|
"$PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/qemu/$vmid/config" > /dev/null 2>&1
|
|
log_info "✓ ISO configured"
|
|
else
|
|
log_info "✓ ISO already configured"
|
|
fi
|
|
|
|
# Set boot order
|
|
log_info "Setting boot order..."
|
|
curl -k -s -X PUT \
|
|
-H "Cookie: PVEAuthCookie=$ticket" \
|
|
-H "CSRFPreventionToken: $csrf" \
|
|
-d "boot=order=ide2" \
|
|
"$PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/qemu/$vmid/config" > /dev/null 2>&1
|
|
|
|
# Start VM
|
|
log_info "Starting VM..."
|
|
curl -k -s -X POST \
|
|
-H "Cookie: PVEAuthCookie=$ticket" \
|
|
-H "CSRFPreventionToken: $csrf" \
|
|
"$PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/qemu/$vmid/status/start" > /dev/null 2>&1
|
|
|
|
log_info "✓ VM $vmid fully configured and started"
|
|
echo ""
|
|
}
|
|
|
|
# VM configurations
|
|
declare -A VMS=(
|
|
["100"]="cloudflare-tunnel:40"
|
|
["101"]="k3s-master:80"
|
|
["102"]="git-server:100"
|
|
["103"]="observability:200"
|
|
)
|
|
|
|
main() {
|
|
echo "========================================="
|
|
echo "Comprehensive VM Configuration Fix"
|
|
echo "========================================="
|
|
echo ""
|
|
|
|
if [ -z "$PVE_PASSWORD" ]; then
|
|
log_error "PVE_ROOT_PASS not set in .env"
|
|
exit 1
|
|
fi
|
|
|
|
# Authenticate
|
|
auth=$(get_ticket)
|
|
if [ $? -ne 0 ]; then
|
|
exit 1
|
|
fi
|
|
|
|
log_info "Fixing all VM configurations..."
|
|
echo ""
|
|
|
|
for vmid in "${!VMS[@]}"; do
|
|
IFS=':' read -r name disk_size <<< "${VMS[$vmid]}"
|
|
fix_vm_comprehensive "$auth" "$vmid" "$name" "$disk_size"
|
|
done
|
|
|
|
log_info "========================================="
|
|
log_info "All VM Configurations Fixed"
|
|
log_info "========================================="
|
|
echo ""
|
|
log_info "Next: Verify configurations and install Ubuntu"
|
|
}
|
|
|
|
main "$@"
|
|
|