Files
loc_az_hci/scripts/vm-management/create/create-first-vm.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

158 lines
3.9 KiB
Bash
Executable File

#!/bin/bash
source ~/.bashrc
# Create First VM (Cloudflare Tunnel) via Proxmox API
# This script helps create a VM using the Proxmox API
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
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}"
# 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
}
# 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"
}
# Main
echo "========================================="
echo "Create Cloudflare Tunnel VM"
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
auth=$(get_ticket)
if [ $? -ne 0 ]; then
exit 1
fi
log_info "Authentication successful"
echo ""
# Check for existing VM
if [ "$(vm_exists "$auth" 100)" = "true" ]; then
log_warn "VM with ID 100 already exists"
read -p "Continue anyway? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 0
fi
fi
# Get next VM ID
next_id=$(get_next_vmid "$auth")
log_info "Next available VM ID: $next_id"
echo ""
log_warn "VM creation via Proxmox API requires:"
log_warn " 1. A VM template (e.g., ubuntu-22.04-template)"
log_warn " 2. Or an ISO image uploaded to Proxmox"
echo ""
log_info "Recommended approach:"
echo " 1. Use Proxmox Web UI to create the first VM"
echo " 2. Convert it to a template for future use"
echo " 3. Then use Terraform or API for additional VMs"
echo ""
log_info "Proxmox Web UI: $PROXMOX_URL"
log_info "See CREATE_VMS.md for step-by-step instructions"
echo ""
log_info "VM Configuration for Cloudflare Tunnel:"
echo " - VM ID: 100"
echo " - Name: cloudflare-tunnel"
echo " - IP: 192.168.1.60"
echo " - CPU: 2 cores"
echo " - RAM: 4096 MB"
echo " - Disk: 40GB"
echo " - OS: Ubuntu 22.04 LTS"
echo ""
log_info "After creating the VM:"
echo " 1. Install Ubuntu 22.04 LTS"
echo " 2. Configure static IP: 192.168.1.60"
echo " 3. Run: sudo bash scripts/setup-cloudflare-tunnel.sh"
echo ""