1
1
#! /bin/sh
2
2
3
-
4
3
VERSION=" 0.1.0"
4
+ VERBOSE=false
5
5
AWS_CLI=$( which aws)
6
6
AWS_ECS=" $AWS_CLI --output json ecs"
7
7
AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION:- eu-west-1}
@@ -11,22 +11,34 @@ AWS_ASSUME_ROLE=true
11
11
function usage() {
12
12
cat << EOM
13
13
-- ecs-cf-deploy --
14
+
14
15
Simple script to force a deployement of an ECS service based on a clouformation stack.
15
16
16
17
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
18
+ -c | --cluster-name Name of cloudformation stack containing the ecs cluster to deploy
19
+ -s | --service-name Name of cloudformation stack containing the service to deploy
20
+ -a | --account ID of the account to assume the role
21
+ -ar | --assume-role-name Name of the role to assume
22
+
23
+ Optional arguments:
24
+ -k | --aws-access-key AWS Access Key ID. May also be set as environment variable AWS_ACCESS_KEY_ID
25
+ -s | --aws-secret-key AWS Secret Access Key. May also be set as environment variable AWS_SECRET_ACCESS_KEY
26
+ -r | --region AWS Region Name. May also be set as environment variable AWS_DEFAULT_REGION or use the default ` eu-west-1`
27
+ -v | --verbose Display debugging information (basically each command)
23
28
24
29
Requirements:
25
30
aws: AWS Command Line Interface
26
31
jq: Command-line JSON processor
27
32
28
- EOM
33
+ Example:
34
+ ecs-cf-deploy -s example-api -c example-cluster -a 1234465780 -ar assumeRoleName -r us-west-1
29
35
36
+ Author:
37
+ Nicolas Ritouet <[email protected] >
38
+
39
+ License:
40
+ MIT (check LICENSE.md)
41
+ EOM
30
42
exit 3
31
43
}
32
44
@@ -56,15 +68,15 @@ function assumeRoleClean() {
56
68
}
57
69
58
70
function getClusterName() {
59
- export CLUSTER =$( $AWS_CLI cloudformation describe-stacks \
71
+ export ECS_CLUSTER_NAME =$( $AWS_CLI cloudformation describe-stacks \
60
72
--stack-name ${ECS_CLUSTER_STACK} \
61
73
--query ' Stacks[0].Outputs[?OutputKey==`Cluster`].OutputValue' \
62
74
--output text --region ${AWS_DEFAULT_REGION}
63
75
)
64
76
}
65
77
66
78
function getServiceName() {
67
- export SERVICE =$( $AWS_CLI cloudformation describe-stacks \
79
+ export ECS_SERVICE_NAME =$( $AWS_CLI cloudformation describe-stacks \
68
80
--stack-name ${ECS_SERVICE_STACK} \
69
81
--query ' Stacks[0].Outputs[?OutputKey==`Service`].OutputValue' \
70
82
--output text --region ${AWS_DEFAULT_REGION}
@@ -73,8 +85,10 @@ function getServiceName() {
73
85
74
86
75
87
function updateService() {
76
- echo " Deploying on cluster $CLUSTER service $SERVICE "
77
- ` $AWS_ECS update-service --cluster $CLUSTER --service $SERVICE --force-new-deployment`
88
+ if [ $VERBOSE == true ]; then
89
+ echo " Deploying on cluster '$ECS_CLUSTER_NAME ' service '$ECS_SERVICE_NAME '" ;
90
+ fi
91
+ $AWS_ECS update-service --cluster $ECS_CLUSTER_NAME --service $ECS_SERVICE_NAME --force-new-deployment
78
92
}
79
93
80
94
@@ -97,14 +111,18 @@ function assertRequiredArgumentsSet() {
97
111
AWS_ECS=" $AWS_ECS --profile $AWS_PROFILE "
98
112
fi
99
113
100
- if [ $ ECS_SERVICE_STACK == false ]; then
114
+ if [ -z " ${ ECS_SERVICE_STACK:- } " ]; then
101
115
echo " SERVICE is required. You can pass the value using -s or --service-name"
102
116
exit 5
103
117
fi
104
- if [ $ECS_SERVICE_STACK != false ] && [ $ ECS_CLUSTER_STACK == false ]; then
118
+ if [ ! -z " ${ECS_SERVICE_STACK :- } " ] && [ -z " ${ ECS_CLUSTER_STACK:- } " ]; then
105
119
echo " CLUSTER is required. You can pass the value using -c or --cluster"
106
120
exit 6
107
121
fi
122
+ if [ -z " ${ACCOUNT_ID:- } " ]; then
123
+ echo " ACCOUNT is required. You can pass the value using -a or --account"
124
+ exit 6
125
+ fi
108
126
109
127
}
110
128
130
148
key=" $1 "
131
149
132
150
case $key in
133
- -k |--aws-access-key )
134
- AWS_ACCESS_KEY_ID =" $2 "
151
+ -c |--cluster-name )
152
+ ECS_CLUSTER_STACK =" $2 "
135
153
shift # past argument
136
154
;;
137
- -s|--aws-secret-key )
138
- AWS_SECRET_ACCESS_KEY =" $2 "
155
+ -s|--service-name )
156
+ ECS_SERVICE_STACK =" $2 "
139
157
shift # past argument
140
158
;;
141
- -r |--region )
142
- AWS_DEFAULT_REGION =" $2 "
159
+ -ar |--assume-role-name )
160
+ CROSS_ACCOUNT_ROLE =" $2 "
143
161
shift # past argument
144
162
;;
145
- -p |--profile )
146
- AWS_PROFILE =" $2 "
163
+ -a |--account )
164
+ ACCOUNT_ID =" $2 "
147
165
shift # past argument
148
166
;;
149
- -- aws-instance-profile )
150
- echo " --aws-instance-profile is not yet in use "
151
- AWS_IAM_ROLE=true
167
+ -k|-- aws-access-key )
168
+ AWS_ACCESS_KEY_ID= " $2 "
169
+ shift # past argument
152
170
;;
153
- -c |--cluster-name )
154
- ECS_CLUSTER_STACK =" $2 "
155
- shift
171
+ -s |--aws-secret-key )
172
+ AWS_SECRET_ACCESS_KEY =" $2 "
173
+ shift # past argument
156
174
;;
157
- -s |--service-name )
158
- ECS_SERVICE_STACK =" $2 "
175
+ -r |--region )
176
+ AWS_DEFAULT_REGION =" $2 "
159
177
shift # past argument
160
178
;;
161
179
-v|--verbose)
166
184
exit 0
167
185
;;
168
186
* )
187
+ echo " Found nothing, will display usage and exit"
169
188
usage
170
189
exit 2
171
190
;;
@@ -184,6 +203,8 @@ if [[ "$AWS_ASSUME_ROLE" != false ]]; then
184
203
assumeRole
185
204
fi
186
205
206
+ getClusterName
207
+ getServiceName
187
208
updateService
188
209
189
210
0 commit comments