From f875e13110e1c57e953e346b72eab29d3daf6790 Mon Sep 17 00:00:00 2001 From: Brian Vu Date: Fri, 19 Aug 2022 12:00:45 -0700 Subject: [PATCH 1/5] feat: added docker label parsing to python script --- src/scripts/update_container_defs.py | 39 +++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/src/scripts/update_container_defs.py b/src/scripts/update_container_defs.py index 2810c547..b1290faf 100644 --- a/src/scripts/update_container_defs.py +++ b/src/scripts/update_container_defs.py @@ -2,10 +2,9 @@ import sys import json - # shellcheck disable=SC1036 # Hold-over from previous iteration. def run(previous_task_definition, container_image_name_updates, - container_env_var_updates, container_secret_updates): + container_env_var_updates, container_secret_updates, container_docker_label_updates): try: definition = json.loads(previous_task_definition) container_definitions = definition['taskDefinition']['containerDefinitions'] @@ -61,6 +60,40 @@ def run(previous_task_definition, container_image_name_updates, except: raise Exception('Environment variable update parameter could not be processed; please check parameter value: ' + container_env_var_updates) + # Expected format: container=...,string=...,string=...,container=...,string=...,string= + + try: + docker_label_kv_pairs = container_docker_label_updates.split(',') + for index, kv_pair in enumerate(docker_label_kv_pairs): + kv = kv_pair.split('=') + key = kv[0].strip() + + if key == 'container': + container_name = kv[1].strip() + docker_label_kv = docker_label_kv_pairs[index+1].split('=') + docker_label_key = docker_label_kv[0].strip() + docker_label_value = docker_label_kv[1].strip() + container_entry = container_map.get(container_name) + if container_entry is None: + raise ValueError('The container ' + container_name + ' is not defined in the existing task definition') + container_index = container_entry['index'] + docker_label_entry = container_entry['environment_map'].get(docker_label_key) + if docker_label_entry is None: + # The existing container definition does not contain environment variables + if container_definitions[container_index].get('dockerLabels') is None: + container_definitions[container_index]['dockerLabels'] = {} + # This env var does not exist in the existing container definition + container_definitions[container_index]['dockerLabels'][docker_label_key] = docker_label_value + else: + docker_label_index = docker_label_entry['index'] + container_definitions[container_index]['dockerLabels'][docker_label_index][docker_label_key] = docker_label_value + # elif key and key not in ['container']: + # raise ValueError('Incorrect key found in environment variable update parameter: ' + key) + except ValueError as value_error: + raise value_error + except: + raise Exception('Docker label update parameter could not be processed; please check parameter value: ' + container_docker_label_updates) + # Expected format: container=...,name=...,valueFrom=...,container=...,name=...,valueFrom=... try: @@ -143,7 +176,7 @@ def run(previous_task_definition, container_image_name_updates, if __name__ == '__main__': try: - print(run(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])) + print(run(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5])) except Exception as e: sys.stderr.write(str(e) + "\n") exit(1) From 6a28dd18fd520a3e48df69c8f5f9d6a0868f91d7 Mon Sep 17 00:00:00 2001 From: Brian Vu Date: Fri, 19 Aug 2022 12:13:28 -0700 Subject: [PATCH 2/5] feat: added container-docker-label-updates parameter to commands and jobs --- src/commands/update-service.yml | 18 ++++++++++++++---- src/commands/update-task-definition.yml | 12 ++++++++++++ src/jobs/deploy-service-update.yml | 12 ++++++++++++ src/jobs/update-task-definition.yml | 13 ++++++++++++- 4 files changed, 50 insertions(+), 5 deletions(-) diff --git a/src/commands/update-service.yml b/src/commands/update-service.yml index 98d46452..188a5e50 100644 --- a/src/commands/update-service.yml +++ b/src/commands/update-service.yml @@ -71,10 +71,8 @@ parameters: default: '' container-secret-updates: description: > - Use this to update or set the values of secrets variables that will be - defined for the containers. - (Existing secrets variables not included in this parameter will not be - removed) + Use this to update or set the values of secret variables that will be defined for the containers. + (Existing secrets variables not included in this parameter will not beremoved) Expected format: container=,name=,valueFrom=,container=...,name=...,valueFrom=..., @@ -82,6 +80,17 @@ parameters: Values should not contain commas. type: string default: '' + container-docker-label-updates: + description: > + Use this to update or set the values of docker label variables that will be defined for the containers. + (Existing secrets variables not included in this parameter will not be removed) + + Expected format: + container=,=,=,container=...,=...,=..., + + Values should not contain commas. + type: string + default: '' force-new-deployment: description: | Whether to force a new deployment of the service. @@ -185,6 +194,7 @@ steps: container-image-name-updates: << parameters.container-image-name-updates >> container-env-var-updates: << parameters.container-env-var-updates >> container-secret-updates: << parameters.container-secret-updates >> + container-docker-label-updates: << parameters.container-docker-label-updates >> profile-name: << parameters.profile-name >> - when: condition: << parameters.skip-task-definition-registration >> diff --git a/src/commands/update-task-definition.yml b/src/commands/update-task-definition.yml index cdb12a95..f2fc7136 100644 --- a/src/commands/update-task-definition.yml +++ b/src/commands/update-task-definition.yml @@ -45,6 +45,17 @@ parameters: Values should not contain commas. type: string default: '' + container-docker-label-updates: + description: > + Use this to update or set the values of docker label variables that will be defined for the containers. + (Existing secrets variables not included in this parameter will not be removed) + + Expected format: + container=,=,=,container=...,=...,=..., + + Values should not contain commas. + type: string + default: '' profile-name: description: AWS profile name to be configured. type: string @@ -66,6 +77,7 @@ steps: ECS_PARAM_PROFILE_NAME: <> ECS_PARAM_PREVIOUS_REVISION_NUMBER: <> ECS_PARAM_CONTAINER_SECRET_UPDATES: <> + ECS_PARAM_CONTAINER_DOCKER_LABEL_UPDATES: << parameters.container-docker-label-updates >> - run: name: Register new task definition command: <> diff --git a/src/jobs/deploy-service-update.yml b/src/jobs/deploy-service-update.yml index fc820ab7..b65e01b2 100644 --- a/src/jobs/deploy-service-update.yml +++ b/src/jobs/deploy-service-update.yml @@ -126,6 +126,17 @@ parameters: Values should not contain commas. type: string default: '' + container-docker-label-updates: + description: > + Use this to update or set the values of docker label variables that will be defined for the containers. + (Existing secrets variables not included in this parameter will not be removed) + + Expected format: + container=,=,=,container=...,=...,=..., + + Values should not contain commas. + type: string + default: '' force-new-deployment: description: | Whether to force a new deployment of the service. @@ -242,6 +253,7 @@ steps: container-image-name-updates: << parameters.container-image-name-updates >> container-env-var-updates: << parameters.container-env-var-updates >> container-secret-updates: << parameters.container-secret-updates >> + container-docker-label-updates: << parameters.container-docker-label-updates >> force-new-deployment: << parameters.force-new-deployment >> verify-revision-is-deployed: << parameters.verify-revision-is-deployed >> max-poll-attempts: << parameters.max-poll-attempts >> diff --git a/src/jobs/update-task-definition.yml b/src/jobs/update-task-definition.yml index 4c4ff111..5e0ac97f 100644 --- a/src/jobs/update-task-definition.yml +++ b/src/jobs/update-task-definition.yml @@ -78,6 +78,17 @@ parameters: Values should not contain commas. type: string default: '' + container-docker-label-updates: + description: > + Use this to update or set the values of docker label variables that will be defined for the containers. + (Existing secrets variables not included in this parameter will not be removed) + + Expected format: + container=,=,=,container=...,=...,=..., + + Values should not contain commas. + type: string + default: '' deploy-scheduled-task: description: > Set this parameter to true to deploy updated task definition to a scheduled task rule. @@ -96,8 +107,8 @@ steps: family: << parameters.family >> container-image-name-updates: << parameters.container-image-name-updates >> container-env-var-updates: << parameters.container-env-var-updates >> - profile-name: << parameters.profile-name >> container-secret-updates: << parameters.container-secret-updates >> + container-docker-label-updates: << parameters.container-docker-label-updates >> - when: condition: <> steps: From 9b86c039da1f79738954fcd195b754c231927dc3 Mon Sep 17 00:00:00 2001 From: Brian Vu Date: Fri, 19 Aug 2022 12:15:37 -0700 Subject: [PATCH 3/5] feat: added ECS_PARAM_CONTAINER_DOCKER_LABEL_UPDATES to script --- src/scripts/get-prev-task.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/scripts/get-prev-task.sh b/src/scripts/get-prev-task.sh index 0437ae93..169100f3 100644 --- a/src/scripts/get-prev-task.sh +++ b/src/scripts/get-prev-task.sh @@ -6,6 +6,7 @@ ECS_PARAM_CONTAINER_IMAGE_NAME_UPDATES=$(eval echo "$ECS_PARAM_CONTAINER_IMAGE_N ECS_PARAM_CONTAINER_ENV_VAR_UPDATES=$(eval echo "$ECS_PARAM_CONTAINER_ENV_VAR_UPDATES") ECS_PARAM_PROFILE_NAME=$(eval echo "$ECS_PARAM_PROFILE_NAME") ECS_PARAM_CONTAINER_SECRET_UPDATES=$(eval echo "$ECS_PARAM_CONTAINER_SECRET_UPDATES") +ECS_PARAM_CONTAINER_DOCKER_LABEL_UPDATES=$(eval echo "$ECS_PARAM_CONTAINER_DOCKER_LABEL_UPDATES") if [ -n "${ECS_PARAM_PROFILE_NAME}" ]; then set -- "$@" --profile "${ECS_PARAM_PROFILE_NAME}" @@ -29,7 +30,7 @@ cat <<< "$ECS_SCRIPT_UPDATE_CONTAINER_DEFS" > "$UPDATE_CONTAINER_DEFS_SCRIPT_FIL # Prepare container definitions -CONTAINER_DEFS=$(python "$UPDATE_CONTAINER_DEFS_SCRIPT_FILE" "$PREVIOUS_TASK_DEFINITION" "$ECS_PARAM_CONTAINER_IMAGE_NAME_UPDATES" "$ECS_PARAM_CONTAINER_ENV_VAR_UPDATES" "$ECS_PARAM_CONTAINER_SECRET_UPDATES") +CONTAINER_DEFS=$(python "$UPDATE_CONTAINER_DEFS_SCRIPT_FILE" "$PREVIOUS_TASK_DEFINITION" "$ECS_PARAM_CONTAINER_IMAGE_NAME_UPDATES" "$ECS_PARAM_CONTAINER_ENV_VAR_UPDATES" "$ECS_PARAM_CONTAINER_SECRET_UPDATES" "$ECS_PARAM_CONTAINER_DOCKER_LABEL_UPDATES") # Escape single quotes from environment variables for BASH_ENV From d3eb2db718f96b50472c65524861d4b42e6076d2 Mon Sep 17 00:00:00 2001 From: Brian Vu Date: Fri, 19 Aug 2022 12:16:59 -0700 Subject: [PATCH 4/5] fix: corrected linting errors --- src/commands/update-task-definition.yml | 2 +- src/jobs/update-task-definition.yml | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/commands/update-task-definition.yml b/src/commands/update-task-definition.yml index f2fc7136..6f2fe0dd 100644 --- a/src/commands/update-task-definition.yml +++ b/src/commands/update-task-definition.yml @@ -55,7 +55,7 @@ parameters: Values should not contain commas. type: string - default: '' + default: '' profile-name: description: AWS profile name to be configured. type: string diff --git a/src/jobs/update-task-definition.yml b/src/jobs/update-task-definition.yml index 5e0ac97f..5a1b1375 100644 --- a/src/jobs/update-task-definition.yml +++ b/src/jobs/update-task-definition.yml @@ -109,6 +109,7 @@ steps: container-env-var-updates: << parameters.container-env-var-updates >> container-secret-updates: << parameters.container-secret-updates >> container-docker-label-updates: << parameters.container-docker-label-updates >> + profile-name: << parameters.profile-name >> - when: condition: <> steps: From 85ec791cbe402b33ecc50739a5e9cf601ccce322 Mon Sep 17 00:00:00 2001 From: Brian Vu Date: Fri, 19 Aug 2022 12:57:37 -0700 Subject: [PATCH 5/5] chore: removed comments --- src/scripts/update_container_defs.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/scripts/update_container_defs.py b/src/scripts/update_container_defs.py index b1290faf..f33ae4cf 100644 --- a/src/scripts/update_container_defs.py +++ b/src/scripts/update_container_defs.py @@ -87,8 +87,6 @@ def run(previous_task_definition, container_image_name_updates, else: docker_label_index = docker_label_entry['index'] container_definitions[container_index]['dockerLabels'][docker_label_index][docker_label_key] = docker_label_value - # elif key and key not in ['container']: - # raise ValueError('Incorrect key found in environment variable update parameter: ' + key) except ValueError as value_error: raise value_error except: