#!/bin/bash source ~/.bashrc # Create Ubuntu Cloud-Init Template via Proxmox API # This attempts to automate template creation as much as possible 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_error() { echo -e "${RED}[ERROR]${NC} $1" } log_step() { echo -e "${BLUE}[STEP]${NC} $1" } log_warn() { echo -e "${YELLOW}[WARN]${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_URL="https://192.168.1.206:8006" PROXMOX_NODE="pve" STORAGE="${STORAGE:-local}" TEMPLATE_ID=9000 TEMPLATE_NAME="ubuntu-24.04-cloudinit" CLOUD_IMAGE="ubuntu-24.04-server-cloudimg-amd64.img" IMAGE_PATH="./downloads/${CLOUD_IMAGE}" # 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 image is uploaded check_image_uploaded() { 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/$STORAGE/content") if echo "$response" | grep -q "$CLOUD_IMAGE"; then return 0 else return 1 fi } # Upload image to Proxmox upload_image() { local auth=$1 local ticket=$(echo "$auth" | cut -d'|' -f1) local csrf=$(echo "$auth" | cut -d'|' -f2) log_step "Uploading cloud image to Proxmox..." if [ ! -f "$IMAGE_PATH" ]; then log_error "Cloud image not found: $IMAGE_PATH" return 1 fi log_warn "Image upload via API is complex. Please upload manually:" log_info "1. Proxmox Web UI → Storage → $STORAGE → Upload" log_info "2. Select file: $IMAGE_PATH" log_info "3. Wait for upload to complete" echo "" read -p "Press Enter after image is uploaded..." if check_image_uploaded "$auth"; then log_info "✓ Image uploaded" return 0 else log_warn "Image upload not detected. Please verify manually." return 1 fi } # Create VM from uploaded image create_vm_from_image() { local auth=$1 local ticket=$(echo "$auth" | cut -d'|' -f1) local csrf=$(echo "$auth" | cut -d'|' -f2) log_step "Creating VM $TEMPLATE_ID from cloud image..." # Check if VM already exists local existing=$(curl -k -s \ -H "Cookie: PVEAuthCookie=$ticket" \ -H "CSRFPreventionToken: $csrf" \ "$PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/qemu/$TEMPLATE_ID/config" 2>&1) if echo "$existing" | grep -q '"name"'; then log_warn "VM $TEMPLATE_ID already exists" read -p "Delete and recreate? (y/N): " confirm if [ "$confirm" != "y" ]; then return 0 fi # Delete existing VM curl -k -s -X DELETE \ -H "Cookie: PVEAuthCookie=$ticket" \ -H "CSRFPreventionToken: $csrf" \ "$PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/qemu/$TEMPLATE_ID" > /dev/null 2>&1 sleep 2 fi log_warn "VM creation from image requires manual steps in Proxmox Web UI:" echo "" log_info "1. Create VM:" log_info " • Click 'Create VM'" log_info " • VM ID: $TEMPLATE_ID" log_info " • Name: $TEMPLATE_NAME" log_info " • OS: 'Do not use any media'" log_info " • Delete default disk" log_info " • Add disk: Import from $CLOUD_IMAGE" log_info " • CPU: 2, Memory: 2048MB" log_info " • Network: vmbr0, VirtIO" echo "" log_info "2. Configure Cloud-Init:" log_info " • Options → Cloud-Init" log_info " • User: ubuntu" log_info " • SSH Public Key: $(cat ~/.ssh/id_rsa.pub 2>/dev/null | head -1 || echo 'Your SSH key')" echo "" log_info "3. Convert to Template:" log_info " • Right-click VM → Convert to Template" echo "" read -p "Press Enter after template is created..." # Verify template exists local template_check=$(curl -k -s \ -H "Cookie: PVEAuthCookie=$ticket" \ -H "CSRFPreventionToken: $csrf" \ "$PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/qemu/$TEMPLATE_ID/config" 2>&1) if echo "$template_check" | grep -q '"template".*1'; then log_info "✓ Template created successfully" return 0 else log_warn "Template verification failed. Please check manually." return 1 fi } main() { log_header "Create Ubuntu Cloud-Init Template" echo "" if [ -z "$PVE_PASSWORD" ]; then log_error "PVE_ROOT_PASS not set in .env" exit 1 fi if [ ! -f "$IMAGE_PATH" ]; then log_error "Cloud image not found: $IMAGE_PATH" log_info "Download it first: ./scripts/download-ubuntu-cloud-image.sh 24.04" exit 1 fi # Authenticate auth=$(get_ticket) if [ $? -ne 0 ]; then exit 1 fi # Step 1: Upload image if ! check_image_uploaded "$auth"; then upload_image "$auth" else log_info "✓ Image already uploaded" fi # Step 2: Create VM and convert to template create_vm_from_image "$auth" log_header "Template Creation Complete!" echo "" log_info "Template $TEMPLATE_ID is ready" log_info "You can now run: ./scripts/recreate-vms-from-template.sh" } main "$@"