mcpField
W
windows-builder

windows-builder

github.com/hashicorp云与 DevOps9092.7k 次安装今天更新
代码仓库

关于 windows-builder

windows-builder 提供使用 Packer 构建 Windows 镜像的平台无关实践,面向基础设施从业者,涵盖 WinRM 配置、PowerShell 预配、清理和常见问题处理。

主要功能

  1. 01为 Packer 配置 WinRM
  2. 02AWS 和 Azure 镜像示例
  3. 03使用 PowerShell 安装软件
  4. 04Windows 更新与清理指南
  5. 05构建超时问题排查

适用场景

  1. 01为 Windows 镜像构建配置 WinRM
  2. 02在 Packer 构建中安装软件
  3. 03在预配期间应用 Windows 更新
  4. 04排查 WinRM 超时和清理问题

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/hashicorp/agent-skills --skill windows-builder

安装后在 Claude Code 中输入/windows-builder确认已加载。

详细介绍

Windows 构建器

用于使用 Packer 构建 Windows 映像的平台无关模式。

参考: WinRM 通信器

注意: Windows 构建会产生大量成本并耗费很长时间。由于 Windows 更新,每次构建预计需要 45-120 分钟。构建失败后可能会留下仍在运行的资源,请务必确认已完成清理。

WinRM 通信器设置

Windows 需要使用 WinRM 与 Packer 通信。

AWS 示例
source "amazon-ebs" "windows" {
  region        = "us-west-2"
  instance_type = "t3.medium"

  source_ami_filter {
    filters = {
      name = "Windows_Server-2022-English-Full-Base-*"
    }
    most_recent = true
    owners      = ["amazon"]
  }

  ami_name = "windows-server-2022-${local.timestamp}"

  communicator   = "winrm"
  winrm_username = "Administrator"
  winrm_use_ssl  = true
  winrm_insecure = true
  winrm_timeout  = "15m"

  user_data_file = "scripts/setup-winrm.ps1"
}
WinRM 设置脚本(scripts/setup-winrm.ps1)
<powershell>
# Configure WinRM
winrm quickconfig -q
winrm set winrm/config '@{MaxTimeoutms="1800000"}'
winrm set winrm/config/service '@{AllowUnencrypted="true"}'
winrm set winrm/config/service/auth '@{Basic="true"}'

# Configure firewall
netsh advfirewall firewall add rule name="WinRM 5985" protocol=TCP dir=in localport=5985 action=allow
netsh advfirewall firewall add rule name="WinRM 5986" protocol=TCP dir=in localport=5986 action=allow

# Restart WinRM
net stop winrm
net start winrm
</powershell>
Azure 示例
source "azure-arm" "windows" {
  client_id       = var.client_id
  client_secret   = var.client_secret
  subscription_id = var.subscription_id
  tenant_id       = var.tenant_id

  managed_image_resource_group_name = "images-rg"
  managed_image_name                = "windows-${local.timestamp}"

  os_type         = "Windows"
  image_publisher = "MicrosoftWindowsServer"
  image_offer     = "WindowsServer"
  image_sku       = "2022-datacenter-g2"

  location = "East US"
  vm_size  = "Standard_D2s_v3"

  # Azure auto-configures WinRM
  communicator   = "winrm"
  winrm_use_ssl  = true
  winrm_insecure = true
  winrm_timeout  = "15m"
  winrm_username = "packer"
}

PowerShell 配置器

安装软件
build {
  sources = ["source.amazon-ebs.windows"]

  # Install Chocolatey
  provisioner "powershell" {
    inline = [
      "Set-ExecutionPolicy Bypass -Scope Process -Force",
      "iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))"
    ]
  }

  # Install applications
  provisioner "powershell" {
    inline = [
      "choco install -y googlechrome",
      "choco install -y 7zip",
    ]
  }

  # Install IIS
  provisioner "powershell" {
    inline = [
      "Install-WindowsFeature -Name Web-Server -IncludeManagementTools"
    ]
  }
}
Windows 更新
provisioner "powershell" {
  inline = [
    "Install-PackageProvider -Name NuGet -Force",
    "Install-Module -Name PSWindowsUpdate -Force",
    "Import-Module PSWindowsUpdate",
    "Get-WindowsUpdate -Install -AcceptAll -AutoReboot",
  ]
  timeout = "2h"
}

# Wait for reboots
provisioner "windows-restart" {
  restart_timeout = "30m"
}

清理

provisioner "powershell" {
  inline = [
    "# Clear temp files",
    "Remove-Item -Path 'C:\\Windows\\Temp\\*' -Recurse -Force -ErrorAction SilentlyContinue",
    "# Clear Windows Update cache",
    "Stop-Service -Name wuauserv -Force",
    "Remove-Item -Path 'C:\\Windows\\SoftwareDistribution\\*' -Recurse -Force -ErrorAction SilentlyContinue",
    "Start-Service -Name wuauserv",
  ]
}

常见问题

WinRM 超时

  • 将 winrm_timeout 增加到 15m 或更长
  • 确认安全组允许端口 5985/5986
  • 检查用户数据脚本是否已成功完成

PowerShell 执行策略

provisioner "powershell" {
  inline = [
    "Set-ExecutionPolicy Bypass -Scope Process -Force",
    "# Your commands here",
  ]
}

构建时间过长

  • Windows 更新可能需要 1-2 小时
  • 在可用时使用已预装补丁的基础映像
  • 设置配置器 timeout = "2h"

参考资料

评论

支持 Markdown