Skip to content

Commit

Permalink
Merge pull request silinternational#139 from silinternational/develop
Browse files Browse the repository at this point in the history
Release 3.3.0 - Fargate and placementConstraints support
  • Loading branch information
fillup authored Apr 2, 2018
2 parents ac2b53c + 1105a0d commit 5471225
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 6 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Usage
Script will only perform deregistration if deployment succeeds.
--enable-rollback Rollback task definition if new version is not running before TIMEOUT
-v | --verbose Verbose output
--version Display the version

Examples:
Simple (Using env vars for AWS settings):
Expand All @@ -50,6 +51,19 @@ Usage
Notes:
- If a tag is not found in image and an ENV var is not used via -e, it will default the tag to "latest"

Installation
------------

* Install and configure [aws-cli](http://docs.aws.amazon.com/cli/latest/userguide/tutorial-ec2-ubuntu.html#install-cli)
* Install [jq](https://github.com/stedolan/jq/wiki/Installation)
* Install ecs-deploy:
```
curl https://raw.githubusercontent.com/silinternational/ecs-deploy/master/ecs-deploy | sudo tee -a /usr/bin/ecs-deploy
sudo chmod +x /usr/bin/ecs-deploy
```


How it works
------------

Expand Down
19 changes: 16 additions & 3 deletions ecs-deploy
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env bash

# Setup default values for variables
VERSION="3.3.0"
CLUSTER=false
SERVICE=false
TASK_DEFINITION=false
Expand Down Expand Up @@ -48,6 +49,7 @@ Optional arguments:
--max-definitions Number of Task Definition Revisions to persist before deregistering oldest revisions.
--enable-rollback Rollback task definition if new version is not running before TIMEOUT
-v | --verbose Verbose output
--version Display the version
Requirements:
aws: AWS Command Line Interface
Expand Down Expand Up @@ -108,11 +110,11 @@ function assertRequiredArgumentsSet() {
fi

if [ $SERVICE == false ] && [ $TASK_DEFINITION == false ]; then
echo "One of SERVICE or TASK DEFINITON is required. You can pass the value using -n / --service-name for a service, or -d / --task-definition for a task"
echo "One of SERVICE or TASK DEFINITION is required. You can pass the value using -n / --service-name for a service, or -d / --task-definition for a task"
exit 5
fi
if [ $SERVICE != false ] && [ $TASK_DEFINITION != false ]; then
echo "Only one of SERVICE or TASK DEFINITON may be specified, but you supplied both"
echo "Only one of SERVICE or TASK DEFINITION may be specified, but you supplied both"
exit 6
fi
if [ $SERVICE != false ] && [ $CLUSTER == false ]; then
Expand Down Expand Up @@ -261,7 +263,7 @@ function createNewTaskDefJson() {
fi

# Default JQ filter for new task definition
NEW_DEF_JQ_FILTER="family: .family, volumes: .volumes, containerDefinitions: .containerDefinitions"
NEW_DEF_JQ_FILTER="family: .family, volumes: .volumes, containerDefinitions: .containerDefinitions, placementConstraints: .placementConstraints"

# Some options in task definition should only be included in new definition if present in
# current definition. If found in current definition, append to JQ filter.
Expand All @@ -273,6 +275,13 @@ function createNewTaskDefJson() {
fi
done

# Updated jq filters for AWS Fargate
REQUIRES_COMPATIBILITIES=$(echo "${DEF}" | jq -r '. | select(.requiresCompatibilities != null) | .requiresCompatibilities[]')
if [[ "${REQUIRES_COMPATIBILITIES}" == 'FARGATE' ]]; then
FARGATE_JQ_FILTER='executionRoleArn: .executionRoleArn, requiresCompatibilities: .requiresCompatibilities, cpu: .cpu, memory: .memory'
NEW_DEF_JQ_FILTER="${NEW_DEF_JQ_FILTER}, ${FARGATE_JQ_FILTER}"
fi

# Build new DEF with jq filter
NEW_DEF=$(echo $DEF | jq "{${NEW_DEF_JQ_FILTER}}")

Expand Down Expand Up @@ -496,6 +505,10 @@ if [ "$BASH_SOURCE" == "$0" ]; then
-v|--verbose)
VERBOSE=true
;;
--version)
echo ${VERSION}
exit 0
;;
*)
usage
exit 2
Expand Down
75 changes: 72 additions & 3 deletions test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,79 @@ setup() {
"volumesFrom": []
}
],
"placementConstraints": null,
"revision": 123
}
}
EOF
)
expected='{ "family": "app-task-def", "volumes": [], "containerDefinitions": [ { "environment": [ { "name": "KEY", "value": "value" } ], "name": "API", "links": [], "mountPoints": [], "image": "121212345678.dkr.ecr.us-east-1.amazonaws.com/acct/repo:1111111111", "essential": true, "portMappings": [ { "protocol": "tcp", "containerPort": 80, "hostPort": 10080 } ], "entryPoint": [], "memory": 128, "command": [ "/data/run.sh" ], "cpu": 200, "volumesFrom": [] } ], "networkMode": "bridge" }'
expected='{ "family": "app-task-def", "volumes": [], "containerDefinitions": [ { "environment": [ { "name": "KEY", "value": "value" } ], "name": "API", "links": [], "mountPoints": [], "image": "121212345678.dkr.ecr.us-east-1.amazonaws.com/acct/repo:1111111111", "essential": true, "portMappings": [ { "protocol": "tcp", "containerPort": 80, "hostPort": 10080 } ], "entryPoint": [], "memory": 128, "command": [ "/data/run.sh" ], "cpu": 200, "volumesFrom": [] } ], "placementConstraints": null, "networkMode": "bridge" }'
run createNewTaskDefJson
[ ! -z $status ]
[ $output == $expected ]
}

@test "test createNewTaskDefJson with single container in definition for AWS Fargate" {
imageWithoutTag="121212345678.dkr.ecr.us-east-1.amazonaws.com/acct/repo"
useImage="121212345678.dkr.ecr.us-east-1.amazonaws.com/acct/repo:1111111111"
TASK_DEFINITION=$(cat <<EOF
{
"taskDefinition": {
"status": "ACTIVE",
"networkMode": "awsvpc",
"family": "app-task-def",
"requiresAttributes": [
{
"name": "com.amazonaws.ecs.capability.ecr-auth"
}
],
"volumes": [],
"taskDefinitionArn": "arn:aws:ecs:us-east-1:121212345678:task-definition/app-task-def:123",
"containerDefinitions": [
{
"environment": [
{
"name": "KEY",
"value": "value"
}
],
"name": "API",
"links": [],
"mountPoints": [],
"image": "121212345678.dkr.ecr.us-east-1.amazonaws.com/acct/repo:1487623908",
"essential": true,
"portMappings": [
{
"protocol": "tcp",
"containerPort": 80,
"hostPort": 10080
}
],
"entryPoint": [],
"memory": 128,
"command": [
"/data/run.sh"
],
"cpu": 200,
"volumesFrom": []
}
],
"revision": 123,
"executionRoleArn": "arn:aws:iam::121212345678:role/ecsTaskExecutionRole",
"compatibilities": [
"EC2",
"FARGATE"
],
"requiresCompatibilities": [
"FARGATE"
],
"cpu": "256",
"memory": "512"
}
}
EOF
)
expected='{ "family": "app-task-def", "volumes": [], "containerDefinitions": [ { "environment": [ { "name": "KEY", "value": "value" } ], "name": "API", "links": [], "mountPoints": [], "image": "121212345678.dkr.ecr.us-east-1.amazonaws.com/acct/repo:1111111111", "essential": true, "portMappings": [ { "protocol": "tcp", "containerPort": 80, "hostPort": 10080 } ], "entryPoint": [], "memory": 128, "command": [ "/data/run.sh" ], "cpu": 200, "volumesFrom": [] } ], "placementConstraints": null, "networkMode": "awsvpc", "executionRoleArn": "arn:aws:iam::121212345678:role/ecsTaskExecutionRole", "requiresCompatibilities": [ "FARGATE" ], "cpu": "256", "memory": "512" }'
run createNewTaskDefJson
[ ! -z $status ]
[ $output == $expected ]
Expand Down Expand Up @@ -352,12 +419,13 @@ EOF
"volumesFrom": []
}
],
"placementConstraints": null,
"revision": 123
}
}
EOF
)
expected='{ "family": "app-task-def", "volumes": [], "containerDefinitions": [ { "environment": [ { "name": "KEY", "value": "value" } ], "name": "API", "links": [], "mountPoints": [], "image": "121212345678.dkr.ecr.us-east-1.amazonaws.com/acct/repo:1111111111", "essential": true, "portMappings": [ { "protocol": "tcp", "containerPort": 80, "hostPort": 10080 } ], "entryPoint": [], "memory": 128, "command": [ "/data/run.sh" ], "cpu": 200, "volumesFrom": [] }, { "environment": [ { "name": "KEY", "value": "value" } ], "name": "cache", "links": [], "mountPoints": [], "image": "redis:latest", "essential": true, "portMappings": [ { "protocol": "tcp", "containerPort": 6376, "hostPort": 10376 } ], "entryPoint": [], "memory": 128, "command": [ "/data/run.sh" ], "cpu": 200, "volumesFrom": [] } ], "networkMode": "bridge" }'
expected='{ "family": "app-task-def", "volumes": [], "containerDefinitions": [ { "environment": [ { "name": "KEY", "value": "value" } ], "name": "API", "links": [], "mountPoints": [], "image": "121212345678.dkr.ecr.us-east-1.amazonaws.com/acct/repo:1111111111", "essential": true, "portMappings": [ { "protocol": "tcp", "containerPort": 80, "hostPort": 10080 } ], "entryPoint": [], "memory": 128, "command": [ "/data/run.sh" ], "cpu": 200, "volumesFrom": [] }, { "environment": [ { "name": "KEY", "value": "value" } ], "name": "cache", "links": [], "mountPoints": [], "image": "redis:latest", "essential": true, "portMappings": [ { "protocol": "tcp", "containerPort": 6376, "hostPort": 10376 } ], "entryPoint": [], "memory": 128, "command": [ "/data/run.sh" ], "cpu": 200, "volumesFrom": [] } ], "placementConstraints": null, "networkMode": "bridge" }'
run createNewTaskDefJson
[ ! -z $status ]
[ $output == $expected ]
Expand Down Expand Up @@ -448,12 +516,13 @@ EOF
"volumesFrom": []
}
],
"placementConstraints": null,
"revision": 123
}
}
EOF
)
expected='{ "family": "app-task-def", "volumes": [], "containerDefinitions": [ { "environment": [ { "name": "KEY", "value": "value" } ], "name": "API", "links": [], "mountPoints": [], "image": "121212345678.dkr.ecr.us-east-1.amazonaws.com/acct/repo:newtag", "essential": true, "portMappings": [ { "protocol": "tcp", "containerPort": 80, "hostPort": 10080 } ], "entryPoint": [], "memory": 128, "command": [ "/data/run.sh" ], "cpu": 200, "volumesFrom": [] }, { "environment": [ { "name": "KEY", "value": "value" } ], "name": "cache", "links": [], "mountPoints": [], "image": "redis:newtag", "essential": true, "portMappings": [ { "protocol": "tcp", "containerPort": 6376, "hostPort": 10376 } ], "entryPoint": [], "memory": 128, "command": [ "/data/run.sh" ], "cpu": 200, "volumesFrom": [] } ], "networkMode": "bridge" }'
expected='{ "family": "app-task-def", "volumes": [], "containerDefinitions": [ { "environment": [ { "name": "KEY", "value": "value" } ], "name": "API", "links": [], "mountPoints": [], "image": "121212345678.dkr.ecr.us-east-1.amazonaws.com/acct/repo:newtag", "essential": true, "portMappings": [ { "protocol": "tcp", "containerPort": 80, "hostPort": 10080 } ], "entryPoint": [], "memory": 128, "command": [ "/data/run.sh" ], "cpu": 200, "volumesFrom": [] }, { "environment": [ { "name": "KEY", "value": "value" } ], "name": "cache", "links": [], "mountPoints": [], "image": "redis:newtag", "essential": true, "portMappings": [ { "protocol": "tcp", "containerPort": 6376, "hostPort": 10376 } ], "entryPoint": [], "memory": 128, "command": [ "/data/run.sh" ], "cpu": 200, "volumesFrom": [] } ], "placementConstraints": null, "networkMode": "bridge" }'
run createNewTaskDefJson
echo $output
[ ! -z $status ]
Expand Down

0 comments on commit 5471225

Please sign in to comment.