2021-09-11 13:43:19 -07:00
|
|
|
# Copyright (c) Microsoft Corporation.
|
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
#
|
|
|
|
|
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Creates a Windows virtual machine image, set up for vcpkg's CI.
|
|
|
|
|
|
|
|
.DESCRIPTION
|
|
|
|
create-image.ps1 creates an Azure Windows VM image, set up for vcpkg's CI system.
|
|
|
|
|
|
|
|
This script assumes you have installed Azure tools into PowerShell by following the instructions
|
|
|
|
at https://docs.microsoft.com/en-us/powershell/azure/install-az-ps?view=azps-3.6.1
|
|
|
|
or are running from Azure Cloud Shell.
|
|
|
|
#>
|
|
|
|
|
2024-02-12 15:07:57 -08:00
|
|
|
$Location = 'westus3'
|
2024-03-18 13:26:24 -07:00
|
|
|
$DatePrefixComponent = Get-Date -Format 'yyyy-MM-dd'
|
|
|
|
$Prefix = "Win-$DatePrefixComponent"
|
|
|
|
$GalleryImageVersion = $DatePrefixComponent.Replace('-','.')
|
2024-02-12 15:07:57 -08:00
|
|
|
$VMSize = 'Standard_D8ads_v5'
|
2021-09-11 13:43:19 -07:00
|
|
|
$ProtoVMName = 'PROTOTYPE'
|
2024-02-12 15:07:57 -08:00
|
|
|
$WindowsServerSku = '2022-datacenter-azure-edition'
|
2021-09-11 13:43:19 -07:00
|
|
|
$ErrorActionPreference = 'Stop'
|
|
|
|
|
|
|
|
$ProgressActivity = 'Creating Windows Image'
|
2024-11-19 14:42:29 -08:00
|
|
|
$TotalProgress = 17
|
2021-09-11 13:43:19 -07:00
|
|
|
$CurrentProgress = 1
|
|
|
|
|
2024-03-18 13:26:24 -07:00
|
|
|
# Assigning this to another variable helps when running the commands in this script manually for
|
|
|
|
# debugging
|
|
|
|
$Root = $PSScriptRoot
|
2021-09-11 13:43:19 -07:00
|
|
|
|
2024-07-03 20:01:35 -07:00
|
|
|
<#
|
|
|
|
.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 {
|
|
|
|
Param ([int] $Length = 32)
|
|
|
|
$alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-"
|
|
|
|
if ($alphabet.Length -ne 64) {
|
|
|
|
throw 'Bad alphabet length'
|
|
|
|
}
|
|
|
|
|
2024-08-27 12:34:26 -07:00
|
|
|
$result = New-Object SecureString
|
2024-07-03 20:01:35 -07:00
|
|
|
for ($idx = 0; $idx -lt $Length; $idx++) {
|
2024-08-30 11:07:40 -07:00
|
|
|
$result.AppendChar($alphabet[(Get-SecureRandom -Maximum $alphabet.Length)])
|
2024-07-03 20:01:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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 ten 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()]
|
|
|
|
Param([string]$ResourceGroupName, [string]$Name)
|
|
|
|
|
|
|
|
Write-Host "Waiting for $Name to stop..."
|
|
|
|
while ($true) {
|
|
|
|
$Vm = Get-AzVM -ResourceGroupName $ResourceGroupName -Name $Name -Status
|
|
|
|
$highestStatus = $Vm.Statuses.Count
|
|
|
|
for ($idx = 0; $idx -lt $highestStatus; $idx++) {
|
|
|
|
if ($Vm.Statuses[$idx].Code -eq 'PowerState/stopped') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Write-Host "... not stopped yet, sleeping for 10 seconds"
|
|
|
|
Start-Sleep -Seconds 10
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-11 13:43:19 -07:00
|
|
|
|
|
|
|
$AdminPW = New-Password
|
2024-08-27 12:34:26 -07:00
|
|
|
$Credential = New-Object System.Management.Automation.PSCredential ("AdminUser", $AdminPW)
|
2021-09-11 13:43:19 -07:00
|
|
|
|
2024-03-18 13:26:24 -07:00
|
|
|
$VirtualNetwork = Get-AzVirtualNetwork -ResourceGroupName 'vcpkg-image-minting' -Name 'vcpkg-image-mintingNetwork'
|
2021-09-11 13:43:19 -07:00
|
|
|
|
|
|
|
####################################################################################################
|
|
|
|
Write-Progress `
|
|
|
|
-Activity $ProgressActivity `
|
|
|
|
-Status 'Creating prototype VM' `
|
|
|
|
-PercentComplete (100 / $TotalProgress * $CurrentProgress++)
|
|
|
|
|
2024-03-18 13:26:24 -07:00
|
|
|
$NicName = $Prefix + 'NIC'
|
2021-09-11 13:43:19 -07:00
|
|
|
$Nic = New-AzNetworkInterface `
|
|
|
|
-Name $NicName `
|
2024-03-18 13:26:24 -07:00
|
|
|
-ResourceGroupName 'vcpkg-image-minting' `
|
2021-09-11 13:43:19 -07:00
|
|
|
-Location $Location `
|
2024-03-18 13:26:24 -07:00
|
|
|
-Subnet $VirtualNetwork.Subnets[0] `
|
|
|
|
-EnableAcceleratedNetworking
|
2021-09-11 13:43:19 -07:00
|
|
|
|
2024-03-18 13:26:24 -07:00
|
|
|
$VM = New-AzVMConfig -Name $ProtoVMName -VMSize $VMSize -SecurityType TrustedLaunch -IdentityType SystemAssigned
|
2021-09-11 13:43:19 -07:00
|
|
|
$VM = Set-AzVMOperatingSystem `
|
|
|
|
-VM $VM `
|
|
|
|
-Windows `
|
|
|
|
-ComputerName $ProtoVMName `
|
|
|
|
-Credential $Credential `
|
|
|
|
-ProvisionVMAgent
|
|
|
|
|
|
|
|
$VM = Add-AzVMNetworkInterface -VM $VM -Id $Nic.Id
|
2024-02-12 15:07:57 -08:00
|
|
|
$VM = Set-AzVMOSDisk -VM $VM -StorageAccountType 'Premium_LRS' -CreateOption 'FromImage'
|
2021-09-11 13:43:19 -07:00
|
|
|
$VM = Set-AzVMSourceImage `
|
|
|
|
-VM $VM `
|
|
|
|
-PublisherName 'MicrosoftWindowsServer' `
|
|
|
|
-Offer 'WindowsServer' `
|
|
|
|
-Skus $WindowsServerSku `
|
|
|
|
-Version latest
|
|
|
|
|
|
|
|
$VM = Set-AzVMBootDiagnostic -VM $VM -Disable
|
|
|
|
New-AzVm `
|
2024-03-18 13:26:24 -07:00
|
|
|
-ResourceGroupName 'vcpkg-image-minting' `
|
2021-09-11 13:43:19 -07:00
|
|
|
-Location $Location `
|
|
|
|
-VM $VM
|
|
|
|
|
2024-03-18 13:26:24 -07:00
|
|
|
$VMCreated = Get-AzVM -ResourceGroupName 'vcpkg-image-minting' -Name $ProtoVMName
|
|
|
|
$VMCreatedOsDisk = $VMCreated.StorageProfile.OsDisk.Name
|
|
|
|
|
|
|
|
####################################################################################################
|
|
|
|
Write-Progress `
|
|
|
|
-Activity $ProgressActivity `
|
2024-07-03 20:01:35 -07:00
|
|
|
-Status 'Minting token for vcpkg-image-minting storage account' `
|
2024-03-18 13:26:24 -07:00
|
|
|
-PercentComplete (100 / $TotalProgress * $CurrentProgress++)
|
|
|
|
|
|
|
|
$VcpkgImageMintingAccount = Get-AzStorageAccount -ResourceGroupName 'vcpkg-image-minting' -Name 'vcpkgimageminting'
|
|
|
|
|
2024-07-03 20:01:35 -07:00
|
|
|
$AssetStorageContext = New-AzStorageContext -StorageAccountName 'vcpkgimageminting' -UseConnectedAccount
|
2024-05-24 22:22:47 -07:00
|
|
|
$StartTime = Get-Date
|
2024-07-03 20:01:35 -07:00
|
|
|
$ExpiryTime = $StartTime.AddHours(4)
|
|
|
|
$AssetsSas = New-AzStorageContainerSASToken -Name 'assets' -Permission r -StartTime $StartTime -ExpiryTime $ExpiryTime -Context $AssetStorageContext
|
2021-09-11 13:43:19 -07:00
|
|
|
|
|
|
|
####################################################################################################
|
|
|
|
function Invoke-ScriptWithPrefix {
|
|
|
|
param(
|
|
|
|
[string]$ScriptName,
|
2024-07-03 20:01:35 -07:00
|
|
|
[switch]$SkipSas
|
2021-09-11 13:43:19 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
Write-Progress `
|
|
|
|
-Activity $ProgressActivity `
|
|
|
|
-Status "Running provisioning script $ScriptName in VM" `
|
|
|
|
-PercentComplete (100 / $TotalProgress * $CurrentProgress++)
|
|
|
|
|
2024-03-18 13:26:24 -07:00
|
|
|
$UtilityPrefixContent = Get-Content "$Root\utility-prefix.ps1" -Encoding utf8NoBOM -Raw
|
2021-09-11 13:43:19 -07:00
|
|
|
|
2024-03-18 13:26:24 -07:00
|
|
|
$tempScriptFilename = "$env:TEMP\temp-script.txt"
|
2021-09-11 13:43:19 -07:00
|
|
|
try {
|
2024-03-18 13:26:24 -07:00
|
|
|
$script = Get-Content "$Root\$ScriptName" -Encoding utf8NoBOM -Raw
|
2024-07-03 20:01:35 -07:00
|
|
|
$replacement = @"
|
|
|
|
if (Test-Path "`$PSScriptRoot/utility-prefix.ps1") {
|
|
|
|
. "`$PSScriptRoot/utility-prefix.ps1"
|
|
|
|
}
|
|
|
|
"@
|
|
|
|
$script = $script.Replace($replacement, $UtilityPrefixContent);
|
2021-09-11 13:43:19 -07:00
|
|
|
Set-Content -Path $tempScriptFilename -Value $script -Encoding utf8NoBOM
|
|
|
|
|
|
|
|
$parameter = $null
|
2024-07-03 20:01:35 -07:00
|
|
|
if (-not $SkipSas) {
|
|
|
|
$parameter = @{SasToken = "`"$AssetsSas`"";}
|
2021-09-11 13:43:19 -07:00
|
|
|
}
|
|
|
|
|
2024-07-03 20:01:35 -07:00
|
|
|
$InvokeResult = Invoke-AzVMRunCommand `
|
2024-03-18 13:26:24 -07:00
|
|
|
-ResourceGroupName 'vcpkg-image-minting' `
|
2021-09-11 13:43:19 -07:00
|
|
|
-VMName $ProtoVMName `
|
|
|
|
-CommandId 'RunPowerShellScript' `
|
|
|
|
-ScriptPath $tempScriptFilename `
|
|
|
|
-Parameter $parameter
|
|
|
|
|
|
|
|
Write-Host "$ScriptName output: $($InvokeResult.value.Message)"
|
|
|
|
} finally {
|
|
|
|
Remove-Item $tempScriptFilename -Force
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-18 13:26:24 -07:00
|
|
|
####################################################################################################
|
2024-07-03 20:01:35 -07:00
|
|
|
Invoke-ScriptWithPrefix -ScriptName 'deploy-tlssettings.ps1' -SkipSas
|
|
|
|
Write-Host 'Waiting 1 minute for VM to reboot...'
|
|
|
|
Start-Sleep -Seconds 60
|
2021-09-11 13:43:19 -07:00
|
|
|
|
|
|
|
####################################################################################################
|
2024-07-03 20:01:35 -07:00
|
|
|
Invoke-ScriptWithPrefix -ScriptName 'deploy-visual-studio.ps1'
|
2021-09-11 13:43:19 -07:00
|
|
|
|
|
|
|
####################################################################################################
|
2024-07-03 20:01:35 -07:00
|
|
|
Invoke-ScriptWithPrefix -ScriptName 'deploy-mpi.ps1'
|
2021-09-11 13:43:19 -07:00
|
|
|
|
2024-05-24 22:22:47 -07:00
|
|
|
####################################################################################################
|
2024-07-03 20:01:35 -07:00
|
|
|
Invoke-ScriptWithPrefix -ScriptName 'deploy-cuda.ps1'
|
2024-05-24 22:22:47 -07:00
|
|
|
|
|
|
|
####################################################################################################
|
2024-07-03 20:01:35 -07:00
|
|
|
Invoke-ScriptWithPrefix -ScriptName 'deploy-cudnn.ps1'
|
2024-05-24 22:22:47 -07:00
|
|
|
|
2021-09-11 13:43:19 -07:00
|
|
|
####################################################################################################
|
2024-07-03 20:01:35 -07:00
|
|
|
Invoke-ScriptWithPrefix -ScriptName 'deploy-inteloneapi.ps1'
|
2021-09-11 13:43:19 -07:00
|
|
|
|
|
|
|
####################################################################################################
|
2024-07-03 20:01:35 -07:00
|
|
|
Invoke-ScriptWithPrefix -ScriptName 'deploy-pwsh.ps1'
|
2021-09-11 13:43:19 -07:00
|
|
|
|
2024-07-15 23:18:52 -07:00
|
|
|
####################################################################################################
|
|
|
|
Invoke-ScriptWithPrefix -ScriptName 'deploy-azure-cli.ps1'
|
|
|
|
|
2021-09-11 13:43:19 -07:00
|
|
|
####################################################################################################
|
2024-07-03 20:01:35 -07:00
|
|
|
Invoke-ScriptWithPrefix -ScriptName 'deploy-settings.txt' -SkipSas
|
2024-03-18 13:26:24 -07:00
|
|
|
Restart-AzVM -ResourceGroupName 'vcpkg-image-minting' -Name $ProtoVMName
|
2021-09-11 13:43:19 -07:00
|
|
|
|
|
|
|
####################################################################################################
|
2024-07-03 20:01:35 -07:00
|
|
|
Invoke-ScriptWithPrefix -ScriptName 'sysprep.ps1'
|
2021-09-11 13:43:19 -07:00
|
|
|
|
|
|
|
####################################################################################################
|
|
|
|
Write-Progress `
|
|
|
|
-Activity $ProgressActivity `
|
|
|
|
-Status 'Waiting for VM to shut down' `
|
|
|
|
-PercentComplete (100 / $TotalProgress * $CurrentProgress++)
|
|
|
|
|
2024-03-18 13:26:24 -07:00
|
|
|
Wait-Shutdown -ResourceGroupName 'vcpkg-image-minting' -Name $ProtoVMName
|
2021-09-11 13:43:19 -07:00
|
|
|
|
|
|
|
####################################################################################################
|
|
|
|
Write-Progress `
|
|
|
|
-Activity $ProgressActivity `
|
|
|
|
-Status 'Converting VM to Image' `
|
|
|
|
-PercentComplete (100 / $TotalProgress * $CurrentProgress++)
|
|
|
|
|
|
|
|
Stop-AzVM `
|
2024-03-18 13:26:24 -07:00
|
|
|
-ResourceGroupName 'vcpkg-image-minting' `
|
2021-09-11 13:43:19 -07:00
|
|
|
-Name $ProtoVMName `
|
|
|
|
-Force
|
|
|
|
|
|
|
|
Set-AzVM `
|
2024-03-18 13:26:24 -07:00
|
|
|
-ResourceGroupName 'vcpkg-image-minting' `
|
2021-09-11 13:43:19 -07:00
|
|
|
-Name $ProtoVMName `
|
|
|
|
-Generalized
|
|
|
|
|
2024-07-15 23:18:52 -07:00
|
|
|
$westus3Location = @{Name = 'West US 3';}
|
2024-11-19 14:42:29 -08:00
|
|
|
$westusLocation = @{Name = 'West US';}
|
2024-07-15 23:18:52 -07:00
|
|
|
|
2024-03-18 13:26:24 -07:00
|
|
|
New-AzGalleryImageVersion `
|
|
|
|
-ResourceGroupName 'vcpkg-image-minting' `
|
|
|
|
-GalleryName 'vcpkg_gallery_wus3' `
|
|
|
|
-GalleryImageDefinitionName 'PrWinWus3-TrustedLaunch' `
|
|
|
|
-Name $GalleryImageVersion `
|
|
|
|
-Location $Location `
|
2024-09-16 17:40:40 -07:00
|
|
|
-SourceImageVMId $VMCreated.ID `
|
2024-03-18 13:26:24 -07:00
|
|
|
-ReplicaCount 1 `
|
|
|
|
-StorageAccountType 'Premium_LRS' `
|
2024-07-15 23:18:52 -07:00
|
|
|
-PublishingProfileExcludeFromLatest `
|
2024-11-19 14:42:29 -08:00
|
|
|
-TargetRegion @($westus3Location, $westusLocation)
|
2021-09-11 13:43:19 -07:00
|
|
|
|
|
|
|
####################################################################################################
|
|
|
|
Write-Progress `
|
|
|
|
-Activity $ProgressActivity `
|
|
|
|
-Status 'Deleting unused temporary resources' `
|
|
|
|
-PercentComplete (100 / $TotalProgress * $CurrentProgress++)
|
|
|
|
|
2024-03-18 13:26:24 -07:00
|
|
|
Remove-AzVM -Id $VMCreated.ID -Force
|
|
|
|
Remove-AzDisk -ResourceGroupName 'vcpkg-image-minting' -Name $VMCreatedOsDisk -Force
|
|
|
|
Remove-AzNetworkInterface -ResourceGroupName 'vcpkg-image-minting' -Name $NicName -Force
|
2021-09-11 13:43:19 -07:00
|
|
|
|
|
|
|
####################################################################################################
|
|
|
|
Write-Progress -Activity $ProgressActivity -Completed
|
2024-03-18 13:26:24 -07:00
|
|
|
Write-Host "Generated Image: $GalleryImageVersion"
|
2021-09-11 13:43:19 -07:00
|
|
|
Write-Host 'Finished!'
|
2024-08-27 12:34:26 -07:00
|
|
|
|
|
|
|
$AdminPW.Dispose()
|