Switch to build managed image instead of VHD (#8167) (#8208)

This commit is contained in:
Vasilii Polikarpov
2023-08-31 16:21:15 +02:00
committed by GitHub
parent d09a712b71
commit 8077d7b42b
14 changed files with 350 additions and 169 deletions

View File

@@ -1,4 +1,4 @@
## Contributing
# Contributing
[fork]: https://github.com/actions/runner-images/fork
[pr]: https://github.com//actions/runner-images/compare
@@ -15,7 +15,7 @@ Please note that this project is released with a [Contributor Code of Conduct][c
1. [Fork][fork] and clone the repository
1. Create a new branch: `git checkout -b my-branch-name`
1. Make your changes, ensure that they include steps to install, validate post-install and update software report (please see [How to add new tool](CONTRIBUTING.md#how-to-add-new-tool) for details).
1. Test your changes by [creating VHD and deploying a VM](docs/create-image-and-azure-resources.md).
1. Test your changes by [creating image and deploying a VM](docs/create-image-and-azure-resources.md).
1. Push to your fork and [submit a pull request][pr]
Here are a few things you can do that will increase the likelihood of your pull request being accepted:
@@ -29,7 +29,9 @@ Here are a few things you can do that will increase the likelihood of your pull
- Create an issue and get an approval from us to add this tool to the image before creating the pull request.
## How to add new tool
### General rules
- For every new tool add validation scripts and update software report script to make sure that it is included to documentation
- If the tool is available in other platforms (macOS, Windows, Linux), make sure you include it in as many as possible.
- If installing a few versions of the tool, consider putting the list of versions in the corresponding `toolset.json` file. It will help other customers to configure their builds flexibly. See [toolset-windows-2019.json](images/win/toolsets/toolset-2019.json) as example.
@@ -37,6 +39,7 @@ Here are a few things you can do that will increase the likelihood of your pull
- Validation scripts should be simple and shouldn't change image content
### Windows
- Add a script that will install the tool and put the script in the `scripts/Installers` folder.
There are a bunch of helper functions that could simplify your code: `Choco-Install`, `Install-Binary`, `Install-VsixExtension`, `Start-DownloadWithRetry`, `Test-IsWin19`, `Test-IsWin22` (find the full list of helpers in [ImageHelpers.psm1](images/win/scripts/ImageHelpers/ImageHelpers.psm1)).
- Add a script that will validate the tool installation and put the script in the `scripts/Tests` folder.
@@ -45,6 +48,7 @@ Add `Invoke-PesterTests -TestFile <testFileName> [-TestName <describeName>]` at
- Add changes to the software report generator `images/win/scripts/SoftwareReport/SoftwareReport.Generator.ps1`. The software report generator is used to generate an image's README file, e.g. [Windows2019-Readme.md](images/win/Windows2019-Readme.md) and uses [MarkdownPS](https://github.com/Sarafian/MarkdownPS).
### Ubuntu
- Add script that will install and validate the tool and put the script in the `scripts/installers` folder.
Use existing scripts such as [github-cli.sh](images/linux/scripts/installers/github-cli.sh) as a starting point.
- Use [helpers](images/linux/scripts/helpers/install.sh) to simplify installation process.
@@ -52,6 +56,7 @@ Use existing scripts such as [github-cli.sh](images/linux/scripts/installers/git
- Add changes to the software report generator `images/linux/scripts/SoftwareReport/SoftwareReport.Generator.ps1`. The software report generator is used to generate an image's README file, e.g. [Ubuntu2004-Readme.md](images/linux/Ubuntu2004-README.md) and it uses [MarkdownPS](https://github.com/Sarafian/MarkdownPS).
### macOS
macOS source lives in this repository and available for everyone. However, macOS image-generation CI doesn't support external contributions yet so we are not able to accept pull-requests for now.
We are in the process of preparing macOS CI to accept contributions. Until then, we appreciate your patience and ask you continue to make tool requests by filing issues.

View File

@@ -12,8 +12,7 @@ If the VM deployment succeeds, Packer connects it using ssh or WinRM and begins
If any step fails, image generation is aborted and the temporary VM is terminated.
Packer also attempts to cleanup all the temporary resources it created (unless otherwise configured).
After successful completion of all installation steps Packer converts snapshot of the temporary VM to VHD image
and uploads it to the specified Azure Storage Account.
After successful completion of all installation steps Packer creates managed image from the temporary VM's disk and deletes the VM.
## Build agent preparation
@@ -147,31 +146,29 @@ Start-Sleep -Seconds 30
## Generated machine deployment
After the successful image generation, Virtual Machine can be created from the generated VHD using [CreateAzureVMFromPackerTemplate](../helpers/CreateAzureVMFromPackerTemplate.ps1) script.
After the successful image generation, Virtual Machine can be created from the generated image using [CreateAzureVMFromPackerTemplate](../helpers/CreateAzureVMFromPackerTemplate.ps1) script.
```powershell
Import-Module .\helpers\CreateAzureVMFromPackerTemplate.ps1
CreateAzureVMFromPackerTemplate -SubscriptionId {YourSubscriptionId} -ResourceGroupName {ResourceGroupName} -TemplateFile "C:\BuildVmImages\temporaryTemplate.json" -VirtualMachineName "testvm1" -AdminUsername "shady1" -AdminPassword "SomeSecurePassword1" -AzureLocation "eastus"
CreateAzureVMFromPackerTemplate -SubscriptionId {YourSubscriptionId} -ResourceGroupName {ResourceGroupName} -ManagedImageName "Runner-Image-Ubuntu2204" -VirtualMachineName "testvm1" -AdminUsername "shady1" -AdminPassword "SomeSecurePassword1" -AzureLocation "eastus"
```
Where:
- `SubscriptionId` - The Azure subscription Id where resources will be created.
- `ResourceGroupName` - The Azure resource group name where the Azure virtual machine will be created.
- `TemplateFilePath` - The path to the json ARM-template generated by packer during image generation locally.*
- `ManagedImageName` - The name of the managed image to be used for the virtual machine creation.
- `VirtualMachineName` - The name of the virtual machine to be generated.
- `AdminUserName` - The administrator username for the virtual machine to be created.
- `AdminPassword` - The administrator password for the virtual machine to be created.
- `AzureLocation` - The location where the Azure virtual machine will be provisioned. Example: "eastus"
\* *ARM-template can be obtained from the Packer output. For now, it seems like there is an [Az CLI bug](https://github.com/Azure/azure-cli/issues/5899) with specifying the template through a URI, so download the template from URI, that will be printed at the bottom of image-generation log, and use the local path of the template file.*
The function creates an Azure VM from a template and generates network resources in Azure to make the VM accessible.
The function creates an Azure VM and generates network resources in Azure to make the VM accessible.
## Manual image generation
If you want more control over image generation process you may run Packer directly. This section describes variables defined in Packer template. Some of them may be set using environment variabes.
If you want more control over image generation process you may run Packer directly. This section describes variables defined in Packer template. Some of them may be set using environment variables.
### Required variables
@@ -182,11 +179,11 @@ If you want more control over image generation process you may run Packer direct
| `client_secret` | `ARM_CLIENT_SECRET` | The password or secret for your service principal; may be omitted if `client_cert_path` is set.
| `client_cert_path` | `ARM_CLIENT_CERT_PATH` | The location of a PEM file containing a certificate and private key for service principal; may be omitted if `client_secret` is set.
| `location` | `ARM_RESOURCE_LOCATION` | Azure datacenter in which your VM will build.
| `resource_group` | `ARM_RESOURCE_GROUP` | Resource group under which the final artifact will be stored.
| `storage_account` | `ARM_STORAGE_ACCOUNT` | Storage account under which the final artifact will be stored.
| `managed_image_resource_group_name` | `ARM_RESOURCE_GROUP` | Resource group under which the final artifact will be stored.
### Optional variables
- `managed_image_name` - Name of the managed image to create. If not specified, "Runner-Image-{{ImageType}}" will be used.
- `build_resource_group_name` - Specify an existing resource group to run the build in it. By default, a temporary resource group will be created and destroyed as part of the build. If you do not have permission to do so, use build_resource_group_name to specify an existing resource group to run the build in it.
- `object_id` - The object ID for the AAD SP. Will be derived from the oAuth token if empty.
- `tenant_id` - The Active Directory tenant identifier with which your `client_id` and `subscription_id` are associated. If not specified, `tenant_id` will be looked up using `subscription_id`.
@@ -195,13 +192,12 @@ If you want more control over image generation process you may run Packer direct
- `virtual_network_name` - Use a pre-existing virtual network for the VM. This option enables private communication with the VM, no public IP address is used or provisioned (unless you set `private_virtual_network_with_public_ip`).
- `virtual_network_resource_group_name` - If `virtual_network_name` is set, this value may also be set. If `virtual_network_name` is set, and this value is not set the builder attempts to determine the resource group containing the virtual network. If the resource group cannot be found, or it cannot be disambiguated, this value should be set.
- `virtual_network_subnet_name` - If `virtual_network_name` is set, this value may also be set. If `virtual_network_name` is set, and this value is not set the builder attempts to determine the subnet to use with the virtual network. If the subnet cannot be found, or it cannot be disambiguated, this value should be set.
- `capture_name_prefix` - VHD prefix. The final artifacts will be named PREFIX-osDisk.UUID and PREFIX-vmTemplate.UUID.
### Builder variables
The `builders` section contains variables for the `azure-arm` builder used in the project. Most of the builder variables are inherited from the `user variables` section, however, the variables can be overwritten to adjust image-generation performance.
- `vm_size` - Size of the VM used for building. This can be changed when you deploy a VM from your VHD.
- `vm_size` - Size of the VM used for building. This can be changed when you deploy a VM from your image.
- `image_os` - Type of OS that will be deployed as a temporary VM.
- `image_version` - Specify version of an OS to boot from.
@@ -221,14 +217,14 @@ Generated tool versions and details can be found in related projects:
> :warning: These scripts are intended to run on a VM deployed in Azure
The user, created during the image generation, does not exist in the result VHD hence some configuration files related to the user's home directory need to be changed as well as the file permissions for some directories. Scripts for that are located in the `post-generation` folder in the repository:
The user, created during the image generation, does not exist in the result image hence some configuration files related to the user's home directory need to be changed as well as the file permissions for some directories. Scripts for that are located in the `post-generation` folder in the repository:
- Windows: <https://github.com/actions/runner-images/tree/main/images/win/post-generation>
- Linux: <https://github.com/actions/runner-images/tree/main/images/linux/post-generation>
**Note:** The default user for Linux should have `sudo privileges`.
The scripts are copied to the VHD during the image generation process to the following paths:
The scripts are copied to the image during the generation process to the following paths:
- Windows: `C:\post-generation`
- Linux: `/opt/post-generation`

View File

@@ -12,8 +12,8 @@ Function CreateAzureVMFromPackerTemplate {
.PARAMETER ResourceGroupName
The Azure resource group name where the Azure virtual machine will be created.
.PARAMETER TemplatFilePath
The path for the json template generated by packer during image generation locally.
.PARAMETER ManagedImageName
The name of the managed image to be used to create the virtual machine.
.PARAMETER VirtualMachineName
The name of the virtual machine to be generated.
@@ -36,7 +36,7 @@ Function CreateAzureVMFromPackerTemplate {
[Parameter(Mandatory = $True)]
[string] $ResourceGroupName,
[Parameter(Mandatory = $True)]
[string] $TemplateFilePath,
[string] $ManagedImageName,
[Parameter(Mandatory = $True)]
[string] $VirtualMachineName,
[Parameter(Mandatory = $True)]
@@ -70,7 +70,16 @@ Function CreateAzureVMFromPackerTemplate {
az network nic ip-config update -g $ResourceGroupName -n ipconfig1 --nic-name $nicName --public-ip-address $publicIpId --subscription $subscriptionId
Write-Host "`nCreating the VM"
az deployment group create -g $ResourceGroupName -n $VirtualMachineName --subscription $subscriptionId --template-file $templateFilePath --parameters vmSize=$vmSize vmName=$VirtualMachineName adminUserName=$AdminUsername adminPassword=$AdminPassword networkInterfaceId=$networkId
az vm create `
--resource-group $ResourceGroupName `
--name $VirtualMachineName `
--image $ManagedImageName `
--size $vmSize `
--admin-username $AdminUsername `
--admin-password $AdminPassword `
--nics $networkId `
--subscription $subscriptionId `
--location $AzureLocation
Write-Host "`nCreated in ${ResourceGroupName}:`n vnet ${vnetName}`n subnet ${subnetName}`n nic ${nicName}`n publicip ${publicIpName}`n vm ${VirtualMachineName}"
}

View File

@@ -77,6 +77,8 @@ Function GenerateResourcesAndImage {
The name of the resource group to create the Azure resources in.
.PARAMETER ImageType
The type of image to generate. Valid values are: Windows2019, Windows2022, Ubuntu2004, Ubuntu2204, UbuntuMinimal.
.PARAMETER ManagedImageName
The name of the managed image to create. The default is "Runner-Image-{{ImageType}}".
.PARAMETER AzureLocation
The Azure location where the Azure resources will be created. For example: "East US"
.PARAMETER ImageGenerationRepositoryRoot
@@ -96,10 +98,6 @@ Function GenerateResourcesAndImage {
Delete the resource group if it exists without user confirmation.
.PARAMETER ReuseResourceGroup
Reuse the resource group if it exists without user confirmation.
.PARAMETER AllowBlobPublicAccess
Allow public access to the generated image blob.
.PARAMETER EnableHttpsTrafficOnly
Enable https traffic only for the generated image blob.
.PARAMETER OnError
Specify how packer handles an error during image creation.
Options:
@@ -120,6 +118,8 @@ Function GenerateResourcesAndImage {
[string] $ResourceGroupName,
[Parameter(Mandatory = $True)]
[ImageType] $ImageType,
[Parameter(Mandatory = $False)]
[string] $ManagedImageName = "Runner-Image-$($ImageType)",
[Parameter(Mandatory = $True)]
[string] $AzureLocation,
[Parameter(Mandatory = $False)]
@@ -139,10 +139,6 @@ Function GenerateResourcesAndImage {
[Parameter(Mandatory = $False)]
[switch] $ReuseResourceGroup,
[Parameter(Mandatory = $False)]
[bool] $AllowBlobPublicAccess = $False,
[Parameter(Mandatory = $False)]
[bool] $EnableHttpsTrafficOnly = $False,
[Parameter(Mandatory = $False)]
[ValidateSet("abort", "ask", "cleanup", "run-cleanup-provisioner")]
[string] $OnError = "ask",
[Parameter(Mandatory = $False)]
@@ -177,16 +173,20 @@ Function GenerateResourcesAndImage {
if ($PSVersionTable.PSVersion.Major -eq 5) {
Write-Verbose "PowerShell 5 detected. Replacing double quotes with escaped double quotes in allowed inbound IP addresses."
$AllowedInboundIpAddresses = '[\"{0}\"]' -f $AgentIp
} else {
}
else {
$AllowedInboundIpAddresses = '["{0}"]' -f $AgentIp
}
} else {
}
else {
$AllowedInboundIpAddresses = $AgentIp
}
} else {
}
else {
if ($TemplatePath.Contains("pkr.hcl")) {
$AllowedInboundIpAddresses = "[]"
} else {
}
else {
$AllowedInboundIpAddresses = ""
}
}
@@ -221,8 +221,8 @@ Function GenerateResourcesAndImage {
"-var=subscription_id=$($SubscriptionId)" `
"-var=tenant_id=fake" `
"-var=location=$($AzureLocation)" `
"-var=resource_group=$($ResourceGroupName)" `
"-var=storage_account=fake" `
"-var=managed_image_name=$($ManagedImageName)" `
"-var=managed_image_resource_group_name=$($ResourceGroupName)" `
"-var=install_password=$($InstallPassword)" `
"-var=allowed_inbound_ip_addresses=$($AllowedInboundIpAddresses)" `
"-var=azure_tags=$($TagsJson)" `
@@ -237,7 +237,8 @@ Function GenerateResourcesAndImage {
if ([string]::IsNullOrEmpty($AzureClientId)) {
Write-Verbose "No AzureClientId was provided, will use interactive login."
az login --output none
} else {
}
else {
Write-Verbose "AzureClientId was provided, will use service principal login."
az login --service-principal --username $AzureClientId --password $AzureClientSecret --tenant $AzureTenantId --output none
}
@@ -263,7 +264,8 @@ Function GenerateResourcesAndImage {
}
Write-Host "Resource group '$ResourceGroupName' was deleted."
$ResourceGroupExists = $false
} else {
}
else {
# Resource group already exists, ask the user what to do
$title = "Resource group '$ResourceGroupName' already exists"
$message = "Do you want to delete the resource group and all resources in it?"
@@ -303,7 +305,8 @@ Function GenerateResourcesAndImage {
Write-Host "Creating resource group '$ResourceGroupName' in location '$AzureLocation'..."
if ($TagsList) {
az group create --name $ResourceGroupName --location $AzureLocation --tags $TagsList --query id
} else {
}
else {
az group create --name $ResourceGroupName --location $AzureLocation --query id
}
if ($LastExitCode -ne 0) {
@@ -311,39 +314,6 @@ Function GenerateResourcesAndImage {
}
}
# Generate proper name for the storage account that follows the recommended naming conventions for azure resources
$StorageAccountName = $ResourceGroupName
if ($ResourceGroupName.EndsWith("-rg")) {
$StorageAccountName = $ResourceGroupName.Substring(0, $ResourceGroupName.Length - 3)
}
$StorageAccountName = $StorageAccountName.Replace("-", "").Replace("_", "").Replace("(", "").Replace(")", "").ToLower()
$StorageAccountName += "001"
if ($StorageAccountName.Length -gt 24) {
$StorageAccountName = $StorageAccountName.Substring(0, 24)
}
try {
$StorageAccountId = (az storage account show --name $StorageAccountName --resource-group $ResourceGroupName --query id 2>$null)
$StorageAccountExists = "$StorageAccountId" -ne ""
} catch {
$StorageAccountExists = $false
}
# Create storage account
if ($StorageAccountExists) {
Write-Verbose "Storage account '$StorageAccountName' already exists."
} else {
Write-Host "Creating storage account..."
if ($TagsList) {
az storage account create --name $StorageAccountName --resource-group $ResourceGroupName --location $AzureLocation --sku Standard_LRS --allow-blob-public-access $AllowBlobPublicAccess --https-only $EnableHttpsTrafficOnly --min-tls-version TLS1_2 --tags $TagsList --query id
} else {
az storage account create --name $StorageAccountName --resource-group $ResourceGroupName --location $AzureLocation --sku Standard_LRS --allow-blob-public-access $AllowBlobPublicAccess --https-only $EnableHttpsTrafficOnly --min-tls-version TLS1_2 --query id
}
if ($LastExitCode -ne 0) {
throw "Failed to create storage account '$StorageAccountName'."
}
}
# Create service principal
if ([string]::IsNullOrEmpty($AzureClientId)) {
Write-Host "Creating service principal for packer..."
@@ -362,7 +332,8 @@ Function GenerateResourcesAndImage {
Write-Verbose "Waiting for service principal to propagate..."
Start-Sleep $SecondsToWaitForServicePrincipalSetup
Write-Host "Service principal created with id '$ServicePrincipalAppId'. It will be deleted after the build."
} else {
}
else {
$ServicePrincipalAppId = $AzureClientId
$ServicePrincipalPassword = $AzureClientSecret
$TenantId = $AzureTenantId
@@ -376,8 +347,8 @@ Function GenerateResourcesAndImage {
-var "subscription_id=$($SubscriptionId)" `
-var "tenant_id=$($TenantId)" `
-var "location=$($AzureLocation)" `
-var "resource_group=$($ResourceGroupName)" `
-var "storage_account=$($StorageAccountName)" `
-var "managed_image_name=$($ManagedImageName)" `
-var "managed_image_resource_group_name=$($ResourceGroupName)" `
-var "install_password=$($InstallPassword)" `
-var "allowed_inbound_ip_addresses=$($AllowedInboundIpAddresses)" `
-var "azure_tags=$($TagsJson)" `

View File

@@ -57,7 +57,7 @@ jobs:
-RepoBranch $(CUSTOM_REPOSITORY_BRANCH)
- task: PowerShell@2
displayName: 'Set image template variables'
displayName: 'Set variables'
inputs:
targetType: 'inline'
script: |
@@ -71,17 +71,26 @@ jobs:
Write-Host "##vso[task.setvariable variable=TemplateDirectoryPath;]$TemplateDirectoryPath"
Write-Host "##vso[task.setvariable variable=TemplatePath;]$TemplatePath"
$ManagedImageName = "${{ parameters.image_type }}-$(Build.BuildId)"
Write-Host "##vso[task.setvariable variable=ManagedImageName;]$ManagedImageName"
$VhdName = "$ManagedImageName.vhd"
Write-Host "##vso[task.setvariable variable=VhdName;]$VhdName"
$TempResourceGroupName = "packer-temp-$ManagedImageName"
Write-Host "##vso[task.setvariable variable=TempResourceGroupName;]$TempResourceGroupName"
- task: PowerShell@2
displayName: 'Build VM'
inputs:
targetType: filePath
filePath: ./images.CI/linux-and-win/build-image.ps1
arguments: -ResourcesNamePrefix $(Build.BuildId) `
-ClientId $(CLIENT_ID) `
arguments: -ClientId $(CLIENT_ID) `
-ClientSecret $(CLIENT_SECRET) `
-TemplatePath $(TemplatePath) `
-ResourceGroup $(AZURE_RESOURCE_GROUP) `
-StorageAccount $(AZURE_STORAGE_ACCOUNT) `
-ImageName "$(ManagedImageName)" `
-ImageResourceGroupName $(AZURE_RESOURCE_GROUP) `
-TempResourceGroupName "$(TempResourceGroupName)" `
-SubscriptionId $(AZURE_SUBSCRIPTION) `
-TenantId $(AZURE_TENANT) `
-Location $(AZURE_LOCATION) `
@@ -93,6 +102,26 @@ jobs:
PACKER_LOG: 1
PACKER_LOG_PATH: "$(Agent.TempDirectory)/packer-log.txt"
- task: PowerShell@2
displayName: 'Convert managed image to VHD'
inputs:
targetType: filePath
filePath: ./images.CI/linux-and-win/convert-to-vhd.ps1
arguments: -SubscriptionId $(AZURE_SUBSCRIPTION) `
-Location $(AZURE_LOCATION) `
-ResourceGroupName $(AZURE_RESOURCE_GROUP) `
-ManagedImageName "$(ManagedImageName)" `
-GalleryName "github_imagegeneration_convert_to_vhd" `
-GalleryImageSku "${{ parameters.image_type }}" `
-GalleryImageVersion "0.0.$(Build.BuildId)" `
-StorageAccountName $(AZURE_STORAGE_ACCOUNT) `
-StorageAccountContainerName "images" `
-VhdName "$(VhdName)" `
-ClientId $(CLIENT_ID) `
-ClientSecret $(CLIENT_SECRET) `
-TenantId $(AZURE_TENANT) `
-RemoveManagedImage
- task: PowerShell@2
displayName: 'Copy image artifacts to the separate directory'
inputs:
@@ -147,7 +176,9 @@ jobs:
-Organization $(RELEASE_TARGET_ORGANIZATION) `
-DefinitionId $(RELEASE_TARGET_DEFINITION_ID) `
-Project $(RELEASE_TARGET_PROJECT) `
-ImageName ${{ parameters.image_type }} `
-ImageName "${{ parameters.image_type }}" `
-StorageAccountContainerName "images" `
-VhdName "$(VhdName)" `
-AccessToken $(RELEASE_TARGET_TOKEN)
- task: PowerShell@2
@@ -156,8 +187,7 @@ jobs:
inputs:
targetType: filePath
filePath: ./images.CI/linux-and-win/cleanup.ps1
arguments: -ResourcesNamePrefix $(Build.BuildId) `
-Image ${{ parameters.image_type }} `
arguments: -TempResourceGroupName "$(TempResourceGroupName)" `
-StorageAccount $(AZURE_STORAGE_ACCOUNT) `
-SubscriptionId $(AZURE_SUBSCRIPTION) `
-ClientId $(CLIENT_ID) `

View File

@@ -2,10 +2,10 @@ param(
[String] [Parameter (Mandatory=$true)] $TemplatePath,
[String] [Parameter (Mandatory=$true)] $ClientId,
[String] [Parameter (Mandatory=$true)] $ClientSecret,
[String] [Parameter (Mandatory=$true)] $ResourcesNamePrefix,
[String] [Parameter (Mandatory=$true)] $Location,
[String] [Parameter (Mandatory=$true)] $ResourceGroup,
[String] [Parameter (Mandatory=$true)] $StorageAccount,
[String] [Parameter (Mandatory=$true)] $ImageName,
[String] [Parameter (Mandatory=$true)] $ImageResourceGroupName,
[String] [Parameter (Mandatory=$true)] $TempResourceGroupName,
[String] [Parameter (Mandatory=$true)] $SubscriptionId,
[String] [Parameter (Mandatory=$true)] $TenantId,
[String] [Parameter (Mandatory=$false)] $VirtualNetworkName,
@@ -19,8 +19,7 @@ if (-not (Test-Path $TemplatePath))
exit 1
}
$Image = [io.path]::GetFileName($TemplatePath).Split(".")[0]
$TempResourceGroupName = "${ResourcesNamePrefix}_${Image}"
$ImageTemplateName = [io.path]::GetFileName($TemplatePath).Split(".")[0]
$InstallPassword = [System.GUID]::NewGuid().ToString().ToUpper()
packer validate -syntax-only $TemplatePath
@@ -38,14 +37,13 @@ $SensitiveData = @(
Write-Host "Show Packer Version"
packer --version
Write-Host "Build $Image VM"
packer build -var "capture_name_prefix=$ResourcesNamePrefix" `
-var "client_id=$ClientId" `
Write-Host "Build $ImageTemplateName VM"
packer build -var "client_id=$ClientId" `
-var "client_secret=$ClientSecret" `
-var "install_password=$InstallPassword" `
-var "location=$Location" `
-var "resource_group=$ResourceGroup" `
-var "storage_account=$StorageAccount" `
-var "managed_image_name=$ImageName" `
-var "managed_image_resource_group_name=$ImageResourceGroupName" `
-var "subscription_id=$SubscriptionId" `
-var "temp_resource_group_name=$TempResourceGroupName" `
-var "tenant_id=$TenantId" `

View File

@@ -1,17 +1,14 @@
param(
[String] [Parameter (Mandatory=$true)] $Image,
[String] [Parameter (Mandatory=$true)] $ResourcesNamePrefix,
[String] [Parameter (Mandatory=$true)] $TempResourceGroupName,
[String] [Parameter (Mandatory=$true)] $StorageAccount,
[String] [Parameter (Mandatory=$true)] $SubscriptionId,
[String] [Parameter (Mandatory=$true)] $ClientId,
[String] [Parameter (Mandatory=$true)] $ClientSecret,
[String] [Parameter (Mandatory=$true)] $SubscriptionId,
[String] [Parameter (Mandatory=$true)] $TenantId
)
az login --service-principal --username $ClientId --password $ClientSecret --tenant $TenantId | Out-Null
$TempResourceGroupName = "${ResourcesNamePrefix}_${Image}"
$groupExist = az group exists --name $TempResourceGroupName --subscription $SubscriptionId
if ($groupExist -eq "true") {
$osDiskName = az deployment group list --resource-group $TempResourceGroupName --query "[].properties.parameters.osDiskName.value" -o tsv

View File

@@ -0,0 +1,185 @@
param (
[String] [Parameter (Mandatory = $true)] $SubscriptionId,
[String] [Parameter (Mandatory = $true)] $Location,
[String] [Parameter (Mandatory = $true)] $ResourceGroupName,
[String] [Parameter (Mandatory = $true)] $ManagedImageName,
[String] [Parameter (Mandatory = $true)] $GalleryName,
[String] [Parameter (Mandatory = $true)] $GalleryImageSku,
[String] [Parameter (Mandatory = $true)] $GalleryImageVersion,
[String] [Parameter (Mandatory = $true)] $StorageAccountName,
[String] [Parameter (Mandatory = $true)] $StorageAccountContainerName,
[String] [Parameter (Mandatory = $true)] $VhdName,
[Switch] [Parameter (Mandatory = $false)] $RemoveManagedImage,
[String] [Parameter (Mandatory = $true)] $ClientId,
[String] [Parameter (Mandatory = $true)] $ClientSecret,
[String] [Parameter (Mandatory = $true)] $TenantId
)
$ErrorActionPreference = "Stop"
# Login to Azure
az login --service-principal --username $ClientId --password $ClientSecret --tenant $TenantId | Out-Null
az account set --subscription $SubscriptionId | Out-Null
# Create Compute Image Gallery if it doesn't exist
Write-Host "Creating Compute Image Gallery '$GalleryName'..."
$galleryExists = az sig list --resource-group $ResourceGroupName --query "[?name=='$GalleryName']" -o tsv
if ($null -eq $galleryExists) {
az sig create --resource-group $ResourceGroupName --gallery-name $GalleryName --location $Location
}
# Create Image Definition if it doesn't exist
Write-Host "Creating Image Definition '$ManagedImageName'..."
$imageDefinitionName = "RunnerImage-$GalleryImageSku"
if ($ManagedImageName -like "*windows*") {
$imageOsType = "Windows"
} elseif ($ManagedImageName -like "*ubuntu*") {
$imageOsType = "Linux"
} else {
throw "Unknown OS type for image '$ManagedImageName'"
}
$galleryImageExists = az sig image-definition list --resource-group $ResourceGroupName --gallery-name $GalleryName --query "[?name=='$imageDefinitionName']" -o tsv
if ($null -eq $galleryImageExists) {
az sig image-definition create `
--resource-group $ResourceGroupName `
--gallery-name $GalleryName `
--gallery-image-definition $imageDefinitionName `
--publisher "GuthubImagesGeneration" `
--offer "RunnerImage" `
--sku $GalleryImageSku `
--os-type $imageOsType `
--location $Location
}
# Create Image Version from existing Managed Image
Write-Host "Creating Image Version '$GalleryImageVersion' from Managed Image '$ManagedImageName'..."
az sig image-version create `
--resource-group $ResourceGroupName `
--gallery-name $GalleryName `
--gallery-image-definition $imageDefinitionName `
--gallery-image-version $GalleryImageVersion `
--managed-image "/subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Compute/images/$ManagedImageName" `
--target-regions $Location `
--replica-count 1 `
--location $Location
# Create Azure Managed Disk from Shared Image Gallery
Write-Host "Creating Azure Managed Disk '$ManagedImageName' from Shared Image Gallery..."
az disk create `
--resource-group $ResourceGroupName `
--name $ManagedImageName `
--location $Location `
--gallery-image-reference "/subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Compute/galleries/$GalleryName/images/$imageDefinitionName/versions/$GalleryImageVersion"
# Generate SAS URL for the Managed Disk
Write-Host "Generating SAS URL for the Managed Disk '$ManagedImageName'..."
$sourceDiskUri = az disk grant-access `
--resource-group $ResourceGroupName `
--name $ManagedImageName `
--duration-in-seconds 86400 `
--access-level Read `
--query [accessSas] -o tsv
Write-Host "Querying key for the storage account '$StorageAccountName'..."
$targetKey = az storage account keys list `
--resource-group $ResourceGroupName `
--account-name $StorageAccountName `
--query "[0].value" -o tsv
Write-Host ("Copying VHD blob from '{0}' to 'https://{1}.blob.core.windows.net/{2}/{3}'..." `
-f $sourceDiskUri.Split('?')[0], $StorageAccountName, $StorageAccountContainerName, $VhdName)
az storage blob copy start `
--source-uri """$sourceDiskUri""" `
--destination-blob $VhdName `
--destination-container $StorageAccountContainerName `
--account-name $StorageAccountName `
--account-key $targetKey `
--only-show-errors
Write-Host "Waiting for the copy to complete..."
while ($true) {
$status = az storage blob show `
--container-name $StorageAccountContainerName `
--name $VhdName `
--account-name $StorageAccountName `
--account-key $targetKey `
--query "properties.copy.status" -o tsv
if ($status -eq "success") {
Write-Host "Copy completed successfully."
break
} elseif ($status -ne "pending") {
Write-Host "Copy failed with status '$status', see blob information below:"
az storage blob show `
--container-name $StorageAccountContainerName `
--name $VhdName `
--account-name $StorageAccountName `
--account-key $targetKey
throw "Copy failed with status '$status'"
}
$progress = az storage blob show `
--container-name $StorageAccountContainerName `
--name $VhdName `
--account-name $StorageAccountName `
--account-key $targetKey `
--query "properties.copy.progress" -o tsv
Write-Host "Progress: $(($progress.Split("/")[0] / $progress.Split("/")[1]).ToString("P"))"
Start-Sleep -Seconds 15
}
Write-Host "Successfully converted '$ManagedImageName' to '$VhdName' in '$StorageAccountName' storage account."
# Remove Managed Image if requested
if ($RemoveManagedImage) {
Write-Host "Removing Managed Image '$ManagedImageName'..."
az image delete `
--resource-group $ResourceGroupName `
--name $ManagedImageName `
--only-show-errors
if ($LastExitCode) {
Write-Host "Warning: Failed to delete the Managed Image '$ManagedImageName'."
}
}
Write-Host "Cleaning up..."
# Revoke SAS URL for the Managed Disk
az disk revoke-access `
--resource-group $ResourceGroupName `
--name $ManagedImageName `
--only-show-errors
if ($LastExitCode) {
Write-Host "Warning: Failed to revoke access to the Managed Disk '$ManagedImageName'."
}
# Delete Azure Managed Disk from Shared Image Gallery
az disk delete `
--resource-group $ResourceGroupName `
--name $ManagedImageName `
--only-show-errors `
--yes
if ($LastExitCode) {
Write-Host "Warning: Failed to delete the Managed Disk '$ManagedImageName'."
}
# Delete Image Version from Shared Image Gallery
az sig image-version delete `
--resource-group $ResourceGroupName `
--gallery-name $GalleryName `
--gallery-image-definition $imageDefinitionName `
--gallery-image-version $GalleryImageVersion `
--only-show-errors
if ($LastExitCode) {
Write-Host "Warning: Failed to delete the Image Version '$GalleryImageVersion'."
}
Write-Host "Done."

View File

@@ -3,6 +3,8 @@ param(
[String] [Parameter (Mandatory)] $Organization,
[String] [Parameter (Mandatory)] $Project,
[String] [Parameter (Mandatory)] $ImageName,
[String] [Parameter (Mandatory)] $StorageAccountContainerName,
[String] [Parameter (Mandatory)] $VhdName,
[String] [Parameter (Mandatory)] $DefinitionId,
[String] [Parameter (Mandatory)] $AccessToken
)
@@ -16,6 +18,12 @@ $Body = @{
ImageName = @{
value = $ImageName
}
ImageStorageContainerName = @{
value = $StorageAccountContainerName
}
ImageBlobPath = @{
value = $VhdName
}
}
isDraft = "false"
} | ConvertTo-Json -Depth 3

View File

@@ -5,8 +5,7 @@
"client_cert_path": "{{env `ARM_CLIENT_CERT_PATH`}}",
"subscription_id": "{{env `ARM_SUBSCRIPTION_ID`}}",
"tenant_id": "{{env `ARM_TENANT_ID`}}",
"resource_group": "{{env `ARM_RESOURCE_GROUP`}}",
"storage_account": "{{env `ARM_STORAGE_ACCOUNT`}}",
"managed_image_resource_group_name": "{{env `ARM_RESOURCE_GROUP`}}",
"build_resource_group_name": "{{env `BUILD_RESOURCE_GROUP_NAME`}}",
"temp_resource_group_name": "{{env `TEMP_RESOURCE_GROUP_NAME`}}",
"location": "{{env `ARM_RESOURCE_LOCATION`}}",
@@ -20,7 +19,7 @@
"installer_script_folder": "/imagegeneration/installers",
"helper_script_folder": "/imagegeneration/helpers",
"vm_size": "Standard_D4s_v4",
"capture_name_prefix": "packer",
"managed_image_name": "packer-ubuntu20-dev",
"image_version": "dev",
"image_os": "ubuntu20",
"run_validation_diskspace": "false",
@@ -40,12 +39,10 @@
"tenant_id": "{{user `tenant_id`}}",
"location": "{{user `location`}}",
"vm_size": "{{user `vm_size`}}",
"resource_group_name": "{{user `resource_group`}}",
"storage_account": "{{user `storage_account`}}",
"managed_image_name": "{{user `managed_image_name`}}",
"managed_image_resource_group_name": "{{user `managed_image_resource_group_name`}}",
"build_resource_group_name": "{{user `build_resource_group_name`}}",
"temp_resource_group_name": "{{user `temp_resource_group_name`}}",
"capture_container_name": "images",
"capture_name_prefix": "{{user `capture_name_prefix`}}",
"virtual_network_name": "{{user `virtual_network_name`}}",
"virtual_network_resource_group_name": "{{user `virtual_network_resource_group_name`}}",
"virtual_network_subnet_name": "{{user `virtual_network_subnet_name`}}",

View File

@@ -1,3 +1,6 @@
locals {
managed_image_name = var.managed_image_name != "" ? var.managed_image_name : "packer-${var.image_os}-${var.image_version}"
}
variable "allowed_inbound_ip_addresses" {
type = list(string)
@@ -14,9 +17,9 @@ variable "build_resource_group_name" {
default = "${env("BUILD_RESOURCE_GROUP_NAME")}"
}
variable "capture_name_prefix" {
variable "managed_image_name" {
type = string
default = "packer"
default = ""
}
variable "client_id" {
@@ -95,7 +98,7 @@ variable "private_virtual_network_with_public_ip" {
default = false
}
variable "resource_group" {
variable "managed_image_resource_group_name" {
type = string
default = "${env("ARM_RESOURCE_GROUP")}"
}
@@ -105,11 +108,6 @@ variable "run_validation_diskspace" {
default = false
}
variable "storage_account" {
type = string
default = "${env("ARM_STORAGE_ACCOUNT")}"
}
variable "subscription_id" {
type = string
default = "${env("ARM_SUBSCRIPTION_ID")}"
@@ -145,11 +143,9 @@ variable "vm_size" {
default = "Standard_D4s_v4"
}
source "azure-arm" "build_vhd" {
source "azure-arm" "build_image" {
allowed_inbound_ip_addresses = "${var.allowed_inbound_ip_addresses}"
build_resource_group_name = "${var.build_resource_group_name}"
capture_container_name = "images"
capture_name_prefix = "${var.capture_name_prefix}"
client_id = "${var.client_id}"
client_secret = "${var.client_secret}"
client_cert_path = "${var.client_cert_path}"
@@ -160,8 +156,8 @@ source "azure-arm" "build_vhd" {
os_disk_size_gb = "86"
os_type = "Linux"
private_virtual_network_with_public_ip = "${var.private_virtual_network_with_public_ip}"
resource_group_name = "${var.resource_group}"
storage_account = "${var.storage_account}"
managed_image_name = "${local.managed_image_name}"
managed_image_resource_group_name = "${var.managed_image_resource_group_name}"
subscription_id = "${var.subscription_id}"
temp_resource_group_name = "${var.temp_resource_group_name}"
tenant_id = "${var.tenant_id}"
@@ -180,7 +176,7 @@ source "azure-arm" "build_vhd" {
}
build {
sources = ["source.azure-arm.build_vhd"]
sources = ["source.azure-arm.build_image"]
provisioner "shell" {
execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'"

View File

@@ -7,6 +7,8 @@ locals {
helper_script_folder = "/imagegeneration/helpers"
installer_script_folder = "/imagegeneration/installers"
imagedata_file = "/imagegeneration/imagedata.json"
managed_image_name = var.managed_image_name != "" ? var.managed_image_name : "packer-${var.image_os}-${var.image_version}"
}
variable "allowed_inbound_ip_addresses" {
@@ -24,9 +26,9 @@ variable "build_resource_group_name" {
default = "${env("BUILD_RESOURCE_GROUP_NAME")}"
}
variable "capture_name_prefix" {
variable "managed_image_name" {
type = string
default = "packer"
default = ""
}
variable "client_id" {
@@ -70,7 +72,7 @@ variable "private_virtual_network_with_public_ip" {
default = false
}
variable "resource_group" {
variable "managed_image_resource_group_name" {
type = string
default = "${env("ARM_RESOURCE_GROUP")}"
}
@@ -80,11 +82,6 @@ variable "run_validation_diskspace" {
default = false
}
variable "storage_account" {
type = string
default = "${env("ARM_STORAGE_ACCOUNT")}"
}
variable "subscription_id" {
type = string
default = "${env("ARM_SUBSCRIPTION_ID")}"
@@ -120,7 +117,7 @@ variable "vm_size" {
default = "Standard_D4s_v4"
}
source "azure-arm" "build_vhd" {
source "azure-arm" "build_image" {
location = "${var.location}"
// Auth
@@ -136,10 +133,8 @@ source "azure-arm" "build_vhd" {
image_sku = "22_04-lts"
// Target location
storage_account = "${var.storage_account}"
resource_group_name = "${var.resource_group}"
capture_container_name = "images"
capture_name_prefix = "${var.capture_name_prefix}"
managed_image_name = "${local.managed_image_name}"
managed_image_resource_group_name = "${var.managed_image_resource_group_name}"
// Resource group for VM
build_resource_group_name = "${var.build_resource_group_name}"
@@ -167,7 +162,7 @@ source "azure-arm" "build_vhd" {
}
build {
sources = ["source.azure-arm.build_vhd"]
sources = ["source.azure-arm.build_image"]
// Create folder to store temporary data
provisioner "shell" {

View File

@@ -6,8 +6,7 @@
"subscription_id": "{{env `ARM_SUBSCRIPTION_ID`}}",
"tenant_id": "{{env `ARM_TENANT_ID`}}",
"object_id": "{{env `ARM_OBJECT_ID`}}",
"resource_group": "{{env `ARM_RESOURCE_GROUP`}}",
"storage_account": "{{env `ARM_STORAGE_ACCOUNT`}}",
"managed_image_resource_group_name": "{{env `ARM_RESOURCE_GROUP`}}",
"build_resource_group_name": "{{env `BUILD_RESOURCE_GROUP_NAME`}}",
"temp_resource_group_name": "{{env `TEMP_RESOURCE_GROUP_NAME`}}",
"location": "{{env `ARM_RESOURCE_LOCATION`}}",
@@ -23,7 +22,7 @@
"agent_tools_directory": "C:\\hostedtoolcache\\windows",
"install_user": "installer",
"install_password": null,
"capture_name_prefix": "packer",
"managed_image_name": "packer-win19-dev",
"image_version": "dev",
"image_os": "win19"
},
@@ -33,7 +32,7 @@
],
"builders": [
{
"name": "vhd",
"name": "image",
"type": "azure-arm",
"client_id": "{{user `client_id`}}",
"client_secret": "{{user `client_secret`}}",
@@ -44,12 +43,10 @@
"os_disk_size_gb": "256",
"location": "{{user `location`}}",
"vm_size": "{{user `vm_size`}}",
"resource_group_name": "{{user `resource_group`}}",
"storage_account": "{{user `storage_account`}}",
"managed_image_name": "{{user `managed_image_name`}}",
"managed_image_resource_group_name": "{{user `managed_image_resource_group_name`}}",
"build_resource_group_name": "{{user `build_resource_group_name`}}",
"temp_resource_group_name": "{{user `temp_resource_group_name`}}",
"capture_container_name": "images",
"capture_name_prefix": "{{user `capture_name_prefix`}}",
"virtual_network_name": "{{user `virtual_network_name`}}",
"virtual_network_resource_group_name": "{{user `virtual_network_resource_group_name`}}",
"virtual_network_subnet_name": "{{user `virtual_network_subnet_name`}}",

View File

@@ -6,8 +6,7 @@
"subscription_id": "{{env `ARM_SUBSCRIPTION_ID`}}",
"tenant_id": "{{env `ARM_TENANT_ID`}}",
"object_id": "{{env `ARM_OBJECT_ID`}}",
"resource_group": "{{env `ARM_RESOURCE_GROUP`}}",
"storage_account": "{{env `ARM_STORAGE_ACCOUNT`}}",
"managed_image_resource_group_name": "{{env `ARM_RESOURCE_GROUP`}}",
"build_resource_group_name": "{{env `BUILD_RESOURCE_GROUP_NAME`}}",
"temp_resource_group_name": "{{env `TEMP_RESOURCE_GROUP_NAME`}}",
"location": "{{env `ARM_RESOURCE_LOCATION`}}",
@@ -23,7 +22,7 @@
"agent_tools_directory": "C:\\hostedtoolcache\\windows",
"install_user": "installer",
"install_password": null,
"capture_name_prefix": "packer",
"managed_image_name": "packer-win22-dev",
"image_version": "dev",
"image_os": "win22"
},
@@ -33,7 +32,7 @@
],
"builders": [
{
"name": "vhd",
"name": "image",
"type": "azure-arm",
"client_id": "{{user `client_id`}}",
"client_secret": "{{user `client_secret`}}",
@@ -44,12 +43,10 @@
"os_disk_size_gb": "256",
"location": "{{user `location`}}",
"vm_size": "{{user `vm_size`}}",
"resource_group_name": "{{user `resource_group`}}",
"storage_account": "{{user `storage_account`}}",
"managed_image_name": "{{user `managed_image_name`}}",
"managed_image_resource_group_name": "{{user `managed_image_resource_group_name`}}",
"build_resource_group_name": "{{user `build_resource_group_name`}}",
"temp_resource_group_name": "{{user `temp_resource_group_name`}}",
"capture_container_name": "images",
"capture_name_prefix": "{{user `capture_name_prefix`}}",
"virtual_network_name": "{{user `virtual_network_name`}}",
"virtual_network_resource_group_name": "{{user `virtual_network_resource_group_name`}}",
"virtual_network_subnet_name": "{{user `virtual_network_subnet_name`}}",