Files
loc_az_hci/scripts/proxmox/create-service-vms.sh
defiQUG c39465c2bd
Some checks failed
Test / test (push) Has been cancelled
Initial commit: loc_az_hci (smom-dbis-138 excluded via .gitignore)
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-08 09:04:46 -08:00

186 lines
4.9 KiB
Bash
Executable File

#!/bin/bash
source ~/.bashrc
# Create Service VMs on Proxmox
# Creates VMs for K3s, Cloudflare Tunnel, Git Server, and Observability
set -e
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
# 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
# Proxmox configuration
PVE_USERNAME="${PVE_USERNAME:-root@pam}"
PVE_PASSWORD="${PVE_ROOT_PASS:-}"
PROXMOX_HOST="${1:-192.168.1.206}" # Default to ML110
PROXMOX_URL="https://${PROXMOX_HOST}:8006"
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_test() {
echo -e "${BLUE}[TEST]${NC} $1"
}
# 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"
}
# Get next available VM ID
get_next_vmid() {
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/cluster/nextid")
echo "$response" | grep -o '"data":"[^"]*' | cut -d'"' -f4
}
# List existing VMs
list_vms() {
local auth=$1
local ticket=$(echo "$auth" | cut -d'|' -f1)
local csrf=$(echo "$auth" | cut -d'|' -f2)
log_info "Listing existing VMs on $PROXMOX_HOST..."
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']
if vms:
print(f'Found {len(vms)} VMs:')
for vm in vms:
print(f\" - {vm.get('name', 'unknown')} (ID: {vm.get('vmid', 'N/A')}, Status: {vm.get('status', 'unknown')})\")
else:
print('No VMs found')
" 2>/dev/null || echo "Could not parse VM list"
}
# Create VM (simplified - requires template)
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 ticket=$(echo "$auth" | cut -d'|' -f1)
local csrf=$(echo "$auth" | cut -d'|' -f2)
log_info "Creating VM: $name (ID: $vmid)"
# Note: This is a simplified example
# Full VM creation requires a template or ISO
# For now, we'll provide instructions
log_warn "VM creation via API requires:"
log_warn " 1. A VM template (e.g., ubuntu-22.04-template)"
log_warn " 2. Or use Proxmox Web UI for initial VM creation"
log_warn " 3. Or use Terraform (recommended)"
echo ""
log_info "Recommended: Use Proxmox Web UI or Terraform"
log_info " Web UI: $PROXMOX_URL"
log_info " Terraform: cd terraform/proxmox && terraform apply"
}
main() {
echo "========================================="
echo "Proxmox Service VM Creation"
echo "========================================="
echo ""
if [ -z "$PVE_PASSWORD" ]; then
log_error "PVE_ROOT_PASS not set in .env"
exit 1
fi
log_info "Connecting to Proxmox: $PROXMOX_URL"
# Authenticate
local auth=$(get_ticket)
if [ $? -ne 0 ]; then
exit 1
fi
log_info "Authentication successful"
echo ""
# List existing VMs
list_vms "$auth"
echo ""
# Get next VM ID
local next_id=$(get_next_vmid "$auth")
log_info "Next available VM ID: $next_id"
echo ""
log_info "Service VMs to create:"
echo " 1. Cloudflare Tunnel VM (ID: $next_id)"
echo " - 2 vCPU, 4GB RAM, 40GB disk"
echo " - IP: 192.168.1.60"
echo ""
echo " 2. K3s Master VM (ID: $((next_id + 1)))"
echo " - 4 vCPU, 8GB RAM, 80GB disk"
echo " - IP: 192.168.1.188"
echo ""
echo " 3. Git Server VM (ID: $((next_id + 2)))"
echo " - 4 vCPU, 8GB RAM, 100GB disk"
echo " - IP: 192.168.1.121"
echo ""
echo " 4. Observability VM (ID: $((next_id + 3)))"
echo " - 4 vCPU, 8GB RAM, 200GB disk"
echo " - IP: 192.168.1.82"
echo ""
log_warn "Full VM creation via API requires templates."
log_info "Options:"
log_info " 1. Use Proxmox Web UI: $PROXMOX_URL"
log_info " 2. Use Terraform: cd terraform/proxmox"
log_info " 3. Create templates first, then use API"
}
main "$@"