/
windows.pkr.hcl
197 lines (169 loc) · 6.12 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
variable "image_name" { type = string }
variable "image_date" { type = string }
variable "build_type" {
type = string
default = "googlecompute"
}
# Packer doesn't capture errors correctly when default execute command is used.
# See $ErrorActionPreference = 'Stop' in the new execute_command.
# So, use new execute_command to handle VM errors correctly
variable "execute_command" {
type = string
default = "powershell -executionpolicy bypass \"& { if (Test-Path variable:global:ProgressPreference){$ProgressPreference='SilentlyContinue'}; $ErrorActionPreference = 'Stop' ;. {{.Vars}}; &'{{.Path}}'; exit $LastExitCode }\""
}
variable "gcp_project" {
type = string
default = ""
}
locals {
image_identity = "${var.image_name}-${var.image_date}"
perl_version = "5.26.3.1"
python_version = "3.10.6"
windows_gcp_images = [
{
task_name = "windows-ci"
},
]
}
source "googlecompute" "windows" {
disk_size = "50"
disk_type = "pd-ssd"
project_id = var.gcp_project
source_image_family = "windows-2022"
image_name = local.image_identity
zone = "us-west1-a"
machine_type = "t2d-standard-4"
instance_name = "build-${local.image_identity}"
communicator = "winrm"
winrm_username = "packer_user"
winrm_insecure = true
winrm_use_ssl = true
winrm_timeout = "10m"
state_timeout = "10m"
metadata = {
windows-startup-script-cmd = "winrm quickconfig -quiet & net user /add packer_user & net localgroup administrators packer_user /add & winrm set winrm/config/service/auth @{Basic=\"true\"}"
}
}
build {
name = "windows"
# for using -only '*.${IMAGE_NAME}' while building images,
# so we can easily combine the packer invocations later
dynamic "source" {
for_each = local.windows_gcp_images
labels = ["source.${var.build_type}.windows"]
iterator = tag
content {
name = tag.value.task_name
}
}
### base installations
# googlecompute only
provisioner "powershell" {
execute_command = var.execute_command
inline = [
"$ErrorActionPreference = 'Stop'",
# disable antivirus
"Set-MpPreference -DisableRealtimeMonitoring $true -SubmitSamplesConsent NeverSend -MAPSReporting Disable",
# install choco
"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'))",
"choco install -y --no-progress 7zip",
"choco install -y --no-progress git --parameters=\"/GitAndUnixToolsOnPath\"",
]
}
provisioner "powershell" {
execute_command = var.execute_command
inline = [
"$ErrorActionPreference = 'Stop'",
# contains useful utilities, including a diff we can use
"[Environment]::SetEnvironmentVariable('PATH', 'C:\\Program Files\\Git\\usr\\bin;' + [Environment]::GetEnvironmentVariable('PATH', 'Machine'), 'Machine')",
# set IMAGE_IDENTITY to distinguish images on CI runs
"[Environment]::SetEnvironmentVariable('IMAGE_IDENTITY', '${local.image_identity}', 'Machine')",
]
}
# install windows debugger
provisioner "powershell" {
execute_command = var.execute_command
script = "scripts/windows_install_dbg.ps1"
}
# install python
provisioner "powershell" {
execute_command = var.execute_command
environment_vars = ["TEMP_PYTHON_VERSION=${local.python_version}"]
script = "scripts/windows_install_python.ps1"
}
# install meson and ninja
provisioner "powershell" {
execute_command = var.execute_command
inline = [
"$ErrorActionPreference = 'Stop'",
"py -m pip install meson ninja"
]
}
# install perl
provisioner "powershell" {
execute_command = var.execute_command
environment_vars = ["TEMP_PERL_VERSION=${local.perl_version}"]
script = "scripts/windows_install_perl.ps1"
}
# set env vars for perl
provisioner "powershell" {
execute_command = var.execute_command
inline = [
"$ErrorActionPreference = 'Stop'",
"[Environment]::SetEnvironmentVariable('DEFAULT_PERL_VERSION', '${local.perl_version}', 'Machine')",
"[Environment]::SetEnvironmentVariable('PATH', \"C:\\strawberry\\${local.perl_version}\\perl\\bin;\" + [Environment]::GetEnvironmentVariable('PATH', 'Machine'), 'Machine')",
]
}
# install openssl
provisioner "powershell" {
execute_command = var.execute_command
script = "scripts/windows_install_openssl.ps1"
}
### end of base installations
### mingw installations
provisioner "powershell" {
execute_command = var.execute_command
inline = [
"$ErrorActionPreference = 'Stop'",
"[Environment]::SetEnvironmentVariable('MSYSTEM', 'UCRT64', 'Machine')",
# this could be reduntant
"$env:MSYSTEM = 'UCRT64'",
]
}
provisioner "powershell" {
execute_command = var.execute_command
script = "scripts/windows_install_mingw64.ps1"
}
# Change default console code page (0) with Windows code page (65001) to get rid of warnings in postgres tests
provisioner "powershell" {
execute_command = var.execute_command
inline = [
"$ErrorActionPreference = 'Stop'",
"chcp 65001"
]
}
# MSYS2 might spawn processes that will stay around in the background forever.
# They need to be killed. See: https://www.msys2.org/docs/ci/
provisioner "powershell" {
execute_command = var.execute_command
inline = [
"$ErrorActionPreference = 'Stop'",
"taskkill /F /FI \"MODULES eq msys-2.0.dll\""
]
}
### end of mingw installations
### vs-2019 installations
provisioner "powershell" {
execute_command = var.execute_command
script = "scripts/windows_install_winflexbison.ps1"
}
provisioner "powershell" {
execute_command = var.execute_command
script = "scripts/windows_install_pg_deps.ps1"
}
provisioner "powershell" {
execute_command = var.execute_command
script = "scripts/windows_install_vs_2019.ps1"
}
### end of vs-2019 installations
}