From c0f21da62d7432cd80573e1eac7b40df77746018 Mon Sep 17 00:00:00 2001 From: Alexey-Ayupov <116575425+Alexey-Ayupov@users.noreply.github.com> Date: Wed, 2 Jul 2025 13:31:15 +0200 Subject: [PATCH] Refactor Packer templates for Windows and Ubuntu images (#12305) Co-authored-by: Shamil Mubarakshin <127750046+shamil-mubarakshin@users.noreply.github.com> --- docs/create-image-and-azure-resources.md | 12 +- helpers/GenerateResourcesAndImage.ps1 | 43 ++- images.CI/linux-and-win/build-image.ps1 | 16 +- ....04.pkr.hcl => build.ubuntu-22_04.pkr.hcl} | 232 +------------- ....04.pkr.hcl => build.ubuntu-24_04.pkr.hcl} | 232 +------------- images/ubuntu/templates/locals.ubuntu.pkr.hcl | 18 ++ images/ubuntu/templates/source.ubuntu.pkr.hcl | 48 +++ .../ubuntu/templates/ubuntu-minimal.pkr.hcl | 282 ------------------ .../ubuntu/templates/variable.ubuntu.pkr.hcl | 171 +++++++++++ ...019.pkr.hcl => build.windows-2019.pkr.hcl} | 259 +--------------- ...022.pkr.hcl => build.windows-2022.pkr.hcl} | 259 +--------------- ...025.pkr.hcl => build.windows-2025.pkr.hcl} | 259 +--------------- .../windows/templates/locals.windows.pkr.hcl | 24 ++ .../windows/templates/source.windows.pkr.hcl | 54 ++++ .../templates/variable.windows.pkr.hcl | 179 +++++++++++ 15 files changed, 547 insertions(+), 1541 deletions(-) rename images/ubuntu/templates/{ubuntu-22.04.pkr.hcl => build.ubuntu-22_04.pkr.hcl} (66%) rename images/ubuntu/templates/{ubuntu-24.04.pkr.hcl => build.ubuntu-24_04.pkr.hcl} (64%) create mode 100644 images/ubuntu/templates/locals.ubuntu.pkr.hcl create mode 100644 images/ubuntu/templates/source.ubuntu.pkr.hcl delete mode 100644 images/ubuntu/templates/ubuntu-minimal.pkr.hcl create mode 100644 images/ubuntu/templates/variable.ubuntu.pkr.hcl rename images/windows/templates/{windows-2019.pkr.hcl => build.windows-2019.pkr.hcl} (66%) rename images/windows/templates/{windows-2022.pkr.hcl => build.windows-2022.pkr.hcl} (65%) rename images/windows/templates/{windows-2025.pkr.hcl => build.windows-2025.pkr.hcl} (65%) create mode 100644 images/windows/templates/locals.windows.pkr.hcl create mode 100644 images/windows/templates/source.windows.pkr.hcl create mode 100644 images/windows/templates/variable.windows.pkr.hcl diff --git a/docs/create-image-and-azure-resources.md b/docs/create-image-and-azure-resources.md index b4ef50083..36e74926d 100644 --- a/docs/create-image-and-azure-resources.md +++ b/docs/create-image-and-azure-resources.md @@ -76,7 +76,6 @@ In any case, you will need these software installed: This repository includes a script that assists in generating images in Azure. All you need is an Azure subscription, a resource group in that subscription and a build agent configured as described above. -We suggest starting with building the UbuntuMinimal image because it includes only basic software and builds in less than 30 minutes. All the commands below should be executed in PowerShell. @@ -99,7 +98,7 @@ Finally, run the `GenerateResourcesAndImage` function, setting the mandatory arg - `ResourceGroupName` - the name of the resource group that will store the resulting artifact (e.g., "imagegen-test"). The resource group must already exist in your Azure subscription; - `AzureLocation` - the location where resources will be created (e.g., "East US"); -- `ImageType` - the type of image to build (we suggest choosing "UbuntuMinimal" here; other valid options are "Windows2019", "Windows2022", "Windows2025", "Ubuntu2204", "Ubuntu2404"). +- `ImageType` - the type of image to build (valid options are "Windows2019", "Windows2022", "Windows2025", "Ubuntu2204", "Ubuntu2404"). This function automatically creates all required Azure resources and initiates the Packer image generation for the selected image type. @@ -200,11 +199,14 @@ Then, you can invoke Packer in your CI/CD pipeline using the following commands: ```powershell packer plugins install github.com/hashicorp/azure 2.2.1 -packer build -var "subscription_id=$SubscriptionId" ` + +packer build -only "$BuildName*" ` + -var "subscription_id=$SubscriptionId" ` -var "client_id=$ClientId" ` -var "client_secret=$ClientSecret" ` -var "install_password=$InstallPassword" ` -var "location=$Location" ` + -var "image_os=$ImageOS" ` -var "managed_image_name=$ImageName" ` -var "managed_image_resource_group_name=$ImageResourceGroupName" ` -var "tenant_id=$TenantId" ` @@ -213,13 +215,15 @@ packer build -var "subscription_id=$SubscriptionId" ` Where: +- `BuildName` - name of the build defined in Packer template's `build{}` block (e.g. "ubuntu-24_04", "windows-2025"); - `SubscriptionId` - your Azure Subscription ID; - `ClientId` and `ClientSecret` - Service Principal credentials; - `TenantId` - Azure Tenant ID; - `InstallPassword` - password for the user used to install software (Windows only); - `Location` - location where resources will be created (e.g., "East US"); +- `ImageOS` - the type of OS that will be deployed as a temporary VM (e.g. "ubuntu24", "win25"); - `ImageName` and `ImageResourceGroupName` - name of the resource group where the managed image will be stored; -- `TemplatePath` - path to the Packer template file (e.g., "images/windows/templates/windows-2022.pkr.hcl"). +- `TemplatePath` - path to the folder with Packer template files (e.g., "images/windows/templates"). ### Required variables diff --git a/helpers/GenerateResourcesAndImage.ps1 b/helpers/GenerateResourcesAndImage.ps1 index c2dea768d..592313e7d 100644 --- a/helpers/GenerateResourcesAndImage.ps1 +++ b/helpers/GenerateResourcesAndImage.ps1 @@ -6,10 +6,9 @@ enum ImageType { Windows2025 = 3 Ubuntu2204 = 4 Ubuntu2404 = 5 - UbuntuMinimal = 6 } -Function Get-PackerTemplatePath { +Function Get-PackerTemplate { param ( [Parameter(Mandatory = $True)] [string] $RepositoryRoot, @@ -20,33 +19,41 @@ Function Get-PackerTemplatePath { switch ($ImageType) { # Note: Double Join-Path is required to support PowerShell 5.1 ([ImageType]::Windows2019) { - $relativeTemplatePath = Join-Path (Join-Path "windows" "templates") "windows-2019.pkr.hcl" + $relativeTemplatePath = Join-Path (Join-Path "windows" "templates") "build.windows-2019.pkr.hcl" + $imageOS = "win19" } ([ImageType]::Windows2022) { - $relativeTemplatePath = Join-Path (Join-Path "windows" "templates") "windows-2022.pkr.hcl" + $relativeTemplatePath = Join-Path (Join-Path "windows" "templates") "build.windows-2022.pkr.hcl" + $imageOS = "win22" } ([ImageType]::Windows2025) { - $relativeTemplatePath = Join-Path (Join-Path "windows" "templates") "windows-2025.pkr.hcl" + $relativeTemplatePath = Join-Path (Join-Path "windows" "templates") "build.windows-2025.pkr.hcl" + $imageOS = "win25" } ([ImageType]::Ubuntu2204) { - $relativeTemplatePath = Join-Path (Join-Path "ubuntu" "templates") "ubuntu-22.04.pkr.hcl" + $relativeTemplatePath = Join-Path (Join-Path "ubuntu" "templates") "build.ubuntu-22_04.pkr.hcl" + $imageOS = "ubuntu22" } ([ImageType]::Ubuntu2404) { - $relativeTemplatePath = Join-Path (Join-Path "ubuntu" "templates") "ubuntu-24.04.pkr.hcl" - } - ([ImageType]::UbuntuMinimal) { - $relativeTemplatePath = Join-Path (Join-Path "ubuntu" "templates") "ubuntu-minimal.pkr.hcl" + $relativeTemplatePath = Join-Path (Join-Path "ubuntu" "templates") "build.ubuntu-24_04.pkr.hcl" + $imageOS = "ubuntu24" } default { throw "Unknown type of image" } } $imageTemplatePath = [IO.Path]::Combine($RepositoryRoot, "images", $relativeTemplatePath) + # Specific template selection using Packer's "-only" functionality + $buildName = [IO.Path]::GetFileName($imageTemplatePath).Split(".")[1] if (-not (Test-Path $imageTemplatePath)) { throw "Template for image '$ImageType' doesn't exist on path '$imageTemplatePath'." } - return $imageTemplatePath; + return [PSCustomObject] @{ + "BuildName" = $buildName + "ImageOS" = $imageOS + "Path" = [IO.Path]::GetDirectoryName($imageTemplatePath) + } } Function Show-LatestCommit { @@ -81,7 +88,7 @@ Function GenerateResourcesAndImage { .PARAMETER ResourceGroupName The name of the resource group to store the resulting artifact. Resource group must already exist. .PARAMETER ImageType - The type of image to generate. Valid values are: Windows2019, Windows2022, Windows2025, Ubuntu2204, Ubuntu2404, UbuntuMinimal. + The type of image to generate. Valid values are: Windows2019, Windows2022, Windows2025, Ubuntu2204, Ubuntu2404. .PARAMETER ManagedImageName The name of the managed image to create. The default is "Runner-Image-{{ImageType}}". .PARAMETER AzureLocation @@ -155,8 +162,8 @@ Function GenerateResourcesAndImage { } # Get template path - $TemplatePath = Get-PackerTemplatePath -RepositoryRoot $ImageGenerationRepositoryRoot -ImageType $ImageType - Write-Debug "Template path: $TemplatePath." + $PackerTemplate = Get-PackerTemplate -RepositoryRoot $ImageGenerationRepositoryRoot -ImageType $ImageType + Write-Debug "Template path: $($PackerTemplate.Path)." # Prepare list of allowed inbound IP addresses if ($RestrictToAgentIpAddress) { @@ -208,17 +215,19 @@ Function GenerateResourcesAndImage { Write-Host "Validating packer template..." & $PackerBinary validate ` + "-only=$($PackerTemplate.BuildName)*" ` "-var=client_id=fake" ` "-var=client_secret=fake" ` "-var=subscription_id=$($SubscriptionId)" ` "-var=tenant_id=fake" ` "-var=location=$($AzureLocation)" ` + "-var=image_os=$($PackerTemplate.ImageOS)" ` "-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)" ` - $TemplatePath + $PackerTemplate.Path if ($LastExitCode -ne 0) { throw "Packer template validation failed." @@ -276,17 +285,19 @@ Function GenerateResourcesAndImage { Write-Debug "Tenant id: $TenantId." & $PackerBinary build -on-error="$($OnError)" ` + -only "$($PackerTemplate.BuildName)*" ` -var "client_id=$($ServicePrincipalAppId)" ` -var "client_secret=$($ServicePrincipalPassword)" ` -var "subscription_id=$($SubscriptionId)" ` -var "tenant_id=$($TenantId)" ` -var "location=$($AzureLocation)" ` + -var "image_os=$($PackerTemplate.ImageOS)" ` -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)" ` - $TemplatePath + $PackerTemplate.Path if ($LastExitCode -ne 0) { throw "Failed to build image." diff --git a/images.CI/linux-and-win/build-image.ps1 b/images.CI/linux-and-win/build-image.ps1 index 7db04fb97..431702ce9 100644 --- a/images.CI/linux-and-win/build-image.ps1 +++ b/images.CI/linux-and-win/build-image.ps1 @@ -1,5 +1,6 @@ param( [String] [Parameter (Mandatory=$true)] $TemplatePath, + [String] [Parameter (Mandatory=$true)] $BuildTemplateName, [String] [Parameter (Mandatory=$true)] $ClientId, [String] [Parameter (Mandatory=$false)] $ClientSecret, [String] [Parameter (Mandatory=$true)] $Location, @@ -8,7 +9,9 @@ param( [String] [Parameter (Mandatory=$true)] $TempResourceGroupName, [String] [Parameter (Mandatory=$true)] $SubscriptionId, [String] [Parameter (Mandatory=$true)] $TenantId, - [String] [Parameter (Mandatory=$false)] $pluginVersion = "2.2.1", + [String] [Parameter (Mandatory=$true)] $ImageOS, # e.g. "ubuntu22", "ubuntu22" or "win19", "win22", "win25" + [String] [Parameter (Mandatory=$false)] $UseAzureCliAuth = "false", + [String] [Parameter (Mandatory=$false)] $PluginVersion = "2.3.3", [String] [Parameter (Mandatory=$false)] $VirtualNetworkName, [String] [Parameter (Mandatory=$false)] $VirtualNetworkRG, [String] [Parameter (Mandatory=$false)] $VirtualNetworkSubnet, @@ -22,7 +25,7 @@ if (-not (Test-Path $TemplatePath)) exit 1 } -$ImageTemplateName = [io.path]::GetFileName($TemplatePath).Split(".")[0] +$buildName = $($BuildTemplateName).Split(".")[1] $InstallPassword = [System.GUID]::NewGuid().ToString().ToUpper() $SensitiveData = @( @@ -44,13 +47,15 @@ Write-Host "Download packer plugins" packer plugins install github.com/hashicorp/azure $pluginVersion Write-Host "Validate packer template" -packer validate -syntax-only $TemplatePath +packer validate -syntax-only -only "$buildName*" $TemplatePath -Write-Host "Build $ImageTemplateName VM" -packer build -var "client_id=$ClientId" ` +Write-Host "Build $buildName VM" +packer build -only "$buildName*" ` + -var "client_id=$ClientId" ` -var "client_secret=$ClientSecret" ` -var "install_password=$InstallPassword" ` -var "location=$Location" ` + -var "image_os=$ImageOS" ` -var "managed_image_name=$ImageName" ` -var "managed_image_resource_group_name=$ImageResourceGroupName" ` -var "subscription_id=$SubscriptionId" ` @@ -60,6 +65,7 @@ packer build -var "client_id=$ClientId" ` -var "virtual_network_resource_group_name=$VirtualNetworkRG" ` -var "virtual_network_subnet_name=$VirtualNetworkSubnet" ` -var "allowed_inbound_ip_addresses=$($AllowedInboundIpAddresses)" ` + -var "use_azure_cli_auth=$UseAzureCliAuth" ` -var "azure_tags=$azure_tags" ` -color=false ` $TemplatePath ` diff --git a/images/ubuntu/templates/ubuntu-22.04.pkr.hcl b/images/ubuntu/templates/build.ubuntu-22_04.pkr.hcl similarity index 66% rename from images/ubuntu/templates/ubuntu-22.04.pkr.hcl rename to images/ubuntu/templates/build.ubuntu-22_04.pkr.hcl index d3486533a..180fbf6d6 100644 --- a/images/ubuntu/templates/ubuntu-22.04.pkr.hcl +++ b/images/ubuntu/templates/build.ubuntu-22_04.pkr.hcl @@ -1,234 +1,6 @@ -variable "allowed_inbound_ip_addresses" { - type = list(string) - default = [] -} - -variable "azure_tags" { - type = map(string) - default = {} -} - -variable "build_resource_group_name" { - type = string - default = "${env("BUILD_RG_NAME")}" -} - -variable "client_cert_path" { - type = string - default = "${env("ARM_CLIENT_CERT_PATH")}" -} - -variable "client_id" { - type = string - default = "${env("ARM_CLIENT_ID")}" -} - -variable "client_secret" { - type = string - default = "${env("ARM_CLIENT_SECRET")}" - sensitive = true -} - -variable "dockerhub_login" { - type = string - default = "${env("DOCKERHUB_LOGIN")}" -} - -variable "dockerhub_password" { - type = string - default = "${env("DOCKERHUB_PASSWORD")}" -} - -variable "helper_script_folder" { - type = string - default = "/imagegeneration/helpers" -} - -variable "image_folder" { - type = string - default = "/imagegeneration" -} - -variable "image_os" { - type = string - default = "ubuntu22" -} - -variable "image_version" { - type = string - default = "dev" -} - -variable "imagedata_file" { - type = string - default = "/imagegeneration/imagedata.json" -} - -variable "installer_script_folder" { - type = string - default = "/imagegeneration/installers" -} - -variable "install_password" { - type = string - default = "" - sensitive = true -} - -variable "location" { - type = string - default = "" -} - -variable "managed_image_name" { - type = string - default = "" -} - -variable "managed_image_resource_group_name" { - type = string - default = "${env("ARM_RESOURCE_GROUP")}" -} - -variable "private_virtual_network_with_public_ip" { - type = bool - default = false -} - -variable "subscription_id" { - type = string - default = "${env("ARM_SUBSCRIPTION_ID")}" -} - -variable "temp_resource_group_name" { - type = string - default = "${env("TEMP_RESOURCE_GROUP_NAME")}" -} - -variable "tenant_id" { - type = string - default = "${env("ARM_TENANT_ID")}" -} - -variable "virtual_network_name" { - type = string - default = "${env("VNET_NAME")}" -} - -variable "virtual_network_resource_group_name" { - type = string - default = "${env("VNET_RESOURCE_GROUP")}" -} - -variable "virtual_network_subnet_name" { - type = string - default = "${env("VNET_SUBNET")}" -} - -variable "vm_size" { - type = string - default = "Standard_D4s_v4" -} - -variable "image_offer" { - type = string - default = "0001-com-ubuntu-server-jammy" -} - -variable "image_publisher" { - type = string - default = "canonical" -} - -variable "image_sku" { - type = string - default = "22_04-lts" -} - -variable "gallery_name" { - type = string - default = "${env("GALLERY_NAME")}" -} - -variable "gallery_resource_group_name" { - type = string - default = "${env("GALLERY_RG_NAME")}" -} - -variable "gallery_image_name" { - type = string - default = "${env("GALLERY_IMAGE_NAME")}" -} - -variable "gallery_image_version" { - type = string - default = "${env("GALLERY_IMAGE_VERSION")}" -} - -variable "gallery_storage_account_type" { - type = string - default = "${env("GALLERY_STORAGE_ACCOUNT_TYPE")}" -} - -variable "use_azure_cli_auth" { - type = bool - default = false -} - -variable "os_disk_size_gb" { - type = number - default = 75 -} - -variable "image_os_type" { - type = string - default = "Linux" -} - -source "azure-arm" "build_image" { - allowed_inbound_ip_addresses = "${var.allowed_inbound_ip_addresses}" - build_resource_group_name = "${var.build_resource_group_name}" - client_cert_path = "${var.client_cert_path}" - client_id = "${var.client_id}" - client_secret = "${var.client_secret}" - use_azure_cli_auth = var.use_azure_cli_auth - image_offer = "${var.image_offer}" - image_publisher = "${var.image_publisher}" - image_sku = "${var.image_sku}" - location = "${var.location}" - managed_image_name = "${var.managed_image_name}" - managed_image_resource_group_name = "${var.managed_image_resource_group_name}" - os_disk_size_gb = var.os_disk_size_gb - os_type = var.image_os_type - private_virtual_network_with_public_ip = "${var.private_virtual_network_with_public_ip}" - subscription_id = "${var.subscription_id}" - temp_resource_group_name = "${var.temp_resource_group_name}" - tenant_id = "${var.tenant_id}" - virtual_network_name = "${var.virtual_network_name}" - virtual_network_resource_group_name = "${var.virtual_network_resource_group_name}" - virtual_network_subnet_name = "${var.virtual_network_subnet_name}" - vm_size = "${var.vm_size}" - - shared_image_gallery_destination { - subscription = var.subscription_id - gallery_name = var.gallery_name - resource_group = var.gallery_resource_group_name - image_name = var.gallery_image_name - image_version = var.gallery_image_version - storage_account_type = var.gallery_storage_account_type - } - - dynamic "azure_tag" { - for_each = var.azure_tags - content { - name = azure_tag.key - value = azure_tag.value - } - } -} - build { - sources = ["source.azure-arm.build_image"] + sources = ["source.azure-arm.image"] + name = "ubuntu-22_04" provisioner "shell" { execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" diff --git a/images/ubuntu/templates/ubuntu-24.04.pkr.hcl b/images/ubuntu/templates/build.ubuntu-24_04.pkr.hcl similarity index 64% rename from images/ubuntu/templates/ubuntu-24.04.pkr.hcl rename to images/ubuntu/templates/build.ubuntu-24_04.pkr.hcl index 65eba995b..6aba4e71f 100644 --- a/images/ubuntu/templates/ubuntu-24.04.pkr.hcl +++ b/images/ubuntu/templates/build.ubuntu-24_04.pkr.hcl @@ -1,234 +1,6 @@ -variable "allowed_inbound_ip_addresses" { - type = list(string) - default = [] -} - -variable "azure_tags" { - type = map(string) - default = {} -} - -variable "build_resource_group_name" { - type = string - default = "${env("BUILD_RG_NAME")}" -} - -variable "client_cert_path" { - type = string - default = "${env("ARM_CLIENT_CERT_PATH")}" -} - -variable "client_id" { - type = string - default = "${env("ARM_CLIENT_ID")}" -} - -variable "client_secret" { - type = string - default = "${env("ARM_CLIENT_SECRET")}" - sensitive = true -} - -variable "dockerhub_login" { - type = string - default = "${env("DOCKERHUB_LOGIN")}" -} - -variable "dockerhub_password" { - type = string - default = "${env("DOCKERHUB_PASSWORD")}" -} - -variable "helper_script_folder" { - type = string - default = "/imagegeneration/helpers" -} - -variable "image_folder" { - type = string - default = "/imagegeneration" -} - -variable "image_os" { - type = string - default = "ubuntu24" -} - -variable "image_version" { - type = string - default = "dev" -} - -variable "imagedata_file" { - type = string - default = "/imagegeneration/imagedata.json" -} - -variable "installer_script_folder" { - type = string - default = "/imagegeneration/installers" -} - -variable "install_password" { - type = string - default = "" - sensitive = true -} - -variable "location" { - type = string - default = "" -} - -variable "managed_image_name" { - type = string - default = "" -} - -variable "managed_image_resource_group_name" { - type = string - default = "${env("ARM_RESOURCE_GROUP")}" -} - -variable "private_virtual_network_with_public_ip" { - type = bool - default = false -} - -variable "subscription_id" { - type = string - default = "${env("ARM_SUBSCRIPTION_ID")}" -} - -variable "temp_resource_group_name" { - type = string - default = "${env("TEMP_RESOURCE_GROUP_NAME")}" -} - -variable "tenant_id" { - type = string - default = "${env("ARM_TENANT_ID")}" -} - -variable "virtual_network_name" { - type = string - default = "${env("VNET_NAME")}" -} - -variable "virtual_network_resource_group_name" { - type = string - default = "${env("VNET_RESOURCE_GROUP")}" -} - -variable "virtual_network_subnet_name" { - type = string - default = "${env("VNET_SUBNET")}" -} - -variable "vm_size" { - type = string - default = "Standard_D4s_v4" -} - -variable "image_offer" { - type = string - default = "ubuntu-24_04-lts" -} - -variable "image_publisher" { - type = string - default = "canonical" -} - -variable "image_sku" { - type = string - default = "server-gen1" -} - -variable "gallery_name" { - type = string - default = "${env("GALLERY_NAME")}" -} - -variable "gallery_resource_group_name" { - type = string - default = "${env("GALLERY_RG_NAME")}" -} - -variable "gallery_image_name" { - type = string - default = "${env("GALLERY_IMAGE_NAME")}" -} - -variable "gallery_image_version" { - type = string - default = "${env("GALLERY_IMAGE_VERSION")}" -} - -variable "gallery_storage_account_type" { - type = string - default = "${env("GALLERY_STORAGE_ACCOUNT_TYPE")}" -} - -variable "use_azure_cli_auth" { - type = bool - default = false -} - -variable "os_disk_size_gb" { - type = number - default = 75 -} - -variable "image_os_type" { - type = string - default = "Linux" -} - -source "azure-arm" "build_image" { - allowed_inbound_ip_addresses = "${var.allowed_inbound_ip_addresses}" - build_resource_group_name = "${var.build_resource_group_name}" - client_cert_path = "${var.client_cert_path}" - client_id = "${var.client_id}" - client_secret = "${var.client_secret}" - use_azure_cli_auth = var.use_azure_cli_auth - image_offer = "${var.image_offer}" - image_publisher = "${var.image_publisher}" - image_sku = "${var.image_sku}" - location = "${var.location}" - managed_image_name = "${var.managed_image_name}" - managed_image_resource_group_name = "${var.managed_image_resource_group_name}" - os_disk_size_gb = var.os_disk_size_gb - os_type = var.image_os_type - private_virtual_network_with_public_ip = "${var.private_virtual_network_with_public_ip}" - subscription_id = "${var.subscription_id}" - temp_resource_group_name = "${var.temp_resource_group_name}" - tenant_id = "${var.tenant_id}" - virtual_network_name = "${var.virtual_network_name}" - virtual_network_resource_group_name = "${var.virtual_network_resource_group_name}" - virtual_network_subnet_name = "${var.virtual_network_subnet_name}" - vm_size = "${var.vm_size}" - - shared_image_gallery_destination { - subscription = var.subscription_id - gallery_name = var.gallery_name - resource_group = var.gallery_resource_group_name - image_name = var.gallery_image_name - image_version = var.gallery_image_version - storage_account_type = var.gallery_storage_account_type - } - - dynamic "azure_tag" { - for_each = var.azure_tags - content { - name = azure_tag.key - value = azure_tag.value - } - } -} - build { - sources = ["source.azure-arm.build_image"] + sources = ["source.azure-arm.image"] + name = "ubuntu-24_04" provisioner "shell" { execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" diff --git a/images/ubuntu/templates/locals.ubuntu.pkr.hcl b/images/ubuntu/templates/locals.ubuntu.pkr.hcl new file mode 100644 index 000000000..fe9b189ab --- /dev/null +++ b/images/ubuntu/templates/locals.ubuntu.pkr.hcl @@ -0,0 +1,18 @@ +locals { + image_properties_map = { + "ubuntu22" = { + publisher = "canonical" + offer = "0001-com-ubuntu-server-jammy" + sku = "22_04-lts" + os_disk_size_gb = coalesce(var.os_disk_size_gb, 75) + }, + "ubuntu24" = { + publisher = "canonical" + offer = "ubuntu-24_04-lts" + sku = "server-gen1" + os_disk_size_gb = coalesce(var.os_disk_size_gb, 75) + } + } + + image_properties = local.image_properties_map[var.image_os] +} diff --git a/images/ubuntu/templates/source.ubuntu.pkr.hcl b/images/ubuntu/templates/source.ubuntu.pkr.hcl new file mode 100644 index 000000000..4080b9b86 --- /dev/null +++ b/images/ubuntu/templates/source.ubuntu.pkr.hcl @@ -0,0 +1,48 @@ +source "azure-arm" "image" { + client_cert_path = var.client_cert_path + client_id = var.client_id + client_secret = var.client_secret + object_id = var.object_id + oidc_request_token = var.oidc_request_token + oidc_request_url = var.oidc_request_url + subscription_id = var.subscription_id + tenant_id = var.tenant_id + use_azure_cli_auth = var.use_azure_cli_auth + + allowed_inbound_ip_addresses = var.allowed_inbound_ip_addresses + build_resource_group_name = var.build_resource_group_name + image_offer = local.image_properties.offer + image_publisher = local.image_properties.publisher + image_sku = local.image_properties.sku + image_version = var.source_image_version + location = var.location + managed_image_name = var.managed_image_name + managed_image_resource_group_name = var.managed_image_resource_group_name + managed_image_storage_account_type = var.managed_image_storage_account_type + os_disk_size_gb = local.image_properties.os_disk_size_gb + os_type = var.image_os_type + private_virtual_network_with_public_ip = var.private_virtual_network_with_public_ip + temp_resource_group_name = var.temp_resource_group_name + virtual_network_name = var.virtual_network_name + virtual_network_resource_group_name = var.virtual_network_resource_group_name + virtual_network_subnet_name = var.virtual_network_subnet_name + vm_size = var.vm_size + winrm_username = var.winrm_username + + shared_image_gallery_destination { + subscription = var.subscription_id + gallery_name = var.gallery_name + resource_group = var.gallery_resource_group_name + image_name = var.gallery_image_name + image_version = var.gallery_image_version + storage_account_type = var.gallery_storage_account_type + } + + dynamic "azure_tag" { + for_each = var.azure_tags + content { + name = azure_tag.key + value = azure_tag.value + } + } +} diff --git a/images/ubuntu/templates/ubuntu-minimal.pkr.hcl b/images/ubuntu/templates/ubuntu-minimal.pkr.hcl deleted file mode 100644 index a2e7d362b..000000000 --- a/images/ubuntu/templates/ubuntu-minimal.pkr.hcl +++ /dev/null @@ -1,282 +0,0 @@ - -locals { - image_os = "ubuntu22" - - toolset_file_name = "toolset-2204.json" - - image_folder = "/imagegeneration" - 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" { - type = list(string) - default = [] -} - -variable "azure_tags" { - type = map(string) - default = {} -} - -variable "build_resource_group_name" { - type = string - default = "${env("BUILD_RESOURCE_GROUP_NAME")}" -} - -variable "client_cert_path" { - type = string - default = "${env("ARM_CLIENT_CERT_PATH")}" -} - -variable "client_id" { - type = string - default = "${env("ARM_CLIENT_ID")}" -} - -variable "client_secret" { - type = string - default = "${env("ARM_CLIENT_SECRET")}" - sensitive = true -} - -variable "image_version" { - type = string - default = "dev" -} - -variable "install_password" { - type = string - default = "" - sensitive = true -} - -variable "location" { - type = string - default = "${env("ARM_RESOURCE_LOCATION")}" -} - -variable "managed_image_name" { - type = string - default = "" -} - -variable "managed_image_resource_group_name" { - type = string - default = "${env("ARM_RESOURCE_GROUP")}" -} - -variable "private_virtual_network_with_public_ip" { - type = bool - default = false -} - -variable "subscription_id" { - type = string - default = "${env("ARM_SUBSCRIPTION_ID")}" -} - -variable "temp_resource_group_name" { - type = string - default = "${env("TEMP_RESOURCE_GROUP_NAME")}" -} - -variable "tenant_id" { - type = string - default = "${env("ARM_TENANT_ID")}" -} - -variable "virtual_network_name" { - type = string - default = "${env("VNET_NAME")}" -} - -variable "virtual_network_resource_group_name" { - type = string - default = "${env("VNET_RESOURCE_GROUP")}" -} - -variable "virtual_network_subnet_name" { - type = string - default = "${env("VNET_SUBNET")}" -} - -variable "vm_size" { - type = string - default = "Standard_D4s_v4" -} - -source "azure-arm" "build_image" { - location = "${var.location}" - - // Auth - tenant_id = "${var.tenant_id}" - subscription_id = "${var.subscription_id}" - client_id = "${var.client_id}" - client_secret = "${var.client_secret}" - client_cert_path = "${var.client_cert_path}" - - // Base image - image_offer = "0001-com-ubuntu-server-jammy" - image_publisher = "canonical" - image_sku = "22_04-lts" - - // Target location - 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}" - temp_resource_group_name = "${var.temp_resource_group_name}" - - // Networking for VM - private_virtual_network_with_public_ip = "${var.private_virtual_network_with_public_ip}" - virtual_network_resource_group_name = "${var.virtual_network_resource_group_name}" - virtual_network_name = "${var.virtual_network_name}" - virtual_network_subnet_name = "${var.virtual_network_subnet_name}" - allowed_inbound_ip_addresses = "${var.allowed_inbound_ip_addresses}" - - // VM Configuration - vm_size = "${var.vm_size}" - os_disk_size_gb = "75" - os_type = "Linux" - - dynamic "azure_tag" { - for_each = var.azure_tags - content { - name = azure_tag.key - value = azure_tag.value - } - } -} - -build { - sources = ["source.azure-arm.build_image"] - - // Create folder to store temporary data - provisioner "shell" { - execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" - inline = ["mkdir ${local.image_folder}", "chmod 777 ${local.image_folder}"] - } - - provisioner "file" { - destination = "${local.helper_script_folder}" - source = "${path.root}/../scripts/helpers" - } - - // Add apt wrapper to implement retries - provisioner "shell" { - execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" - script = "${path.root}/../scripts/build/configure-apt-mock.sh" - } - - // Install MS package repos, Configure apt - provisioner "shell" { - environment_vars = ["HELPER_SCRIPTS=${local.helper_script_folder}","DEBIAN_FRONTEND=noninteractive"] - execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" - scripts = [ - "${path.root}/../scripts/build/install-ms-repos.sh", - "${path.root}/../scripts/build/configure-apt.sh" - ] - } - - // Configure limits - provisioner "shell" { - execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" - script = "${path.root}/../scripts/build/configure-limits.sh" - } - - provisioner "file" { - destination = "${local.installer_script_folder}" - source = "${path.root}/../scripts/build" - } - - provisioner "file" { - destination = "${local.image_folder}" - sources = [ - "${path.root}/../assets/post-gen", - "${path.root}/../scripts/tests" - ] - } - - provisioner "file" { - destination = "${local.installer_script_folder}/toolset.json" - source = "${path.root}/../toolsets/${local.toolset_file_name}" - } - - provisioner "shell" { - execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" - inline = ["mv ${local.image_folder}/post-gen ${local.image_folder}/post-generation"] - } - - // Generate image data file - provisioner "shell" { - environment_vars = ["IMAGE_VERSION=${var.image_version}", "IMAGEDATA_FILE=${local.imagedata_file}"] - execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" - scripts = ["${path.root}/../scripts/build/configure-image-data.sh"] - } - - // Create /etc/environment, configure waagent etc. - provisioner "shell" { - environment_vars = ["IMAGE_VERSION=${var.image_version}", "IMAGE_OS=${local.image_os}", "HELPER_SCRIPTS=${local.helper_script_folder}"] - execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" - scripts = ["${path.root}/../scripts/build/configure-environment.sh"] - } - - provisioner "shell" { - environment_vars = ["DEBIAN_FRONTEND=noninteractive", "HELPER_SCRIPTS=${local.helper_script_folder}", "INSTALLER_SCRIPT_FOLDER=${local.installer_script_folder}"] - execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" - scripts = ["${path.root}/../scripts/build/install-apt-vital.sh"] - } - - provisioner "shell" { - environment_vars = ["DEBIAN_FRONTEND=noninteractive", "HELPER_SCRIPTS=${local.helper_script_folder}", "INSTALLER_SCRIPT_FOLDER=${local.installer_script_folder}"] - execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" - scripts = ["${path.root}/../scripts/build/install-powershell.sh"] - } - - provisioner "shell" { - environment_vars = ["HELPER_SCRIPTS=${local.helper_script_folder}", "INSTALLER_SCRIPT_FOLDER=${local.installer_script_folder}"] - execute_command = "sudo sh -c '{{ .Vars }} pwsh -f {{ .Path }}'" - scripts = ["${path.root}/../scripts/build/Install-PowerShellModules.ps1"] - } - - provisioner "shell" { - environment_vars = ["DEBIAN_FRONTEND=noninteractive", "HELPER_SCRIPTS=${local.helper_script_folder}", "INSTALLER_SCRIPT_FOLDER=${local.installer_script_folder}"] - execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" - scripts = [ - "${path.root}/../scripts/build/install-git.sh", - "${path.root}/../scripts/build/install-git-lfs.sh", - "${path.root}/../scripts/build/install-github-cli.sh", - "${path.root}/../scripts/build/install-zstd.sh" - ] - } - - provisioner "shell" { - execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" - expect_disconnect = true - inline = ["echo 'Reboot VM'", "sudo reboot"] - } - - provisioner "shell" { - execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" - pause_before = "1m0s" - scripts = ["${path.root}/../scripts/build/cleanup.sh"] - start_retry_timeout = "10m" - } - - provisioner "shell" { - environment_vars = ["HELPER_SCRIPT_FOLDER=${local.helper_script_folder}", "INSTALLER_SCRIPT_FOLDER=${local.installer_script_folder}", "IMAGE_FOLDER=${local.image_folder}"] - execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" - scripts = ["${path.root}/../scripts/build/configure-system.sh"] - } - - provisioner "shell" { - execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" - inline = ["sleep 30", "/usr/sbin/waagent -force -deprovision+user && export HISTSIZE=0 && sync"] - } - -} diff --git a/images/ubuntu/templates/variable.ubuntu.pkr.hcl b/images/ubuntu/templates/variable.ubuntu.pkr.hcl new file mode 100644 index 000000000..05ba85877 --- /dev/null +++ b/images/ubuntu/templates/variable.ubuntu.pkr.hcl @@ -0,0 +1,171 @@ +// Authentication related variables +variable "client_cert_path" { + type = string + default = "${env("ARM_CLIENT_CERT_PATH")}" +} +variable "client_id" { + type = string + default = "${env("ARM_CLIENT_ID")}" +} +variable "client_secret" { + type = string + default = "${env("ARM_CLIENT_SECRET")}" + sensitive = true +} +variable "object_id" { + type = string + default = "${env("ARM_OBJECT_ID")}" +} +variable "oidc_request_token" { + type = string + default = "" +} +variable "oidc_request_url" { + type = string + default = "" +} +variable "subscription_id" { + type = string + default = "${env("ARM_SUBSCRIPTION_ID")}" +} +variable "tenant_id" { + type = string + default = "${env("ARM_TENANT_ID")}" +} +variable "use_azure_cli_auth" { + type = bool + default = false +} + +// Azure environment related variables +variable "allowed_inbound_ip_addresses" { + type = list(string) + default = [] +} +variable "azure_tags" { + type = map(string) + default = {} +} +variable "build_resource_group_name" { + type = string + default = "${env("BUILD_RG_NAME")}" +} +variable "gallery_image_name" { + type = string + default = "${env("GALLERY_IMAGE_NAME")}" +} +variable "gallery_image_version" { + type = string + default = "${env("GALLERY_IMAGE_VERSION")}" +} +variable "gallery_name" { + type = string + default = "${env("GALLERY_NAME")}" +} +variable "gallery_resource_group_name" { + type = string + default = "${env("GALLERY_RG_NAME")}" +} +variable "gallery_storage_account_type" { + type = string + default = "${env("GALLERY_STORAGE_ACCOUNT_TYPE")}" +} +variable "image_os_type" { + type = string + default = "Linux" +} +variable "location" { + type = string + default = "" +} +variable "managed_image_name" { + type = string + default = "" +} +variable "managed_image_resource_group_name" { + type = string + default = "${env("ARM_RESOURCE_GROUP")}" +} +variable "managed_image_storage_account_type" { + type = string + default = "Premium_LRS" +} +variable "private_virtual_network_with_public_ip" { + type = bool + default = false +} +variable "os_disk_size_gb" { + type = number + default = 0 +} +variable "source_image_version" { + type = string + default = "latest" +} +variable "temp_resource_group_name" { + type = string + default = "${env("TEMP_RESOURCE_GROUP_NAME")}" +} +variable "virtual_network_name" { + type = string + default = "${env("VNET_NAME")}" +} +variable "virtual_network_resource_group_name" { + type = string + default = "${env("VNET_RESOURCE_GROUP")}" +} +variable "virtual_network_subnet_name" { + type = string + default = "${env("VNET_SUBNET")}" +} +variable "vm_size" { + type = string + default = "Standard_D4s_v4" +} +variable "winrm_username" { // The username used to connect to the VM via WinRM + type = string // Also applies to the username used to create the VM + default = "packer" +} + +// Image related variables +variable "dockerhub_login" { + type = string + default = "${env("DOCKERHUB_LOGIN")}" +} +variable "dockerhub_password" { + type = string + default = "${env("DOCKERHUB_PASSWORD")}" +} +variable "helper_script_folder" { + type = string + default = "/imagegeneration/helpers" +} +variable "image_folder" { + type = string + default = "/imagegeneration" +} +variable "image_os" { + type = string + default = "" +} +variable "image_version" { + type = string + default = "dev" +} +variable "imagedata_file" { + type = string + default = "/imagegeneration/imagedata.json" +} +variable "installer_script_folder" { + type = string + default = "/imagegeneration/installers" +} +variable "install_password" { + type = string + default = "" + sensitive = true +} +variable "install_user" { + type = string + default = "installer" +} diff --git a/images/windows/templates/windows-2019.pkr.hcl b/images/windows/templates/build.windows-2019.pkr.hcl similarity index 66% rename from images/windows/templates/windows-2019.pkr.hcl rename to images/windows/templates/build.windows-2019.pkr.hcl index 858b7c0d4..303966492 100644 --- a/images/windows/templates/windows-2019.pkr.hcl +++ b/images/windows/templates/build.windows-2019.pkr.hcl @@ -1,263 +1,6 @@ -variable "agent_tools_directory" { - type = string - default = "C:\\hostedtoolcache\\windows" -} - -variable "allowed_inbound_ip_addresses" { - type = list(string) - default = [] -} - -variable "azure_tags" { - type = map(string) - default = {} -} - -variable "build_resource_group_name" { - type = string - default = "${env("BUILD_RG_NAME")}" -} - -variable "client_cert_path" { - type = string - default = "${env("ARM_CLIENT_CERT_PATH")}" -} - -variable "client_id" { - type = string - default = "${env("ARM_CLIENT_ID")}" -} - -variable "client_secret" { - type = string - default = "${env("ARM_CLIENT_SECRET")}" - sensitive = true -} - -variable "helper_script_folder" { - type = string - default = "C:\\Program Files\\WindowsPowerShell\\Modules\\" -} - -variable "image_folder" { - type = string - default = "C:\\image" -} - -variable "image_os" { - type = string - default = "win19" -} - -variable "image_version" { - type = string - default = "dev" -} - -variable "imagedata_file" { - type = string - default = "C:\\imagedata.json" -} - -variable "temp_dir" { - type = string - default = "D:\\temp" -} - -variable "install_password" { - type = string - default = "" - sensitive = true -} - -variable "install_user" { - type = string - default = "installer" -} - -variable "location" { - type = string - default = "" -} - -variable "managed_image_name" { - type = string - default = "" -} - -variable "managed_image_resource_group_name" { - type = string - default = "${env("ARM_RESOURCE_GROUP")}" -} - -variable "managed_image_storage_account_type" { - type = string - default = "Premium_LRS" -} - -variable "object_id" { - type = string - default = "${env("ARM_OBJECT_ID")}" -} - -variable "private_virtual_network_with_public_ip" { - type = bool - default = false -} - -variable "subscription_id" { - type = string - default = "${env("ARM_SUBSCRIPTION_ID")}" -} - -variable "temp_resource_group_name" { - type = string - default = "${env("TEMP_RESOURCE_GROUP_NAME")}" -} - -variable "tenant_id" { - type = string - default = "${env("ARM_TENANT_ID")}" -} - -variable "virtual_network_name" { - type = string - default = "${env("VNET_NAME")}" -} - -variable "virtual_network_resource_group_name" { - type = string - default = "${env("VNET_RESOURCE_GROUP")}" -} - -variable "virtual_network_subnet_name" { - type = string - default = "${env("VNET_SUBNET")}" -} - -variable "vm_size" { - type = string - default = "Standard_F8s_v2" -} - -variable "image_offer" { - type = string - default = "WindowsServer" -} - -variable "image_publisher" { - type = string - default = "MicrosoftWindowsServer" -} - -variable "image_sku" { - type = string - default = "2019-Datacenter" -} - -variable "gallery_name" { - type = string - default = "${env("GALLERY_NAME")}" -} - -variable "gallery_resource_group_name" { - type = string - default = "${env("GALLERY_RG_NAME")}" -} - -variable "gallery_image_name" { - type = string - default = "${env("GALLERY_IMAGE_NAME")}" -} - -variable "gallery_image_version" { - type = string - default = "${env("GALLERY_IMAGE_VERSION")}" -} - -variable "gallery_storage_account_type" { - type = string - default = "${env("GALLERY_STORAGE_ACCOUNT_TYPE")}" -} - -variable "build_key_vault_name" { - type = string - default = "${env("BUILD_KEY_VAULT_NAME")}" -} - -variable "build_key_vault_secret_name" { - type = string - default = "${env("BUILD_KEY_VAULT_SECRET_NAME")}" -} - -variable "use_azure_cli_auth" { - type = bool - default = false -} - -variable "os_disk_size_gb" { - type = number - default = 256 -} - -variable "image_os_type" { - type = string - default = "Windows" -} - -source "azure-arm" "image" { - allowed_inbound_ip_addresses = "${var.allowed_inbound_ip_addresses}" - build_resource_group_name = "${var.build_resource_group_name}" - client_cert_path = "${var.client_cert_path}" - client_id = "${var.client_id}" - client_secret = "${var.client_secret}" - use_azure_cli_auth = var.use_azure_cli_auth - communicator = "winrm" - image_offer = "${var.image_offer}" - image_publisher = "${var.image_publisher}" - image_sku = "${var.image_sku}" - location = "${var.location}" - managed_image_name = "${var.managed_image_name}" - managed_image_resource_group_name = "${var.managed_image_resource_group_name}" - managed_image_storage_account_type = "${var.managed_image_storage_account_type}" - object_id = "${var.object_id}" - os_disk_size_gb = var.os_disk_size_gb - os_type = var.image_os_type - private_virtual_network_with_public_ip = "${var.private_virtual_network_with_public_ip}" - subscription_id = "${var.subscription_id}" - temp_resource_group_name = "${var.temp_resource_group_name}" - tenant_id = "${var.tenant_id}" - virtual_network_name = "${var.virtual_network_name}" - virtual_network_resource_group_name = "${var.virtual_network_resource_group_name}" - virtual_network_subnet_name = "${var.virtual_network_subnet_name}" - vm_size = "${var.vm_size}" - winrm_insecure = "true" - winrm_use_ssl = "true" - winrm_username = "packer" - winrm_expiration_time = "1440h" - build_key_vault_name = var.build_key_vault_name - build_key_vault_secret_name = var.build_key_vault_secret_name - - shared_image_gallery_destination { - subscription = var.subscription_id - gallery_name = var.gallery_name - resource_group = var.gallery_resource_group_name - image_name = var.gallery_image_name - image_version = var.gallery_image_version - storage_account_type = var.gallery_storage_account_type - } - - dynamic "azure_tag" { - for_each = var.azure_tags - content { - name = azure_tag.key - value = azure_tag.value - } - } -} - build { sources = ["source.azure-arm.image"] + name = "windows-2019" provisioner "powershell" { inline = [ diff --git a/images/windows/templates/windows-2022.pkr.hcl b/images/windows/templates/build.windows-2022.pkr.hcl similarity index 65% rename from images/windows/templates/windows-2022.pkr.hcl rename to images/windows/templates/build.windows-2022.pkr.hcl index 76069d719..ff8433515 100644 --- a/images/windows/templates/windows-2022.pkr.hcl +++ b/images/windows/templates/build.windows-2022.pkr.hcl @@ -1,263 +1,6 @@ -variable "agent_tools_directory" { - type = string - default = "C:\\hostedtoolcache\\windows" -} - -variable "allowed_inbound_ip_addresses" { - type = list(string) - default = [] -} - -variable "azure_tags" { - type = map(string) - default = {} -} - -variable "build_resource_group_name" { - type = string - default = "${env("BUILD_RG_NAME")}" -} - -variable "client_cert_path" { - type = string - default = "${env("ARM_CLIENT_CERT_PATH")}" -} - -variable "client_id" { - type = string - default = "${env("ARM_CLIENT_ID")}" -} - -variable "client_secret" { - type = string - default = "${env("ARM_CLIENT_SECRET")}" - sensitive = true -} - -variable "helper_script_folder" { - type = string - default = "C:\\Program Files\\WindowsPowerShell\\Modules\\" -} - -variable "image_folder" { - type = string - default = "C:\\image" -} - -variable "image_os" { - type = string - default = "win22" -} - -variable "image_version" { - type = string - default = "dev" -} - -variable "imagedata_file" { - type = string - default = "C:\\imagedata.json" -} - -variable "temp_dir" { - type = string - default = "D:\\temp" -} - -variable "install_password" { - type = string - default = "" - sensitive = true -} - -variable "install_user" { - type = string - default = "installer" -} - -variable "location" { - type = string - default = "" -} - -variable "managed_image_name" { - type = string - default = "" -} - -variable "managed_image_resource_group_name" { - type = string - default = "${env("ARM_RESOURCE_GROUP")}" -} - -variable "managed_image_storage_account_type" { - type = string - default = "Premium_LRS" -} - -variable "object_id" { - type = string - default = "${env("ARM_OBJECT_ID")}" -} - -variable "private_virtual_network_with_public_ip" { - type = bool - default = false -} - -variable "subscription_id" { - type = string - default = "${env("ARM_SUBSCRIPTION_ID")}" -} - -variable "temp_resource_group_name" { - type = string - default = "${env("TEMP_RESOURCE_GROUP_NAME")}" -} - -variable "tenant_id" { - type = string - default = "${env("ARM_TENANT_ID")}" -} - -variable "virtual_network_name" { - type = string - default = "${env("VNET_NAME")}" -} - -variable "virtual_network_resource_group_name" { - type = string - default = "${env("VNET_RESOURCE_GROUP")}" -} - -variable "virtual_network_subnet_name" { - type = string - default = "${env("VNET_SUBNET")}" -} - -variable "vm_size" { - type = string - default = "Standard_F8s_v2" -} - -variable "image_offer" { - type = string - default = "WindowsServer" -} - -variable "image_publisher" { - type = string - default = "MicrosoftWindowsServer" -} - -variable "image_sku" { - type = string - default = "2022-Datacenter" -} - -variable "gallery_name" { - type = string - default = "${env("GALLERY_NAME")}" -} - -variable "gallery_resource_group_name" { - type = string - default = "${env("GALLERY_RG_NAME")}" -} - -variable "gallery_image_name" { - type = string - default = "${env("GALLERY_IMAGE_NAME")}" -} - -variable "gallery_image_version" { - type = string - default = "${env("GALLERY_IMAGE_VERSION")}" -} - -variable "gallery_storage_account_type" { - type = string - default = "${env("GALLERY_STORAGE_ACCOUNT_TYPE")}" -} - -variable "build_key_vault_name" { - type = string - default = "${env("BUILD_KEY_VAULT_NAME")}" -} - -variable "build_key_vault_secret_name" { - type = string - default = "${env("BUILD_KEY_VAULT_SECRET_NAME")}" -} - -variable "use_azure_cli_auth" { - type = bool - default = false -} - -variable "os_disk_size_gb" { - type = number - default = 256 -} - -variable "image_os_type" { - type = string - default = "Windows" -} - -source "azure-arm" "image" { - allowed_inbound_ip_addresses = "${var.allowed_inbound_ip_addresses}" - build_resource_group_name = "${var.build_resource_group_name}" - client_cert_path = "${var.client_cert_path}" - client_id = "${var.client_id}" - client_secret = "${var.client_secret}" - use_azure_cli_auth = var.use_azure_cli_auth - communicator = "winrm" - image_offer = "${var.image_offer}" - image_publisher = "${var.image_publisher}" - image_sku = "${var.image_sku}" - location = "${var.location}" - managed_image_name = "${var.managed_image_name}" - managed_image_resource_group_name = "${var.managed_image_resource_group_name}" - managed_image_storage_account_type = "${var.managed_image_storage_account_type}" - object_id = "${var.object_id}" - os_disk_size_gb = var.os_disk_size_gb - os_type = var.image_os_type - private_virtual_network_with_public_ip = "${var.private_virtual_network_with_public_ip}" - subscription_id = "${var.subscription_id}" - temp_resource_group_name = "${var.temp_resource_group_name}" - tenant_id = "${var.tenant_id}" - virtual_network_name = "${var.virtual_network_name}" - virtual_network_resource_group_name = "${var.virtual_network_resource_group_name}" - virtual_network_subnet_name = "${var.virtual_network_subnet_name}" - vm_size = "${var.vm_size}" - winrm_insecure = "true" - winrm_use_ssl = "true" - winrm_username = "packer" - winrm_expiration_time = "1440h" - build_key_vault_name = var.build_key_vault_name - build_key_vault_secret_name = var.build_key_vault_secret_name - - shared_image_gallery_destination { - subscription = var.subscription_id - gallery_name = var.gallery_name - resource_group = var.gallery_resource_group_name - image_name = var.gallery_image_name - image_version = var.gallery_image_version - storage_account_type = var.gallery_storage_account_type - } - - dynamic "azure_tag" { - for_each = var.azure_tags - content { - name = azure_tag.key - value = azure_tag.value - } - } -} - build { sources = ["source.azure-arm.image"] + name = "windows-2022" provisioner "powershell" { inline = [ diff --git a/images/windows/templates/windows-2025.pkr.hcl b/images/windows/templates/build.windows-2025.pkr.hcl similarity index 65% rename from images/windows/templates/windows-2025.pkr.hcl rename to images/windows/templates/build.windows-2025.pkr.hcl index a375bbb67..fbb1ea933 100644 --- a/images/windows/templates/windows-2025.pkr.hcl +++ b/images/windows/templates/build.windows-2025.pkr.hcl @@ -1,263 +1,6 @@ -variable "agent_tools_directory" { - type = string - default = "C:\\hostedtoolcache\\windows" -} - -variable "allowed_inbound_ip_addresses" { - type = list(string) - default = [] -} - -variable "azure_tags" { - type = map(string) - default = {} -} - -variable "build_resource_group_name" { - type = string - default = "${env("BUILD_RG_NAME")}" -} - -variable "client_cert_path" { - type = string - default = "${env("ARM_CLIENT_CERT_PATH")}" -} - -variable "client_id" { - type = string - default = "${env("ARM_CLIENT_ID")}" -} - -variable "client_secret" { - type = string - default = "${env("ARM_CLIENT_SECRET")}" - sensitive = true -} - -variable "helper_script_folder" { - type = string - default = "C:\\Program Files\\WindowsPowerShell\\Modules\\" -} - -variable "image_folder" { - type = string - default = "C:\\image" -} - -variable "image_os" { - type = string - default = "win25" -} - -variable "image_version" { - type = string - default = "dev" -} - -variable "imagedata_file" { - type = string - default = "C:\\imagedata.json" -} - -variable "temp_dir" { - type = string - default = "D:\\temp" -} - -variable "install_password" { - type = string - default = "" - sensitive = true -} - -variable "install_user" { - type = string - default = "installer" -} - -variable "location" { - type = string - default = "" -} - -variable "managed_image_name" { - type = string - default = "" -} - -variable "managed_image_resource_group_name" { - type = string - default = "${env("ARM_RESOURCE_GROUP")}" -} - -variable "managed_image_storage_account_type" { - type = string - default = "Premium_LRS" -} - -variable "object_id" { - type = string - default = "${env("ARM_OBJECT_ID")}" -} - -variable "private_virtual_network_with_public_ip" { - type = bool - default = false -} - -variable "subscription_id" { - type = string - default = "${env("ARM_SUBSCRIPTION_ID")}" -} - -variable "temp_resource_group_name" { - type = string - default = "${env("TEMP_RESOURCE_GROUP_NAME")}" -} - -variable "tenant_id" { - type = string - default = "${env("ARM_TENANT_ID")}" -} - -variable "virtual_network_name" { - type = string - default = "${env("VNET_NAME")}" -} - -variable "virtual_network_resource_group_name" { - type = string - default = "${env("VNET_RESOURCE_GROUP")}" -} - -variable "virtual_network_subnet_name" { - type = string - default = "${env("VNET_SUBNET")}" -} - -variable "vm_size" { - type = string - default = "Standard_F8s_v2" -} - -variable "image_offer" { - type = string - default = "WindowsServer" -} - -variable "image_publisher" { - type = string - default = "MicrosoftWindowsServer" -} - -variable "image_sku" { - type = string - default = "2025-Datacenter" -} - -variable "gallery_name" { - type = string - default = "${env("GALLERY_NAME")}" -} - -variable "gallery_resource_group_name" { - type = string - default = "${env("GALLERY_RG_NAME")}" -} - -variable "gallery_image_name" { - type = string - default = "${env("GALLERY_IMAGE_NAME")}" -} - -variable "gallery_image_version" { - type = string - default = "${env("GALLERY_IMAGE_VERSION")}" -} - -variable "gallery_storage_account_type" { - type = string - default = "${env("GALLERY_STORAGE_ACCOUNT_TYPE")}" -} - -variable "build_key_vault_name" { - type = string - default = "${env("BUILD_KEY_VAULT_NAME")}" -} - -variable "build_key_vault_secret_name" { - type = string - default = "${env("BUILD_KEY_VAULT_SECRET_NAME")}" -} - -variable "use_azure_cli_auth" { - type = bool - default = false -} - -variable "os_disk_size_gb" { - type = number - default = 150 -} - -variable "image_os_type" { - type = string - default = "Windows" -} - -source "azure-arm" "image" { - allowed_inbound_ip_addresses = "${var.allowed_inbound_ip_addresses}" - build_resource_group_name = "${var.build_resource_group_name}" - client_cert_path = "${var.client_cert_path}" - client_id = "${var.client_id}" - client_secret = "${var.client_secret}" - use_azure_cli_auth = var.use_azure_cli_auth - communicator = "winrm" - image_offer = "${var.image_offer}" - image_publisher = "${var.image_publisher}" - image_sku = "${var.image_sku}" - location = "${var.location}" - managed_image_name = "${var.managed_image_name}" - managed_image_resource_group_name = "${var.managed_image_resource_group_name}" - managed_image_storage_account_type = "${var.managed_image_storage_account_type}" - object_id = "${var.object_id}" - os_disk_size_gb = var.os_disk_size_gb - os_type = var.image_os_type - private_virtual_network_with_public_ip = "${var.private_virtual_network_with_public_ip}" - subscription_id = "${var.subscription_id}" - temp_resource_group_name = "${var.temp_resource_group_name}" - tenant_id = "${var.tenant_id}" - virtual_network_name = "${var.virtual_network_name}" - virtual_network_resource_group_name = "${var.virtual_network_resource_group_name}" - virtual_network_subnet_name = "${var.virtual_network_subnet_name}" - vm_size = "${var.vm_size}" - winrm_insecure = "true" - winrm_use_ssl = "true" - winrm_username = "packer" - winrm_expiration_time = "1440h" - build_key_vault_name = var.build_key_vault_name - build_key_vault_secret_name = var.build_key_vault_secret_name - - shared_image_gallery_destination { - subscription = var.subscription_id - gallery_name = var.gallery_name - resource_group = var.gallery_resource_group_name - image_name = var.gallery_image_name - image_version = var.gallery_image_version - storage_account_type = var.gallery_storage_account_type - } - - dynamic "azure_tag" { - for_each = var.azure_tags - content { - name = azure_tag.key - value = azure_tag.value - } - } -} - build { sources = ["source.azure-arm.image"] + name = "windows-2025" provisioner "powershell" { inline = [ diff --git a/images/windows/templates/locals.windows.pkr.hcl b/images/windows/templates/locals.windows.pkr.hcl new file mode 100644 index 000000000..d03ab4524 --- /dev/null +++ b/images/windows/templates/locals.windows.pkr.hcl @@ -0,0 +1,24 @@ +locals { + image_properties_map = { + "win19" = { + publisher = "MicrosoftWindowsServer" + offer = "WindowsServer" + sku = "2019-Datacenter" + os_disk_size_gb = coalesce(var.os_disk_size_gb, 256) + }, + "win22" = { + publisher = "MicrosoftWindowsServer" + offer = "WindowsServer" + sku = "2022-Datacenter" + os_disk_size_gb = coalesce(var.os_disk_size_gb, 256) + }, + "win25" = { + publisher = "MicrosoftWindowsServer" + offer = "WindowsServer" + sku = "2025-Datacenter" + os_disk_size_gb = coalesce(var.os_disk_size_gb, 150) + } + } + + image_properties = local.image_properties_map[var.image_os] +} diff --git a/images/windows/templates/source.windows.pkr.hcl b/images/windows/templates/source.windows.pkr.hcl new file mode 100644 index 000000000..36aac4035 --- /dev/null +++ b/images/windows/templates/source.windows.pkr.hcl @@ -0,0 +1,54 @@ +source "azure-arm" "image" { + client_cert_path = var.client_cert_path + client_id = var.client_id + client_secret = var.client_secret + object_id = var.object_id + oidc_request_token = var.oidc_request_token + oidc_request_url = var.oidc_request_url + subscription_id = var.subscription_id + tenant_id = var.tenant_id + use_azure_cli_auth = var.use_azure_cli_auth + + allowed_inbound_ip_addresses = var.allowed_inbound_ip_addresses + build_key_vault_name = var.build_key_vault_name + build_key_vault_secret_name = var.build_key_vault_secret_name + build_resource_group_name = var.build_resource_group_name + communicator = "winrm" + image_offer = local.image_properties.offer + image_publisher = local.image_properties.publisher + image_sku = local.image_properties.sku + image_version = var.source_image_version + location = var.location + managed_image_name = var.managed_image_name + managed_image_resource_group_name = var.managed_image_resource_group_name + managed_image_storage_account_type = var.managed_image_storage_account_type + os_disk_size_gb = local.image_properties.os_disk_size_gb + os_type = var.image_os_type + private_virtual_network_with_public_ip = var.private_virtual_network_with_public_ip + temp_resource_group_name = var.temp_resource_group_name + virtual_network_name = var.virtual_network_name + virtual_network_resource_group_name = var.virtual_network_resource_group_name + virtual_network_subnet_name = var.virtual_network_subnet_name + vm_size = var.vm_size + winrm_expiration_time = var.winrm_expiration_time + winrm_insecure = "true" + winrm_use_ssl = "true" + winrm_username = var.winrm_username + + shared_image_gallery_destination { + subscription = var.subscription_id + gallery_name = var.gallery_name + resource_group = var.gallery_resource_group_name + image_name = var.gallery_image_name + image_version = var.gallery_image_version + storage_account_type = var.gallery_storage_account_type + } + + dynamic "azure_tag" { + for_each = var.azure_tags + content { + name = azure_tag.key + value = azure_tag.value + } + } +} diff --git a/images/windows/templates/variable.windows.pkr.hcl b/images/windows/templates/variable.windows.pkr.hcl new file mode 100644 index 000000000..daf69c66f --- /dev/null +++ b/images/windows/templates/variable.windows.pkr.hcl @@ -0,0 +1,179 @@ +// Authentication related variables +variable "client_cert_path" { + type = string + default = "${env("ARM_CLIENT_CERT_PATH")}" +} +variable "client_id" { + type = string + default = "${env("ARM_CLIENT_ID")}" +} +variable "client_secret" { + type = string + default = "${env("ARM_CLIENT_SECRET")}" + sensitive = true +} +variable "object_id" { + type = string + default = "${env("ARM_OBJECT_ID")}" +} +variable "oidc_request_token" { + type = string + default = "" +} +variable "oidc_request_url" { + type = string + default = "" +} +variable "subscription_id" { + type = string + default = "${env("ARM_SUBSCRIPTION_ID")}" +} +variable "tenant_id" { + type = string + default = "${env("ARM_TENANT_ID")}" +} +variable "use_azure_cli_auth" { + type = bool + default = false +} + +// Azure environment related variables +variable "allowed_inbound_ip_addresses" { + type = list(string) + default = [] +} +variable "azure_tags" { + type = map(string) + default = {} +} +variable "build_key_vault_name" { + type = string + default = "${env("BUILD_KEY_VAULT_NAME")}" +} +variable "build_key_vault_secret_name" { + type = string + default = "${env("BUILD_KEY_VAULT_SECRET_NAME")}" +} +variable "build_resource_group_name" { + type = string + default = "${env("BUILD_RG_NAME")}" +} +variable "gallery_image_name" { + type = string + default = "${env("GALLERY_IMAGE_NAME")}" +} +variable "gallery_image_version" { + type = string + default = "${env("GALLERY_IMAGE_VERSION")}" +} +variable "gallery_name" { + type = string + default = "${env("GALLERY_NAME")}" +} +variable "gallery_resource_group_name" { + type = string + default = "${env("GALLERY_RG_NAME")}" +} +variable "gallery_storage_account_type" { + type = string + default = "${env("GALLERY_STORAGE_ACCOUNT_TYPE")}" +} +variable "image_os_type" { + type = string + default = "Windows" +} +variable "location" { + type = string + default = "" +} +variable "managed_image_name" { + type = string + default = "" +} +variable "managed_image_resource_group_name" { + type = string + default = "${env("ARM_RESOURCE_GROUP")}" +} +variable "managed_image_storage_account_type" { + type = string + default = "Premium_LRS" +} +variable "private_virtual_network_with_public_ip" { + type = bool + default = false +} +variable "os_disk_size_gb" { + type = number + default = 0 +} +variable "source_image_version" { + type = string + default = "latest" +} +variable "temp_resource_group_name" { + type = string + default = "${env("TEMP_RESOURCE_GROUP_NAME")}" +} +variable "virtual_network_name" { + type = string + default = "${env("VNET_NAME")}" +} +variable "virtual_network_resource_group_name" { + type = string + default = "${env("VNET_RESOURCE_GROUP")}" +} +variable "virtual_network_subnet_name" { + type = string + default = "${env("VNET_SUBNET")}" +} +variable "vm_size" { + type = string + default = "Standard_F8s_v2" +} +variable "winrm_expiration_time" { // A time duration with which to set the WinRM certificate to expire + type = string // Also applies to key vault secret expiration time + default = "1440h" +} +variable "winrm_username" { // The username used to connect to the VM via WinRM + type = string // Also applies to the username used to create the VM + default = "packer" +} + +// Image related variables +variable "agent_tools_directory" { + type = string + default = "C:\\hostedtoolcache\\windows" +} +variable "helper_script_folder" { + type = string + default = "C:\\Program Files\\WindowsPowerShell\\Modules\\" +} +variable "image_folder" { + type = string + default = "C:\\image" +} +variable "image_os" { + type = string + default = "" +} +variable "image_version" { + type = string + default = "dev" +} +variable "imagedata_file" { + type = string + default = "C:\\imagedata.json" +} +variable "install_password" { + type = string + default = "" + sensitive = true +} +variable "install_user" { + type = string + default = "installer" +} +variable "temp_dir" { + type = string + default = "D:\\temp" +}