-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
create-1es-hosted-pool.ps1
407 lines (314 loc) · 13.2 KB
/
create-1es-hosted-pool.ps1
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
<#
.SYNOPSIS
Creates a 1ES Hosted Pool, set up for the STL's CI.
.DESCRIPTION
See https://github.com/microsoft/STL/wiki/Checklist-for-Toolset-Updates for more information.
#>
$ErrorActionPreference = 'Stop'
$CurrentDate = Get-Date
$Location = 'eastus'
$VMSize = 'Standard_D32ads_v5'
$ProtoVMName = 'PROTOTYPE'
$ImagePublisher = 'MicrosoftWindowsServer'
$ImageOffer = 'WindowsServer'
$ImageSku = '2022-datacenter-g2'
$ProgressActivity = 'Preparing STL CI pool'
$TotalProgress = 26
$CurrentProgress = 1
<#
.SYNOPSIS
Displays an updated progress bar.
.DESCRIPTION
Display-ProgressBar increments $CurrentProgress and displays $Status in the progress bar.
.PARAMETER Status
A message describing the current operation being performed.
#>
function Display-ProgressBar {
[CmdletBinding(PositionalBinding=$false)]
Param([Parameter(Mandatory)][string]$Status)
Write-Progress `
-Activity $ProgressActivity `
-Status $Status `
-PercentComplete (100 * $script:CurrentProgress++ / $TotalProgress)
}
<#
.SYNOPSIS
Generates a random password.
.DESCRIPTION
New-Password generates a password, randomly, of length $Length, containing
only alphanumeric characters, underscore, and dash.
.PARAMETER Length
The length of the returned password.
#>
function New-Password {
[CmdletBinding(PositionalBinding=$false)]
Param([int]$Length = 32)
# This 64-character alphabet generates 6 bits of entropy per character.
$alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-'.ToCharArray()
[SecureString] $result = [SecureString]::new()
for ($idx = 0; $idx -lt $Length; $idx++) {
$result.AppendChar((Get-SecureRandom -InputObject $alphabet))
}
return $result
}
<#
.SYNOPSIS
Waits for the shutdown of the specified resource.
.DESCRIPTION
Wait-Shutdown takes a VM, and checks if there's a 'PowerState/stopped'
code; if there is, it returns. If there isn't, it waits 10 seconds and
tries again.
.PARAMETER ResourceGroupName
The name of the resource group to look up the VM in.
.PARAMETER Name
The name of the virtual machine to wait on.
#>
function Wait-Shutdown {
[CmdletBinding(PositionalBinding=$false)]
Param(
[Parameter(Mandatory)][string]$ResourceGroupName,
[Parameter(Mandatory)][string]$Name
)
Write-Host "Waiting for $Name to stop..."
$StoppedCode = 'PowerState/stopped'
while ($StoppedCode -notin (Get-AzVM -ResourceGroupName $ResourceGroupName -Name $Name -Status).Statuses.Code) {
Write-Host '... not stopped yet, sleeping for 10 seconds'
Start-Sleep -Seconds 10
}
}
####################################################################################################
Display-ProgressBar -Status 'Silencing breaking change warnings'
# https://aka.ms/azps-changewarnings
$Env:SuppressAzurePowerShellBreakingChangeWarnings = 'true'
Update-AzConfig `
-DisplayBreakingChangeWarning $false `
-Scope 'Process' | Out-Null
####################################################################################################
Display-ProgressBar -Status 'Setting the subscription context'
Set-AzContext `
-SubscriptionName 'CPP_STL_GitHub' | Out-Null
####################################################################################################
Display-ProgressBar -Status 'Creating resource group'
$ResourceGroupName = 'StlBuild-' + $CurrentDate.ToString('yyyy-MM-ddTHHmm')
New-AzResourceGroup `
-Name $ResourceGroupName `
-Location $Location | Out-Null
####################################################################################################
Display-ProgressBar -Status 'Creating credentials'
$AdminPWSecure = New-Password
$Credential = New-Object System.Management.Automation.PSCredential ('AdminUser', $AdminPWSecure)
####################################################################################################
Display-ProgressBar -Status 'Creating virtual network'
$NetworkSecurityGroupName = $ResourceGroupName + '-NetworkSecurity'
$NetworkSecurityGroup = New-AzNetworkSecurityGroup `
-Name $NetworkSecurityGroupName `
-ResourceGroupName $ResourceGroupName `
-Location $Location
$SubnetName = $ResourceGroupName + '-Subnet'
$Subnet = New-AzVirtualNetworkSubnetConfig `
-Name $SubnetName `
-AddressPrefix '10.0.0.0/16' `
-NetworkSecurityGroup $NetworkSecurityGroup
$VirtualNetworkName = $ResourceGroupName + '-Network'
$VirtualNetwork = New-AzVirtualNetwork `
-Name $VirtualNetworkName `
-ResourceGroupName $ResourceGroupName `
-Location $Location `
-AddressPrefix '10.0.0.0/16' `
-Subnet $Subnet
####################################################################################################
Display-ProgressBar -Status 'Creating network interface'
$NicName = $ResourceGroupName + '-NIC'
$Nic = New-AzNetworkInterface `
-Name $NicName `
-ResourceGroupName $ResourceGroupName `
-Location $Location `
-Subnet $VirtualNetwork.Subnets[0]
####################################################################################################
Display-ProgressBar -Status 'Creating prototype VM'
# Previously: -Priority 'Spot'
$VM = New-AzVMConfig `
-Name $ProtoVMName `
-VMSize $VMSize `
-Priority 'Regular'
$VM = Set-AzVMOperatingSystem `
-VM $VM `
-Windows `
-ComputerName $ProtoVMName `
-Credential $Credential `
-ProvisionVMAgent
$VM = Add-AzVMNetworkInterface `
-VM $VM `
-Id $Nic.Id
$VM = Set-AzVMSourceImage `
-VM $VM `
-PublisherName $ImagePublisher `
-Offer $ImageOffer `
-Skus $ImageSku `
-Version 'latest'
$VM = Set-AzVMBootDiagnostic `
-VM $VM `
-Disable
$VM = Set-AzVMSecurityProfile `
-VM $VM `
-SecurityType 'TrustedLaunch'
$VM = Set-AzVMUefi `
-VM $VM `
-EnableVtpm $true `
-EnableSecureBoot $true
New-AzVm `
-ResourceGroupName $ResourceGroupName `
-Location $Location `
-VM $VM | Out-Null
####################################################################################################
Display-ProgressBar -Status 'Running provision-image.ps1 in VM'
$ProvisionImageResult = Invoke-AzVMRunCommand `
-ResourceGroupName $ResourceGroupName `
-VMName $ProtoVMName `
-CommandId 'RunPowerShellScript' `
-ScriptPath "$PSScriptRoot\provision-image.ps1"
Write-Host $ProvisionImageResult.value.Message
####################################################################################################
Display-ProgressBar -Status 'Restarting VM'
Restart-AzVM -ResourceGroupName $ResourceGroupName -Name $ProtoVMName | Out-Null
####################################################################################################
Display-ProgressBar -Status 'Sleeping after restart'
# The VM appears to be busy immediately after restarting.
# This workaround waits for a minute before attempting to run sysprep.ps1.
Start-Sleep -Seconds 60
####################################################################################################
Display-ProgressBar -Status 'Running sysprep.ps1 in VM'
Invoke-AzVMRunCommand `
-ResourceGroupName $ResourceGroupName `
-VMName $ProtoVMName `
-CommandId 'RunPowerShellScript' `
-ScriptPath "$PSScriptRoot\sysprep.ps1" | Out-Null
####################################################################################################
Display-ProgressBar -Status 'Waiting for VM to shut down'
Wait-Shutdown -ResourceGroupName $ResourceGroupName -Name $ProtoVMName
####################################################################################################
Display-ProgressBar -Status 'Stopping VM'
Stop-AzVM `
-ResourceGroupName $ResourceGroupName `
-Name $ProtoVMName `
-Force | Out-Null
####################################################################################################
Display-ProgressBar -Status 'Generalizing VM'
Set-AzVM `
-ResourceGroupName $ResourceGroupName `
-Name $ProtoVMName `
-Generalized | Out-Null
$VM = Get-AzVM `
-ResourceGroupName $ResourceGroupName `
-Name $ProtoVMName
$PrototypeOSDiskName = $VM.StorageProfile.OsDisk.Name
####################################################################################################
Display-ProgressBar -Status 'Creating gallery'
$GalleryName = 'StlBuild_' + $CurrentDate.ToString('yyyy_MM_ddTHHmm') + '_Gallery'
$Gallery = New-AzGallery `
-Location $Location `
-ResourceGroupName $ResourceGroupName `
-Name $GalleryName
####################################################################################################
Display-ProgressBar -Status 'Granting access to 1ES Resource Management'
$ServicePrincipalObjectId = (Get-AzADServicePrincipal -DisplayName '1ES Resource Management' -First 1).Id
New-AzRoleAssignment `
-ObjectId $ServicePrincipalObjectId `
-RoleDefinitionName 'Reader' `
-Scope $Gallery.Id | Out-Null
####################################################################################################
Display-ProgressBar -Status 'Creating image definition'
$ImageDefinitionName = $ResourceGroupName + '-ImageDefinition'
New-AzGalleryImageDefinition `
-Location $Location `
-ResourceGroupName $ResourceGroupName `
-GalleryName $GalleryName `
-Name $ImageDefinitionName `
-OsState 'Generalized' `
-OsType 'Windows' `
-Publisher $ImagePublisher `
-Offer $ImageOffer `
-Sku $ImageSku `
-Feature @(@{ Name = 'SecurityType'; Value = 'TrustedLaunch'; }) `
-HyperVGeneration 'V2' | Out-Null
####################################################################################################
Display-ProgressBar -Status 'Creating image version'
$ImageVersionName = $CurrentDate.ToString('yyyyMMdd.HHmm.0')
$ImageVersion = New-AzGalleryImageVersion `
-Location $Location `
-ResourceGroupName $ResourceGroupName `
-GalleryName $GalleryName `
-GalleryImageDefinitionName $ImageDefinitionName `
-Name $ImageVersionName `
-SourceImageVMId $VM.ID
####################################################################################################
Display-ProgressBar -Status 'Registering CloudTest resource provider'
Register-AzResourceProvider `
-ProviderNamespace 'Microsoft.CloudTest' | Out-Null
####################################################################################################
Display-ProgressBar -Status 'Creating 1ES image'
$ImageName = $ResourceGroupName + '-Image'
New-AzResource `
-Location $Location `
-ResourceGroupName $ResourceGroupName `
-ResourceType 'Microsoft.CloudTest/Images' `
-ResourceName $ImageName `
-Properties @{ 'imageType' = 'SharedImageGallery'; 'resourceId' = $ImageVersion.Id; } `
-Force | Out-Null
####################################################################################################
Display-ProgressBar -Status 'Creating 1ES Hosted Pool'
$PoolName = $ResourceGroupName + '-Pool'
$PoolProperties = @{
'organization' = 'https://dev.azure.com/vclibs'
'projects' = @('STL')
'sku' = @{ 'name' = $VMSize; 'tier' = 'StandardSSD'; 'enableSpot' = $false; }
'images' = @(@{ 'imageName' = $ImageName; 'poolBufferPercentage' = '100'; })
'maxPoolSize' = 64
'agentProfile' = @{ 'type' = 'Stateless'; }
}
New-AzResource `
-Location $Location `
-ResourceGroupName $ResourceGroupName `
-ResourceType 'Microsoft.CloudTest/hostedpools' `
-ResourceName $PoolName `
-Properties $PoolProperties `
-Force | Out-Null
####################################################################################################
Display-ProgressBar -Status 'Deleting unused VM'
Remove-AzVM `
-Id $VM.ID `
-Force | Out-Null
####################################################################################################
Display-ProgressBar -Status 'Deleting unused disk'
Remove-AzDisk `
-ResourceGroupName $ResourceGroupName `
-DiskName $PrototypeOSDiskName `
-Force | Out-Null
####################################################################################################
Display-ProgressBar -Status 'Deleting unused network interface'
Remove-AzNetworkInterface `
-ResourceGroupName $ResourceGroupName `
-Name $NicName `
-Force | Out-Null
####################################################################################################
Display-ProgressBar -Status 'Deleting unused virtual network'
Remove-AzVirtualNetwork `
-ResourceGroupName $ResourceGroupName `
-Name $VirtualNetworkName `
-Force | Out-Null
####################################################################################################
Display-ProgressBar -Status 'Deleting unused network security group'
Remove-AzNetworkSecurityGroup `
-ResourceGroupName $ResourceGroupName `
-Name $NetworkSecurityGroupName `
-Force | Out-Null
####################################################################################################
Write-Progress -Activity $ProgressActivity -Completed
Write-Host "Elapsed time: $(((Get-Date) - $CurrentDate).ToString('hh\:mm\:ss'))"
if ((Get-AzResource -ResourceGroupName $ResourceGroupName -Name $PoolName) -ne $null) {
Write-Host "Created pool: $PoolName"
} else {
Write-Error "Failed to create pool: $PoolName"
}