From 1295a656e2fe4e6772d02680f5d2f004ff518179 Mon Sep 17 00:00:00 2001 From: ThrawnCA Date: Thu, 5 Dec 2024 15:36:55 +1000 Subject: [PATCH 1/2] [QOLDEV-833] add the ability to reuse AMIs instead of creating new every time --- build-CKAN.sh | 15 ++++++++++++++- create-AMI.yml | 3 ++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/build-CKAN.sh b/build-CKAN.sh index c494048b..65843df5 100755 --- a/build-CKAN.sh +++ b/build-CKAN.sh @@ -45,8 +45,21 @@ run-deployment () { } create-amis () { - ANSIBLE_EXTRA_VARS="$ANSIBLE_EXTRA_VARS timestamp=`date -u +%Y-%m-%dT%H:%M:%SZ`" + run-playbook "AMI-templates.yml" "state=absent" + # try to match an existing image instead of creating a new one + if [ "$IMAGE_VERSION" = "" ]; then + IMAGE_VERSION=`git rev-parse HEAD` || true + fi + if [ "$IMAGE_VERSION" != "" ]; then + echo "Checking for existing $ENVIRONMENT $INSTANCE_NAME images tagged with version ${IMAGE_VERSION}..." + IMAGE_IDS=$(aws ec2 describe-images --filters "Name=tag:Environment,Values=$ENVIRONMENT" "Name=tag:Service,Values=$INSTANCE_NAME" "Name=tag:Version,Values=$IMAGE_VERSION" --query "Images[].ImageId" --output text) + if [ "$IMAGE_IDS" != "" ]; then + echo "Found existing image(s): $IMAGE_IDS" + return 0 + fi + fi run-playbook "AMI-templates.yml" + ANSIBLE_EXTRA_VARS="$ANSIBLE_EXTRA_VARS timestamp=`date -u +%Y-%m-%dT%H:%M:%SZ` image_version=$IMAGE_VERSION" run-playbook "create-AMI" "layer=Batch" & BATCH_PID=$! run-playbook "create-AMI" "layer=Web" & WEB_PID=$! run-playbook "create-AMI" "layer=Solr" diff --git a/create-AMI.yml b/create-AMI.yml index cee6e63a..c56e4dbe 100644 --- a/create-AMI.yml +++ b/create-AMI.yml @@ -95,9 +95,10 @@ Name: "{{ InstanceName }}-{{ timestamp | replace(':', '-') }}" Environment: "{{ Environment }}" Service: "{{ service_name }}" + Layer: "{{ layer | lower }}" Division: "{{ Division }}" Owner: "{{ Owner }}" - Version: "1.0" + Version: "{{ image_version | default('1.0') }}" register: new_image - name: Record AMI ID From 496ead49ab7f250f41c856951f189b22563f25d3 Mon Sep 17 00:00:00 2001 From: ThrawnCA Date: Thu, 5 Dec 2024 16:44:41 +1000 Subject: [PATCH 2/2] [QOLDEV-833] allow overwriting the SSM parameter identifying the image ID --- create-AMI.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/create-AMI.yml b/create-AMI.yml index c56e4dbe..68bb9df8 100644 --- a/create-AMI.yml +++ b/create-AMI.yml @@ -103,4 +103,4 @@ - name: Record AMI ID shell: | - aws ssm put-parameter --type String --name "/config/CKAN/{{ Environment }}/app/{{ service_name_lower }}/{{ layer }}AmiId" --value "{{ new_image.image_id }}" + aws ssm put-parameter --overwrite --type String --name "/config/CKAN/{{ Environment }}/app/{{ service_name_lower }}/{{ layer }}AmiId" --value "{{ new_image.image_id }}"