blob: 9fb7d81f07070ff3e76929c7390ac0d94190e314 [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]"
garciadeblasa1fc4572017-04-24 19:08:21 +020018 echo -e "Install OSM from binaries or source code (by default, from binaries)"
garciadeblas93c61312016-09-28 15:12:48 +020019 echo -e " OPTIONS"
garciadeblas0d934c12017-11-17 15:39:17 +010020 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"
Mike Marchetti425f8ce2017-06-15 13:02:16 -040022 echo -e " -r <repo>: use specified repository name for osm packages"
garciadeblas0d934c12017-11-17 15:39:17 +010023 echo -e " -R <release>: use specified release for osm binaries (deb packages, lxd images, ...)"
Mike Marchetti425f8ce2017-06-15 13:02:16 -040024 echo -e " -u <repo base>: use specified repository url for osm packages"
25 echo -e " -k <repo key>: use specified repository public key url"
garciadeblas0d934c12017-11-17 15:39:17 +010026 echo -e " -b <refspec>: install OSM from source code using a specific branch (master, v2.0, ...) or tag"
27 echo -e " -b master (main dev branch)"
28 echo -e " -b v2.0 (v2.0 branch)"
29 echo -e " -b tags/v1.1.0 (a specific tag)"
30 echo -e " ..."
31 echo -e " --lxdimages: download lxd images from OSM repository instead of creating them from scratch"
32 echo -e " -l <lxd_repo>: use specified repository url for lxd images"
33 echo -e " --develop: (deprecated, use '-b master') install OSM from source code using the master branch"
34# echo -e " --reconfigure: reconfigure the modules (DO NOT change NAT rules)"
35 echo -e " --nat: install only NAT rules"
36 echo -e " --noconfigure: DO NOT install osmclient, DO NOT install NAT rules, DO NOT configure modules"
37# echo -e " --update: update to the latest stable release or to the latest commit if using a specific branch"
38 echo -e " --showopts: print chosen options and exit (only for debugging)"
39 echo -e " -y: do not prompt for confirmation, assumes yes"
40 echo -e " -h / --help: print this help"
garciadeblas93c61312016-09-28 15:12:48 +020041}
42
garciadeblas55490d42016-10-29 14:22:03 +020043#Uninstall OSM: remove containers
44function uninstall(){
garciadeblasa1fc4572017-04-24 19:08:21 +020045 echo -e "\nUninstalling OSM"
garciadeblas55490d42016-10-29 14:22:03 +020046 if [ $RC_CLONE ] || [ -n "$TEST_INSTALLER" ]; then
47 $OSM_DEVOPS/jenkins/host/clean_container RO
48 $OSM_DEVOPS/jenkins/host/clean_container VCA
49 $OSM_DEVOPS/jenkins/host/clean_container SO
50 #$OSM_DEVOPS/jenkins/host/clean_container UI
51 else
52 lxc stop RO && lxc delete RO
53 lxc stop VCA && lxc delete VCA
54 lxc stop SO-ub && lxc delete SO-ub
55 fi
56}
57
58#Configure NAT rules, based on the current IP addresses of containers
59function nat(){
60 echo -e "\nChecking required packages: iptables-persistent"
61 dpkg -l iptables-persistent &>/dev/null || ! echo -e " Not installed.\nInstalling iptables-persistent requires root privileges" || \
62 sudo DEBIAN_FRONTEND=noninteractive apt-get -yq install iptables-persistent
63 echo -e "\nConfiguring NAT rules"
64 echo -e " Required root privileges"
65 sudo $OSM_DEVOPS/installers/nat_osm
66}
67
Mike Marchettid8577f52017-10-19 15:27:48 -040068function FATAL(){
69 echo "FATAL error: Cannot install OSM due to \"$1\""
70 exit 1
71}
72
garciadeblas2a382732017-01-12 12:11:14 +010073#Update RO, SO and UI:
74function update(){
75 echo -e "\nUpdating components"
76
77 echo -e " Updating RO"
78 CONTAINER="RO"
79 MDG="RO"
80 INSTALL_FOLDER="/opt/openmano"
81 echo -e " Fetching the repo"
82 lxc exec $CONTAINER -- git -C $INSTALL_FOLDER fetch --all
83 BRANCH=""
84 BRANCH=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER status -sb | head -n1 | sed -n 's/^## \(.*\).*/\1/p'|awk '{print $1}' |sed 's/\(.*\)\.\.\..*/\1/'`
Jokin Garayc17db012017-03-08 17:45:39 +010085 [ -z "$BRANCH" ] && FATAL "Could not find the current branch in use in the '$MDG'"
garciadeblas2a382732017-01-12 12:11:14 +010086 CURRENT=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER status |head -n1`
87 CURRENT_COMMIT_ID=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER rev-parse HEAD`
88 echo " FROM: $CURRENT ($CURRENT_COMMIT_ID)"
89 # COMMIT_ID either was previously set with -b option, or is an empty string
90 CHECKOUT_ID=$COMMIT_ID
91 [ -z "$CHECKOUT_ID" ] && [ "$BRANCH" == "HEAD" ] && CHECKOUT_ID="tags/$LATEST_STABLE_DEVOPS"
92 [ -z "$CHECKOUT_ID" ] && [ "$BRANCH" != "HEAD" ] && CHECKOUT_ID="$BRANCH"
93 if [[ $CHECKOUT_ID == "tags/"* ]]; then
94 REMOTE_COMMIT_ID=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER rev-list -n 1 $CHECKOUT_ID`
95 else
96 REMOTE_COMMIT_ID=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER rev-parse origin/$CHECKOUT_ID`
97 fi
98 echo " TO: $CHECKOUT_ID ($REMOTE_COMMIT_ID)"
99 if [ "$CURRENT_COMMIT_ID" == "$REMOTE_COMMIT_ID" ]; then
100 echo " Nothing to be done."
101 else
102 echo " Update required."
garciadeblasa1fc4572017-04-24 19:08:21 +0200103 lxc exec $CONTAINER -- service osm-ro stop
garciadeblas2a382732017-01-12 12:11:14 +0100104 lxc exec $CONTAINER -- git -C /opt/openmano stash
105 lxc exec $CONTAINER -- git -C /opt/openmano pull --rebase
106 lxc exec $CONTAINER -- git -C /opt/openmano checkout $CHECKOUT_ID
107 lxc exec $CONTAINER -- git -C /opt/openmano stash pop
108 lxc exec $CONTAINER -- /opt/openmano/database_utils/migrate_mano_db.sh
garciadeblasa1fc4572017-04-24 19:08:21 +0200109 lxc exec $CONTAINER -- service osm-ro start
garciadeblas2a382732017-01-12 12:11:14 +0100110 fi
111 echo
112
113 echo -e " Updating SO and UI"
114 CONTAINER="SO-ub"
115 MDG="SO"
116 INSTALL_FOLDER="" # To be filled in
117 echo -e " Fetching the repo"
118 lxc exec $CONTAINER -- git -C $INSTALL_FOLDER fetch --all
119 BRANCH=""
120 BRANCH=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER status -sb | head -n1 | sed -n 's/^## \(.*\).*/\1/p'|awk '{print $1}' |sed 's/\(.*\)\.\.\..*/\1/'`
Jokin Garayc17db012017-03-08 17:45:39 +0100121 [ -z "$BRANCH" ] && FATAL "Could not find the current branch in use in the '$MDG'"
garciadeblas2a382732017-01-12 12:11:14 +0100122 CURRENT=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER status |head -n1`
123 CURRENT_COMMIT_ID=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER rev-parse HEAD`
124 echo " FROM: $CURRENT ($CURRENT_COMMIT_ID)"
125 # COMMIT_ID either was previously set with -b option, or is an empty string
126 CHECKOUT_ID=$COMMIT_ID
127 [ -z "$CHECKOUT_ID" ] && [ "$BRANCH" == "HEAD" ] && CHECKOUT_ID="tags/$LATEST_STABLE_DEVOPS"
128 [ -z "$CHECKOUT_ID" ] && [ "$BRANCH" != "HEAD" ] && CHECKOUT_ID="$BRANCH"
129 if [[ $CHECKOUT_ID == "tags/"* ]]; then
130 REMOTE_COMMIT_ID=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER rev-list -n 1 $CHECKOUT_ID`
131 else
132 REMOTE_COMMIT_ID=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER rev-parse origin/$CHECKOUT_ID`
133 fi
134 echo " TO: $CHECKOUT_ID ($REMOTE_COMMIT_ID)"
135 if [ "$CURRENT_COMMIT_ID" == "$REMOTE_COMMIT_ID" ]; then
136 echo " Nothing to be done."
137 else
138 echo " Update required."
139 # Instructions to be added
140 # lxc exec SO-ub -- ...
141 fi
142 echo
143}
144
Mike Marchetti2b951282017-10-10 15:43:15 -0400145function so_is_up() {
garciadeblas0d934c12017-11-17 15:39:17 +0100146 if [ -n "$1" ]; then
147 SO_IP=$1
148 else
149 SO_IP=`lxc list SO-ub -c 4|grep eth0 |awk '{print $2}'`
150 fi
Mike Marchetti6930bc02017-05-31 16:33:02 -0400151 time=0
152 step=5
153 timelength=300
154 while [ $time -le $timelength ]
155 do
Mike Marchetti2b951282017-10-10 15:43:15 -0400156 if [[ `curl -k -X GET https://$SO_IP:8008/api/operational/vcs/info \
157 -H 'accept: application/vnd.yang.data+json' \
158 -H 'authorization: Basic YWRtaW46YWRtaW4=' \
159 -H 'cache-control: no-cache' 2> /dev/null | jq '.[].components.component_info[] | select(.component_name=="RW.Restconf")' 2>/dev/null | grep "RUNNING" | wc -l` -eq 1 ]]
160 then
161 echo "RW.Restconf running....SO is up"
162 return 0
Mike Marchetti6930bc02017-05-31 16:33:02 -0400163 fi
Mike Marchetti2b951282017-10-10 15:43:15 -0400164
Mike Marchetti6930bc02017-05-31 16:33:02 -0400165 sleep $step
166 echo -n "."
167 time=$((time+step))
168 done
Mike Marchetti2b951282017-10-10 15:43:15 -0400169
garciadeblas0d934c12017-11-17 15:39:17 +0100170 FATAL "OSM Failed to startup. SO failed to startup"
Mike Marchetti6930bc02017-05-31 16:33:02 -0400171}
172
garciadeblas0d934c12017-11-17 15:39:17 +0100173function vca_is_up() {
174 if [[ `lxc exec VCA -- juju status | grep "osm" | wc -l` -eq 1 ]]; then
175 echo "VCA is up and running"
176 return 0
177 fi
garciadeblas55490d42016-10-29 14:22:03 +0200178
garciadeblas0d934c12017-11-17 15:39:17 +0100179 FATAL "OSM Failed to startup. VCA failed to startup"
180}
181
182function ro_is_up() {
183 if [ -n "$1" ]; then
184 RO_IP=$1
185 else
186 RO_IP=`lxc list RO -c 4|grep eth0 |awk '{print $2}'`
187 fi
188 time=0
189 step=2
190 timelength=20
191 while [ $time -le $timelength ]; do
192 if [[ `curl http://$RO_IP:9090/openmano/ | grep "works" | wc -l` -eq 1 ]]; then
193 echo "RO is up and running"
194 return 0
195 fi
196 sleep $step
197 echo -n "."
198 time=$((time+step))
199 done
200
201 FATAL "OSM Failed to startup. RO failed to startup"
202}
203
204
205function configure_RO(){
206 . $OSM_DEVOPS/installers/export_ips
garciadeblas55490d42016-10-29 14:22:03 +0200207 echo -e " Configuring RO"
garciadeblasa1fc4572017-04-24 19:08:21 +0200208 lxc exec RO -- sed -i -e "s/^\#\?log_socket_host:.*/log_socket_host: $SO_CONTAINER_IP/g" /etc/osm/openmanod.cfg
209 lxc exec RO -- service osm-ro restart
Mike Marchetti6930bc02017-05-31 16:33:02 -0400210
garciadeblas0d934c12017-11-17 15:39:17 +0100211 ro_is_up
Mike Marchetti6930bc02017-05-31 16:33:02 -0400212
garciadeblasd8718f12016-10-30 19:39:01 +0100213 lxc exec RO -- openmano tenant-delete -f osm >/dev/null
tierno15f4f222017-06-14 13:23:01 +0200214 lxc exec RO -- sed -i '/export OPENMANO_TENANT=osm/d' .bashrc
215 lxc exec RO -- sed -i '$ i export OPENMANO_TENANT=osm' .bashrc
216 #lxc exec RO -- sh -c 'echo "export OPENMANO_TENANT=osm" >> .bashrc'
garciadeblas0d934c12017-11-17 15:39:17 +0100217}
garciadeblas55490d42016-10-29 14:22:03 +0200218
garciadeblas0d934c12017-11-17 15:39:17 +0100219function configure_VCA(){
garciadeblas55490d42016-10-29 14:22:03 +0200220 echo -e " Configuring VCA"
221 JUJU_PASSWD=`date +%s | sha256sum | base64 | head -c 32`
222 echo -e "$JUJU_PASSWD\n$JUJU_PASSWD" | lxc exec VCA -- juju change-user-password
garciadeblas0d934c12017-11-17 15:39:17 +0100223}
224
225function configure_SOUI(){
226 . $OSM_DEVOPS/installers/export_ips
garciadeblas55490d42016-10-29 14:22:03 +0200227 JUJU_CONTROLLER_IP=`lxc exec VCA -- lxc list -c 4 |grep eth0 |awk '{print $2}'`
garciadeblas0d934c12017-11-17 15:39:17 +0100228 RO_TENANT_ID=`lxc exec RO -- openmano tenant-create osm |awk '{print $1}'`
garciadeblas55490d42016-10-29 14:22:03 +0200229
230 echo -e " Configuring SO"
231 sudo route add -host $JUJU_CONTROLLER_IP gw $VCA_CONTAINER_IP
garciadeblas2f4c93b2017-04-10 18:12:12 +0200232 sudo sed -i "$ i route add -host $JUJU_CONTROLLER_IP gw $VCA_CONTAINER_IP" /etc/rc.local
Jeremy Mordkoff5cc6ccc2017-10-05 15:21:07 -0400233 # make journaling persistent
234 lxc exec SO-ub -- mkdir -p /var/log/journal
235 lxc exec SO-ub -- systemd-tmpfiles --create --prefix /var/log/journal
236 lxc exec SO-ub -- systemctl restart systemd-journald
237
Jeremy Mordkoff3b3edea2017-10-03 16:21:56 -0400238 echo RIFT_EXTERNAL_ADDRESS=$DEFAULT_IP | lxc exec SO-ub -- tee -a /usr/rift/etc/default/launchpad
239
Mike Marchetti6930bc02017-05-31 16:33:02 -0400240 lxc exec SO-ub -- systemctl restart launchpad
garciadeblas55490d42016-10-29 14:22:03 +0200241
Mike Marchetti6930bc02017-05-31 16:33:02 -0400242 so_is_up $SO_CONTAINER_IP
243
244 #delete existing config agent (could be there on reconfigure)
245 curl -k --request DELETE \
246 --url https://$SO_CONTAINER_IP:8008/api/config/config-agent/account/osmjuju \
247 --header 'accept: application/vnd.yang.data+json' \
248 --header 'authorization: Basic YWRtaW46YWRtaW4=' \
249 --header 'cache-control: no-cache' \
250 --header 'content-type: application/vnd.yang.data+json' &> /dev/null
251
252 result=$(curl -k --request POST \
garciadeblas55490d42016-10-29 14:22:03 +0200253 --url https://$SO_CONTAINER_IP:8008/api/config/config-agent \
254 --header 'accept: application/vnd.yang.data+json' \
255 --header 'authorization: Basic YWRtaW46YWRtaW4=' \
256 --header 'cache-control: no-cache' \
257 --header 'content-type: application/vnd.yang.data+json' \
Mike Marchetti6930bc02017-05-31 16:33:02 -0400258 --data '{"account": [ { "name": "osmjuju", "account-type": "juju", "juju": { "ip-address": "'$JUJU_CONTROLLER_IP'", "port": "17070", "user": "admin", "secret": "'$JUJU_PASSWD'" } } ]}')
259 [[ $result =~ .*success.* ]] || FATAL "Failed config-agent configuration: $result"
garciadeblas55490d42016-10-29 14:22:03 +0200260
Mike Marchetti397a65d2017-10-02 22:36:16 -0400261 #R1/R2 config line
262 #result=$(curl -k --request PUT \
263 # --url https://$SO_CONTAINER_IP:8008/api/config/resource-orchestrator \
264 # --header 'accept: application/vnd.yang.data+json' \
265 # --header 'authorization: Basic YWRtaW46YWRtaW4=' \
266 # --header 'cache-control: no-cache' \
267 # --header 'content-type: application/vnd.yang.data+json' \
268 # --data '{ "openmano": { "host": "'$RO_CONTAINER_IP'", "port": "9090", "tenant-id": "'$RO_TENANT_ID'" }, "name": "osmopenmano", "account-type": "openmano" }')
269
Mike Marchetti6930bc02017-05-31 16:33:02 -0400270 result=$(curl -k --request PUT \
Mike Marchetti397a65d2017-10-02 22:36:16 -0400271 --url https://$SO_CONTAINER_IP:8008/api/config/project/default/ro-account/account \
garciadeblas55490d42016-10-29 14:22:03 +0200272 --header 'accept: application/vnd.yang.data+json' \
273 --header 'authorization: Basic YWRtaW46YWRtaW4=' \
Mike Marchetti397a65d2017-10-02 22:36:16 -0400274 --header 'cache-control: no-cache' \
garciadeblas55490d42016-10-29 14:22:03 +0200275 --header 'content-type: application/vnd.yang.data+json' \
Mike Marchetti397a65d2017-10-02 22:36:16 -0400276 --data '{"rw-ro-account:account": [ { "openmano": { "host": "'$RO_CONTAINER_IP'", "port": "9090", "tenant-id": "'$RO_TENANT_ID'"}, "name": "osmopenmano", "ro-account-type": "openmano" }]}')
Mike Marchetti6930bc02017-05-31 16:33:02 -0400277 [[ $result =~ .*success.* ]] || FATAL "Failed resource-orchestrator configuration: $result"
Jeremy Mordkoff2059b392017-10-03 17:34:10 -0400278
Jeremy Mordkoff34151f22017-10-04 19:45:37 -0400279 result=$(curl -k --request PATCH \
280 --url https://$SO_CONTAINER_IP:8008/v2/api/config/openidc-provider-config/rw-ui-client/redirect-uri \
281 --header 'accept: application/vnd.yang.data+json' \
282 --header 'authorization: Basic YWRtaW46YWRtaW4=' \
283 --header 'cache-control: no-cache' \
284 --header 'content-type: application/vnd.yang.data+json' \
285 --data '{"redirect-uri": "https://'$DEFAULT_IP':8443/callback" }')
286 [[ $result =~ .*success.* ]] || FATAL "Failed redirect-uri configuration: $result"
287
288 result=$(curl -k --request PATCH \
289 --url https://$SO_CONTAINER_IP:8008/v2/api/config/openidc-provider-config/rw-ui-client/post-logout-redirect-uri \
290 --header 'accept: application/vnd.yang.data+json' \
291 --header 'authorization: Basic YWRtaW46YWRtaW4=' \
292 --header 'cache-control: no-cache' \
293 --header 'content-type: application/vnd.yang.data+json' \
294 --data '{"post-logout-redirect-uri": "https://'$DEFAULT_IP':8443/?api_server=https://'$DEFAULT_IP'" }')
295 [[ $result =~ .*success.* ]] || FATAL "Failed post-logout-redirect-uri configuration: $result"
296
Jeremy Mordkofff4b6e622017-10-04 12:05:41 -0400297 lxc exec SO-ub -- tee /etc/network/interfaces.d/60-rift.cfg <<EOF
298auto lo:1
299iface lo:1 inet static
300 address $DEFAULT_IP
301 netmask 255.255.255.255
302EOF
303 lxc exec SO-ub ifup lo:1
garciadeblas0d934c12017-11-17 15:39:17 +0100304}
Jeremy Mordkoff2059b392017-10-03 17:34:10 -0400305
garciadeblas0d934c12017-11-17 15:39:17 +0100306#Configure RO, VCA, and SO with the initial configuration:
307# RO -> tenant:osm, logs to be sent to SO
308# VCA -> juju-password
309# SO -> route to Juju Controller, add RO account, add VCA account
310function configure(){
311 #Configure components
312 echo -e "\nConfiguring components"
313 configure_RO
314 configure_VCA
315 configure_SOUI
garciadeblas55490d42016-10-29 14:22:03 +0200316}
317
garciadeblas2a5a6512016-12-19 10:19:52 +0100318function install_lxd() {
garciadeblas0d934c12017-11-17 15:39:17 +0100319 sudo apt-get update
320 sudo apt-get install -y lxd
321 newgrp lxd
garciadeblas2a5a6512016-12-19 10:19:52 +0100322 lxd init --auto
323 lxd waitready
garciadeblas0d934c12017-11-17 15:39:17 +0100324 lxc network create lxdbr0 ipv4.address=auto ipv4.nat=true ipv6.address=none ipv6.nat=false
325 DEFAULT_INTERFACE=$(route -n | awk '$1~/^0.0.0.0/ {print $8}')
326 DEFAULT_MTU=$( ip addr show $DEFAULT_INTERFACE | perl -ne 'if (/mtu\s(\d+)/) {print $1;}')
327 lxc profile device set default eth0 mtu $DEFAULT_MTU
328 #sudo systemctl stop lxd-bridge
329 #sudo systemctl --system daemon-reload
330 #sudo systemctl enable lxd-bridge
331 #sudo systemctl start lxd-bridge
garciadeblas2a5a6512016-12-19 10:19:52 +0100332}
333
tierno53881d72017-04-25 11:58:57 +0200334function ask_user(){
335 # ask to the user and parse a response among 'y', 'yes', 'n' or 'no'. Case insensitive
336 # Params: $1 text to ask; $2 Action by default, can be 'y' for yes, 'n' for no, other or empty for not allowed
337 # Return: true(0) if user type 'yes'; false (1) if user type 'no'
338 read -e -p "$1" USER_CONFIRMATION
339 while true ; do
340 [ -z "$USER_CONFIRMATION" ] && [ "$2" == 'y' ] && return 0
341 [ -z "$USER_CONFIRMATION" ] && [ "$2" == 'n' ] && return 1
342 [ "${USER_CONFIRMATION,,}" == "yes" ] || [ "${USER_CONFIRMATION,,}" == "y" ] && return 0
343 [ "${USER_CONFIRMATION,,}" == "no" ] || [ "${USER_CONFIRMATION,,}" == "n" ] && return 1
344 read -e -p "Please type 'yes' or 'no': " USER_CONFIRMATION
345 done
346}
garciadeblas2a5a6512016-12-19 10:19:52 +0100347
garciadeblas0d934c12017-11-17 15:39:17 +0100348function launch_container_from_lxd(){
349 export OSM_MDG=$1
350 OSM_load_config
351 export OSM_BASE_IMAGE=$2
352 if ! container_exists $OSM_BUILD_CONTAINER; then
353 CONTAINER_OPTS=""
354 [[ "$OSM_BUILD_CONTAINER_PRIVILEGED" == yes ]] && CONTAINER_OPTS="$CONTAINER_OPTS -c security.privileged=true"
355 [[ "$OSM_BUILD_CONTAINER_ALLOW_NESTED" == yes ]] && CONTAINER_OPTS="$CONTAINER_OPTS -c security.nesting=true"
356 create_container $OSM_BASE_IMAGE $OSM_BUILD_CONTAINER $CONTAINER_OPTS
357 wait_container_up $OSM_BUILD_CONTAINER
358 fi
359}
360
361function install_osmclient(){
362 CLIENT_RELEASE=${RELEASE#"-R "}
363 CLIENT_REPOSITORY_KEY="OSM%20ETSI%20Release%20Key.gpg"
364 CLIENT_REPOSITORY="stable"
365 [ -z "$REPOSITORY_BASE" ] && REPOSITORY_BASE="-u https://osm-download.etsi.org/repository/osm/debian"
366 CLIENT_REPOSITORY_BASE=${REPOSITORY_BASE#"-u "}
367 key_location=$CLIENT_REPOSITORY_BASE/$CLIENT_RELEASE/$CLIENT_REPOSITORY_KEY
368 curl $key_location | sudo apt-key add -
369 sudo add-apt-repository -y "deb [arch=amd64] $CLIENT_REPOSITORY_BASE/$CLIENT_RELEASE $CLIENT_REPOSITORY osmclient"
370 sudo apt-get update
371 sudo apt-get install -y python-osmclient
372 export OSM_HOSTNAME=`lxc list | awk '($2=="SO-ub"){print $6}'`
373 export OSM_RO_HOSTNAME=`lxc list | awk '($2=="RO"){print $6}'`
374 echo -e "\nOSM client installed"
375 echo -e "You might be interested in adding the following OSM client env variables to your .bashrc file:"
376 echo " export OSM_HOSTNAME=${OSM_HOSTNAME}"
377 echo " export OSM_RO_HOSTNAME=${OSM_RO_HOSTNAME}"
378}
379
380function install_from_lxdimages(){
381 LXD_RELEASE=${RELEASE#"-R "}
382 LXD_IMAGE_DIR="$(mktemp -d -q --tmpdir "osmimages.XXXXXX")"
383 trap 'rm -rf "$LXD_IMAGE_DIR"' EXIT
384 wget -O $LXD_IMAGE_DIR/osm-ro.tar.gz $LXD_REPOSITORY_BASE/$LXD_RELEASE/osm-ro.tar.gz
385 lxc image import $LXD_IMAGE_DIR/osm-ro.tar.gz --alias osm-ro
386 rm -f $LXD_IMAGE_DIR/osm-ro.tar.gz
387 wget -O $LXD_IMAGE_DIR/osm-vca.tar.gz $LXD_REPOSITORY_BASE/$LXD_RELEASE/osm-vca.tar.gz
388 lxc image import $LXD_IMAGE_DIR/osm-vca.tar.gz --alias osm-vca
389 rm -f $LXD_IMAGE_DIR/osm-vca.tar.gz
390 wget -O $LXD_IMAGE_DIR/osm-soui.tar.gz $LXD_REPOSITORY_BASE/$LXD_RELEASE/osm-soui.tar.gz
391 lxc image import $LXD_IMAGE_DIR/osm-soui.tar.gz --alias osm-soui
392 rm -f $LXD_IMAGE_DIR/osm-soui.tar.gz
393 launch_container_from_lxd RO osm-ro
394 ro_is_up && track RO
395 launch_container_from_lxd VCA osm-vca
396 vca_is_up && track VCA
397 launch_container_from_lxd SO osm-soui
398 #so_is_up && track SOUI
399 track SOUI
400}
401
peusterm3a3e7a52017-12-22 13:10:54 +0100402function install_docker_ce() {
403 # installs and configures Docker CE
404 echo "Installing Docker CE ..."
405 sudo apt-get install apt-transport-https ca-certificates software-properties-common
406 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
407 sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
408 sudo apt-get -qq update
409 sudo apt-get install docker-ce
410 # user management
411 echo "Adding user to group 'docker'"
412 sudo groupadd docker
413 sudo usermod -aG docker $USER
414 echo "... Docker CE installation done"
415}
416
417function install_vimemu() {
418 # install Docker
419 install_docker_ce
420 # clone vim-emu repository (attention: branch is currently master only)
421 echo "Cloning vim-emu repository ..."
422 git clone https://osm.etsi.org/gerrit/osm/vim-emu.git
423 # build vim-emu docker
424 echo "Building vim-emu Docker container..."
425 docker build -t vim-emu-img -f vim-emu/Dockerfile vim-emu/
426 # start vim-emu container as daemon
427 echo "Starting vim-emu Docker container 'vim-emu' ..."
428 docker run --name vim-emu -t -d --rm --privileged --pid='host' -v /var/run/docker.sock:/var/run/docker.sock vim-emu-img python examples/osm_default_daemon_topology_2_pop.py
429 echo "Waiting for 'vim-emu' container to start ..."
430 sleep 5
431 export VIMEMU_HOSTNAME=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' vim-emu)
432 echo "vim-emu running at $VIMEMU_HOSTNAME ..."
433 echo -e "You might be interested in adding the following env variables to your .bashrc file:"
434 echo " export VIMEMU_HOSTNAME=${VIMEMU_HOSTNAME}"
435 echo -e "\nTo add the emulated VIM to OSM you should do:"
436 echo " osm vim-create --name emu-vim1 --user username --password password --auth_url http://$VIM_EMU_IP:6001/v2.0 --tenant tenantName --account_type openstack"
437}
438
garciadeblas0d934c12017-11-17 15:39:17 +0100439function dump_vars(){
440 echo "DEVELOP=$DEVELOP"
441 echo "INSTALL_FROM_SOURCE=$INSTALL_FROM_SOURCE"
442 echo "UNINSTALL=$UNINSTALL"
443 echo "NAT=$NAT"
444 echo "UPDATE=$UPDATE"
445 echo "RECONFIGURE=$RECONFIGURE"
446 echo "TEST_INSTALLER=$TEST_INSTALLER"
peusterm3a3e7a52017-12-22 13:10:54 +0100447 echo "INSTALL_VIMEMU=$INSTALL_VIMEMU"
garciadeblas0d934c12017-11-17 15:39:17 +0100448 echo "INSTALL_LXD=$INSTALL_LXD"
449 echo "INSTALL_FROM_LXDIMAGES=$INSTALL_FROM_LXDIMAGES"
450 echo "LXD_REPOSITORY_BASE=$LXD_REPOSITORY_BASE"
451 echo "RELEASE=$RELEASE"
452 echo "REPOSITORY=$REPOSITORY"
453 echo "REPOSITORY_BASE=$REPOSITORY_BASE"
454 echo "REPOSITORY_KEY=$REPOSITORY_KEY"
455 echo "NOCONFIGURE=$NOCONFIGURE"
456 echo "SHOWOPTS=$SHOWOPTS"
457 echo "Install from specific refspec (-b): $COMMIT_ID"
458}
459
460function track(){
461 ctime=`date +%s`
462 duration=$((ctime - SESSION_ID))
463 url="http://www.woopra.com/track/ce?project=osm.etsi.org&cookie=${SESSION_ID}"
464 #url="${url}&ce_campaign_name=${CAMPAIGN_NAME}"
465 event_name="bin"
466 [ -n "$INSTALL_FROM_SOURCE" ] && event_name="src"
467 [ -n "$INSTALL_FROM_LXDIMAGES" ] && event_name="lxd"
468 event_name="${event_name}_$1"
469 url="${url}&event=${event_name}&ce_duration=${duration}"
470 wget -q -O /dev/null $url
471}
472
garciadeblas93c61312016-09-28 15:12:48 +0200473UNINSTALL=""
474DEVELOP=""
475NAT=""
garciadeblas2a382732017-01-12 12:11:14 +0100476UPDATE=""
garciadeblas55490d42016-10-29 14:22:03 +0200477RECONFIGURE=""
478TEST_INSTALLER=""
garciadeblas0d934c12017-11-17 15:39:17 +0100479INSTALL_LXD=""
garciadeblas2a5a6512016-12-19 10:19:52 +0100480SHOWOPTS=""
481COMMIT_ID=""
tierno2cc8e252017-03-08 17:12:05 +0100482ASSUME_YES=""
garciadeblasa1fc4572017-04-24 19:08:21 +0200483INSTALL_FROM_SOURCE=""
garciadeblas82442292017-11-16 14:48:12 +0100484RELEASE="-R ReleaseTHREE"
peusterm3a3e7a52017-12-22 13:10:54 +0100485INSTALL_VIMEMU=""
garciadeblas0d934c12017-11-17 15:39:17 +0100486INSTALL_FROM_LXDIMAGES=""
487LXD_REPOSITORY_BASE="https://osm-download.etsi.org/repository/osm/lxd"
488NOCONFIGURE=""
Mike Marchettica56c642017-12-16 16:04:52 -0500489RELEASE_DAILY=""
garciadeblas0d934c12017-11-17 15:39:17 +0100490SESSION_ID=`date +%s`
491
492while getopts ":hy-:b:r:k:u:R:l:" o; do
garciadeblas93c61312016-09-28 15:12:48 +0200493 case "${o}" in
494 h)
495 usage && exit 0
496 ;;
garciadeblas2a5a6512016-12-19 10:19:52 +0100497 b)
498 COMMIT_ID=${OPTARG}
499 ;;
Mike Marchetti425f8ce2017-06-15 13:02:16 -0400500 r)
501 REPOSITORY="-r ${OPTARG}"
502 ;;
503 R)
504 RELEASE="-R ${OPTARG}"
505 ;;
506 k)
507 REPOSITORY_KEY="-k ${OPTARG}"
508 ;;
509 u)
510 REPOSITORY_BASE="-u ${OPTARG}"
511 ;;
garciadeblas0d934c12017-11-17 15:39:17 +0100512 l)
513 LXD_REPOSITORY_BASE="${OPTARG}"
514 ;;
garciadeblas93c61312016-09-28 15:12:48 +0200515 -)
516 [ "${OPTARG}" == "help" ] && usage && exit 0
garciadeblasa1fc4572017-04-24 19:08:21 +0200517 [ "${OPTARG}" == "source" ] && INSTALL_FROM_SOURCE="y" && continue
garciadeblas93c61312016-09-28 15:12:48 +0200518 [ "${OPTARG}" == "develop" ] && DEVELOP="y" && continue
519 [ "${OPTARG}" == "uninstall" ] && UNINSTALL="y" && continue
520 [ "${OPTARG}" == "nat" ] && NAT="y" && continue
garciadeblas2a382732017-01-12 12:11:14 +0100521 [ "${OPTARG}" == "update" ] && UPDATE="y" && continue
garciadeblas55490d42016-10-29 14:22:03 +0200522 [ "${OPTARG}" == "reconfigure" ] && RECONFIGURE="y" && continue
523 [ "${OPTARG}" == "test" ] && TEST_INSTALLER="y" && continue
garciadeblas0d934c12017-11-17 15:39:17 +0100524 [ "${OPTARG}" == "lxdinstall" ] && INSTALL_LXD="y" && continue
525 [ "${OPTARG}" == "lxdimages" ] && INSTALL_FROM_LXDIMAGES="y" && continue
peusterm3a3e7a52017-12-22 13:10:54 +0100526 [ "${OPTARG}" == "vimemu" ] && INSTALL_VIMEMU="y" && continue
garciadeblas0d934c12017-11-17 15:39:17 +0100527 [ "${OPTARG}" == "noconfigure" ] && NOCONFIGURE="y" && continue
garciadeblas2a5a6512016-12-19 10:19:52 +0100528 [ "${OPTARG}" == "showopts" ] && SHOWOPTS="y" && continue
Mike Marchettica56c642017-12-16 16:04:52 -0500529 [ "${OPTARG}" == "daily" ] && RELEASE_DAILY="y" && continue
garciadeblas93c61312016-09-28 15:12:48 +0200530 echo -e "Invalid option: '--$OPTARG'\n" >&2
531 usage && exit 1
532 ;;
533 \?)
534 echo -e "Invalid option: '-$OPTARG'\n" >&2
535 usage && exit 1
536 ;;
tierno2cc8e252017-03-08 17:12:05 +0100537 y)
538 ASSUME_YES="y"
539 ;;
garciadeblas93c61312016-09-28 15:12:48 +0200540 *)
541 usage && exit 1
542 ;;
543 esac
544done
545
garciadeblasa6ff9862017-04-07 02:00:29 +0200546if [ -n "$SHOWOPTS" ]; then
garciadeblas0d934c12017-11-17 15:39:17 +0100547 dump_vars
garciadeblasa6ff9862017-04-07 02:00:29 +0200548 exit 0
549fi
550
Mike Marchettica56c642017-12-16 16:04:52 -0500551[ -n "$RELEASE_DAILY" ] && echo -e "\nInstalling from daily build repo" && RELEASE="-R ReleaseTHREE-daily" && REPOSITORY="-r testing" && COMMIT_ID="master"
552
Mike Marchetti6930bc02017-05-31 16:33:02 -0400553# if develop, we force master
garciadeblas2a5a6512016-12-19 10:19:52 +0100554[ -z "$COMMIT_ID" ] && [ -n "$DEVELOP" ] && COMMIT_ID="master"
Mike Marchetti6930bc02017-05-31 16:33:02 -0400555
Mike Marchetti0d567602017-09-29 14:30:28 -0400556# forcing source from master removed. Now only install from source when explicit
557# [ -n "$COMMIT_ID" ] && [ "$COMMIT_ID" == "master" ] && INSTALL_FROM_SOURCE="y"
garciadeblas2a5a6512016-12-19 10:19:52 +0100558
garciadeblas55490d42016-10-29 14:22:03 +0200559if [ -n "$TEST_INSTALLER" ]; then
560 echo -e "\nUsing local devops repo for OSM installation"
561 TEMPDIR="$(dirname $(realpath $(dirname $0)))"
562else
563 echo -e "\nCreating temporary dir for OSM installation"
564 TEMPDIR="$(mktemp -d -q --tmpdir "installosm.XXXXXX")"
565 trap 'rm -rf "$TEMPDIR"' EXIT
566fi
garciadeblas93c61312016-09-28 15:12:48 +0200567
Mike Marchetti2b951282017-10-10 15:43:15 -0400568need_packages="git jq"
569for package in $need_packages; do
570 echo -e "Checking required packages: $package"
Mike Marchettid8577f52017-10-19 15:27:48 -0400571 dpkg -l $package &>/dev/null \
572 || ! echo -e " $package not installed.\nInstalling $package requires root privileges" \
573 || sudo apt-get install -y $package \
574 || FATAL "failed to install $package"
Mike Marchetti2b951282017-10-10 15:43:15 -0400575done
576
garciadeblas55490d42016-10-29 14:22:03 +0200577if [ -z "$TEST_INSTALLER" ]; then
578 echo -e "\nCloning devops repo temporarily"
579 git clone https://osm.etsi.org/gerrit/osm/devops.git $TEMPDIR
580 RC_CLONE=$?
garciadeblas55490d42016-10-29 14:22:03 +0200581fi
garciadeblas2a382732017-01-12 12:11:14 +0100582
583echo -e "\nGuessing the current stable release"
Mike Marchetti91df8352017-10-17 13:47:48 -0400584LATEST_STABLE_DEVOPS=`git -C $TEMPDIR tag -l v[0-9].* | sort -V | tail -n1`
garciadeblas2a382732017-01-12 12:11:14 +0100585[ -z "$COMMIT_ID" ] && [ -z "$LATEST_STABLE_DEVOPS" ] && echo "Could not find the current latest stable release" && exit 0
garciadeblasa6ff9862017-04-07 02:00:29 +0200586echo "Latest tag in devops repo: $LATEST_STABLE_DEVOPS"
587[ -z "$COMMIT_ID" ] && [ -n "$LATEST_STABLE_DEVOPS" ] && COMMIT_ID="tags/$LATEST_STABLE_DEVOPS"
Mike Marchettica56c642017-12-16 16:04:52 -0500588
589if [ -n "$RELEASE_DAILY" ]; then
590 echo "Using master/HEAD devops"
591 git -C $TEMPDIR checkout master
592elif [ -z "$TEST_INSTALLER" ]; then
593 git -C $TEMPDIR checkout tags/$LATEST_STABLE_DEVOPS
594fi
garciadeblas2a382732017-01-12 12:11:14 +0100595
garciadeblas93c61312016-09-28 15:12:48 +0200596OSM_DEVOPS=$TEMPDIR
garciadeblas0fe45dd2016-10-04 07:54:17 +0200597OSM_JENKINS="$TEMPDIR/jenkins"
598. $OSM_JENKINS/common/all_funcs
garciadeblas93c61312016-09-28 15:12:48 +0200599
garciadeblasd8718f12016-10-30 19:39:01 +0100600[ -n "$UNINSTALL" ] && uninstall && echo -e "\nDONE" && exit 0
601[ -n "$NAT" ] && nat && echo -e "\nDONE" && exit 0
garciadeblas2a382732017-01-12 12:11:14 +0100602[ -n "$UPDATE" ] && update && echo -e "\nDONE" && exit 0
garciadeblasd8718f12016-10-30 19:39:01 +0100603[ -n "$RECONFIGURE" ] && configure && echo -e "\nDONE" && exit 0
garciadeblas93c61312016-09-28 15:12:48 +0200604
605#Installation starts here
garciadeblasa1fc4572017-04-24 19:08:21 +0200606echo -e "\nInstalling OSM from refspec: $COMMIT_ID"
607if [ -n "$INSTALL_FROM_SOURCE" ] && [ -z "$ASSUME_YES" ]; then
tierno53881d72017-04-25 11:58:57 +0200608 ! ask_user "The installation will take about 75-90 minutes. Continue (Y/n)? " y && echo "Cancelled!" && exit 1
tierno2cc8e252017-03-08 17:12:05 +0100609fi
610
garciadeblasa1fc4572017-04-24 19:08:21 +0200611echo -e "\nChecking required packages: wget, curl, tar"
612dpkg -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
613
Jokin Garayc0a65962017-03-09 14:51:48 +0100614echo -e "Checking required packages: lxd"
garciadeblasd880f722017-04-07 20:14:57 +0200615lxd --version &>/dev/null || FATAL "lxd not present, exiting."
garciadeblas0d934c12017-11-17 15:39:17 +0100616[ -n "$INSTALL_LXD" ] && echo -e "\nInstalling and configuring lxd" && install_lxd
garciadeblas2a5a6512016-12-19 10:19:52 +0100617
Mike Marchettib884a462017-10-05 13:28:33 -0400618wget -q -O- https://osm-download.etsi.org/ftp/osm-3.0-three/README.txt &> /dev/null
garciadeblas0d934c12017-11-17 15:39:17 +0100619track start
garciadeblas93c61312016-09-28 15:12:48 +0200620
Mike Marchettid1e08b92017-09-29 16:14:11 -0400621# use local devops for containers
622export OSM_USE_LOCAL_DEVOPS=true
garciadeblas0d934c12017-11-17 15:39:17 +0100623if [ -n "$INSTALL_FROM_SOURCE" ]; then #install from source
garciadeblasa1fc4572017-04-24 19:08:21 +0200624 echo -e "\nCreating the containers and building from source ..."
625 $OSM_DEVOPS/jenkins/host/start_build RO --notest checkout $COMMIT_ID || FATAL "RO container build failed (refspec: '$COMMIT_ID')"
garciadeblas0d934c12017-11-17 15:39:17 +0100626 ro_is_up && track RO
garciadeblasa1fc4572017-04-24 19:08:21 +0200627 $OSM_DEVOPS/jenkins/host/start_build VCA || FATAL "VCA container build failed"
garciadeblas0d934c12017-11-17 15:39:17 +0100628 vca_is_up && track VCA
garciadeblasa1fc4572017-04-24 19:08:21 +0200629 $OSM_DEVOPS/jenkins/host/start_build SO checkout $COMMIT_ID || FATAL "SO container build failed (refspec: '$COMMIT_ID')"
630 $OSM_DEVOPS/jenkins/host/start_build UI checkout $COMMIT_ID || FATAL "UI container build failed (refspec: '$COMMIT_ID')"
garciadeblas0d934c12017-11-17 15:39:17 +0100631 #so_is_up && track SOUI
632 track SOUI
633elif [ -n "$INSTALL_FROM_LXDIMAGES" ]; then #install from LXD images stored in OSM repo
634 echo -e "\nInstalling from lxd images ..."
635 install_from_lxdimages
636else #install from binaries
637 echo -e "\nCreating the containers and installing from binaries ..."
638 $OSM_DEVOPS/jenkins/host/install RO $REPOSITORY $RELEASE $REPOSITORY_KEY $REPOSITORY_BASE || FATAL "RO install failed"
639 ro_is_up && track RO
640 $OSM_DEVOPS/jenkins/host/start_build VCA || FATAL "VCA install failed"
641 vca_is_up && track VCA
642 $OSM_DEVOPS/jenkins/host/install SO $REPOSITORY $RELEASE $REPOSITORY_KEY $REPOSITORY_BASE || FATAL "SO install failed"
643 $OSM_DEVOPS/jenkins/host/install UI $REPOSITORY $RELEASE $REPOSITORY_KEY $REPOSITORY_BASE || FATAL "UI install failed"
644 #so_is_up && track SOUI
645 track SOUI
garciadeblasa1fc4572017-04-24 19:08:21 +0200646fi
garciadeblas93c61312016-09-28 15:12:48 +0200647
garciadeblas55490d42016-10-29 14:22:03 +0200648#Install iptables-persistent and configure NAT rules
garciadeblas0d934c12017-11-17 15:39:17 +0100649[ -z "$NOCONFIGURE" ] && nat
garciadeblas93c61312016-09-28 15:12:48 +0200650
651#Configure components
garciadeblas0d934c12017-11-17 15:39:17 +0100652[ -z "$NOCONFIGURE" ] && configure
653
654#Install osmclient
655[ -z "$NOCONFIGURE" ] && install_osmclient
garciadeblas93c61312016-09-28 15:12:48 +0200656
peusterm3a3e7a52017-12-22 13:10:54 +0100657#Install vim-emu (optional)
658if [ -n "$INSTALL_VIMEMU" ]; then
659 echo -e "\nInstalling vim-emu ..."
660 install_vimemu
661fi
662
Mike Marchettib884a462017-10-05 13:28:33 -0400663wget -q -O- https://osm-download.etsi.org/ftp/osm-3.0-three/README2.txt &> /dev/null
garciadeblas0d934c12017-11-17 15:39:17 +0100664track end
Jokin Garay3eb9ce72017-03-09 14:48:11 +0100665echo -e "\nDONE"
garciadeblas0d934c12017-11-17 15:39:17 +0100666