mcpField
W
windows-builder

windows-builder

github.com/hashicorpクラウド・DevOps9092.7k 回インストール今日更新
リポジトリ

windows-builder について

windows-builderは、PackerでWindowsイメージを構築するためのプラットフォーム非依存の手法をまとめたスキルです。インフラ担当者向けに、WinRM設定、PowerShellによるプロビジョニング、クリーンアップ、一般的な問題への対処を扱います。

主な機能

  1. 01Packer向けWinRM設定
  2. 02AWSとAzureのイメージ例
  3. 03PowerShellによるソフトウェア導入
  4. 04Windows Updateとクリーンアップの手順
  5. 05ビルドタイムアウトのトラブルシューティング

活用シーン

  1. 01Windowsイメージ構築用のWinRM設定
  2. 02Packerビルド中のソフトウェア導入
  3. 03プロビジョニング中のWindows Update適用
  4. 04WinRMタイムアウトやクリーンアップの問題調査

インストール

コマンドをコピーしてターミナルで実行します。

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

インストール後、Claude Code で/windows-builderを入力して読み込まれたか確認します。

詳細

Windows ビルダー

Packer で Windows イメージをビルドするための、プラットフォームに依存しないパターンです。

リファレンス: WinRM Communicator

Note: Windows のビルドには多くのコストと時間がかかります。Windows Update により、ビルドごとに45~120分かかることを想定してください。ビルドに失敗するとリソースが実行中のまま残ることがあります。必ずクリーンアップを確認してください。

WinRM コミュニケーターのセットアップ

Packer との通信には Windows の WinRM が必要です。

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 Update
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 Update に1~2時間かかる場合があります
  • 利用可能な場合は、パッチ適用済みのベースイメージを使用する
  • プロビジョナーに timeout = "2h" を設定する

リファレンス

コメント