blob: 5a87068b3074ce011b28063103a0180423554e44 [file] [log] [blame]
garciadeblas93c61312016-09-28 15:12:48 +02001#!/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
16function usage(){
17 echo -e "usage: $0 [OPTIONS]"
18 echo -e "Install OSM from source code"
19 echo -e " OPTIONS"
20 echo -e " --uninstall: uninstall OSM: remove the containers and delete NAT rules"
garciadeblas2a5a6512016-12-19 10:19:52 +010021 echo -e " -b <branch>: install OSM from source code using a specific branch (master, v1.0, ...)"
garciadeblas2a382732017-01-12 12:11:14 +010022 echo -e " -b master"
23 echo -e " -b v1.0"
24 echo -e " ..."
garciadeblas2a5a6512016-12-19 10:19:52 +010025 echo -e " --develop: (deprecated, use '-b master') install OSM from source code using the master branch"
garciadeblas93c61312016-09-28 15:12:48 +020026 echo -e " --nat: install only NAT rules"
garciadeblas2a382732017-01-12 12:11:14 +010027 echo -e " --update: update to the latest stable release or to the latest commit if using a specific branch"
28 echo -e " --showopts: show current options"
garciadeblas93c61312016-09-28 15:12:48 +020029 echo -e " -h / --help: print this help"
30}
31
garciadeblas55490d42016-10-29 14:22:03 +020032#Uninstall OSM: remove containers
33function uninstall(){
34 if [ $RC_CLONE ] || [ -n "$TEST_INSTALLER" ]; then
35 $OSM_DEVOPS/jenkins/host/clean_container RO
36 $OSM_DEVOPS/jenkins/host/clean_container VCA
37 $OSM_DEVOPS/jenkins/host/clean_container SO
38 #$OSM_DEVOPS/jenkins/host/clean_container UI
39 else
40 lxc stop RO && lxc delete RO
41 lxc stop VCA && lxc delete VCA
42 lxc stop SO-ub && lxc delete SO-ub
43 fi
44}
45
46#Configure NAT rules, based on the current IP addresses of containers
47function nat(){
48 echo -e "\nChecking required packages: iptables-persistent"
49 dpkg -l iptables-persistent &>/dev/null || ! echo -e " Not installed.\nInstalling iptables-persistent requires root privileges" || \
50 sudo DEBIAN_FRONTEND=noninteractive apt-get -yq install iptables-persistent
51 echo -e "\nConfiguring NAT rules"
52 echo -e " Required root privileges"
53 sudo $OSM_DEVOPS/installers/nat_osm
54}
55
garciadeblas2a382732017-01-12 12:11:14 +010056#Update RO, SO and UI:
57function update(){
58 echo -e "\nUpdating components"
59
60 echo -e " Updating RO"
61 CONTAINER="RO"
62 MDG="RO"
63 INSTALL_FOLDER="/opt/openmano"
64 echo -e " Fetching the repo"
65 lxc exec $CONTAINER -- git -C $INSTALL_FOLDER fetch --all
66 BRANCH=""
67 BRANCH=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER status -sb | head -n1 | sed -n 's/^## \(.*\).*/\1/p'|awk '{print $1}' |sed 's/\(.*\)\.\.\..*/\1/'`
68 [ -z "$BRANCH" ] && echo " Could not find the current branch in use in the $MDG" && exit 1
69 CURRENT=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER status |head -n1`
70 CURRENT_COMMIT_ID=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER rev-parse HEAD`
71 echo " FROM: $CURRENT ($CURRENT_COMMIT_ID)"
72 # COMMIT_ID either was previously set with -b option, or is an empty string
73 CHECKOUT_ID=$COMMIT_ID
74 [ -z "$CHECKOUT_ID" ] && [ "$BRANCH" == "HEAD" ] && CHECKOUT_ID="tags/$LATEST_STABLE_DEVOPS"
75 [ -z "$CHECKOUT_ID" ] && [ "$BRANCH" != "HEAD" ] && CHECKOUT_ID="$BRANCH"
76 if [[ $CHECKOUT_ID == "tags/"* ]]; then
77 REMOTE_COMMIT_ID=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER rev-list -n 1 $CHECKOUT_ID`
78 else
79 REMOTE_COMMIT_ID=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER rev-parse origin/$CHECKOUT_ID`
80 fi
81 echo " TO: $CHECKOUT_ID ($REMOTE_COMMIT_ID)"
82 if [ "$CURRENT_COMMIT_ID" == "$REMOTE_COMMIT_ID" ]; then
83 echo " Nothing to be done."
84 else
85 echo " Update required."
86 lxc exec $CONTAINER -- service openmano stop
87 lxc exec $CONTAINER -- git -C /opt/openmano stash
88 lxc exec $CONTAINER -- git -C /opt/openmano pull --rebase
89 lxc exec $CONTAINER -- git -C /opt/openmano checkout $CHECKOUT_ID
90 lxc exec $CONTAINER -- git -C /opt/openmano stash pop
91 lxc exec $CONTAINER -- /opt/openmano/database_utils/migrate_mano_db.sh
92 lxc exec $CONTAINER -- service openmano start
93 fi
94 echo
95
96 echo -e " Updating SO and UI"
97 CONTAINER="SO-ub"
98 MDG="SO"
99 INSTALL_FOLDER="" # To be filled in
100 echo -e " Fetching the repo"
101 lxc exec $CONTAINER -- git -C $INSTALL_FOLDER fetch --all
102 BRANCH=""
103 BRANCH=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER status -sb | head -n1 | sed -n 's/^## \(.*\).*/\1/p'|awk '{print $1}' |sed 's/\(.*\)\.\.\..*/\1/'`
104 [ -z "$BRANCH" ] && echo " Could not find the current branch in use in the $MDG" && exit 1
105 CURRENT=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER status |head -n1`
106 CURRENT_COMMIT_ID=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER rev-parse HEAD`
107 echo " FROM: $CURRENT ($CURRENT_COMMIT_ID)"
108 # COMMIT_ID either was previously set with -b option, or is an empty string
109 CHECKOUT_ID=$COMMIT_ID
110 [ -z "$CHECKOUT_ID" ] && [ "$BRANCH" == "HEAD" ] && CHECKOUT_ID="tags/$LATEST_STABLE_DEVOPS"
111 [ -z "$CHECKOUT_ID" ] && [ "$BRANCH" != "HEAD" ] && CHECKOUT_ID="$BRANCH"
112 if [[ $CHECKOUT_ID == "tags/"* ]]; then
113 REMOTE_COMMIT_ID=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER rev-list -n 1 $CHECKOUT_ID`
114 else
115 REMOTE_COMMIT_ID=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER rev-parse origin/$CHECKOUT_ID`
116 fi
117 echo " TO: $CHECKOUT_ID ($REMOTE_COMMIT_ID)"
118 if [ "$CURRENT_COMMIT_ID" == "$REMOTE_COMMIT_ID" ]; then
119 echo " Nothing to be done."
120 else
121 echo " Update required."
122 # Instructions to be added
123 # lxc exec SO-ub -- ...
124 fi
125 echo
126}
127
garciadeblas55490d42016-10-29 14:22:03 +0200128#Configure VCA, SO and RO with the initial configuration:
129# RO -> tenant:osm, logs to be sent to SO
130# VCA -> juju-password
131# SO -> route to Juju Controller, add RO account, add VCA account
132function configure(){
133 #Configure components
134 echo -e "\nConfiguring components"
135 . $OSM_DEVOPS/installers/export_ips
136
137 echo -e " Configuring RO"
138 lxc exec RO -- sed -i -e "s/^\#\?log_socket_host:.*/log_socket_host: $SO_CONTAINER_IP/g" /opt/openmano/openmanod.cfg
139 lxc exec RO -- service openmano restart
garciadeblasd8718f12016-10-30 19:39:01 +0100140 time=0; step=2; timelength=20; while [ $time -le $timelength ]; do sleep $step; echo -n "."; time=$((time+step)); done; echo
141 lxc exec RO -- openmano tenant-delete -f osm >/dev/null
garciadeblas55490d42016-10-29 14:22:03 +0200142 RO_TENANT_ID=`lxc exec RO -- openmano tenant-create osm |awk '{print $1}'`
143
144 echo -e " Configuring VCA"
145 JUJU_PASSWD=`date +%s | sha256sum | base64 | head -c 32`
146 echo -e "$JUJU_PASSWD\n$JUJU_PASSWD" | lxc exec VCA -- juju change-user-password
147 JUJU_CONTROLLER_IP=`lxc exec VCA -- lxc list -c 4 |grep eth0 |awk '{print $2}'`
148
149 echo -e " Configuring SO"
150 sudo route add -host $JUJU_CONTROLLER_IP gw $VCA_CONTAINER_IP
garciadeblasd05ae752016-12-07 12:13:00 +0100151 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 &
garciadeblasd8718f12016-10-30 19:39:01 +0100152 time=0; step=30; timelength=300; while [ $time -le $timelength ]; do sleep $step; echo -n "."; time=$((time+step)); done; echo
garciadeblas55490d42016-10-29 14:22:03 +0200153
154 curl -k --request POST \
155 --url https://$SO_CONTAINER_IP:8008/api/config/config-agent \
156 --header 'accept: application/vnd.yang.data+json' \
157 --header 'authorization: Basic YWRtaW46YWRtaW4=' \
158 --header 'cache-control: no-cache' \
159 --header 'content-type: application/vnd.yang.data+json' \
160 --data '{"account": [ { "name": "osmjuju", "account-type": "juju", "juju": { "ip-address": "'$JUJU_CONTROLLER_IP'", "port": "17070", "user": "admin", "secret": "'$JUJU_PASSWD'" } } ]}'
161
162 curl -k --request PUT \
163 --url https://$SO_CONTAINER_IP:8008/api/config/resource-orchestrator \
164 --header 'accept: application/vnd.yang.data+json' \
165 --header 'authorization: Basic YWRtaW46YWRtaW4=' \
166 --header 'cache-control: no-cache' \
167 --header 'content-type: application/vnd.yang.data+json' \
168 --data '{ "openmano": { "host": "'$RO_CONTAINER_IP'", "port": "9090", "tenant-id": "'$RO_TENANT_ID'" }, "name": "osmopenmano", "account-type": "openmano" }'
169
170}
171
garciadeblas2a5a6512016-12-19 10:19:52 +0100172function install_lxd() {
173 lxd init --auto
174 lxd waitready
175 systemctl stop lxd-bridge
176 systemctl --system daemon-reload
177 systemctl enable lxd-bridge
178 systemctl start lxd-bridge
179}
180
181
garciadeblas93c61312016-09-28 15:12:48 +0200182UNINSTALL=""
183DEVELOP=""
184NAT=""
garciadeblas2a382732017-01-12 12:11:14 +0100185UPDATE=""
garciadeblas55490d42016-10-29 14:22:03 +0200186RECONFIGURE=""
187TEST_INSTALLER=""
garciadeblas2a5a6512016-12-19 10:19:52 +0100188LXD=""
189SHOWOPTS=""
190COMMIT_ID=""
garciadeblas2a382732017-01-12 12:11:14 +0100191
garciadeblas2a5a6512016-12-19 10:19:52 +0100192while getopts ":h-:b:" o; do
garciadeblas93c61312016-09-28 15:12:48 +0200193 case "${o}" in
194 h)
195 usage && exit 0
196 ;;
garciadeblas2a5a6512016-12-19 10:19:52 +0100197 b)
198 COMMIT_ID=${OPTARG}
199 ;;
garciadeblas93c61312016-09-28 15:12:48 +0200200 -)
201 [ "${OPTARG}" == "help" ] && usage && exit 0
202 [ "${OPTARG}" == "develop" ] && DEVELOP="y" && continue
203 [ "${OPTARG}" == "uninstall" ] && UNINSTALL="y" && continue
204 [ "${OPTARG}" == "nat" ] && NAT="y" && continue
garciadeblas2a382732017-01-12 12:11:14 +0100205 [ "${OPTARG}" == "update" ] && UPDATE="y" && continue
garciadeblas55490d42016-10-29 14:22:03 +0200206 [ "${OPTARG}" == "reconfigure" ] && RECONFIGURE="y" && continue
207 [ "${OPTARG}" == "test" ] && TEST_INSTALLER="y" && continue
garciadeblas2a5a6512016-12-19 10:19:52 +0100208 [ "${OPTARG}" == "lxd" ] && LXD="y" && continue
209 [ "${OPTARG}" == "showopts" ] && SHOWOPTS="y" && continue
garciadeblas93c61312016-09-28 15:12:48 +0200210 echo -e "Invalid option: '--$OPTARG'\n" >&2
211 usage && exit 1
212 ;;
213 \?)
214 echo -e "Invalid option: '-$OPTARG'\n" >&2
215 usage && exit 1
216 ;;
217 *)
218 usage && exit 1
219 ;;
220 esac
221done
222
garciadeblas2a5a6512016-12-19 10:19:52 +0100223[ -z "$COMMIT_ID" ] && [ -n "$DEVELOP" ] && COMMIT_ID="master"
garciadeblas2a382732017-01-12 12:11:14 +0100224[ -n "$TEST_INSTALLER" ] && [ -z "$COMMIT_ID" ] && echo "Use -b option to specify the branch to use for the test (e.g.: v1.0)" && exit 0
garciadeblas2a5a6512016-12-19 10:19:52 +0100225
garciadeblas55490d42016-10-29 14:22:03 +0200226if [ -n "$TEST_INSTALLER" ]; then
227 echo -e "\nUsing local devops repo for OSM installation"
228 TEMPDIR="$(dirname $(realpath $(dirname $0)))"
229else
230 echo -e "\nCreating temporary dir for OSM installation"
231 TEMPDIR="$(mktemp -d -q --tmpdir "installosm.XXXXXX")"
232 trap 'rm -rf "$TEMPDIR"' EXIT
233fi
garciadeblas93c61312016-09-28 15:12:48 +0200234
garciadeblas95f164e2016-10-19 13:00:54 +0200235echo -e "Checking required packages: git"
236dpkg -l git &>/dev/null || ! echo -e " git not installed.\nInstalling git requires root privileges" || sudo apt install -y git
garciadeblas55490d42016-10-29 14:22:03 +0200237if [ -z "$TEST_INSTALLER" ]; then
238 echo -e "\nCloning devops repo temporarily"
239 git clone https://osm.etsi.org/gerrit/osm/devops.git $TEMPDIR
240 RC_CLONE=$?
garciadeblas55490d42016-10-29 14:22:03 +0200241fi
garciadeblas2a382732017-01-12 12:11:14 +0100242
243echo -e "\nGuessing the current stable release"
244LATEST_STABLE_DEVOPS=`git -C $TEMPDIR tag -l v[0-9].* | tail -n1`
245[ -z "$COMMIT_ID" ] && [ -z "$LATEST_STABLE_DEVOPS" ] && echo "Could not find the current latest stable release" && exit 0
246
247if [ -n "$SHOWOPTS" ]; then
248 echo "DEVELOP=$DEVELOP"
249 echo "UNINSTALL=$UNINSTALL"
250 echo "NAT=$NAT"
251 echo "UPDATE=$UPDATE"
252 echo "RECONFIGURE=$RECONFIGURE"
253 echo "TEST_INSTALLER=$TEST_INSTALLER"
254 echo "LXD=$LXD"
255 echo "SHOWOPTS=$SHOWOPTS"
256 echo "Latest tag in devops repo: $LATEST_STABLE_DEVOPS"
257 echo "Commit to be installed (-b): $COMMIT_ID"
258 exit 0
259fi
260
garciadeblas93c61312016-09-28 15:12:48 +0200261OSM_DEVOPS=$TEMPDIR
garciadeblas0fe45dd2016-10-04 07:54:17 +0200262OSM_JENKINS="$TEMPDIR/jenkins"
263. $OSM_JENKINS/common/all_funcs
garciadeblas93c61312016-09-28 15:12:48 +0200264
garciadeblasd8718f12016-10-30 19:39:01 +0100265[ -n "$UNINSTALL" ] && uninstall && echo -e "\nDONE" && exit 0
266[ -n "$NAT" ] && nat && echo -e "\nDONE" && exit 0
garciadeblas2a382732017-01-12 12:11:14 +0100267[ -n "$UPDATE" ] && update && echo -e "\nDONE" && exit 0
garciadeblasd8718f12016-10-30 19:39:01 +0100268[ -n "$RECONFIGURE" ] && configure && echo -e "\nDONE" && exit 0
garciadeblas93c61312016-09-28 15:12:48 +0200269
270#Installation starts here
garciadeblas2a382732017-01-12 12:11:14 +0100271[ -z "$COMMIT_ID" ] && [ -n "$LATEST_STABLE_DEVOPS" ] && COMMIT_ID="tags/$LATEST_STABLE_DEVOPS"
272echo -e "\n Installing OSM from refspec: $COMMIT_ID"
273
garciadeblas0fe45dd2016-10-04 07:54:17 +0200274wget -q -O- https://osm-download.etsi.org/ftp/osm-1.0-one/README.txt &> /dev/null
275
garciadeblas2a5a6512016-12-19 10:19:52 +0100276[ -n "$LXD" ] && echo -e "\nConfiguring lxd" && install_lxd
277
garciadeblas95f164e2016-10-19 13:00:54 +0200278echo -e "\nChecking required packages: wget, curl, tar"
279dpkg -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 install -y wget curl tar
garciadeblas93c61312016-09-28 15:12:48 +0200280
281echo -e "\nCreating the containers and building ..."
garciadeblasb9aec3e2017-01-20 14:28:17 +0100282$OSM_DEVOPS/jenkins/host/start_build RO --notest checkout $COMMIT_ID
garciadeblas93c61312016-09-28 15:12:48 +0200283$OSM_DEVOPS/jenkins/host/start_build VCA
garciadeblas0fe45dd2016-10-04 07:54:17 +0200284$OSM_DEVOPS/jenkins/host/start_build SO checkout $COMMIT_ID
285$OSM_DEVOPS/jenkins/host/start_build UI checkout $COMMIT_ID
garciadeblas93c61312016-09-28 15:12:48 +0200286
garciadeblas55490d42016-10-29 14:22:03 +0200287#Install iptables-persistent and configure NAT rules
288nat
garciadeblas93c61312016-09-28 15:12:48 +0200289
290#Configure components
garciadeblas55490d42016-10-29 14:22:03 +0200291configure
garciadeblas93c61312016-09-28 15:12:48 +0200292
garciadeblas0fe45dd2016-10-04 07:54:17 +0200293echo -e "\nDONE"
garciadeblas93c61312016-09-28 15:12:48 +0200294
295