Install from binaries, including renaming of installer, host install and install...
[osm/devops.git] / installers / install_osm.sh
1 #!/bin/bash
2 # Copyright 2016 Telefónica Investigación y Desarrollo S.A.U.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 function usage(){
17 echo -e "usage: $0 [OPTIONS]"
18 echo -e "Install OSM from binaries or source code (by default, from binaries)"
19 echo -e " OPTIONS"
20 echo -e " --uninstall: uninstall OSM: remove the containers and delete NAT rules"
21 echo -e " --source: install OSM from source code using the latest stable tag"
22 echo -e " -b <refspec>: install OSM from source code using a specific branch (master, v2.0, ...) or tag"
23 echo -e " -b master (main dev branch)"
24 echo -e " -b v2.0 (v2.0 branch)"
25 echo -e " -b tags/v1.1.0 (a specific tag)"
26 echo -e " ..."
27 echo -e " --develop: (deprecated, use '-b master') install OSM from source code using the master branch"
28 echo -e " --nat: install only NAT rules"
29 # echo -e " --update: update to the latest stable release or to the latest commit if using a specific branch"
30 echo -e " --showopts: print chosen options and exit (only for debugging)"
31 echo -e " -y: do not prompt for confirmation, assumes yes"
32 echo -e " -h / --help: print this help"
33 }
34
35 #Uninstall OSM: remove containers
36 function uninstall(){
37 echo -e "\nUninstalling OSM"
38 if [ $RC_CLONE ] || [ -n "$TEST_INSTALLER" ]; then
39 $OSM_DEVOPS/jenkins/host/clean_container RO
40 $OSM_DEVOPS/jenkins/host/clean_container VCA
41 $OSM_DEVOPS/jenkins/host/clean_container SO
42 #$OSM_DEVOPS/jenkins/host/clean_container UI
43 else
44 lxc stop RO && lxc delete RO
45 lxc stop VCA && lxc delete VCA
46 lxc stop SO-ub && lxc delete SO-ub
47 fi
48 }
49
50 #Configure NAT rules, based on the current IP addresses of containers
51 function nat(){
52 echo -e "\nChecking required packages: iptables-persistent"
53 dpkg -l iptables-persistent &>/dev/null || ! echo -e " Not installed.\nInstalling iptables-persistent requires root privileges" || \
54 sudo DEBIAN_FRONTEND=noninteractive apt-get -yq install iptables-persistent
55 echo -e "\nConfiguring NAT rules"
56 echo -e " Required root privileges"
57 sudo $OSM_DEVOPS/installers/nat_osm
58 }
59
60 #Update RO, SO and UI:
61 function update(){
62 echo -e "\nUpdating components"
63
64 echo -e " Updating RO"
65 CONTAINER="RO"
66 MDG="RO"
67 INSTALL_FOLDER="/opt/openmano"
68 echo -e " Fetching the repo"
69 lxc exec $CONTAINER -- git -C $INSTALL_FOLDER fetch --all
70 BRANCH=""
71 BRANCH=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER status -sb | head -n1 | sed -n 's/^## \(.*\).*/\1/p'|awk '{print $1}' |sed 's/\(.*\)\.\.\..*/\1/'`
72 [ -z "$BRANCH" ] && FATAL "Could not find the current branch in use in the '$MDG'"
73 CURRENT=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER status |head -n1`
74 CURRENT_COMMIT_ID=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER rev-parse HEAD`
75 echo " FROM: $CURRENT ($CURRENT_COMMIT_ID)"
76 # COMMIT_ID either was previously set with -b option, or is an empty string
77 CHECKOUT_ID=$COMMIT_ID
78 [ -z "$CHECKOUT_ID" ] && [ "$BRANCH" == "HEAD" ] && CHECKOUT_ID="tags/$LATEST_STABLE_DEVOPS"
79 [ -z "$CHECKOUT_ID" ] && [ "$BRANCH" != "HEAD" ] && CHECKOUT_ID="$BRANCH"
80 if [[ $CHECKOUT_ID == "tags/"* ]]; then
81 REMOTE_COMMIT_ID=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER rev-list -n 1 $CHECKOUT_ID`
82 else
83 REMOTE_COMMIT_ID=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER rev-parse origin/$CHECKOUT_ID`
84 fi
85 echo " TO: $CHECKOUT_ID ($REMOTE_COMMIT_ID)"
86 if [ "$CURRENT_COMMIT_ID" == "$REMOTE_COMMIT_ID" ]; then
87 echo " Nothing to be done."
88 else
89 echo " Update required."
90 lxc exec $CONTAINER -- service osm-ro stop
91 lxc exec $CONTAINER -- git -C /opt/openmano stash
92 lxc exec $CONTAINER -- git -C /opt/openmano pull --rebase
93 lxc exec $CONTAINER -- git -C /opt/openmano checkout $CHECKOUT_ID
94 lxc exec $CONTAINER -- git -C /opt/openmano stash pop
95 lxc exec $CONTAINER -- /opt/openmano/database_utils/migrate_mano_db.sh
96 lxc exec $CONTAINER -- service osm-ro start
97 fi
98 echo
99
100 echo -e " Updating SO and UI"
101 CONTAINER="SO-ub"
102 MDG="SO"
103 INSTALL_FOLDER="" # To be filled in
104 echo -e " Fetching the repo"
105 lxc exec $CONTAINER -- git -C $INSTALL_FOLDER fetch --all
106 BRANCH=""
107 BRANCH=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER status -sb | head -n1 | sed -n 's/^## \(.*\).*/\1/p'|awk '{print $1}' |sed 's/\(.*\)\.\.\..*/\1/'`
108 [ -z "$BRANCH" ] && FATAL "Could not find the current branch in use in the '$MDG'"
109 CURRENT=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER status |head -n1`
110 CURRENT_COMMIT_ID=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER rev-parse HEAD`
111 echo " FROM: $CURRENT ($CURRENT_COMMIT_ID)"
112 # COMMIT_ID either was previously set with -b option, or is an empty string
113 CHECKOUT_ID=$COMMIT_ID
114 [ -z "$CHECKOUT_ID" ] && [ "$BRANCH" == "HEAD" ] && CHECKOUT_ID="tags/$LATEST_STABLE_DEVOPS"
115 [ -z "$CHECKOUT_ID" ] && [ "$BRANCH" != "HEAD" ] && CHECKOUT_ID="$BRANCH"
116 if [[ $CHECKOUT_ID == "tags/"* ]]; then
117 REMOTE_COMMIT_ID=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER rev-list -n 1 $CHECKOUT_ID`
118 else
119 REMOTE_COMMIT_ID=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER rev-parse origin/$CHECKOUT_ID`
120 fi
121 echo " TO: $CHECKOUT_ID ($REMOTE_COMMIT_ID)"
122 if [ "$CURRENT_COMMIT_ID" == "$REMOTE_COMMIT_ID" ]; then
123 echo " Nothing to be done."
124 else
125 echo " Update required."
126 # Instructions to be added
127 # lxc exec SO-ub -- ...
128 fi
129 echo
130 }
131
132 #Configure VCA, SO and RO with the initial configuration:
133 # RO -> tenant:osm, logs to be sent to SO
134 # VCA -> juju-password
135 # SO -> route to Juju Controller, add RO account, add VCA account
136 function configure(){
137 #Configure components
138 echo -e "\nConfiguring components"
139 . $OSM_DEVOPS/installers/export_ips
140
141 echo -e " Configuring RO"
142 lxc exec RO -- sed -i -e "s/^\#\?log_socket_host:.*/log_socket_host: $SO_CONTAINER_IP/g" /etc/osm/openmanod.cfg
143 lxc exec RO -- service osm-ro restart
144 time=0; step=2; timelength=20; while [ $time -le $timelength ]; do sleep $step; echo -n "."; time=$((time+step)); done; echo
145 lxc exec RO -- openmano tenant-delete -f osm >/dev/null
146 RO_TENANT_ID=`lxc exec RO -- openmano tenant-create osm |awk '{print $1}'`
147
148 echo -e " Configuring VCA"
149 JUJU_PASSWD=`date +%s | sha256sum | base64 | head -c 32`
150 echo -e "$JUJU_PASSWD\n$JUJU_PASSWD" | lxc exec VCA -- juju change-user-password
151 JUJU_CONTROLLER_IP=`lxc exec VCA -- lxc list -c 4 |grep eth0 |awk '{print $2}'`
152
153 echo -e " Configuring SO"
154 sudo route add -host $JUJU_CONTROLLER_IP gw $VCA_CONTAINER_IP
155 sudo sed -i "$ i route add -host $JUJU_CONTROLLER_IP gw $VCA_CONTAINER_IP" /etc/rc.local
156 lxc exec SO-ub -- nohup sudo -b -H /usr/rift/rift-shell -r -i /usr/rift -a /usr/rift/.artifacts -- ./demos/launchpad.py --use-xml-mode &
157 time=0; step=30; timelength=300; while [ $time -le $timelength ]; do sleep $step; echo -n "."; time=$((time+step)); done; echo
158
159 curl -k --request POST \
160 --url https://$SO_CONTAINER_IP:8008/api/config/config-agent \
161 --header 'accept: application/vnd.yang.data+json' \
162 --header 'authorization: Basic YWRtaW46YWRtaW4=' \
163 --header 'cache-control: no-cache' \
164 --header 'content-type: application/vnd.yang.data+json' \
165 --data '{"account": [ { "name": "osmjuju", "account-type": "juju", "juju": { "ip-address": "'$JUJU_CONTROLLER_IP'", "port": "17070", "user": "admin", "secret": "'$JUJU_PASSWD'" } } ]}'
166
167 curl -k --request PUT \
168 --url https://$SO_CONTAINER_IP:8008/api/config/resource-orchestrator \
169 --header 'accept: application/vnd.yang.data+json' \
170 --header 'authorization: Basic YWRtaW46YWRtaW4=' \
171 --header 'cache-control: no-cache' \
172 --header 'content-type: application/vnd.yang.data+json' \
173 --data '{ "openmano": { "host": "'$RO_CONTAINER_IP'", "port": "9090", "tenant-id": "'$RO_TENANT_ID'" }, "name": "osmopenmano", "account-type": "openmano" }'
174
175 }
176
177 function install_lxd() {
178 lxd init --auto
179 lxd waitready
180 systemctl stop lxd-bridge
181 systemctl --system daemon-reload
182 systemctl enable lxd-bridge
183 systemctl start lxd-bridge
184 }
185
186
187 UNINSTALL=""
188 DEVELOP=""
189 NAT=""
190 UPDATE=""
191 RECONFIGURE=""
192 TEST_INSTALLER=""
193 LXD=""
194 SHOWOPTS=""
195 COMMIT_ID=""
196 ASSUME_YES=""
197 INSTALL_FROM_SOURCE=""
198
199 while getopts ":hy-:b:" o; do
200 case "${o}" in
201 h)
202 usage && exit 0
203 ;;
204 b)
205 COMMIT_ID=${OPTARG}
206 ;;
207 -)
208 [ "${OPTARG}" == "help" ] && usage && exit 0
209 [ "${OPTARG}" == "source" ] && INSTALL_FROM_SOURCE="y" && continue
210 [ "${OPTARG}" == "develop" ] && DEVELOP="y" && continue
211 [ "${OPTARG}" == "uninstall" ] && UNINSTALL="y" && continue
212 [ "${OPTARG}" == "nat" ] && NAT="y" && continue
213 [ "${OPTARG}" == "update" ] && UPDATE="y" && continue
214 [ "${OPTARG}" == "reconfigure" ] && RECONFIGURE="y" && continue
215 [ "${OPTARG}" == "test" ] && TEST_INSTALLER="y" && continue
216 [ "${OPTARG}" == "lxd" ] && LXD="y" && continue
217 [ "${OPTARG}" == "showopts" ] && SHOWOPTS="y" && continue
218 echo -e "Invalid option: '--$OPTARG'\n" >&2
219 usage && exit 1
220 ;;
221 \?)
222 echo -e "Invalid option: '-$OPTARG'\n" >&2
223 usage && exit 1
224 ;;
225 y)
226 ASSUME_YES="y"
227 ;;
228 *)
229 usage && exit 1
230 ;;
231 esac
232 done
233
234 if [ -n "$SHOWOPTS" ]; then
235 echo "DEVELOP=$DEVELOP"
236 echo "INSTALL_FROM_SOURCE=$INSTALL_FROM_SOURCE"
237 echo "UNINSTALL=$UNINSTALL"
238 echo "NAT=$NAT"
239 echo "UPDATE=$UPDATE"
240 echo "RECONFIGURE=$RECONFIGURE"
241 echo "TEST_INSTALLER=$TEST_INSTALLER"
242 echo "LXD=$LXD"
243 echo "SHOWOPTS=$SHOWOPTS"
244 echo "Install from specific refspec (-b): $COMMIT_ID"
245 exit 0
246 fi
247
248 [ -z "$COMMIT_ID" ] && [ -n "$DEVELOP" ] && COMMIT_ID="master"
249 [ -n "$COMMIT_ID" ] && INSTALL_FROM_SOURCE="y"
250
251 if [ -n "$TEST_INSTALLER" ]; then
252 echo -e "\nUsing local devops repo for OSM installation"
253 TEMPDIR="$(dirname $(realpath $(dirname $0)))"
254 else
255 echo -e "\nCreating temporary dir for OSM installation"
256 TEMPDIR="$(mktemp -d -q --tmpdir "installosm.XXXXXX")"
257 trap 'rm -rf "$TEMPDIR"' EXIT
258 fi
259
260 echo -e "Checking required packages: git"
261 dpkg -l git &>/dev/null || ! echo -e " git not installed.\nInstalling git requires root privileges" || sudo apt-get install -y git
262 if [ -z "$TEST_INSTALLER" ]; then
263 echo -e "\nCloning devops repo temporarily"
264 git clone https://osm.etsi.org/gerrit/osm/devops.git $TEMPDIR
265 RC_CLONE=$?
266 fi
267
268 echo -e "\nGuessing the current stable release"
269 LATEST_STABLE_DEVOPS=`git -C $TEMPDIR tag -l v[0-9].* | tail -n1`
270 [ -z "$COMMIT_ID" ] && [ -z "$LATEST_STABLE_DEVOPS" ] && echo "Could not find the current latest stable release" && exit 0
271 echo "Latest tag in devops repo: $LATEST_STABLE_DEVOPS"
272 [ -z "$COMMIT_ID" ] && [ -n "$LATEST_STABLE_DEVOPS" ] && COMMIT_ID="tags/$LATEST_STABLE_DEVOPS"
273 [ -z "$TEST_INSTALLER" ] && git -C $TEMPDIR checkout tags/$LATEST_STABLE_DEVOPS
274
275 OSM_DEVOPS=$TEMPDIR
276 OSM_JENKINS="$TEMPDIR/jenkins"
277 . $OSM_JENKINS/common/all_funcs
278
279 [ -n "$UNINSTALL" ] && uninstall && echo -e "\nDONE" && exit 0
280 [ -n "$NAT" ] && nat && echo -e "\nDONE" && exit 0
281 [ -n "$UPDATE" ] && update && echo -e "\nDONE" && exit 0
282 [ -n "$RECONFIGURE" ] && configure && echo -e "\nDONE" && exit 0
283
284 #Installation starts here
285 echo -e "\nInstalling OSM from refspec: $COMMIT_ID"
286 if [ -n "$INSTALL_FROM_SOURCE" ] && [ -z "$ASSUME_YES" ]; then
287 read -e -p "The installation will take about 75-90 minutes. Continue (Y/n)?" USER_CONFIRMATION
288 [ -n "$USER_CONFIRMATION" ] && [ "$USER_CONFIRMATION" != "yes" ] && \
289 [ "$USER_CONFIRMATION" != "y" ] && echo "Cancelled!" && exit 0
290 fi
291
292 echo -e "\nChecking required packages: wget, curl, tar"
293 dpkg -l wget curl tar &>/dev/null || ! echo -e " One or several packages are not installed.\nInstalling required packages\n Root privileges are required" || sudo apt-get install -y wget curl tar
294
295 echo -e "Checking required packages: lxd"
296 lxd --version &>/dev/null || FATAL "lxd not present, exiting."
297 [ -n "$LXD" ] && echo -e "\nConfiguring lxd" && install_lxd
298
299 wget -q -O- https://osm-download.etsi.org/ftp/osm-2.0-two/README.txt &> /dev/null
300
301 if [ -z "$INSTALL_FROM_SOURCE" ]; then
302 echo -e "\nCreating the containers and installing from binaries ..."
303 $OSM_DEVOPS/jenkins/host/install RO || FATAL "RO install failed"
304 $OSM_DEVOPS/jenkins/host/start_build VCA || FATAL "VCA install failed"
305 $OSM_DEVOPS/jenkins/host/install SO || FATAL "SO install failed"
306 $OSM_DEVOPS/jenkins/host/install UI || FATAL "UI install failed"
307 else #install from source
308 echo -e "\nCreating the containers and building from source ..."
309 $OSM_DEVOPS/jenkins/host/start_build RO --notest checkout $COMMIT_ID || FATAL "RO container build failed (refspec: '$COMMIT_ID')"
310 $OSM_DEVOPS/jenkins/host/start_build VCA || FATAL "VCA container build failed"
311 $OSM_DEVOPS/jenkins/host/start_build SO checkout $COMMIT_ID || FATAL "SO container build failed (refspec: '$COMMIT_ID')"
312 $OSM_DEVOPS/jenkins/host/start_build UI checkout $COMMIT_ID || FATAL "UI container build failed (refspec: '$COMMIT_ID')"
313 fi
314
315 #Install iptables-persistent and configure NAT rules
316 nat
317
318 #Configure components
319 configure
320
321 wget -q -O- https://osm-download.etsi.org/ftp/osm-2.0-two/README2.txt &> /dev/null
322 echo -e "\nDONE"