Skip to content

Commit

Permalink
Merge pull request #183 from CircleCI-Public/implement-dockerLabels
Browse files Browse the repository at this point in the history
feat: implement container-docker-label-updates
  • Loading branch information
brivu authored Aug 19, 2022
2 parents a00014f + 85ec791 commit 05ae702
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 9 deletions.
18 changes: 14 additions & 4 deletions src/commands/update-service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,26 @@ 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=<container-name>,name=<env-var-name>,valueFrom=<env-var-value>,container=...,name=...,valueFrom=...,
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-name>,<key>=<env-var-name>,<key>=<env-var-value>,container=...,<key>=...,<key>=...,
Values should not contain commas.
type: string
default: ''
force-new-deployment:
description: |
Whether to force a new deployment of the service.
Expand Down Expand Up @@ -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 >>
Expand Down
12 changes: 12 additions & 0 deletions src/commands/update-task-definition.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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-name>,<key>=<env-var-name>,<key>=<env-var-value>,container=...,<key>=...,<key>=...,
Values should not contain commas.
type: string
default: ''
profile-name:
description: AWS profile name to be configured.
type: string
Expand All @@ -66,6 +77,7 @@ steps:
ECS_PARAM_PROFILE_NAME: <<parameters.profile-name>>
ECS_PARAM_PREVIOUS_REVISION_NUMBER: <<parameters.previous-revision-number>>
ECS_PARAM_CONTAINER_SECRET_UPDATES: <<parameters.container-secret-updates>>
ECS_PARAM_CONTAINER_DOCKER_LABEL_UPDATES: << parameters.container-docker-label-updates >>
- run:
name: Register new task definition
command: <<include(scripts/register-new-task-def.sh)>>
Expand Down
12 changes: 12 additions & 0 deletions src/jobs/deploy-service-update.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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-name>,<key>=<env-var-name>,<key>=<env-var-value>,container=...,<key>=...,<key>=...,
Values should not contain commas.
type: string
default: ''
force-new-deployment:
description: |
Whether to force a new deployment of the service.
Expand Down Expand Up @@ -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 >>
Expand Down
14 changes: 13 additions & 1 deletion src/jobs/update-task-definition.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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-name>,<key>=<env-var-name>,<key>=<env-var-value>,container=...,<key>=...,<key>=...,
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.
Expand All @@ -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: <<parameters.deploy-scheduled-task>>
steps:
Expand Down
3 changes: 2 additions & 1 deletion src/scripts/get-prev-task.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -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

Expand Down
37 changes: 34 additions & 3 deletions src/scripts/update_container_defs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)

0 comments on commit 05ae702

Please sign in to comment.