1
+ #! /bin/sh
2
+
3
+
4
+ VERSION=" 0.1.0"
5
+ AWS_CLI=$( which aws)
6
+ AWS_ECS=" $AWS_CLI --output json ecs"
7
+ AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION:- eu-west-1}
8
+ AWS_ASSUME_ROLE=true
9
+
10
+
11
+ function usage() {
12
+ cat << EOM
13
+ -- ecs-cf-deploy --
14
+ Simple script to force a deployement of an ECS service based on a clouformation stack.
15
+
16
+ Required arguments:
17
+ -c | --cluster-name Name of Name of cloudformation stack containing the ecs cluster to deploy
18
+ -s | --service-name Name of cloudformation stack containing the service to deploy
19
+ -k | --aws-access-key AWS Access Key ID. May also be set as environment variable AWS_ACCESS_KEY_ID
20
+ -s | --aws-secret-key AWS Secret Access Key. May also be set as environment variable AWS_SECRET_ACCESS_KEY
21
+ -r | --region AWS Region Name. May also be set as environment variable AWS_DEFAULT_REGION
22
+ -p | --profile AWS Profile to use - If you set this aws-access-key, aws-secret-key and region are needed
23
+
24
+ Requirements:
25
+ aws: AWS Command Line Interface
26
+ jq: Command-line JSON processor
27
+
28
+ EOM
29
+
30
+ exit 3
31
+ }
32
+
33
+
34
+ # Check requirements
35
+ function require() {
36
+ command -v " $1 " > /dev/null 2>&1 || {
37
+ echo " Some of the required software is not installed:"
38
+ echo " please install $1 " >&2 ;
39
+ exit 4;
40
+ }
41
+ }
42
+
43
+ function assumeRole() {
44
+
45
+ temp_role=$( $AWS_CLI sts assume-role --role-arn " arn:aws:iam::${ACCOUNT_ID} :role/${CROSS_ACCOUNT_ROLE} " --role-session-name " $( date +" %s" ) " )
46
+
47
+ export AWS_ACCESS_KEY_ID=$( echo $temp_role | jq .Credentials.AccessKeyId | xargs)
48
+ export AWS_SECRET_ACCESS_KEY=$( echo $temp_role | jq .Credentials.SecretAccessKey | xargs)
49
+ export AWS_SESSION_TOKEN=$( echo $temp_role | jq .Credentials.SessionToken | xargs)
50
+ }
51
+
52
+ function assumeRoleClean() {
53
+ unset AWS_ACCESS_KEY_ID
54
+ unset AWS_SECRET_ACCESS_KEY
55
+ unset AWS_SESSION_TOKEN
56
+ }
57
+
58
+ function getClusterName() {
59
+ export CLUSTER=$( $AWS_CLI cloudformation describe-stacks \
60
+ --stack-name ${ECS_CLUSTER_STACK} \
61
+ --query ' Stacks[0].Outputs[?OutputKey==`Cluster`].OutputValue' \
62
+ --output text --region ${AWS_DEFAULT_REGION}
63
+ )
64
+ }
65
+
66
+ function getServiceName() {
67
+ export SERVICE=$( $AWS_CLI cloudformation describe-stacks \
68
+ --stack-name ${ECS_SERVICE_STACK} \
69
+ --query ' Stacks[0].Outputs[?OutputKey==`Service`].OutputValue' \
70
+ --output text --region ${AWS_DEFAULT_REGION}
71
+ )
72
+ }
73
+
74
+
75
+ function updateService() {
76
+ echo " Deploying on cluster $CLUSTER service $SERVICE "
77
+ ` $AWS_ECS update-service --cluster $CLUSTER --service $SERVICE --force-new-deployment`
78
+ }
79
+
80
+
81
+
82
+
83
+ # Check that all required variables/combinations are set
84
+ function assertRequiredArgumentsSet() {
85
+
86
+ # AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_DEFAULT_REGION and AWS_PROFILE can be set as environment variables
87
+ if [ -z ${AWS_ACCESS_KEY_ID+x} ]; then unset AWS_ACCESS_KEY_ID; fi
88
+ if [ -z ${AWS_SECRET_ACCESS_KEY+x} ]; then unset AWS_SECRET_ACCESS_KEY; fi
89
+ if [ -z ${AWS_DEFAULT_REGION+x} ];
90
+ then unset AWS_DEFAULT_REGION
91
+ else
92
+ AWS_ECS=" $AWS_ECS --region $AWS_DEFAULT_REGION "
93
+ fi
94
+ if [ -z ${AWS_PROFILE+x} ];
95
+ then unset AWS_PROFILE
96
+ else
97
+ AWS_ECS=" $AWS_ECS --profile $AWS_PROFILE "
98
+ fi
99
+
100
+ if [ $ECS_SERVICE_STACK == false ]; then
101
+ echo " SERVICE is required. You can pass the value using -s or --service-name"
102
+ exit 5
103
+ fi
104
+ if [ $ECS_SERVICE_STACK != false ] && [ $ECS_CLUSTER_STACK == false ]; then
105
+ echo " CLUSTER is required. You can pass the value using -c or --cluster"
106
+ exit 6
107
+ fi
108
+
109
+ }
110
+
111
+ # #####################################################
112
+ # When not being tested, run application as expected #
113
+ # #####################################################
114
+ set -o errexit
115
+ set -o pipefail
116
+ set -u
117
+ set -e
118
+
119
+ # If no args are provided, display usage information
120
+ if [ $# == 0 ]; then usage; fi
121
+
122
+ # Check for AWS, AWS Command Line Interface
123
+ require aws
124
+ # Check for jq, Command-line JSON processor
125
+ require jq
126
+
127
+ # Loop through arguments, two at a time for key and value
128
+ while [[ $# -gt 0 ]]
129
+ do
130
+ key=" $1 "
131
+
132
+ case $key in
133
+ -k|--aws-access-key)
134
+ AWS_ACCESS_KEY_ID=" $2 "
135
+ shift # past argument
136
+ ;;
137
+ -s|--aws-secret-key)
138
+ AWS_SECRET_ACCESS_KEY=" $2 "
139
+ shift # past argument
140
+ ;;
141
+ -r|--region)
142
+ AWS_DEFAULT_REGION=" $2 "
143
+ shift # past argument
144
+ ;;
145
+ -p|--profile)
146
+ AWS_PROFILE=" $2 "
147
+ shift # past argument
148
+ ;;
149
+ --aws-instance-profile)
150
+ echo " --aws-instance-profile is not yet in use"
151
+ AWS_IAM_ROLE=true
152
+ ;;
153
+ -c|--cluster-name)
154
+ ECS_CLUSTER_STACK=" $2 "
155
+ shift
156
+ ;;
157
+ -s|--service-name)
158
+ ECS_SERVICE_STACK=" $2 "
159
+ shift # past argument
160
+ ;;
161
+ -v|--verbose)
162
+ VERBOSE=true
163
+ ;;
164
+ --version)
165
+ echo ${VERSION}
166
+ exit 0
167
+ ;;
168
+ * )
169
+ usage
170
+ exit 2
171
+ ;;
172
+ esac
173
+ shift # past argument or value
174
+ done
175
+
176
+ if [ $VERBOSE == true ]; then
177
+ set -x
178
+ fi
179
+
180
+ # Check that required arguments are provided
181
+ assertRequiredArgumentsSet
182
+
183
+ if [[ " $AWS_ASSUME_ROLE " != false ]]; then
184
+ assumeRole
185
+ fi
186
+
187
+ updateService
188
+
189
+
190
+ if [[ " $AWS_ASSUME_ROLE " != false ]]; then
191
+ assumeRoleClean
192
+ fi
193
+
194
+ exit 0
195
+
196
+ # ############################
197
+ # End application run logic #
198
+ # ############################
0 commit comments