@@ -4,10 +4,9 @@ VERSION="0.1.0"
4
4
VERBOSE=false
5
5
AWS_CLI=$( which aws)
6
6
AWS_ECS=" $AWS_CLI --output json ecs"
7
- AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION:- eu-west-1}
7
+ AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION:= " eu-west-1" }
8
8
AWS_ASSUME_ROLE=true
9
9
10
-
11
10
function usage() {
12
11
cat << EOM
13
12
-- ecs-cf-deploy --
@@ -55,7 +54,7 @@ function require() {
55
54
function assumeRole() {
56
55
57
56
temp_role=$( $AWS_CLI sts assume-role --role-arn " arn:aws:iam::${ACCOUNT_ID} :role/${CROSS_ACCOUNT_ROLE} " --role-session-name " $( date +" %s" ) " )
58
-
57
+
59
58
export AWS_ACCESS_KEY_ID=$( echo $temp_role | jq .Credentials.AccessKeyId | xargs)
60
59
export AWS_SECRET_ACCESS_KEY=$( echo $temp_role | jq .Credentials.SecretAccessKey | xargs)
61
60
export AWS_SESSION_TOKEN=$( echo $temp_role | jq .Credentials.SessionToken | xargs)
@@ -73,6 +72,10 @@ function getClusterName() {
73
72
--query ' Stacks[0].Outputs[?OutputKey==`Cluster`].OutputValue' \
74
73
--output text --region ${AWS_DEFAULT_REGION}
75
74
)
75
+ if [ -z $ECS_CLUSTER_NAME ]; then
76
+ echo " Could not find the service under the '$ECS_CLUSTER_STACK ' stack"
77
+ exit 9
78
+ fi
76
79
}
77
80
78
81
function getServiceName() {
@@ -81,6 +84,10 @@ function getServiceName() {
81
84
--query ' Stacks[0].Outputs[?OutputKey==`Service`].OutputValue' \
82
85
--output text --region ${AWS_DEFAULT_REGION}
83
86
)
87
+ if [ -z $ECS_SERVICE_NAME ]; then
88
+ echo " Could not find the service under the '$ECS_SERVICE_STACK ' stack"
89
+ exit 10
90
+ fi
84
91
}
85
92
86
93
@@ -121,99 +128,103 @@ function assertRequiredArgumentsSet() {
121
128
fi
122
129
if [ -z " ${ACCOUNT_ID:- } " ]; then
123
130
echo " ACCOUNT is required. You can pass the value using -a or --account"
124
- exit 6
131
+ exit 7
132
+ fi
133
+ if [ -z " ${CROSS_ACCOUNT_ROLE:- } " ]; then
134
+ echo " ROLE_NAME is required. You can pass the value using -ar or --assume-role-name"
135
+ exit 8
125
136
fi
126
137
127
138
}
128
139
129
140
# #####################################################
130
141
# When not being tested, run application as expected #
131
142
# #####################################################
132
- set -o errexit
133
- set -o pipefail
134
- set -u
135
- set -e
136
-
137
- # If no args are provided, display usage information
138
- if [ $# == 0 ]; then usage; fi
139
-
140
- # Check for AWS, AWS Command Line Interface
141
- require aws
142
- # Check for jq, Command-line JSON processor
143
- require jq
144
-
145
- # Loop through arguments, two at a time for key and value
146
- while [[ $# -gt 0 ]]
147
- do
148
- key=" $1 "
149
-
150
- case $key in
151
- -c|--cluster-name)
152
- ECS_CLUSTER_STACK=" $2 "
153
- shift # past argument
154
- ;;
155
- -s|--service-name)
156
- ECS_SERVICE_STACK=" $2 "
157
- shift # past argument
158
- ;;
159
- -ar|--assume-role-name)
160
- CROSS_ACCOUNT_ROLE=" $2 "
161
- shift # past argument
162
- ;;
163
- -a|--account)
164
- ACCOUNT_ID=" $2 "
165
- shift # past argument
166
- ;;
167
- -k|--aws-access-key)
168
- AWS_ACCESS_KEY_ID=" $2 "
169
- shift # past argument
170
- ;;
171
- -s|--aws-secret-key)
172
- AWS_SECRET_ACCESS_KEY=" $2 "
173
- shift # past argument
174
- ;;
175
- -r|--region)
176
- AWS_DEFAULT_REGION=" $2 "
177
- shift # past argument
178
- ;;
179
- -v|--verbose)
180
- VERBOSE=true
181
- ;;
182
- --version)
183
- echo ${VERSION}
184
- exit 0
185
- ;;
186
- * )
187
- echo " Found nothing, will display usage and exit"
188
- usage
189
- exit 2
190
- ;;
191
- esac
192
- shift # past argument or value
193
- done
194
-
195
- if [ $VERBOSE == true ]; then
196
- set -x
197
- fi
143
+ if [ -z " $TEST_ENV " ]; then
144
+ set -o errexit
145
+ set -o pipefail
146
+ set -u
147
+ set -e
148
+ # If no args are provided, display usage information
149
+ if [ $# == 0 ]; then usage; fi
150
+
151
+ # Check for AWS, AWS Command Line Interface
152
+ require aws
153
+ # Check for jq, Command-line JSON processor
154
+ require jq
155
+
156
+ # Loop through arguments, two at a time for key and value
157
+ while [[ $# -gt 0 ]]
158
+ do
159
+ key=" $1 "
160
+
161
+ case $key in
162
+ -c|--cluster-name)
163
+ ECS_CLUSTER_STACK=" $2 "
164
+ shift # past argument
165
+ ;;
166
+ -s|--service-name)
167
+ ECS_SERVICE_STACK=" $2 "
168
+ shift # past argument
169
+ ;;
170
+ -ar|--assume-role-name)
171
+ CROSS_ACCOUNT_ROLE=" $2 "
172
+ shift # past argument
173
+ ;;
174
+ -a|--account)
175
+ ACCOUNT_ID=" $2 "
176
+ shift # past argument
177
+ ;;
178
+ -k|--aws-access-key)
179
+ AWS_ACCESS_KEY_ID=" $2 "
180
+ shift # past argument
181
+ ;;
182
+ -s|--aws-secret-key)
183
+ AWS_SECRET_ACCESS_KEY=" $2 "
184
+ shift # past argument
185
+ ;;
186
+ -r|--region)
187
+ AWS_DEFAULT_REGION=" $2 "
188
+ shift # past argument
189
+ ;;
190
+ -v|--verbose)
191
+ VERBOSE=true
192
+ ;;
193
+ --version)
194
+ echo ${VERSION}
195
+ exit 0
196
+ ;;
197
+ * )
198
+ echo " Found nothing, will display usage and exit"
199
+ usage
200
+ exit 2
201
+ ;;
202
+ esac
203
+ shift # past argument or value
204
+ done
198
205
199
- # Check that required arguments are provided
200
- assertRequiredArgumentsSet
206
+ if [ $VERBOSE == true ]; then
207
+ set -x
208
+ fi
201
209
202
- if [[ " $AWS_ASSUME_ROLE " != false ]]; then
203
- assumeRole
204
- fi
210
+ # Check that required arguments are provided
211
+ assertRequiredArgumentsSet
205
212
206
- getClusterName
207
- getServiceName
208
- updateService
213
+ if [[ " $AWS_ASSUME_ROLE " != false ]] ; then
214
+ assumeRole
215
+ fi
209
216
217
+ getClusterName
218
+ getServiceName
219
+ updateService
210
220
211
- if [[ " $AWS_ASSUME_ROLE " != false ]]; then
212
- assumeRoleClean
213
- fi
214
221
215
- exit 0
222
+ if [[ " $AWS_ASSUME_ROLE " != false ]]; then
223
+ assumeRoleClean
224
+ fi
216
225
226
+ exit 0
227
+ fi
217
228
# ############################
218
229
# End application run logic #
219
230
# ############################
0 commit comments