forked from Osthanes/update_service
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathactivedeploy_step_2.sh
executable file
·131 lines (113 loc) · 4.49 KB
/
activedeploy_step_2.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
#!/bin/bash
#********************************************************************************
# Copyright 2016 IBM
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
#********************************************************************************
#set $DEBUG to 1 for set -x output
if [ $DEBUG -eq 1 ]; then
set -x # trace steps
fi
SCRIPTDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
source ${SCRIPTDIR}/check_and_set_env.sh
echo "TARGET_PLATFORM = $TARGET_PLATFORM"
echo "NAME = $NAME"
echo "AD_ENDPOINT = $AD_ENDPOINT"
echo "CONCURRENT_VERSIONS = $CONCURRENT_VERSIONS"
echo "TOOLCHAIN_AVAILABLE = $TOOLCHAIN_AVAILABLE"
# cd to target so can read ccs.py when needed (for group deletion)
cd ${SCRIPTDIR}
# Initial deploy case
originals=($(groupList))
# Nothing to do in initial deploy scenario
if [[ 1 = ${#originals[@]} ]]; then
echo "INFO: Initial version (single version deployed); exiting"
exit 0
fi
# If a problem was found with $AD_ENDPOINT, fail now
if [[ -n ${MUSTFAIL_ACTIVEDEPLOY} ]]; then
echo -e "${red}Active deploy service unavailable; failing.${no_color}"
exit 128
fi
# Identify the active deploy in progress. We do so by looking for a deploy
# involving the add / container named "${NAME}"
in_prog=$(with_retry active_deploy list | grep "${NAME}" | grep "in_progress")
read -a array <<< "$in_prog"
update_id=${array[0]}
if [[ -z "${update_id}" ]]; then
echo "INFO: Initial version (no update containing ${NAME}); exiting"
with_retry active_deploy list
exit 0
fi
# Identify URL for visualization of update. To do this:
# The active deploy api server and GUI server were computed in check
show_link "Deployment URL" \
"${update_gui_url}/deployments/${update_id}?ace_config={%22spaceGuid%22:%22${CF_SPACE_ID}%22}" \
${green}
echo "INFO: Not initial version (part of update ${update_id})"
with_retry active_deploy show ${update_id}
IFS=$'\n' properties=($(with_retry active_deploy show ${update_id} | grep ':'))
update_status=$(get_property 'status' ${properties[@]})
# TODO handle other statuses better: could be rolled back, rolling back, paused, failed, ...
# Insufficient to leave it and let the wait_phase_completion deal with it; the call to advance/rollback could fail
if [[ "${update_status}" != 'in_progress' ]]; then
echo "Deployment in unexpected status: ${update_status}"
rollback ${update_id}
delete ${update_id}
exit 1
fi
# If TEST_RESULT_FOR_AD not set, assume the test succeeded. If the value wasn't set, then the user
# didn't modify the test job. However, we got to this job, so the test job must have
# completed successfully. Note that we are assuming that a test failure would terminate
# the pipeline.
if [[ -z ${TEST_RESULT_FOR_AD} ]]; then
TEST_RESULT_FOR_AD=0;
fi
# Either rampdown and complete (on test success) or rollback (on test failure)
if [[ ${TEST_RESULT_FOR_AD} -eq 0 ]]; then
echo "Test success -- completing update ${update_id}"
# First advance to rampdown phase
advance ${update_id} && rc=$? || rc=$?
# If failure doing advance, then rollback
if (( $rc )); then
echo "ERROR: Advance to rampdown failed; rolling back update ${update_id}"
rollback ${update_id} || true
if (( $rollback_rc )); then
echo "WARN: Unable to rollback update"
echo $(wait_comment $rollback_rc)
fi
fi
# Second advance to final phase
advance ${update_id} && rc=$? || rc=$?
if (( $rc )); then
echo "ERROR: Unable to advance to final phase"
fi
else
echo "Test failure -- rolling back update ${update_id}"
rollback ${update_id} && rc=$? || rc=$?
if (( $rc )); then echo $(wait_comment $rc); fi
# rc will be the exit code; we want a failure code if there was a rollback
rc=2
fi
# Cleanup - delete older updates
clean && clean_rc=$? || clean_rc=$?
if (( $clean_rc )); then
echo "WARN: Unable to delete old versions."
echo $(wait_comment $clean_rc)
fi
# Cleanup - delete update record
echo "Deleting upate record"
delete ${update_id} && delete_rc=$? || delete_rc=$?
if (( $delete_rc )); then
echo "WARN: Unable to delete update record ${update_id}"
fi
exit $rc