Skip to content

Commit

Permalink
TES-277: Fixes in the SIG image definition creation script
Browse files Browse the repository at this point in the history
- The `gallery_subscription_id` is now a required variable
- Fixed `create_image_definition.sh` to use `gallery_subscription_id`
  • Loading branch information
mihailradkov committed Feb 16, 2024
1 parent 4a94601 commit 4b86754
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 34 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

All notable changes to the Packer template for creating GraphDB Azure VM images will be documented in this file.

## 1.2.0

### Changes

- The `gallery_subscription_id` is now a required variable instead of defaulting to `subscription_id`.

### Fixes

- Fixed [create_image_definition.sh](create_image_definition.sh) to use `gallery_subscription_id`

## 1.1.0

- Installed Azure CLI
Expand Down
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,20 @@ Follow these steps to build an AMI for GraphDB using Packer:
The Packer configuration allows you to customize various parameters, such as the GraphDB version, Azure build and
replication regions, subscription, client and tenant IDs. To do so, create a variables file `variables.pkrvars.hcl`,
example file:
```bash
subscription_id = "<your_azure_subscription_id>"
tenant_id = "<your_azure_tenant_id>"
client_id = "<your_azure_service_principal_id>"
client_secret = "<your_azure_service_principal_secret>"
build_location = "East US"
graphdb_version = "10.4.0"
gallery_image_definition = "10.4.0-x86_64"
gallery_resource_group = "Packer-RG"
gallery_name = "GraphDB"
gallery_image_replication_regions = ["North Europe", "UK South"]
build_allowed_inbound_ip_addresses = "<your_public_IP_address>"
```
```bash
subscription_id = "<your_azure_subscription_id>"
tenant_id = "<your_azure_tenant_id>"
client_id = "<your_azure_service_principal_id>"
client_secret = "<your_azure_service_principal_secret>"
build_location = "East US"
graphdb_version = "10.4.0"
gallery_subscription_id = "<your_azure_image_gallery_subscription_id>"
gallery_image_definition = "10.4.0-x86_64"
gallery_resource_group = "Packer-RG"
gallery_name = "GraphDB"
gallery_image_replication_regions = ["North Europe", "UK South"]
build_allowed_inbound_ip_addresses = "<your_public_IP_address>"
```

4. **Build the AMI**:

Expand Down Expand Up @@ -85,7 +86,7 @@ The following points can be customized in a packer variables file `variables.pkr

**Subscription Configuration**

* `subscription_id` (string): Your Azure subscription ID.
* `subscription_id` (string): Your Azure subscription ID. VM images will be built in this subscription.
* `tenant_id` (string): Your Azure Active Directory tenant ID.
* `client_id` (string): The client ID (Service Principal ID) used for authentication.
* `client_secret` (string): The client secret (Service Principal Secret) used for authentication.
Expand Down Expand Up @@ -116,8 +117,7 @@ The following points can be customized in a packer variables file `variables.pkr

**Gallery Configuration**

* `gallery_subscription_id` (string): ID of the subscription where the Shared Image Gallery is located. Will use subscription_id as default if
unspecified
* `gallery_subscription_id` (string): ID of the subscription where the Shared Image Gallery is located. Can be the same as `subscription_id`.
* `gallery_resource_group` (string): The resource group where the image gallery is located.
* `gallery_name` (string): The name of the image gallery.
* `gallery_image_definition` (string): The name of the x86_64 image to use.
Expand Down
5 changes: 2 additions & 3 deletions azure.pkr.hcl
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
locals {
timestamp = regex_replace(timestamp(), "[- TZ:]", "")
# See https://github.com/hashicorp/packer-plugin-azure/issues/65
version_timestamp = formatdate("YYYY.MM.DD", timestamp())
gallery_subscription_id = var.gallery_subscription_id != null ? var.gallery_subscription_id : var.subscription_id
version_timestamp = formatdate("YYYY.MM.DD", timestamp())
}

source azure-arm ubuntu-x86-64 {
Expand All @@ -21,7 +20,7 @@ source azure-arm ubuntu-x86-64 {
image_sku = var.base_image_sku

shared_image_gallery_destination {
subscription = local.gallery_subscription_id
subscription = var.gallery_subscription_id
resource_group = var.gallery_resource_group
gallery_name = var.gallery_name
image_name = var.gallery_image_definition
Expand Down
37 changes: 24 additions & 13 deletions create_image_definition.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,43 +20,54 @@ if ! [ -f "$variables_file" ]; then
fi

# Extracts required variables from the variables_file
subscription_id=$(grep 'subscription_id' "$variables_file" | cut -d '"' -f 2)
echo "Subscription ID: $subscription_id"
gallery_subscription_id=$(grep 'gallery_subscription_id' "$variables_file" | cut -d '"' -f 2)
echo "Subscription ID: $gallery_subscription_id"

gallery_image_definition=$(grep 'gallery_image_definition' "$variables_file" | cut -d '"' -f 2)
echo "Image definition name: $gallery_image_definition"

graphdb_version=$(grep 'graphdb_version' "$variables_file" | cut -d '"' -f 2)
echo "GraphDB version: $graphdb_version"

gallery_resource_group=$(grep 'gallery_resource_group' "$variables_file" | cut -d '"' -f 2)
echo "Resource group: $gallery_resource_group"

gallery_name=$(grep 'gallery_name' "$variables_file" | cut -d '"' -f 2)
echo "Gallery: $gallery_name"

# Checks if any of the required variables is empty
if [ -z "$gallery_image_definition" ] || [ -z "$graphdb_version" ] || [ -z "$gallery_resource_group" ] || [ -z "$gallery_name" ] || [ -z "$subscription_id" ]; then
if [ -z "$gallery_image_definition" ] || [ -z "$graphdb_version" ] || [ -z "$gallery_resource_group" ] || [ -z "$gallery_name" ] || [ -z "$gallery_subscription_id" ]; then
echo "One or more required variables are not defined in $variables_file."
exit 1
fi

# Constructs the az sig image-definition create command
az_command="az sig image-definition create \
--subscription $subscription_id \
-g $gallery_resource_group \
--gallery-name $gallery_name \
--gallery-image-definition "$gallery_image_definition" \
--subscription \"$gallery_subscription_id\" \
--resource-group \"$gallery_resource_group\" \
--gallery-name \"$gallery_name\" \
--gallery-image-definition \"$gallery_image_definition\" \
--publisher Ontotext \
--offer GraphDB \
--sku "$graphdb_version" \
--sku \"$graphdb_version\" \
--os-type Linux \
--hyper-v-generation v2 \
--minimum-cpu-core 4 \
--maximum-cpu-core 64 \
--minimum-memory 4 \
--maximum-memory 128 "

# TODO: defuk is this :
echo "Extracted variables and constructed Azure CLI command:"
echo "Creating SIG"
echo "$az_command"

echo "Creating SIG image definition"
eval "$az_command"
# Waits for the Shared Image Gallery to be created
az sig image-definition wait -i "$gallery_image_definition" -r "$gallery_name" -g "$gallery_resource_group" --created --subscription $subscription_id
echo "Begin building of the Azure VM image"

echo "Waiting for the SIG image definition creation to complete"
az sig image-definition wait \
--subscription "$gallery_subscription_id" \
--resource-group "$gallery_resource_group" \
--gallery-name "$gallery_name" \
--gallery-image-definition "$gallery_image_definition" \
--created
echo "SUCCESS: SIG image definition created"
3 changes: 1 addition & 2 deletions variables.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,8 @@ variable base_image_sku {
########################################

variable gallery_subscription_id {
description = "ID of the subscription where the Shared Image Gallery is located. Will use subscription_id as default if unspecified."
description = "ID of the subscription where the Shared Image Gallery is located. Can be the same as subscription_id."
type = string
default = null
}

variable gallery_resource_group {
Expand Down

0 comments on commit 4b86754

Please sign in to comment.