#!/bin/bash source ~/.bashrc # Check Template Status and Guide Recreation # This script checks if template exists and guides the process set -e # Colors GREEN='\033[0;32m' YELLOW='\033[1;33m' RED='\033[0;31m' 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_URL="https://192.168.1.206:8006" PROXMOX_NODE="pve" TEMPLATE_ID=9000 # Check if template exists 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) if echo "$config" | grep -q '"name"'; then return 0 else return 1 fi } main() { log_header "Template Status Check" echo "" if [ -z "$PVE_PASSWORD" ]; then log_error "PVE_ROOT_PASS not set in .env" exit 1 fi log_step "Checking if template $TEMPLATE_ID exists..." if check_template; then log_info "✓ Template $TEMPLATE_ID exists!" echo "" log_step "Template is ready. Proceeding with VM recreation..." echo "" # Run recreation script export SSH_KEY="$HOME/.ssh/id_rsa" export SSH_USER="ubuntu" ./scripts/recreate-vms-from-template.sh else log_warn "Template $TEMPLATE_ID does not exist yet" echo "" log_info "You need to create the template first:" echo "" log_step "Quick Steps:" echo " 1. Upload cloud image to Proxmox:" echo " • Proxmox Web UI → Storage → local → Upload" echo " • File: ./downloads/ubuntu-24.04-server-cloudimg-amd64.img" echo "" echo " 2. Create VM 9000 from image:" echo " • Create VM (ID: 9000, Name: ubuntu-24.04-cloudinit)" echo " • Import disk from uploaded image" echo " • Configure Cloud-Init with 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 instructions" echo "" log_info "After creating template, run this script again:" echo " ./scripts/check-and-recreate.sh" echo "" log_info "Or run directly:" echo " ./scripts/recreate-vms-from-template.sh" fi } main "$@"