-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.sh
144 lines (120 loc) · 4.14 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
#!/bin/bash
#############
# Colors#
#############
green='\e[0;32m'
red='\e[0;31m'
label_color='\e[0;33m'
no_color='\e[0m' # No Color
#############
# Variables#
#############
# Used to create a public route within the space for accessing the deployed
# application. The reserved CF_SPACE variable (configured in the deployer) is
# used to unique the route name per space.
if [ "${CF_SPACE}" == "prod" ]; then
PUBLIC_HOST=${CF_APP}
else
PUBLIC_HOST="${CF_SPACE}-${CF_APP}"
fi
# Extract the selected build number from the reserved BUILD_SELECTOR variable.
SELECTED_BUILD=$(grep -Eo '[0-9]{1,100}' <<< "${BUILD_SELECTOR}")
# Compute a unique app name using the reserved CF_APP name (configured in the
# deployer or from the manifest.yml file), the build number, and a
# timestamp (allowing multiple deploys for the same build).
NEW_APP_NAME="${CF_APP}-$SELECTED_BUILD-$(date +%s)"
# Domain can be customized if needed.
DOMAIN=mybluemix.net
# Used to define a temporary route to test the newly deployed app
TEST_HOST=$NEW_APP_NAME
MEMORY=256M
echo `cat /etc/*-release`
echo `cf --version`
CF_TRACE=true
echo -e "${label_color}Variables:${no_color}"
echo "PUBLIC_HOST=$PUBLIC_HOST"
echo "DOMAIN=$DOMAIN"
echo "NEW_APP_NAME=$NEW_APP_NAME"
echo "TEST_HOST=$TEST_HOST"
echo "MEMORY"=$MEMORY
#############
# Steps #
#############
#############
# (1) Find existing deployed apps (usually one) to the space
# based on the binding to the PUBLIC_HOST
#############
rm -f apps.txt
echo -e "${label_color}Find all existing deployments:${no_color}"
cf apps | { grep $PUBLIC_HOST || true; } | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g" &> apps.txt
cat apps.txt
# Store just the deployed application names
awk '{ print $1 }' apps.txt > app_names.txt
rm -f apps.txt
#############
# (2) Push the updates from the build to create a newly deployed
# application.
#############
echo -e "${label_color}Pushing new deployment - ${green}$NEW_APP_NAME${no_color}"
cf push $NEW_APP_NAME -d $DOMAIN -n $TEST_HOST -m $MEMORY
DEPLOY_RESULT=$?
if [ $DEPLOY_RESULT -ne 0 ]; then
echo -e "${red}Deployment of $NEW_APP_NAME failed!!"
cf logs $NEW_APP_NAME --recent
return $DEPLOY_RESULT
fi
echo -e "${label_color}APPS:${no_color}"
cf apps | grep ${CF_APP}
#############
# (3) Inject testing to ensure that the new app version is good.
#############
echo -e "${label_color}Testing new app - ${green}$NEW_APP_NAME${no_color}"
curl "http://$TEST_HOST.$DOMAIN"
TEST_RESULT=$?
if [ $TEST_RESULT -ne 0 ]; then
echo -e "${red}New app did not deploy properly - ${green}$NEW_APP_NAME${no_color}"
return $TEST_RESULT
else
echo -e "${green}Test PASSED!!!!${no_color}"
fi
#############
# (4) Map traffic to the new version by binding to the
# public host.
# NOTE: The old version(s) is still taking traffic to avoid
# disruption in service.
#############
echo -e "${label_color}Map public space route to new app - ${green}$NEW_APP_NAME${no_color}"
cf map-route $NEW_APP_NAME $DOMAIN -n $PUBLIC_HOST
echo -e "${label_color}Public route bindings:${no_color}"
cf routes | { grep $PUBLIC_HOST || true; }
#############
# (5) Delete the temporary route that was used for testing
# since it is no longer needed.
#############
echo -e "${label_color}Remove and delete test route - ${green}$TEST_HOST${no_color}"
cf unmap-route $NEW_APP_NAME $DOMAIN -n $TEST_HOST
if [ $? -ne 0 ]; then
echo -e "${label_color}Test route isn't mapped and doesn't need to be removed.${no_color}"
fi
cf delete-route $DOMAIN -n $TEST_HOST -f
#############
# (6) Delete the old application(s) at this point.
# They are no longer needed.
#############
while read name
do
if [ "$name" != "$NEW_APP_NAME" ]; then
echo -e "${label_color}Deleting old deployed application - $name${no_color}"
cf delete $name -f
fi
done < app_names.txt
rm -f app_names.txt
#############
# Summary #
#############
echo -e "${label_color}Deployed Applications:${no_color}"
cf apps | grep ${CF_APP}
echo -e "${label_color}Public route bindings:${no_color}"
cf routes | grep $PUBLIC_HOST
echo -e "${label_color}You have successfully executed a rolling deployment of ${green}$NEW_APP_NAME${no_color}."
echo -e "${green}SIMPLY AWESOME!!!!${no_color}"