Some checks failed
Test / test (push) Has been cancelled
Co-authored-by: Cursor <cursoragent@cursor.com>
387 lines
12 KiB
Bash
Executable File
387 lines
12 KiB
Bash
Executable File
#!/bin/bash
|
|
source ~/.bashrc
|
|
# Create All Service VMs via Proxmox API using ISO
|
|
# Uploads ISO and creates all VMs automatically
|
|
|
|
set -e
|
|
|
|
# Colors
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
CYAN='\033[0;36m'
|
|
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"
|
|
}
|
|
|
|
log_header() {
|
|
echo -e "${CYAN}========================================${NC}"
|
|
echo -e "${CYAN}$1${NC}"
|
|
echo -e "${CYAN}========================================${NC}"
|
|
}
|
|
|
|
# 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
|
|
else
|
|
log_error ".env file not found!"
|
|
exit 1
|
|
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}"
|
|
ISO_PATH="${ISO_PATH:-./${ISO_FILE}}"
|
|
|
|
# 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 ISO exists locally
|
|
check_iso() {
|
|
if [ ! -f "$ISO_PATH" ]; then
|
|
log_error "ISO file not found: $ISO_PATH"
|
|
log_info "Looking for ISO in project root..."
|
|
ISO_PATH="./ubuntu-24.04.3-live-server-amd64.iso"
|
|
if [ ! -f "$ISO_PATH" ]; then
|
|
log_error "ISO file not found. Please ensure ubuntu-24.04.3-live-server-amd64.iso is in the project root."
|
|
exit 1
|
|
fi
|
|
fi
|
|
log_info "Found ISO: $ISO_PATH"
|
|
ISO_SIZE=$(du -h "$ISO_PATH" | cut -f1)
|
|
log_info "ISO size: $ISO_SIZE"
|
|
}
|
|
|
|
# Check if ISO already exists in Proxmox
|
|
iso_exists() {
|
|
local auth=$1
|
|
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/storage/local/content")
|
|
|
|
echo "$response" | python3 -c "
|
|
import sys, json
|
|
data = json.load(sys.stdin)
|
|
isos = [f for f in data.get('data', []) if f.get('content') == 'iso' and '$ISO_FILE' in f.get('volid', '')]
|
|
print('true' if isos else 'false')
|
|
" 2>/dev/null || echo "false"
|
|
}
|
|
|
|
# Upload ISO to Proxmox
|
|
upload_iso() {
|
|
local auth=$1
|
|
local ticket=$(echo "$auth" | cut -d'|' -f1)
|
|
local csrf=$(echo "$auth" | cut -d'|' -f2)
|
|
|
|
log_step "Uploading ISO to Proxmox..."
|
|
log_warn "This may take several minutes depending on ISO size and network speed..."
|
|
|
|
# Upload ISO using multipart form
|
|
local result=$(curl -k -X POST \
|
|
-H "Cookie: PVEAuthCookie=$ticket" \
|
|
-H "CSRFPreventionToken: $csrf" \
|
|
-F "content=iso" \
|
|
-F "filename=@$ISO_PATH" \
|
|
"$PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/storage/local/upload" 2>&1)
|
|
|
|
if echo "$result" | grep -q "error"; then
|
|
log_error "ISO upload failed: $result"
|
|
return 1
|
|
fi
|
|
|
|
log_info "✓ ISO uploaded successfully"
|
|
return 0
|
|
}
|
|
|
|
# Check if VM exists
|
|
vm_exists() {
|
|
local auth=$1
|
|
local vmid=$2
|
|
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/cluster/resources?type=vm")
|
|
|
|
echo "$response" | python3 -c "
|
|
import sys, json
|
|
data = json.load(sys.stdin)
|
|
vms = [v for v in data.get('data', []) if v.get('type') == 'qemu' and str(v.get('vmid')) == '$vmid']
|
|
print('true' if vms else 'false')
|
|
" 2>/dev/null || echo "false"
|
|
}
|
|
|
|
# Create VM via API
|
|
create_vm() {
|
|
local auth=$1
|
|
local vmid=$2
|
|
local name=$3
|
|
local cores=$4
|
|
local memory=$5
|
|
local disk_size=$6
|
|
local ip_address=$7
|
|
local gateway=$8
|
|
|
|
local ticket=$(echo "$auth" | cut -d'|' -f1)
|
|
local csrf=$(echo "$auth" | cut -d'|' -f2)
|
|
|
|
log_step "Creating VM: $name (ID: $vmid)..."
|
|
|
|
# First, verify ISO exists in storage
|
|
local iso_volid="local:iso/${ISO_FILE}"
|
|
log_info "Using ISO: $iso_volid"
|
|
|
|
# Create VM with proper API format
|
|
# Note: Proxmox API requires specific parameter format
|
|
log_info "API Call: POST $PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/qemu"
|
|
log_info "Parameters: vmid=$vmid, name=$name, cores=$cores, memory=$memory"
|
|
|
|
# Strategy: Create VM with minimal config, then add hardware via separate API calls
|
|
log_info "Step 1: Creating VM skeleton..."
|
|
local create_response=$(curl -k -s -X POST \
|
|
-H "Cookie: PVEAuthCookie=$ticket" \
|
|
-H "CSRFPreventionToken: $csrf" \
|
|
-d "vmid=$vmid" \
|
|
-d "name=$name" \
|
|
"$PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/qemu" 2>&1)
|
|
|
|
if echo "$create_response" | grep -q '"errors"'; then
|
|
log_error "Failed to create VM skeleton:"
|
|
echo "$create_response" | python3 -c "import sys, json; d=json.load(sys.stdin); print(json.dumps(d.get('errors', {}), indent=2))" 2>/dev/null || echo "$create_response"
|
|
return 1
|
|
fi
|
|
|
|
log_info "✓ VM skeleton created"
|
|
sleep 1
|
|
|
|
# Step 2: Configure basic VM settings
|
|
log_info "Step 2: Configuring CPU and memory..."
|
|
curl -k -s -X PUT \
|
|
-H "Cookie: PVEAuthCookie=$ticket" \
|
|
-H "CSRFPreventionToken: $csrf" \
|
|
-d "cores=$cores" \
|
|
-d "memory=$memory" \
|
|
-d "ostype=l26" \
|
|
-d "agent=1" \
|
|
"$PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/qemu/$vmid/config" > /dev/null 2>&1
|
|
|
|
# Step 3: Add disk (simplest format)
|
|
log_info "Step 3: Adding disk..."
|
|
local disk_response=$(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" 2>&1)
|
|
|
|
if echo "$disk_response" | grep -q '"errors"'; then
|
|
log_warn "Disk configuration warning (continuing anyway)"
|
|
fi
|
|
|
|
# Step 4: Add ISO
|
|
log_info "Step 4: Adding ISO..."
|
|
local iso_response=$(curl -k -s -X PUT \
|
|
-H "Cookie: PVEAuthCookie=$ticket" \
|
|
-H "CSRFPreventionToken: $csrf" \
|
|
-d "ide2=$iso_volid" \
|
|
"$PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/qemu/$vmid/config" 2>&1)
|
|
|
|
if echo "$iso_response" | grep -q '"errors"'; then
|
|
log_warn "ISO configuration warning (continuing anyway)"
|
|
fi
|
|
|
|
# Step 5: Add network (try simplest format)
|
|
log_info "Step 5: Adding network..."
|
|
local net_response=$(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" 2>&1)
|
|
|
|
if echo "$net_response" | grep -q '"errors"'; then
|
|
log_warn "Network configuration warning (may need manual configuration)"
|
|
fi
|
|
|
|
# Step 6: Set boot order
|
|
log_info "Step 6: Configuring 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
|
|
|
|
# Verify VM config file was created
|
|
sleep 2
|
|
local verify_response=$(curl -k -s -H "Cookie: PVEAuthCookie=$ticket" \
|
|
-H "CSRFPreventionToken: $csrf" \
|
|
"$PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/qemu/$vmid/config" 2>&1)
|
|
|
|
if echo "$verify_response" | grep -q '"errors"'; then
|
|
log_error "VM $name was not created properly. Config file missing."
|
|
log_error "Response: $verify_response"
|
|
return 1
|
|
fi
|
|
|
|
log_info "✓ VM $name created successfully"
|
|
|
|
# Configure Cloud-Init if IP is provided
|
|
if [ -n "$ip_address" ] && [ -n "$gateway" ]; then
|
|
log_info "Configuring Cloud-Init for $name..."
|
|
local config_response=$(curl -k -s -X PUT \
|
|
-H "Cookie: PVEAuthCookie=$ticket" \
|
|
-H "CSRFPreventionToken: $csrf" \
|
|
-d "ipconfig0=ip=${ip_address}/24,gw=${gateway}" \
|
|
"$PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/qemu/$vmid/config" 2>&1)
|
|
|
|
if echo "$config_response" | grep -q '"errors"'; then
|
|
log_warn "Cloud-Init configuration may have failed (VM will use DHCP)"
|
|
echo "$config_response" | python3 -c "import sys, json; d=json.load(sys.stdin); print(d.get('errors', {}).get('errors', 'Unknown error'))" 2>/dev/null || echo "$config_response"
|
|
else
|
|
log_info "✓ Network configured for $name"
|
|
fi
|
|
fi
|
|
|
|
return 0
|
|
}
|
|
|
|
# Start VM
|
|
start_vm() {
|
|
local auth=$1
|
|
local vmid=$2
|
|
local ticket=$(echo "$auth" | cut -d'|' -f1)
|
|
local csrf=$(echo "$auth" | cut -d'|' -f2)
|
|
|
|
log_info "Starting VM $vmid..."
|
|
local start_response=$(curl -k -s -X POST \
|
|
-H "Cookie: PVEAuthCookie=$ticket" \
|
|
-H "CSRFPreventionToken: $csrf" \
|
|
"$PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/qemu/$vmid/status/start")
|
|
|
|
if echo "$start_response" | grep -q '"error"'; then
|
|
log_warn "Failed to start VM $vmid: $start_response"
|
|
return 1
|
|
fi
|
|
|
|
log_info "✓ VM $vmid started"
|
|
return 0
|
|
}
|
|
|
|
# VM configurations
|
|
declare -A VMS=(
|
|
["100"]="cloudflare-tunnel:2:4096:40:192.168.1.60:192.168.1.254"
|
|
["101"]="k3s-master:4:8192:80:192.168.1.188:192.168.1.254"
|
|
["102"]="git-server:4:8192:100:192.168.1.121:192.168.1.254"
|
|
["103"]="observability:4:8192:200:192.168.1.82:192.168.1.254"
|
|
)
|
|
|
|
main() {
|
|
log_header "Create All Service VMs via Proxmox API"
|
|
echo ""
|
|
|
|
if [ -z "$PVE_PASSWORD" ]; then
|
|
log_error "PVE_ROOT_PASS not set in .env"
|
|
exit 1
|
|
fi
|
|
|
|
# Check ISO file
|
|
check_iso
|
|
echo ""
|
|
|
|
# Authenticate
|
|
log_step "Authenticating with Proxmox..."
|
|
auth=$(get_ticket)
|
|
if [ $? -ne 0 ]; then
|
|
exit 1
|
|
fi
|
|
log_info "✓ Authentication successful"
|
|
echo ""
|
|
|
|
# Check if ISO already uploaded
|
|
if [ "$(iso_exists "$auth")" = "true" ]; then
|
|
log_info "✓ ISO already exists in Proxmox storage"
|
|
else
|
|
# Upload ISO
|
|
if ! upload_iso "$auth"; then
|
|
log_error "Failed to upload ISO"
|
|
exit 1
|
|
fi
|
|
fi
|
|
echo ""
|
|
|
|
# Create VMs
|
|
log_step "Creating VMs..."
|
|
echo ""
|
|
|
|
for vmid in "${!VMS[@]}"; do
|
|
IFS=':' read -r name cores memory disk_size ip_address gateway <<< "${VMS[$vmid]}"
|
|
|
|
# Check if VM already exists
|
|
if [ "$(vm_exists "$auth" "$vmid")" = "true" ]; then
|
|
log_warn "VM $name (ID: $vmid) already exists. Skipping..."
|
|
continue
|
|
fi
|
|
|
|
# Create VM
|
|
if create_vm "$auth" "$vmid" "$name" "$cores" "$memory" "$disk_size" "$ip_address" "$gateway"; then
|
|
# Start VM
|
|
start_vm "$auth" "$vmid"
|
|
echo ""
|
|
else
|
|
log_error "Failed to create VM $name"
|
|
fi
|
|
done
|
|
|
|
log_header "VM Creation Complete"
|
|
echo ""
|
|
log_info "All VMs have been created and started!"
|
|
echo ""
|
|
log_info "Next steps:"
|
|
echo " 1. Access each VM console via Proxmox Web UI: $PROXMOX_URL"
|
|
echo " 2. Complete Ubuntu installation on each VM"
|
|
echo " 3. After OS installation, run setup scripts:"
|
|
echo " - scripts/setup-cloudflare-tunnel.sh (on 192.168.1.60)"
|
|
echo " - scripts/setup-k3s.sh (on 192.168.1.188)"
|
|
echo " - scripts/setup-git-server.sh (on 192.168.1.121)"
|
|
echo " - scripts/setup-observability.sh (on 192.168.1.82)"
|
|
echo ""
|
|
}
|
|
|
|
main "$@"
|
|
|