-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathrun.sh
executable file
·383 lines (312 loc) · 12.8 KB
/
run.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!/bin/bash
set +e
set -o noglob
#
# Set Colors
#
bold=$(tput bold)
underline=$(tput sgr 0 1)
reset=$(tput sgr0)
red=$(tput setaf 1)
green=$(tput setaf 76)
white=$(tput setaf 7)
tan=$(tput setaf 202)
blue=$(tput setaf 25)
#
# Headers and Logging
#
underline() { printf "${underline}${bold}%s${reset}\n" "$@"
}
h1() { printf "\n${underline}${bold}${blue}%s${reset}\n" "$@"
}
h2() { printf "\n${underline}${bold}${white}%s${reset}\n" "$@"
}
debug() { printf "${white}%s${reset}\n" "$@"
}
info() { printf "${white}➜ %s${reset}\n" "$@"
}
success() { printf "${green}✔ %s${reset}\n" "$@"
}
error() { printf "${red}✖ %s${reset}\n" "$@"
}
warn() { printf "${tan}➜ %s${reset}\n" "$@"
}
bold() { printf "${bold}%s${reset}\n" "$@"
}
note() { printf "\n${underline}${bold}${blue}Note:${reset} ${blue}%s${reset}\n" "$@"
}
type_exists() {
if [ $(type -P $1) ]; then
return 0
fi
return 1
}
jsonValue() {
key=$1
num=$2
awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~/'$key'\042/){print $(i+1)}}}' | tr -d '"' | sed -n ${num}p
}
# Check variables
if [ -z "$WERCKER_AWS_CODE_DEPLOY_KEY" ]; then
error "Please set the 'key' variable"
exit 1
fi
if [ -z "$WERCKER_AWS_CODE_DEPLOY_SECRET" ]; then
error "Please set the 'secret' variable"
exit 1
fi
if [ -z "$WERCKER_AWS_CODE_DEPLOY_APPLICATION_NAME" ]; then
error "Please set the 'application-name' variable"
exit 1
fi
if [ -z "$WERCKER_AWS_CODE_DEPLOY_DEPLOYMENT_GROUP_NAME" ]; then
error "Please set the 'deployment-group' variable"
exit 1
fi
if [ -z "$WERCKER_AWS_CODE_DEPLOY_S3_BUCKET" ]; then
error "Please set the 's3-bucket' variable"
exit 1
fi
# ----- Install AWS Cli -----
# see documentation http://docs.aws.amazon.com/cli/latest/userguide/installing.html
# ---------------------------
# Check AWS is installed
if ! type_exists 'aws'; then
h1 "Installing AWS CLI"
INSTALL_AWSCLI="sudo pip install awscli"
info "$INSTALL_AWSCLI"
INSTALL_AWSCLI_OUTPUT=$($INSTALL_AWSCLI 2>&1)
if [ $? -ne 0 ]; then
warn "$INSTALL_AWSCLI_OUTPUT"
exit 1
fi
success "Installing AWS CLI (`aws --version`) succeeded"
fi
# ----- Configure -----
# see documentation
# http://docs.aws.amazon.com/cli/latest/reference/configure/index.html
# ----------------------
set -e
h1 "Step 1: Configuring AWS"
h2 "Configuring AWS Access Key ID"
CONFIGURE_KEY_OUTPUT=$(aws configure set aws_access_key_id $WERCKER_AWS_CODE_DEPLOY_KEY 2>&1)
success "Configuring AWS Access Key ID succeeded"
h2 "Configuring AWS Secret Access Key"
CONFIGURE_SECRET_OUTPUT=$(aws configure set aws_secret_access_key $WERCKER_AWS_CODE_DEPLOY_SECRET 2>&1)
success "Configuring AWS Secret Access Key succeeded"
if [ -n "$WERCKER_AWS_CODE_DEPLOY_REGION" ]; then
h2 "Configuring AWS default region"
CONFIGURE_REGION_OUTPUT=$(aws configure set default.region $WERCKER_AWS_CODE_DEPLOY_REGION 2>&1)
success "Configuring AWS default region succeeded"
fi
set +e
# ----- Application -----
# see documentation
# http://docs.aws.amazon.com/cli/latest/reference/deploy/get-application.html
# http://docs.aws.amazon.com/cli/latest/reference/deploy/create-application.html
# ----------------------
# Application variables
APPLICATION_NAME="$WERCKER_AWS_CODE_DEPLOY_APPLICATION_NAME"
APPLICATION_VERSION=${WERCKER_AWS_CODE_DEPLOY_APPLICATION_VERSION:-${WERCKER_GIT_COMMIT:0:7}}
# Check application exists
h1 "Step 2: Defining application"
h2 "Checking application '$APPLICATION_NAME' exists"
APPLICATION_EXISTS="aws deploy get-application --application-name $APPLICATION_NAME"
info "$APPLICATION_EXISTS"
APPLICATION_EXISTS_OUTPUT=$($APPLICATION_EXISTS 2>&1)
if [ $? -ne 0 ]; then
warn "$APPLICATION_EXISTS_OUTPUT"
h2 "Creating application '$APPLICATION_NAME'"
# Create application
APPLICATION_CREATE="aws deploy create-application --application-name $APPLICATION_NAME"
info "$APPLICATION_CREATE"
APPLICATION_CREATE_OUTPUT=$($APPLICATION_CREATE 2>&1)
if [ $? -ne 0 ]; then
warn "$APPLICATION_CREATE_OUTPUT"
error "Creating application '$APPLICATION_NAME' failed"
exit 1
fi
success "Creating application '$APPLICATION_NAME' succeeded"
else
success "Application '$APPLICATION_NAME' already exists"
fi
# ----- Deployment config (optional) -----
# see documentation http://docs.aws.amazon.com/cli/latest/reference/deploy/create-deployment-config.html
# ----------------------
DEPLOYMENT_CONFIG_NAME=${WERCKER_AWS_CODE_DEPLOY_DEPLOYMENT_CONFIG_NAME:-CodeDeployDefault.OneAtATime}
MINIMUM_HEALTHY_HOSTS=${WERCKER_AWS_CODE_DEPLOY_MINIMUM_HEALTHY_HOSTS:-type=FLEET_PERCENT,value=75}
# Ckeck deployment config exists
h1 "Step 3: Defining deployment config"
h2 "Checking deployment config '$DEPLOYMENT_CONFIG_NAME' exists"
DEPLOYMENT_CONFIG_EXISTS="aws deploy get-deployment-config --deployment-config-name $DEPLOYMENT_CONFIG_NAME"
info "$DEPLOYMENT_CONFIG_EXISTS"
DEPLOYMENT_CONFIG_EXISTS_OUTPUT=$($DEPLOYMENT_CONFIG_EXISTS 2>&1)
if [ $? -ne 0 ]; then
warn "$DEPLOYMENT_CONFIG_EXISTS_OUTPUT"
h2 "Creating deployment config '$DEPLOYMENT_CONFIG_NAME'"
# Create deployment config
DEPLOYMENT_CONFIG_CREATE="aws deploy create-deployment-config --deployment-config-name $DEPLOYMENT_CONFIG_NAME --minimum-healthy-hosts $MINIMUM_HEALTHY_HOSTS"
info "$DEPLOYMENT_CONFIG_CREATE"
DEPLOYMENT_CONFIG_CREATE_OUTPUT=$($DEPLOYMENT_CONFIG_CREATE 2>&1)
if [ $? -ne 0 ]; then
warn "$DEPLOYMENT_CONFIG_CREATE_OUTPUT"
error "Creating deployment config '$DEPLOYMENT_CONFIG_NAME' failed"
exit 1
fi
success "Creating deployment config '$DEPLOYMENT_CONFIG_NAME' succeeded"
else
success "Deployment config '$DEPLOYMENT_CONFIG_NAME' already exists"
fi
# ----- Deployment group -----
# see documentation http://docs.aws.amazon.com/cli/latest/reference/deploy/create-deployment-config.html
# ----------------------
# Deployment group variables
DEPLOYMENT_GROUP=${WERCKER_AWS_CODE_DEPLOY_DEPLOYMENT_GROUP_NAME:-$WERCKER_DEPLOYTARGET_NAME}
AUTO_SCALING_GROUPS="$WERCKER_AWS_CODE_DEPLOY_AUTO_SCALING_GROUPS"
EC2_TAG_FILTERS="$WERCKER_AWS_CODE_DEPLOY_EC2_TAG_FILTERS"
SERVICE_ROLE_ARN="$WERCKER_AWS_CODE_DEPLOY_SERVICE_ROLE_ARN"
# Ckeck deployment group exists
h1 "Step 4: Defining deployment group"
h2 "Checking deployment group '$DEPLOYMENT_GROUP' exists for application '$APPLICATION_NAME'"
DEPLOYMENT_GROUP_EXISTS="aws deploy get-deployment-group --application-name $APPLICATION_NAME --deployment-group-name $DEPLOYMENT_GROUP"
info "$DEPLOYMENT_GROUP_EXISTS"
DEPLOYMENT_GROUP_EXISTS_OUTPUT=$($DEPLOYMENT_GROUP_EXISTS 2>&1)
if [ $? -ne 0 ]; then
warn "$DEPLOYMENT_GROUP_EXISTS_OUTPUT"
h2 "Creating deployment group '$DEPLOYMENT_GROUP' for application '$APPLICATION_NAME'"
# Create deployment group
DEPLOYMENT_GROUP_CREATE="aws deploy create-deployment-group --application-name $APPLICATION_NAME --deployment-group-name $DEPLOYMENT_GROUP --deployment-config-name $DEPLOYMENT_CONFIG_NAME"
if [ -n "$SERVICE_ROLE_ARN" ]; then
DEPLOYMENT_GROUP_CREATE="$DEPLOYMENT_GROUP_CREATE --service-role-arn $SERVICE_ROLE_ARN"
fi
if [ -n "$AUTO_SCALING_GROUPS" ]; then
DEPLOYMENT_GROUP_CREATE="$DEPLOYMENT_GROUP_CREATE --auto-scaling-groups $AUTO_SCALING_GROUPS"
fi
if [ -n "$EC2_TAG_FILTERS" ]; then
DEPLOYMENT_GROUP_CREATE="$DEPLOYMENT_GROUP_CREATE --ec2-tag-filters $EC2_TAG_FILTERS"
fi
info "$DEPLOYMENT_GROUP_CREATE"
DEPLOYMENT_GROUP_CREATE_OUTPUT=$($DEPLOYMENT_GROUP_CREATE 2>&1)
if [ $? -ne 0 ]; then
warn "$DEPLOYMENT_GROUP_CREATE_OUTPUT"
error "Creating deployment group '$DEPLOYMENT_GROUP' for application '$APPLICATION_NAME' failed"
exit 1
fi
success "Creating deployment group '$DEPLOYMENT_GROUP' for application '$APPLICATION_NAME' succeeded"
else
success "Deployment group '$DEPLOYMENT_GROUP' already exists for application '$APPLICATION_NAME'"
fi
DEPLOY_CMD = "aws deploy create-deployment --github-location repository='$GIT_REPO',commitId='$COMMIT_ID' --application-name '$APPLICATION_NAME' --deployment-group $DEPLOYMENT_GROUP"
# ----- Push a revision to S3 -----
# see documentation http://docs.aws.amazon.com/cli/latest/reference/deploy/push.html
# ----------------------
REVISION=${WERCKER_AWS_CODE_DEPLOY_REVISION:-$APPLICATION_NAME-$APPLICATION_VERSION.zip}
REVISION_DESCRIPTION="$WERCKER_AWS_CODE_DEPLOY_REVISION_DESCRIPTION"
S3_BUCKET="$WERCKER_AWS_CODE_DEPLOY_S3_BUCKET"
S3_SOURCE=${WERCKER_AWS_CODE_DEPLOY_S3_SOURCE:-.}
S3_KEY=${WERCKER_AWS_CODE_DEPLOY_S3_KEY:-$APPLICATION_NAME}
# Build S3 Location
S3_LOCATION="s3://$S3_BUCKET"
if [ -n "$S3_KEY" ]; then
S3_LOCATION="$S3_LOCATION/$S3_KEY"
fi
S3_LOCATION="$S3_LOCATION/$REVISION"
h1 "Step 5: Pushing to S3"
PUSH_S3="aws deploy push --application-name $APPLICATION_NAME --s3-location $S3_LOCATION --source $S3_SOURCE"
if [ -n "$REVISION_DESCRIPTION" ]; then
PUSH_S3="$PUSH_S3 --description '$REVISION_DESCRIPTION'"
fi
info "$PUSH_S3"
PUSH_S3_OUTPUT=$($PUSH_S3 2>&1)
if [ $? -ne 0 ]; then
warn "$PUSH_S3_OUTPUT"
error "Pushing revision '$REVISION' to S3 failed"
exit 1
fi
success "Pushing revision '$REVISION' to S3 succeeded"
# ----- Register revision -----
# see documentation http://docs.aws.amazon.com/cli/latest/reference/deploy/register-application-revision.html
# ----------------------
h1 "Step 6: Registering revision"
# Build S3 Location
BUNDLE_TYPE=${REVISION##*.}
S3_LOCATION="bucket=$S3_BUCKET,bundleType=$BUNDLE_TYPE"
if [ -n "$S3_KEY" ]; then
S3_LOCATION="$S3_LOCATION,key=$S3_KEY/$REVISION"
else
S3_LOCATION="$S3_LOCATION,key=$REVISION"
fi
# Define egister-application-revision command
REGISTER_REVISION="aws deploy register-application-revision --application-name $APPLICATION_NAME --s3-location $S3_LOCATION"
if [ -n "$REVISION_DESCRIPTION" ]; then
REGISTER_REVISION="$REGISTER_REVISION --description '$REVISION_DESCRIPTION'"
fi
info "$REGISTER_REVISION"
REGISTER_REVISION_OUTPUT=$($REGISTER_REVISION 2>&1)
if [ $? -ne 0 ]; then
warn "$REGISTER_REVISION_OUTPUT"
error "Registering revision '$REVISION' failed"
exit 1
fi
success "Registering revision '$REVISION' succeeded"
# ----- Deployment -----
# see documentation http://docs.aws.amazon.com/cli/latest/reference/deploy/create-deployment.html
# ----------------------
DEPLOYMENT_DESCRIPTION="$WERCKER_AWS_CODE_DEPLOY_DEPLOYMENT_DESCRIPTION"
DEPLOYMENT_OVERVIEW=${WERCKER_AWS_CODE_DEPLOY_DEPLOYMENT_OVERVIEW:-true}
h1 "Step 7: Creating deployment"
DEPLOYMENT="aws deploy create-deployment --application-name $APPLICATION_NAME --deployment-config-name $DEPLOYMENT_CONFIG_NAME --deployment-group-name $DEPLOYMENT_GROUP --s3-location $S3_LOCATION"
if [ -n "$DEPLOYMENT_DESCRIPTION" ]; then
DEPLOYMENT="$DEPLOYMENT --description \"$DEPLOYMENT_DESCRIPTION\""
fi
info "$DEPLOYMENT"
DEPLOYMENT_OUTPUT=$($DEPLOYMENT 2>&1)
if [ $? -ne 0 ]; then
warn "$DEPLOYMENT_OUTPUT"
error "Deployment of application '$APPLICATION_NAME' on deployment group '$DEPLOYMENT_GROUP' failed"
exit 1
fi
DEPLOYMENT_ID=$(echo $DEPLOYMENT_OUTPUT | jsonValue 'deploymentId' | tr -d ' ')
note "You can follow your deployment at: https://console.aws.amazon.com/codedeploy/home#/deployments/$DEPLOYMENT_ID"
if [ 'true' = "$DEPLOYMENT_OVERVIEW" ]; then
h1 "Deployment Overview"
DEPLOYMENT_GET="aws deploy get-deployment --deployment-id $DEPLOYMENT_ID"
info "$DEPLOYMENT_GET"
h2 "Deploying application '$APPLICATION_NAME' on deployment group '$DEPLOYMENT_GROUP'"
while :
do
sleep 5
DEPLOYMENT_GET_OUTPUT=$($DEPLOYMENT_GET 2>&1 > /tmp/$DEPLOYMENT_ID)
if [ $? -ne 0 ]; then
printf "\n"
warn "$DEPLOYMENT_OUTPUT"
error "Deployment of application '$APPLICATION_NAME' on deployment group '$DEPLOYMENT_GROUP' failed"
exit 1
fi
# Deployment Status
STATUS=$(cat /tmp/$DEPLOYMENT_ID | jsonValue 'status' | tr -d '\r\n' | tr -d ' ')
ERROR_MESSAGE=$(cat /tmp/$DEPLOYMENT_ID | jsonValue 'message')
# Deployment failed
if [ "$STATUS" = 'Failed' ]; then
printf "\n"
error "Deployment of application '$APPLICATION_NAME' on deployment group '$DEPLOYMENT_GROUP' failed: $ERROR_MESSAGE"
exit 1
fi
# Deployment Overview
IN_PROGRESS=$(cat /tmp/$DEPLOYMENT_ID | jsonValue 'InProgress' | tr -d '\r\n' | tr -d ' ')
PENDING=$(cat /tmp/$DEPLOYMENT_ID | jsonValue 'Pending' | tr -d '\r\n' | tr -d ' ')
SKIPPED=$(cat /tmp/$DEPLOYMENT_ID | jsonValue 'Skipped' | tr -d '\r\n' | tr -d ' ')
SUCCEEDED=$(cat /tmp/$DEPLOYMENT_ID | jsonValue 'Succeeded' | tr -d '\r\n' | tr -d ' ')
FAILED=$(cat /tmp/$DEPLOYMENT_ID | jsonValue 'Failed' | tr -d '\r\n' | tr -d ' ')
printf "\r| In Progress: $IN_PROGRESS | Pending: $PENDING | Skipped: $SKIPPED | Succeeded: $SUCCEEDED | Failed: $FAILED |"
# Deployment succeeded
if [ "$STATUS" = 'Succeeded' ]; then
printf "\n"
success "Deployment of application '$APPLICATION_NAME' on deployment group '$DEPLOYMENT_GROUP' succeeded"
break
fi
done
else
info "Deployment of application '$APPLICATION_NAME' on deployment group '$DEPLOYMENT_GROUP' in progress"
fi
set -e