Some checks failed
Test / test (push) Has been cancelled
Co-authored-by: Cursor <cursoragent@cursor.com>
179 lines
5.1 KiB
Bash
Executable File
179 lines
5.1 KiB
Bash
Executable File
#!/bin/bash
|
|
source ~/.bashrc
|
|
# Complete VM Setup - Final Configuration and Cloud-Init Setup
|
|
# Attempts to configure everything possible via API
|
|
|
|
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
|
|
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"
|
|
}
|
|
|
|
# Complete VM setup with Cloud-Init
|
|
setup_vm_complete() {
|
|
local auth=$1
|
|
local vmid=$2
|
|
local name=$3
|
|
local ip_address=$4
|
|
local gateway=$5
|
|
|
|
local ticket=$(echo "$auth" | cut -d'|' -f1)
|
|
local csrf=$(echo "$auth" | cut -d'|' -f2)
|
|
|
|
log_step "Complete setup for $name (ID: $vmid)..."
|
|
|
|
# Stop 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 2
|
|
|
|
# Enable Cloud-Init
|
|
log_info "Enabling Cloud-Init..."
|
|
curl -k -s -X PUT \
|
|
-H "Cookie: PVEAuthCookie=$ticket" \
|
|
-H "CSRFPreventionToken: $csrf" \
|
|
-d "ipconfig0=ip=${ip_address}/24,gw=${gateway}" \
|
|
-d "nameserver=8.8.8.8" \
|
|
-d "ciuser=ubuntu" \
|
|
-d "cipassword=" \
|
|
"$PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/qemu/$vmid/config" > /dev/null 2>&1 || log_warn "Cloud-Init config may have issues"
|
|
|
|
# Configure network (try POST method)
|
|
log_info "Configuring network interface..."
|
|
curl -k -s -X POST \
|
|
-H "Cookie: PVEAuthCookie=$ticket" \
|
|
-H "CSRFPreventionToken: $csrf" \
|
|
-d "net0=virtio,bridge=vmbr0" \
|
|
"$PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/qemu/$vmid/config" > /dev/null 2>&1 || log_warn "Network config may need manual setup"
|
|
|
|
# Configure ISO (try POST method)
|
|
log_info "Configuring ISO..."
|
|
curl -k -s -X POST \
|
|
-H "Cookie: PVEAuthCookie=$ticket" \
|
|
-H "CSRFPreventionToken: $csrf" \
|
|
-d "ide2=local:iso/${ISO_FILE},media=cdrom" \
|
|
"$PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/qemu/$vmid/config" > /dev/null 2>&1 || log_warn "ISO config may need manual setup"
|
|
|
|
# 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 "✓ $name configured and started"
|
|
}
|
|
|
|
# VM configurations with IPs
|
|
declare -A VMS=(
|
|
["100"]="cloudflare-tunnel:192.168.1.60:192.168.1.254"
|
|
["101"]="k3s-master:192.168.1.188:192.168.1.254"
|
|
["102"]="git-server:192.168.1.121:192.168.1.254"
|
|
["103"]="observability:192.168.1.82:192.168.1.254"
|
|
)
|
|
|
|
main() {
|
|
log_header "Complete VM Setup with Cloud-Init"
|
|
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 "Configuring all VMs with Cloud-Init..."
|
|
echo ""
|
|
|
|
for vmid in "${!VMS[@]}"; do
|
|
IFS=':' read -r name ip_address gateway <<< "${VMS[$vmid]}"
|
|
setup_vm_complete "$auth" "$vmid" "$name" "$ip_address" "$gateway"
|
|
echo ""
|
|
done
|
|
|
|
log_header "Setup Complete"
|
|
echo ""
|
|
log_info "All VMs configured with:"
|
|
echo " ✓ Cloud-Init enabled"
|
|
echo " ✓ Network interfaces configured"
|
|
echo " ✓ ISO attached"
|
|
echo " ✓ Boot order set"
|
|
echo ""
|
|
log_warn "Note: Network and ISO may need manual verification via Proxmox Web UI"
|
|
log_info "Next: Install Ubuntu via console (network/ISO should be ready)"
|
|
}
|
|
|
|
main "$@"
|
|
|