1

tl;博士

如何以编程方式从 Azure Image Builder 模板构建实例向下钻取以查找构建customization.log文件?


长版

我最近开始使用 Azure Image Builder,经过一段时间后,我有了一个 CI 管道,可以将源存储库中的文件中的图像模板部署和构建到共享图像库中。

在我的构建过程结束时,我想检索packerlogs\<guid>\customization.log由 Azure Image Builder 生成的文件 - 我可以通过在门户中单击来找到它(见下面的屏幕截图),但我正在努力遵循任何类型的面包屑线索以编程方式定位 blob。

在此处输入图像描述

也许有一个非常简单的方法可以做到这一点,我正在制作一个巨大的猪耳朵,但这是我到目前为止所得到的:

  1. 在生成过程中,Azure Image Builder 会创建一个名为的临时资源组IT_<gallery-resource-group-name>_<image-template-name>_<guid>,用于存储自定义日志。这还具有以下标签,可用于查找正确的资源组:

    • "createdBy" = "AzureVMImageBuilder"
    • "imageTemplateResourceGroupName" = "<shared image gallery resource group name>"
    • "imageTemplateName" = "<image template name>"
  2. 在临时资源组中,有一个具有 24 个字符随机名称的存储帐户 - 例如abc123def456ghi789j01234

  3. 在存储帐户中有一个名为packerlogs

  4. 在存储帐户容器中,一些 blob 以<guid>\customization.log.

  5. 到目前为止,我不知道<guid>使用哪个来过滤特定构建实例的结果。我可以使用时间戳来读取最新的时间戳,但我怎么知道这对于任何给定的图像模板构建都是正确的。例如,哪个<guid>属于我的图像模板的图像版本 1.0.55 的构建过程?

这是我到目前为止的代码:

# this is my starting point - I've got an image template that I've built using Start-AzImageBuilderTemplate
# and it's successfully created a new image version in a shared image gallery
$myImageGallery    = ...
$myImageDefinition = ...
$myImageTemplate   = ...
$myImageVersion    = ...


# find the temporary resource group with the appropriate tags for the image template
$temporaryResourceGroup = Get-AzResourceGroup -Tag @{ "createdBy" = "AzureVMImageBuilder" } `
     | where-object {
         ($_.Tags.imageTemplateResourceGroupName -eq $myImageGallery.ResourceGroupName) -and
         ($_.Tags.imageTemplateName -eq $myImageTemplate.Name)
     };

# get a reference to the single storage account in the resource group
$storageAccount = Get-AzResource -ResourceGroupName $temporaryResourceGroup.ResourceGroupName `
                                 -ResourceType      "Microsoft.Storage/storageAccounts";


# find the "packerlogs" container
$storageKey = ($storageAccount | Get-AzStorageAccountKey)[0].Value
$context = New-AzStorageContext -StorageAccountName $storageAccount.Name -StorageAccountKey $storageKey
$packerlogs = Get-AzStorageContainer -Context $context -Name "packerlogs";


# get the list of blobs that represent the customization logs from each of the individual template builds
$blobs = Get-AzStorageBlob -Context $context -Container $packerlogs.Name -Blob "*/customization.log";


# but which blob do I want for $myImageVersion?
4

1 回答 1

2

最后,我现在每次使用它来构建新的图像版本时都删除并重新创建图像模板。

这会导致临时资源组也被删除并重新创建,因此在构建过程结束时 packerlogs 容器下只有一个文件夹。

结果是Get-AzStorageBlob -Context $context -Container $packerlogs.Name -Blob "*/customization.log"只返回一个 blob,我可以安全地(?)假设这是我构建的那个。

这似乎是一种信仰的飞跃,但我看不到任何其他可靠的方法可以将构建实例与打包程序日志容器下的相关文件夹相关联,所以现在必须这样做......

于 2021-05-22T20:50:07.237 回答