-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdocker-entrypoint
executable file
·212 lines (172 loc) · 5.94 KB
/
docker-entrypoint
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
#!/usr/bin/env bash
set -e
domain_from_route() {
echo $1 | grep -Po '(?<=\.)(.+)$'
}
hostname_from_route() {
echo $1 | grep -Po '^([^.]+)'
}
app_guid() {
cf app $1 --guid | head -1
}
app_process_guid() {
cf curl /v3/apps/$1/processes | jq -Mr '.resources[0].guid'
}
count_running_instances() {
cf curl /v3/processes/$1/stats | jq -Mr '[.resources[] | select(.state=="RUNNING")] | length'
}
login() {
# Login
echo
cf api ${CF_API}
cf auth # Uses CF_USERNAME and CF_PASSWORD env vars
cf target -o ${CF_ORG} -s ${CF_SPACE}
}
run_hook() {
hook_path=/hooks/$1
if [ -f "${hook_path}" ]; then
echo
echo "Running hook $1"
echo
${hook_path} "${@:2}"
fi
}
wait_for_running_instances() {
new_app_name=$1
instance_count=$2
new_app_guid=$(app_guid ${new_app_name})
new_app_process_guid=$(app_process_guid ${new_app_guid})
startup_timeout=${CF_STARTUP_TIMEOUT}
startup_interval=3
echo
echo "Allowing ${startup_timeout}s for ${instance_count} RUNNING instances..."
new_app_running_instances=$(count_running_instances ${new_app_process_guid})
startup_start=$(date +%s)
while [ ${new_app_running_instances} -lt ${instance_count} ]; do
startup_elapsed=$(($(date +%s) - ${startup_start}))
if [ ${startup_elapsed} -ge ${startup_timeout} ]; then
echo
echo "ERROR: Failed to start ${instance_count} RUNNING instances in ${startup_timeout}s"
exit 1
fi
echo "${new_app_running_instances}/${instance_count}..."
sleep ${startup_interval} && startup_timer=$((${startup_timer} + ${startup_interval}))
new_app_running_instances=$(count_running_instances ${new_app_process_guid})
done
echo "${new_app_running_instances}/${instance_count}..."
echo "OK"
}
deploy() {
push_args="$@"
echo "push_args=$push_args"
run_hook "pre-login"
login
run_hook "post-login"
# Get the primary route from the first in CF_ROUTES
routes=(${CF_ROUTES})
primary_route=${routes[0]}
echo "Primary route: ${primary_route}"
primary_route_domain=$(domain_from_route ${primary_route})
primary_route_hostname=$(hostname_from_route ${primary_route})
# Get the GUID of the domain
domain_guid=$(cf curl "/v3/domains?names=${primary_route_domain}" | jq -Mr .resources[0].guid)
# Get the GUID of the app with the hostname + domain route mapped to it
old_app_guid=$(cf curl "/v3/routes?domain_guids=${domain_guid}&hosts=${primary_route_hostname}" | jq -Mr .resources[0].destinations[0].app.guid)
if [ "${old_app_guid}" = "null" ]; then
echo "No old mirror: assuming first push."
new_mirror="blue"
old_mirror="null"
old_app_name="null"
else
# Get the name of the live, i.e. old, app
old_app_name=$(cf curl "/v3/apps/${old_app_guid}" | jq -Mr .name)
if [ "${old_app_name}" = "${CF_APP_NAME}-blue" ]; then
old_mirror="blue"
new_mirror="green"
elif [ "${old_app_name}" = "${CF_APP_NAME}-green" ]; then
old_mirror="green"
new_mirror="blue"
else
echo "Unknown old mirror: ${old_app_name}"
exit 1
fi
fi
new_app_name=${CF_APP_NAME}-${new_mirror}
echo
echo "Old mirror: ${old_mirror}"
echo "New mirror: ${new_mirror}"
cf create-app ${new_app_name} --app-type ${CF_APP_TYPE}
if [ "${CF_AUTOSCALING}" == "true" ]; then
# Prevent pre-existing autoscaling from interfering with the initial scaling up
echo
echo "Detaching autoscaling policy"
cf detach-autoscaling-policy ${new_app_name}
autoscaling_policy_file="/autoscaling-policy.json"
fi
if [ "${old_app_name}" = "null" ]; then
instance_count=1
else
# If no autoscaling policy supplied, copy from old mirror
if [ "${CF_AUTOSCALING}" == "true" ]; then
if [ ! -f ${autoscaling_policy_file} ]; then
echo
echo "Copying autoscaling policy from ${old_app_name}"
set +e
cf autoscaling-policy ${old_app_name} | tail -n +2 > ${autoscaling_policy_file}
set -e
fi
fi
old_app_process_guid=$(app_process_guid ${old_app_guid})
instance_count=$(count_running_instances ${old_app_process_guid})
fi
# Adjust instance count based on autoscaling policy
if [ "${CF_AUTOSCALING}" == "true" ]; then
if [ -f ${autoscaling_policy_file} ]; then
autoscaling_policy_min_count=$(cat ${autoscaling_policy_file} | jq -Mr .instance_min_count)
autoscaling_policy_max_count=$(cat ${autoscaling_policy_file} | jq -Mr .instance_max_count)
if [ ${autoscaling_policy_min_count} -gt ${instance_count} ]; then
instance_count=${autoscaling_policy_min_count}
fi
if [ ${autoscaling_policy_max_count} -lt ${instance_count} ]; then
instance_count=${autoscaling_policy_max_count}
fi
fi
fi
# Push & scale
run_hook "pre-push" ${old_app_name} ${new_app_name}
cf push ${new_app_name} --instances ${instance_count} ${push_args}
if [ "${CF_AUTOSCALING}" == "true" ]; then
wait_for_running_instances ${new_app_name} ${instance_count}
if [ -f ${autoscaling_policy_file} ]; then
echo
echo "Attaching autoscaling policy"
cf attach-autoscaling-policy ${new_app_name} ${autoscaling_policy_file}
fi
fi
run_hook "post-push" ${old_app_name} ${new_app_name}
# Map routes
for route in ${CF_ROUTES}; do
echo
echo "Remapping ${route}"
route_domain=$(domain_from_route ${route})
route_hostname=$(hostname_from_route ${route})
cf map-route ${new_app_name} ${route_domain} -n ${route_hostname}
if [ "${old_mirror}" != "null" ]; then
cf unmap-route ${old_app_name} ${route_domain} -n ${route_hostname}
fi
done
run_hook "post-routing" ${old_app_name} ${new_app_name}
if [ "${old_mirror}" != "null" ]; then
cf stop ${old_app_name}
fi
run_hook "post-deploy" ${old_app_name} ${new_app_name}
echo
echo "Blue-green deployment done."
}
if [ "$1" == "deploy" ]; then
# Default command performs blue-green deployment, with additional args following
deploy "${@:2}"
else
# Otherwise run whatever was in the command
exec "$@"
fi