-
Notifications
You must be signed in to change notification settings - Fork 1
/
update_horizon
executable file
·243 lines (205 loc) · 6.05 KB
/
update_horizon
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
#!/bin/bash
# This script removes the currently running horizon, cleans the env associated
# with it. It then loads the latest verson of the horizon. It supports both
# horizon deb packages and horizon snaps.
#
# For bluehorizon snap support: If you want to supply a local snap,
# please input a fully qualified snap path (with name) to the command.
# If no snap name and path is supplied, the script will go to the Ubuntu store
# to download the latest snap.
#
# Usage:
# update_horizon [snap_path]
# setup environmental variables
source "$(dirname "$(readlink -f "$0")")/env_vars"
content="-H Content-Type:application/json"
curlBasicArgs="-s -w %{http_code} -H Accept:application/json"
# return code
RC_SUCCESS=0
RC_FAILED_REMOVE_SNAP=180
RC_FAILED_INSTALL_SNAP=181
RC_FAILED_STOP_HORIZON_SERVICE=183
RC_FAILED_START_HORIZON_SERVICE=184
RC_FAILED_UPGRADE_HORIZON_PKG=185
#####
# This function parses the first parameter which is a json structure + a number.
# It matches the number with the second parameter. The function reterns true if
# the two numbers equal, otherwise false.
check_status_code()
{
http_ret=$1
exp_code=$2
rc=$(echo $http_ret | sed 's/.*}\([0-9]*\)$/\1/')
if [[ $VERBOSE -eq 1 ]]; then
echo $rc,$exp_code
fi
if [ $rc -eq $exp_code ]; then
return 0 # 0 = true
else
return 1 # 1 = false
fi
}
#####
# This function returns parses the input string which is a json + number.
# It returns the json part.
get_message()
{
msg=`echo $1 | sed 's/\(.*}\)\([0-9]*\)$/\1/'`
echo $msg
}
#####
# This function removes all the docker containers for the workloads.
remove_workloads() {
echo "*Remove workload containers from docker."
# make sure anax is running
pidof anax > /dev/null
if [[ $? -ne 0 ]]; then
echo "anax is not running."
return 1
fi
# get the workload from anax
ret=$(curl -X GET $curlBasicArgs "$ANAX_API/workload")
if ! check_status_code "$ret" 200; then
echo "failed to get the workloads from the anax api." $(get_message "$ret")
return 1
fi
# remove the docker containers associated with the workload
names=$(get_message "$ret" | jq '.workloads[].Names[]')
if [[ $VERBOSE -eq 1 ]]; then
echo "workload names: $names"
fi
if [ -n "$names" ]; then
for workload in $names
do
workload=$(echo ${workload//\"/})
echo "removing docker container ${workload:1}"
docker rm -f ${workload:1};
if [[ $? -ne 0 ]]; then
echo "failed to remove docker container ${workload:1}"
fi
done
fi
return $RC_SUCCESS
}
#####
# This function removes bluehorizon snap.
remove_snap() {
echo "*Remove bluehorizon snap."
snap list | grep bluehorizon
if [[ $? -ne 0 ]]; then
echo "bluehorizon is not installed."
else
snap remove bluehorizon
fi
if [[ $? -ne 0 ]]; then
echo "Failed to remove bluehorizon snap"
return $RC_FAILED_REMOVE_SNAP
fi
return $RC_SUCCESS
}
#####
# This function stops the horizon service and clean the db and necessary files
# so that the horizen can start up again as if it is new.
clean_horizon_deb() {
# stop the horizon service
systemctl stop horizon.service 2>&1
if [[ $? -ne 0 ]]; then
echo "Failed to stop horizon service."
return $RC_FAILED_STOP_HORIZON_SERVICE
fi
# clean the db and other runtime generated files
# these are default locations
rm -f /var/horizon/*.db
rm -Rf /etc/horizon/policy.d/*
rm -Rf /var/horizon/userkeys/*
}
#####
# This function installs bluehorizon snap.
install_snap() {
snap=$1
echo "*Copy the configuration files."
# set up the configuration files for anax
mkdir -p /var/snap/bluehorizon/common/config
if [ -e /boot/firmware/horizon/config ]; then
cp -af /boot/firmware/horizon/config /var/snap/bluehorizon/common/
fi
# stuff from /var may overwrite the boot stuff
if [ -e /var/horizon/config ]; then
cp -af /var/horizon/config /var/snap/bluehorizon/common/
fi
# install the snap
echo "*Install bluehorizon snap. "
if [[ -n $snap ]]; then
echo "get bluehoizon snap from local file: $snap."
snap install --devmode $snap
else
echo "get bluehorizon snap from the Ubuntu store."
d_ver_fn="/var/snap/bluehorizon/common/config/horizon_directory_version"
if [ -f $d_ver_fn ] && grep -q "999" $d_ver_fn; then
channel="edge"
else
channel="beta"
fi
snap install --devmode --$channel bluehorizon
fi
if [[ $? -ne 0 ]]; then
echo "Failed to install the bluehorizon snap"
return $RC_FAILED_INSTALL_SNAP
fi
return $RC_SUCCESS
}
#####
# This function updates the deb packages and starts the horizon service with current configuration
update_horizon_deb() {
apt-get update
apt-get install --only-upgrade -y horizon bluehorizon bluehorizon-ui
if [[ $? -ne 0 ]]; then
echo "Failed to upgrade horizon packages."
return $RC_FAILED_STOP_HORIZON_SERVICE
fi
# start the horizon service
systemctl start horizon.service 2>&1
if [[ $? -ne 0 ]]; then
echo "Failed to start horizon service."
return $RC_FAILED_UPGRADE_HORIZON_PKG
fi
}
### The main program starts here
USE_SNAP=1
# get local snap path (with name) if it is supplied
snap_path=""
if [ $# -gt 0 ]; then
snap_path=$1
fi
dpkg -l | grep horizon
if [[ $? -eq 0 ]]; then
USE_SNAP=0
fi
if [ -n "$ENV_VARS_FILE_NAME" ] && [ -f "$ENV_VARS_FILE_NAME" ]; then
source $ENV_VARS_FILE_NAME
fi
# Global variables
ANAX_API="http://localhost:$ANAX_API_PORT"
# remove the workloads
remove_workloads
if [ $USE_SNAP -eq 1 ]; then
# remove the old snap
remove_snap
else
# clean up deb
clean_horizon_deb
fi
rc=$?
if [ $rc != $RC_SUCCESS ]; then
exit $rc
fi
# update and restart
if [ $USE_SNAP -eq 1 ]; then
install_snap $snap_path
else
update_horizon_deb
fi
rc=$?
if [ $rc != $RC_SUCCESS ]; then
exit $rc
fi