-
Notifications
You must be signed in to change notification settings - Fork 9
/
windows.pkr.hcl
128 lines (102 loc) · 3.65 KB
/
windows.pkr.hcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
variable "client_id" {
type = string
description = "Azure Service Principal App ID."
sensitive = true
}
variable "client_secret" {
type = string
description = "Azure Service Principal Secret."
sensitive = true
}
variable "subscription_id" {
type = string
description = "Azure Subscription ID."
sensitive = true
}
variable "tenant_id" {
type = string
description = "Azure Tenant ID."
sensitive = true
}
variable "artifacts_resource_group" {
type = string
description = "Packer Artifacts Resource Group."
}
variable "build_resource_group" {
type = string
description = "Packer Build Resource Group."
}
variable "source_image_publisher" {
type = string
description = "Windows Image Publisher."
}
variable "source_image_offer" {
type = string
description = "Windows Image Offer."
}
variable "source_image_sku" {
type = string
description = "Windows Image SKU."
}
variable "source_image_version" {
type = string
description = "Windows Image Version."
}
source "azure-arm" "avd" {
# WinRM Communicator
communicator = "winrm"
winrm_use_ssl = true
winrm_insecure = true
winrm_timeout = "5m"
winrm_username = "packer"
# Service Principal Authentication
client_id = var.client_id
client_secret = var.client_secret
subscription_id = var.subscription_id
tenant_id = var.tenant_id
# Source Image
os_type = "Windows"
image_publisher = var.source_image_publisher
image_offer = var.source_image_offer
image_sku = var.source_image_sku
image_version = var.source_image_version
# Destination Image
managed_image_resource_group_name = var.artifacts_resource_group
managed_image_name = "${var.source_image_sku}-${var.source_image_version}"
# Packer Computing Resources
build_resource_group_name = var.build_resource_group
vm_size = "Standard_D4ds_v4"
}
build {
source "azure-arm.avd" {}
# Install Chocolatey: https://chocolatey.org/install#individual
provisioner "powershell" {
inline = ["Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))"]
}
# Install Chocolatey packages
provisioner "file" {
source = "./packages.config"
destination = "D:/packages.config"
}
provisioner "powershell" {
inline = ["choco install --confirm D:/packages.config"]
# See https://docs.chocolatey.org/en-us/choco/commands/install#exit-codes
valid_exit_codes = [0, 3010]
}
provisioner "windows-restart" {}
# Azure PowerShell Modules
provisioner "powershell" {
script = "./install-azure-powershell.ps1"
}
# Generalize image using Sysprep
# See https://www.packer.io/docs/builders/azure/arm#windows
# See https://docs.microsoft.com/en-us/azure/virtual-machines/windows/build-image-with-packer#define-packer-template
provisioner "powershell" {
inline = [
"while ((Get-Service RdAgent).Status -ne 'Running') { Start-Sleep -s 5 }",
"while ((Get-Service WindowsAzureGuestAgent).Status -ne 'Running') { Start-Sleep -s 5 }",
"& $env:SystemRoot\\System32\\Sysprep\\Sysprep.exe /oobe /generalize /quiet /quit /mode:vm",
"while ($true) { $imageState = Get-ItemProperty HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Setup\\State | Select ImageState; if($imageState.ImageState -ne 'IMAGE_STATE_GENERALIZE_RESEAL_TO_OOBE') { Write-Output $imageState.ImageState; Start-Sleep -s 10 } else { break } }"
]
}
}