|
| 1 | +#!/bin/bash |
| 2 | +set -euxo pipefail |
| 3 | +# Find the absolute path of the directory containing this script |
| 4 | +SCRIPTS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" |
| 5 | +. "$SCRIPTS_DIR/common.sh" |
| 6 | + |
| 7 | +storage_account_url="https://$STORAGE_ACCOUNT_NAME.blob.core.windows.net" |
| 8 | +storage_account_resource_id="/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP_NAME/providers/Microsoft.Storage/storageAccounts/$STORAGE_ACCOUNT_NAME" |
| 9 | + |
| 10 | +replicationMode="Shallow" |
| 11 | +storage_blob_endpoint="$storage_account_url/$STORAGE_CONTAINER_NAME/$STORAGE_BLOB_NAME" |
| 12 | + |
| 13 | +az account set --subscription "$SUBSCRIPTION_ID" |
| 14 | + |
| 15 | +if [ "$(az group exists -n "$RESOURCE_GROUP_NAME")" == "false" ]; then |
| 16 | + az group create \ |
| 17 | + --name "$RESOURCE_GROUP_NAME" \ |
| 18 | + --location "$LOCATION" |
| 19 | +fi |
| 20 | + |
| 21 | +# Ensure STORAGE_ACCOUNT_NAME exists and the managed identity has access |
| 22 | +if ! az storage account show --ids "$storage_account_resource_id"; then |
| 23 | + echo "Could not find storage account \"$STORAGE_ACCOUNT_NAME\" in the expected location. Creating the storage account." |
| 24 | + |
| 25 | + if [ "$(az storage account check-name --name "$STORAGE_ACCOUNT_NAME" --query nameAvailable)" == "false" ]; then |
| 26 | + echo "Storage account name $STORAGE_ACCOUNT_NAME is not available" |
| 27 | + exit 1 |
| 28 | + fi |
| 29 | + az storage account create \ |
| 30 | + --resource-group "$RESOURCE_GROUP_NAME" \ |
| 31 | + --name "$STORAGE_ACCOUNT_NAME" \ |
| 32 | + --location "$LOCATION" \ |
| 33 | + --allow-shared-key-access false |
| 34 | +fi |
| 35 | + |
| 36 | +# Ensure "build_target" storage container exists |
| 37 | +containerExists=$(az storage container exists --account-name "$STORAGE_ACCOUNT_NAME" --name "$STORAGE_CONTAINER_NAME" --auth-mode login | jq .exists) |
| 38 | +if [[ $containerExists != "true" ]]; then |
| 39 | + echo "Could not find container \"$STORAGE_CONTAINER_NAME\". Creating container \"$STORAGE_CONTAINER_NAME\" in storage account \"$STORAGE_ACCOUNT_NAME\"..." |
| 40 | + az storage container create \ |
| 41 | + --account-name "$STORAGE_ACCOUNT_NAME" \ |
| 42 | + --name "$STORAGE_CONTAINER_NAME" \ |
| 43 | + --auth-mode login |
| 44 | +fi |
| 45 | + |
| 46 | +# Upload the image artifact to Storage Account |
| 47 | +azcopy copy "$IMAGE_PATH" "$storage_blob_endpoint" --blob-type=PageBlob |
| 48 | + |
| 49 | +# Ensure GALLERY_NAME exists |
| 50 | +if ! az sig show -r "$GALLERY_NAME" -g "$RESOURCE_GROUP_NAME"; then |
| 51 | + echo "Could not find image gallery \"$GALLERY_NAME\" in resource group \"$RESOURCE_GROUP_NAME\". Creating the gallery." |
| 52 | + az sig create \ |
| 53 | + --resource-group "$RESOURCE_GROUP_NAME" \ |
| 54 | + --gallery-name "$GALLERY_NAME" \ |
| 55 | + --location "$LOCATION" |
| 56 | +fi |
| 57 | + |
| 58 | +# Ensure the "build_target" image-definition exists |
| 59 | +# Note: We publish only the VHD from the secure-prod the SIG |
| 60 | +imageDefinitionExists=$(az sig image-definition list -r "$GALLERY_NAME" -g "$RESOURCE_GROUP_NAME" | grep "name" | grep -c "$GALLERY_IMAGE_DEFINITION" || :;) # the "|| :;" prevents grep from halting the script when it finds no matches and exits with exit code 1 |
| 61 | +if [[ $imageDefinitionExists -eq 0 ]]; then |
| 62 | + echo "Could not find image-definition \"$GALLERY_IMAGE_DEFINITION\". Creating definition \"$GALLERY_IMAGE_DEFINITION\" in gallery \"$GALLERY_NAME\"..." |
| 63 | + az sig image-definition create \ |
| 64 | + --gallery-image-definition "$GALLERY_IMAGE_DEFINITION" \ |
| 65 | + --publisher "$PUBLISHER" \ |
| 66 | + --offer "$OFFER" \ |
| 67 | + --sku "$GALLERY_IMAGE_DEFINITION" \ |
| 68 | + --gallery-name "$GALLERY_NAME" \ |
| 69 | + --resource-group "$RESOURCE_GROUP_NAME" \ |
| 70 | + --location "$LOCATION" \ |
| 71 | + --os-type Linux |
| 72 | +fi |
| 73 | + |
| 74 | +image_version="$(increment-version "$(get-image-version)")" |
| 75 | + |
| 76 | +# Convert comma-separated regions to JSON array for bicep template |
| 77 | +# Note: Using a single region for now |
| 78 | +REGIONS_JSON=$(echo "$LOCATION" | awk -F, '{ |
| 79 | + printf "["; |
| 80 | + for(i=1;i<=NF;i++) { |
| 81 | + printf "\"%s\"", $i; |
| 82 | + if(i<NF) printf ","; |
| 83 | + } |
| 84 | + printf "]"; |
| 85 | +}') |
| 86 | +# Create Image Version from storage account blob |
| 87 | +az deployment group create \ |
| 88 | + --name "$GALLERY_IMAGE_DEFINITION-$image_version" \ |
| 89 | + --resource-group "$RESOURCE_GROUP_NAME" \ |
| 90 | + --template-file "$SCRIPTS_DIR/azure-gallery-image-base.bicep" \ |
| 91 | + --parameters galleryName="$GALLERY_NAME" \ |
| 92 | + imageDefinitionName="$GALLERY_IMAGE_DEFINITION" \ |
| 93 | + versionName="$image_version" \ |
| 94 | + location="$LOCATION" \ |
| 95 | + regions="$REGIONS_JSON" \ |
| 96 | + sourceStorageAccountId="$storage_account_resource_id" \ |
| 97 | + sourceVhdUri="$storage_blob_endpoint" \ |
| 98 | + replicationMode="$replicationMode" |
0 commit comments