#!/bin/bash source ~/.bashrc # Auto-Complete Template Setup and VM Recreation # Monitors for template creation and automatically recreates VMs set -e # Colors 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_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_URL="https://192.168.1.206:8006" PROXMOX_NODE="pve" TEMPLATE_ID=9000 check_template() { local response=$(curl -k -s -d "username=$PVE_USERNAME&password=$PVE_PASSWORD" \ "$PROXMOX_URL/api2/json/access/ticket" 2>/dev/null) 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 return 1 fi local config=$(curl -k -s \ -H "Cookie: PVEAuthCookie=$ticket" \ -H "CSRFPreventionToken: $csrf" \ "$PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/qemu/$TEMPLATE_ID/config" 2>&1) # Check if it exists and is a template if echo "$config" | grep -q '"name"' && echo "$config" | grep -q '"template".*1'; then return 0 else return 1 fi } main() { log_header "Auto-Complete Template Setup" echo "" log_step "Step 1: Template Creation (Manual - Required)" echo "" log_info "Please complete these steps in Proxmox Web UI:" echo "" echo "1. Upload Cloud Image:" echo " • Proxmox → Storage → local → Upload" echo " • File: ./downloads/ubuntu-24.04-server-cloudimg-amd64.img" echo "" echo "2. Create VM 9000:" echo " • Create VM (ID: 9000, Name: ubuntu-24.04-cloudinit)" echo " • Import disk from uploaded image" echo " • Configure Cloud-Init (User: ubuntu, SSH key)" echo "" echo "3. Convert to Template:" echo " • Right-click VM 9000 → Convert to Template" echo "" log_info "See: QUICK_TEMPLATE_GUIDE.md for detailed steps" echo "" log_step "Step 2: Monitoring for Template" log_info "Checking every 10 seconds for template creation..." echo "" local check_count=0 local max_checks=180 # 30 minutes while [ $check_count -lt $max_checks ]; do check_count=$((check_count + 1)) if check_template; then echo "" log_info "✓ Template detected! Proceeding with VM recreation..." echo "" # Run VM recreation export SSH_KEY="$HOME/.ssh/id_rsa" export SSH_USER="ubuntu" ./scripts/recreate-vms-from-template.sh exit $? fi if [ $((check_count % 6)) -eq 0 ]; then echo -n "." fi sleep 10 done echo "" log_info "Template not detected after 30 minutes" log_info "Please create template manually, then run:" echo " ./scripts/check-and-recreate.sh" } main "$@"