-
Notifications
You must be signed in to change notification settings - Fork 14
/
deploy.sh
executable file
·259 lines (201 loc) · 5.53 KB
/
deploy.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/bin/bash
#
# Validates and deploys an AWS CloudFormation Stack
#
#######################################
AWS_REGION="${AWS_REGION:-us-west-2}"
ORIG_DIR=`pwd`
SCRIPT_NAME="$0"
SCRIPT_DIR="$(cd "$(dirname "$0")"; pwd)"
ARGS=("$@")
# ------------------------------------------------------------------------
# script functions
log_error() {
>&2 echo "ERROR: $1"
}
do_usage_mini() {
cat >&2 <<EOF
Usage:
$SCRIPT_NAME <options> [ create | update | delete | describe | geturls | validate | test <email> ]
Options:
-d The Slack team domain
-t The Slack authentication token
-o The full origin domain (e.g. http://www.myhost.com/)
-v Display verbose output
EOF
}
do_usage() {
do_usage_mini
cat >&2 <<EOF
Description:
Deploys an AWS CloudFormation that creates a Slack registration service.
EOF
}
# ------------------------------------------------------------------------
_pre_process_template() {
# if [ -f "$SCRIPT_DIR/cloudformation.json" ]; then
# log_error "WARN: Found existing cloudformation.json"
# rm "$SCRIPT_DIR/cloudformation.json"
# fi
#
# sed -e "s/\${SLACK_TOKEN}/$SLACK_TOKEN/" "$SCRIPT_DIR/template.json" > "$SCRIPT_DIR/cloudformation.json"
TEMPLATE="$SCRIPT_DIR/cloudformation.json"
}
_clean_template() {
if [ -f "$SCRIPT_DIR/cloudformation.json" ]; then
rm "$SCRIPT_DIR/cloudformation.json"
fi
}
# ------------------------------------------------------------------------
do_validate() {
_pre_process_template
if [ -z "$AWS_REGION" ]; then
do_usage_mini
log_error "Please set the AWS region"
exit 1
else
echo "Using AWS Region: $AWS_REGION"
fi
# TODO CAPTURE ERROR CODE & exit
RET=`$AWS_CLI --region $AWS_REGION cloudformation validate-template --template-body file:////$TEMPLATE 2>&1`
RES="$?"
if [ ! $RES == 0 ]; then
echo "$RET"
fi
return $RES
}
do_create() {
if [ -z "$SLACK_DOMAIN" ]; then
do_usage_mini
log_error "Please set the Slack domain parameter."
exit 1
fi
if [ -z "$SLACK_TOKEN" ]; then
do_usage_mini
log_error "Please set the Slack token parameter."
exit 1
fi
if [ -z "$WEB_ORIGIN" ]; then
do_usage_mini
log_error "Please set the origin parameter for CORS."
exit 1
fi
do_validate \
&& $AWS_CLI --region $AWS_REGION cloudformation create-stack --parameters \
ParameterKey=SlackTeamSubDomain,ParameterValue=$SLACK_DOMAIN \
ParameterKey=SlackAuthToken,ParameterValue=$SLACK_TOKEN \
ParameterKey=Origin,ParameterValue=$WEB_ORIGIN \
--capabilities CAPABILITY_NAMED_IAM \
--stack-name slackinviter --template-body file:////$TEMPLATE | jq \
&& echo "Deploy requested, waiting on completion..." \
&& $AWS_CLI --region $AWS_REGION cloudformation wait stack-create-complete --stack-name slackinviter
RES="$?"
if [ 0 == $RES ]; then
do_geturls
else
do_describe | jq '.StackEvents[] | select(.ResourceStatus | contains("FAILED"))'
fi
}
do_update() {
echo "update"
do_validate
# do_validate \
# && $AWS_CLI cloudformation update-stack --stack-name slackinviter --template-body file:////$TEMPLATE \
# && echo "Deployed, waiting on completion..." \
# && $AWS_CLI cloudformation wait stack-update-complete --stack-name slackinviter
}
do_geturls() {
$AWS_CLI --region $AWS_REGION cloudformation describe-stacks | jq '.Stacks[] | {id: .StackId, url: .Outputs[].OutputValue}'
}
do_describe() {
$AWS_CLI --region $AWS_REGION cloudformation describe-stack-events --stack-name slackinviter
}
do_delete() {
$AWS_CLI --region $AWS_REGION cloudformation delete-stack --stack-name slackinviter \
&& echo "Delete requested, waiting on completion..." \
&& $AWS_CLI --region $AWS_REGION cloudformation wait stack-delete-complete --stack-name slackinviter
}
do_test() {
URL="$(do_geturls | jq -r '.url')"
EMAIL="${PARAMS[0]}"
if [ -z "$EMAIL" ]; then
log_error "'test' needs an email address as a parameter"
exit 1
fi
echo "Testing url: $URL with email: $EMAIL"
curl -vvv -X POST $URL -H 'Content-type: application/json' -d "{ \"email\": \"$EMAIL\" }"
}
do_estimate() {
do_validate \
&& $AWS_CLI --region $AWS_REGION cloudformation estimate-template-cost --parameters \
ParameterKey=SlackTeamSubDomain,ParameterValue=$SLACK_DOMAIN \
ParameterKey=SlackAuthToken,ParameterValue=$SLACK_TOKEN \
--template-body file:////$TEMPLATE
}
# ------------------------------------------------------------------------
# read the options
while getopts ":d:t:o:v" opt; do
case $opt in
d)
SLACK_DOMAIN="$OPTARG";
;;
t)
SLACK_TOKEN="$OPTARG";
;;
o)
WEB_ORIGIN="$OPTARG";
;;
v)
OUTPUT_VERBOSE="-vvv";
;;
*)
;;
esac
done
# Extract the overflowing arguments
PARAMS=("${ARGS[@]:$OPTIND}")
CMD_INDEX=`expr $OPTIND - 1`
CMD=${ARGS[ $CMD_INDEX ]}
# ------------------------------------------------------------------------
# check other utils are present
if [ ! `command -v aws` ]; then
log_error "The AWS CLI command was not found"
exit 1
fi
AWS_CLI=`command -v aws`
if [ ! `command -v jq` ]; then
log_error "The 'jq' command was not found."
exit 1
fi
# ------------------------------------------------------------------------
# process sub-command
case "$CMD" in
create)
do_create
;;
update)
do_update
;;
delete)
do_delete
;;
describe)
do_describe
;;
geturls)
do_geturls
;;
test)
do_test
;;
estimate)
do_estimate
;;
validate)
do_validate
;;
*)
do_usage
;;
esac
exit 0