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..6f2fe0dd 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..5a1b1375 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,9 @@ 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 >> + profile-name: << parameters.profile-name >> - when: condition: <> steps: 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 diff --git a/src/scripts/update_container_defs.py b/src/scripts/update_container_defs.py index 2810c547..f33ae4cf 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,38 @@ 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 + 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 +174,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)