diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..19c12f5 --- /dev/null +++ b/.env.example @@ -0,0 +1,19 @@ +# Windows Automation Environment Configuration +# Copy this file to .env and fill in your values + +# Proxmox Configuration +export PROXMOX_URL="https://la-vmh-07:8006/api2/json" +export PROXMOX_USERNAME="root@pam" +export PROXMOX_PASSWORD="your-proxmox-root-password" +export PROXMOX_NODE="la-vmh-07" + +# Windows Configuration +export WINRM_PASSWORD="PackerPassword123!" + +# Proxmox API Token (alternative to password) +export PM_API_TOKEN_ID="root@pam!forgejo-automation" +export PM_API_TOKEN_SECRET="your-api-token-secret" + +# Terraform Variables +export TF_VAR_build_id="001" +export TF_VAR_template_vm_id="9000" diff --git a/build-template.sh b/build-template.sh new file mode 100755 index 0000000..7157cc8 --- /dev/null +++ b/build-template.sh @@ -0,0 +1,199 @@ +#!/bin/bash +# Windows Packer Build Script +# This script builds the Windows 11 LTSC template on Proxmox + +set -e + +# Colors +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +NC='\033[0m' + +echo -e "${BLUE}╔════════════════════════════════════════════════════════════╗${NC}" +echo -e "${BLUE}║${NC} ${GREEN}Windows 11 LTSC Packer Build${NC} ${BLUE}║${NC}" +echo -e "${BLUE}║${NC} ${GREEN}Proxmox Virtual Environment${NC} ${BLUE}║${NC}" +echo -e "${BLUE}╚════════════════════════════════════════════════════════════╝${NC}" +echo "" + +# Default values +PROXMOX_NODE="la-vmh-07" +PROXMOX_URL="https://${PROXMOX_NODE}:8006/api2/json" +PROXMOX_USERNAME="root@pam" + +# Check for .env file +if [ -f ".env" ]; then + echo -e "${GREEN}Loading configuration from .env file...${NC}" + source .env +fi + +# Parse command line arguments +while [[ $# -gt 0 ]]; do + case $1 in + --node) + PROXMOX_NODE="$2" + PROXMOX_URL="https://${PROXMOX_NODE}:8006/api2/json" + shift 2 + ;; + --url) + PROXMOX_URL="$2" + shift 2 + ;; + --user) + PROXMOX_USERNAME="$2" + shift 2 + ;; + --password) + export PKR_VAR_proxmox_password="$2" + shift 2 + ;; + --check) + echo "Running validation only..." + CHECK_MODE=true + shift + ;; + --help|-h) + echo "Usage: $0 [OPTIONS]" + echo "" + echo "Options:" + echo " --node HOSTNAME Proxmox node name (default: la-vmh-07)" + echo " --url URL Proxmox API URL" + echo " --user USERNAME Proxmox username (default: root@pam)" + echo " --password PASS Proxmox password" + echo " --check Validate only, don't build" + echo "" + echo "Environment variables:" + echo " PKR_VAR_proxmox_password Proxmox password" + echo " PKR_VAR_proxmox_url Proxmox API URL" + echo " PKR_VAR_proxmox_node Proxmox node name" + exit 0 + ;; + *) + echo -e "${RED}Unknown option: $1${NC}" + exit 1 + ;; + esac +done + +# Determine script directory +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +cd "$SCRIPT_DIR" + +echo -e "${YELLOW}Configuration:${NC}" +echo " Proxmox URL: $PROXMOX_URL" +echo " Proxmox User: $PROXMOX_USERNAME" +echo " Proxmox Node: $PROXMOX_NODE" +echo "" + +# Check if running on Proxmox host +echo -e "${YELLOW}Checking environment...${NC}" +if hostname | grep -q "pve\|proxmox"; then + echo -e "${GREEN}✓ Running on Proxmox host${NC}" + ON_PROXMOX=true +else + echo -e "${YELLOW}⚠ Running on remote host${NC}" + echo -e "${YELLOW} Make sure Proxmox API is accessible from this machine${NC}" + ON_PROXMOX=false +fi + +# Check for Packer +echo "" +echo -e "${YELLOW}Checking for Packer...${NC}" +if ! command -v packer &> /dev/null; then + echo -e "${RED}Packer not found!${NC}" + echo "" + echo "To install Packer:" + echo "" + if [ "$ON_PROXMOX" = true ]; then + echo " # On Proxmox:" + echo " wget https://apt.releases.hashicorp.com/pool/main/h/hashicorp_*_amd64.deb" + echo " apt install -y ./hashicorp_*.deb" + echo " rm hashicorp_*.deb" + else + echo " # Download from:" + echo " https://www.packer.io/downloads" + echo "" + echo " # Or via Homebrew (macOS):" + echo " brew install packer" + echo "" + echo " # Or via Chocolatey (Windows):" + echo " choco install packer" + fi + exit 1 +fi + +echo -e "${GREEN}✓ Packer version: $(packer --version)${NC}" + +# Initialize Packer +echo "" +echo -e "${YELLOW}Initializing Packer plugins...${NC}" +packer init packer/ + +# Validate configuration +echo "" +echo -e "${YELLOW}Validating Packer configuration...${NC}" +export PKR_VAR_proxmox_url="$PROXMOX_URL" +export PKR_VAR_proxmox_node="$PROXMOX_NODE" +export PKR_VAR_proxmox_username="$PROXMOX_USERNAME" + +if ! packer validate packer/windows.pkr.hcl; then + echo -e "${RED}❌ Packer validation failed!${NC}" + exit 1 +fi +echo -e "${GREEN}✓ Configuration validated${NC}" + +# Check mode - just validate and exit +if [ "$CHECK_MODE" = true ]; then + echo "" + echo -e "${GREEN}Validation complete. No build performed.${NC}" + exit 0 +fi + +# Build the template +echo "" +echo -e "${BLUE}╔════════════════════════════════════════════════════════════╗${NC}" +echo -e "${BLUE}║${NC} ${GREEN}Starting Build${NC} ${BLUE}║${NC}" +echo -e "${BLUE}╚════════════════════════════════════════════════════════════╝${NC}" +echo "" +echo -e "${YELLOW}⏱ Estimated time: 15-25 minutes${NC}" +echo -e "${YELLOW}📝 Build logs will be shown in real-time${NC}" +echo "" + +# Set environment variables +export PKR_VAR_proxmox_url="$PROXMOX_URL" +export PKR_VAR_proxmox_node="$PROXMOX_NODE" + +# Run Packer build +if packer build -timestamp-ui packer/windows.pkr.hcl; then + echo "" + echo -e "${GREEN}╔════════════════════════════════════════════════════════════╗${NC}" + echo -e "${GREEN}║${NC} ${GREEN}✅ BUILD SUCCESSFUL!${NC} ${GREEN}║${NC}" + echo -e "${GREEN}╚════════════════════════════════════════════════════════════╝${NC}" + echo "" + echo -e "${GREEN}Template created successfully!${NC}" + echo "" + echo "Next steps:" + echo " 1. Verify in Proxmox: ${YELLOW}qm list | grep win11-ltsc${NC}" + echo " 2. Note the VM ID (e.g., 9000)" + echo " 3. Update terraform/variables.tf with the VM ID" + echo " 4. Test provisioning: ${YELLOW}cd terraform && tofu init && tofu apply${NC}" + echo "" +else + echo "" + echo -e "${RED}╔══════════════════════════════════════════════════════╗${NC}" + echo -e "${RED}║${NC} ${RED}❌ BUILD FAILED${NC} ${RED}║${NC}" + echo -e "${RED}╚══════════════════════════════════════════════════════╝${NC}" + echo "" + echo -e "${YELLOW}Troubleshooting steps:${NC}" + echo " 1. Verify Proxmox is running and accessible" + echo " 2. Check ISO files exist in /mnt/pve-07-iso-nvme/template/iso/" + echo " 3. Verify credentials are correct" + echo " 4. Check network connectivity" + echo "" + echo -e "${YELLOW}Common issues:${NC}" + echo " - ISO not found: Verify path in variables.pkr.hcl" + echo " - WinRM timeout: Check Autounattend.xml configuration" + echo " - Permission denied: Verify Proxmox user permissions" + exit 1 +fi diff --git a/packer/variables.pkr.hcl b/packer/variables.pkr.hcl new file mode 100644 index 0000000..fcdff1d --- /dev/null +++ b/packer/variables.pkr.hcl @@ -0,0 +1,79 @@ +variable "proxmox_url" { + type = string + default = "https://la-vmh-07:8006/api2/json" + description = "Proxmox API URL" +} + +variable "proxmox_username" { + type = string + default = "root@pam" + description = "Proxmox authentication user" +} + +variable "proxmox_password" { + type = string + default = "" + description = "Proxmox password (set via PKR_VAR_proxmox_password env var)" + sensitive = true +} + +variable "proxmox_node" { + type = string + default = "la-vmh-07" + description = "Proxmox node name" +} + +variable "vm_name" { + type = string + default = "win11-ltsc-template" + description = "Name of the template VM" +} + +variable "vm_memory" { + type = number + default = 8192 + description = "VM memory in MB" +} + +variable "vm_cores" { + type = number + default = 4 + description = "Number of CPU cores" +} + +variable "vm_disk_size" { + type = string + default = "60G" + description = "Disk size" +} + +variable "iso_storage" { + type = string + default = "local" + description = "Proxmox storage for ISOs" +} + +variable "windows_iso" { + type = string + default = "CLIENT_LTSC_EVAL_x64FRE_en-us.iso" + description = "Windows ISO filename" +} + +variable "disk_storage" { + type = string + default = "local-lvm" + description = "Proxmox storage for disks" +} + +variable "virtio_iso" { + type = string + default = "local:iso/virtio-win.iso" + description = "Path to VirtIO ISO" +} + +variable "winrm_password" { + type = string + default = "PackerPassword123!" + description = "Windows Administrator password for WinRM" + sensitive = true +} diff --git a/packer/windows.pkr.hcl b/packer/windows.pkr.hcl index a8a1d87..093d3a7 100644 --- a/packer/windows.pkr.hcl +++ b/packer/windows.pkr.hcl @@ -8,51 +8,52 @@ packer { } source "proxmox-iso" "windows-11" { - proxmox_url = "https://proxmox-host:8006/api2/json" - username = "root@pam" - password = "secret" - node = "la-vmh-07" + proxmox_url = "${var.proxmox_url}" + username = "${var.proxmox_username}" + password = "${var.proxmox_password}" + node = "${var.proxmox_node}" - vm_name = "win11-ltsc-template" + vm_name = "${var.vm_name}" template_description = "Built with Packer on ${timestamp()}" - iso_file = "local:iso/CLIENT_LTSC_EVAL_x64FRE_en-us.iso" + iso_file = "${var.iso_storage}:iso/${var.windows_iso}" qemu_agent = true - cores = 4 - memory = 8192 + cores = "${var.vm_cores}" + memory = "${var.vm_memory}" machine = "q35" bios = "ovmf" efi_config { - efi_storage_pool = "local-lvm" + efi_storage_pool = "${var.disk_storage}" pre_enrolled_keys = true } tpm_config { version = "2.0" - tpm_storage_pool = "local-lvm" + tpm_storage_pool = "${var.disk_storage}" } scsi_controller = "virtio-scsi-pci" disks { - disk_size = "60G" - storage_pool = "local-lvm" + disk_size = "${var.vm_disk_size}" + storage_pool = "${var.disk_storage}" type = "virtio" format = "raw" cache_mode = "writeback" } + additional_iso_files { - device = "sata1" - iso_file = "local:iso/virtio-win.iso" + device = "sata1" + iso_file = "${var.virtio_iso}" } communicator = "winrm" winrm_username = "Administrator" - winrm_password = "PackerPassword123!" + winrm_password = "${var.winrm_password}" winrm_insecure = true winrm_use_ssl = true boot_command = [ - "","","", - "","" + "", "", "", + "", "" ] boot_wait = "10s" }