fix merge issue
[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 " -r <repo>: use specified repository name for osm packages"
23 echo -e " -R <release>: use specified release for osm binaries (deb packages, lxd images, ...)"
24 echo -e " -u <repo base>: use specified repository url for osm packages"
25 echo -e " -k <repo key>: use specified repository public key url"
26 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 " -p <path>: use specified repository path for lxd images"
34 echo -e " --vimemu: additionally fetch, build, and deploy the VIM emulator as a docker container"
35 echo -e " --develop: (deprecated, use '-b master') install OSM from source code using the master branch"
36 # echo -e " --reconfigure: reconfigure the modules (DO NOT change NAT rules)"
37 echo -e " --nat: install only NAT rules"
38 echo -e " --noconfigure: DO NOT install osmclient, DO NOT install NAT rules, DO NOT configure modules"
39 # echo -e " --update: update to the latest stable release or to the latest commit if using a specific branch"
40 echo -e " --showopts: print chosen options and exit (only for debugging)"
41 echo -e " -y: do not prompt for confirmation, assumes yes"
42 echo -e " -h / --help: print this help"
43 }
44
45 #Uninstall OSM: remove containers
46 function uninstall(){
47 echo -e "\nUninstalling OSM"
48 if [ $RC_CLONE ] || [ -n "$TEST_INSTALLER" ]; then
49 $OSM_DEVOPS/jenkins/host/clean_container RO
50 $OSM_DEVOPS/jenkins/host/clean_container VCA
51 $OSM_DEVOPS/jenkins/host/clean_container MON
52 $OSM_DEVOPS/jenkins/host/clean_container SO
53 #$OSM_DEVOPS/jenkins/host/clean_container UI
54 else
55 lxc stop RO && lxc delete RO
56 lxc stop VCA && lxc delete VCA
57 lxc stop MON && lxc delete MON
58 lxc stop SO-ub && lxc delete SO-ub
59 fi
60 echo -e "\nDeleting imported lxd images if they exist"
61 lxc image show osm-ro &>/dev/null && lxc image delete osm-ro
62 lxc image show osm-vca &>/dev/null && lxc image delete osm-vca
63 lxc image show osm-soui &>/dev/null && lxc image delete osm-soui
64 return 0
65 }
66
67 #Configure NAT rules, based on the current IP addresses of containers
68 function nat(){
69 echo -e "\nChecking required packages: iptables-persistent"
70 dpkg -l iptables-persistent &>/dev/null || ! echo -e " Not installed.\nInstalling iptables-persistent requires root privileges" || \
71 sudo DEBIAN_FRONTEND=noninteractive apt-get -yq install iptables-persistent
72 echo -e "\nConfiguring NAT rules"
73 echo -e " Required root privileges"
74 sudo $OSM_DEVOPS/installers/nat_osm
75 }
76
77 function FATAL(){
78 echo "FATAL error: Cannot install OSM due to \"$1\""
79 exit 1
80 }
81
82 #Update RO, SO and UI:
83 function update(){
84 echo -e "\nUpdating components"
85
86 echo -e " Updating RO"
87 CONTAINER="RO"
88 MDG="RO"
89 INSTALL_FOLDER="/opt/openmano"
90 echo -e " Fetching the repo"
91 lxc exec $CONTAINER -- git -C $INSTALL_FOLDER fetch --all
92 BRANCH=""
93 BRANCH=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER status -sb | head -n1 | sed -n 's/^## \(.*\).*/\1/p'|awk '{print $1}' |sed 's/\(.*\)\.\.\..*/\1/'`
94 [ -z "$BRANCH" ] && FATAL "Could not find the current branch in use in the '$MDG'"
95 CURRENT=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER status |head -n1`
96 CURRENT_COMMIT_ID=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER rev-parse HEAD`
97 echo " FROM: $CURRENT ($CURRENT_COMMIT_ID)"
98 # COMMIT_ID either was previously set with -b option, or is an empty string
99 CHECKOUT_ID=$COMMIT_ID
100 [ -z "$CHECKOUT_ID" ] && [ "$BRANCH" == "HEAD" ] && CHECKOUT_ID="tags/$LATEST_STABLE_DEVOPS"
101 [ -z "$CHECKOUT_ID" ] && [ "$BRANCH" != "HEAD" ] && CHECKOUT_ID="$BRANCH"
102 if [[ $CHECKOUT_ID == "tags/"* ]]; then
103 REMOTE_COMMIT_ID=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER rev-list -n 1 $CHECKOUT_ID`
104 else
105 REMOTE_COMMIT_ID=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER rev-parse origin/$CHECKOUT_ID`
106 fi
107 echo " TO: $CHECKOUT_ID ($REMOTE_COMMIT_ID)"
108 if [ "$CURRENT_COMMIT_ID" == "$REMOTE_COMMIT_ID" ]; then
109 echo " Nothing to be done."
110 else
111 echo " Update required."
112 lxc exec $CONTAINER -- service osm-ro stop
113 lxc exec $CONTAINER -- git -C /opt/openmano stash
114 lxc exec $CONTAINER -- git -C /opt/openmano pull --rebase
115 lxc exec $CONTAINER -- git -C /opt/openmano checkout $CHECKOUT_ID
116 lxc exec $CONTAINER -- git -C /opt/openmano stash pop
117 lxc exec $CONTAINER -- /opt/openmano/database_utils/migrate_mano_db.sh
118 lxc exec $CONTAINER -- service osm-ro start
119 fi
120 echo
121
122 echo -e " Updating SO and UI"
123 CONTAINER="SO-ub"
124 MDG="SO"
125 INSTALL_FOLDER="" # To be filled in
126 echo -e " Fetching the repo"
127 lxc exec $CONTAINER -- git -C $INSTALL_FOLDER fetch --all
128 BRANCH=""
129 BRANCH=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER status -sb | head -n1 | sed -n 's/^## \(.*\).*/\1/p'|awk '{print $1}' |sed 's/\(.*\)\.\.\..*/\1/'`
130 [ -z "$BRANCH" ] && FATAL "Could not find the current branch in use in the '$MDG'"
131 CURRENT=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER status |head -n1`
132 CURRENT_COMMIT_ID=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER rev-parse HEAD`
133 echo " FROM: $CURRENT ($CURRENT_COMMIT_ID)"
134 # COMMIT_ID either was previously set with -b option, or is an empty string
135 CHECKOUT_ID=$COMMIT_ID
136 [ -z "$CHECKOUT_ID" ] && [ "$BRANCH" == "HEAD" ] && CHECKOUT_ID="tags/$LATEST_STABLE_DEVOPS"
137 [ -z "$CHECKOUT_ID" ] && [ "$BRANCH" != "HEAD" ] && CHECKOUT_ID="$BRANCH"
138 if [[ $CHECKOUT_ID == "tags/"* ]]; then
139 REMOTE_COMMIT_ID=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER rev-list -n 1 $CHECKOUT_ID`
140 else
141 REMOTE_COMMIT_ID=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER rev-parse origin/$CHECKOUT_ID`
142 fi
143 echo " TO: $CHECKOUT_ID ($REMOTE_COMMIT_ID)"
144 if [ "$CURRENT_COMMIT_ID" == "$REMOTE_COMMIT_ID" ]; then
145 echo " Nothing to be done."
146 else
147 echo " Update required."
148 # Instructions to be added
149 # lxc exec SO-ub -- ...
150 fi
151 echo
152 echo -e "Updating MON Container"
153 CONTAINER="MON"
154 MDG="MON"
155 INSTALL_FOLDER="/root/MON"
156 echo -e " Fetching the repo"
157 lxc exec $CONTAINER -- git -C $INSTALL_FOLDER fetch --all
158 BRANCH=""
159 BRANCH=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER status -sb | head -n1 | sed -n 's/^## \(.*\).*/\1/p'|awk '{print $1}' |sed 's/\(.*\)\.\.\..*/\1/'`
160 [ -z "$BRANCH" ] && FATAL "Could not find the current branch in use in the '$MDG'"
161 CURRENT=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER status |head -n1`
162 CURRENT_COMMIT_ID=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER rev-parse HEAD`
163 echo " FROM: $CURRENT ($CURRENT_COMMIT_ID)"
164 # COMMIT_ID either was previously set with -b option, or is an empty string
165 CHECKOUT_ID=$COMMIT_ID
166 [ -z "$CHECKOUT_ID" ] && [ "$BRANCH" == "HEAD" ] && CHECKOUT_ID="tags/$LATEST_STABLE_DEVOPS"
167 [ -z "$CHECKOUT_ID" ] && [ "$BRANCH" != "HEAD" ] && CHECKOUT_ID="$BRANCH"
168 if [[ $CHECKOUT_ID == "tags/"* ]]; then
169 REMOTE_COMMIT_ID=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER rev-list -n 1 $CHECKOUT_ID`
170 else
171 REMOTE_COMMIT_ID=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER rev-parse origin/$CHECKOUT_ID`
172 fi
173 echo " TO: $CHECKOUT_ID ($REMOTE_COMMIT_ID)"
174 if [ "$CURRENT_COMMIT_ID" == "$REMOTE_COMMIT_ID" ]; then
175 echo " Nothing to be done."
176 else
177 echo " Update required."
178 fi
179 echo
180 }
181
182 function so_is_up() {
183 if [ -n "$1" ]; then
184 SO_IP=$1
185 else
186 SO_IP=`lxc list SO-ub -c 4|grep eth0 |awk '{print $2}'`
187 fi
188 time=0
189 step=5
190 timelength=300
191 while [ $time -le $timelength ]
192 do
193 if [[ `curl -k -X GET https://$SO_IP:8008/api/operational/vcs/info \
194 -H 'accept: application/vnd.yang.data+json' \
195 -H 'authorization: Basic YWRtaW46YWRtaW4=' \
196 -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 ]]
197 then
198 echo "RW.Restconf running....SO is up"
199 return 0
200 fi
201
202 sleep $step
203 echo -n "."
204 time=$((time+step))
205 done
206
207 FATAL "OSM Failed to startup. SO failed to startup"
208 }
209
210 function vca_is_up() {
211 if [[ `lxc exec VCA -- juju status | grep "osm" | wc -l` -eq 1 ]]; then
212 echo "VCA is up and running"
213 return 0
214 fi
215
216 FATAL "OSM Failed to startup. VCA failed to startup"
217 }
218
219 function mon_is_up() {
220 if [[ `curl http://$RO_IP:9090/openmano/ | grep "works" | wc -l` -eq 1 ]]; then
221 echo "MON is up and running"
222 return 0
223 fi
224
225 FATAL "OSM Failed to startup. MON failed to startup"
226 }
227
228 function ro_is_up() {
229 if [ -n "$1" ]; then
230 RO_IP=$1
231 else
232 RO_IP=`lxc list RO -c 4|grep eth0 |awk '{print $2}'`
233 fi
234 time=0
235 step=2
236 timelength=20
237 while [ $time -le $timelength ]; do
238 if [[ `curl http://$RO_IP:9090/openmano/ | grep "works" | wc -l` -eq 1 ]]; then
239 echo "RO is up and running"
240 return 0
241 fi
242 sleep $step
243 echo -n "."
244 time=$((time+step))
245 done
246
247 FATAL "OSM Failed to startup. RO failed to startup"
248 }
249
250
251 function configure_RO(){
252 . $OSM_DEVOPS/installers/export_ips
253 echo -e " Configuring RO"
254 lxc exec RO -- sed -i -e "s/^\#\?log_socket_host:.*/log_socket_host: $SO_CONTAINER_IP/g" /etc/osm/openmanod.cfg
255 lxc exec RO -- service osm-ro restart
256
257 ro_is_up
258
259 lxc exec RO -- openmano tenant-delete -f osm >/dev/null
260 lxc exec RO -- openmano tenant-create osm > /dev/null
261 lxc exec RO -- sed -i '/export OPENMANO_TENANT=osm/d' .bashrc
262 lxc exec RO -- sed -i '$ i export OPENMANO_TENANT=osm' .bashrc
263 lxc exec RO -- sh -c 'echo "export OPENMANO_TENANT=osm" >> .bashrc'
264 }
265
266 function configure_VCA(){
267 echo -e " Configuring VCA"
268 JUJU_PASSWD=`date +%s | sha256sum | base64 | head -c 32`
269 echo -e "$JUJU_PASSWD\n$JUJU_PASSWD" | lxc exec VCA -- juju change-user-password
270 }
271
272 function configure_SOUI(){
273 . $OSM_DEVOPS/installers/export_ips
274 JUJU_CONTROLLER_IP=`lxc exec VCA -- lxc list -c 4 |grep eth0 |awk '{print $2}'`
275 RO_TENANT_ID=`lxc exec RO -- openmano tenant-list osm |awk '{print $1}'`
276
277 echo -e " Configuring MON"
278 #Information to be added about SO socket for logging
279
280 echo -e " Configuring SO"
281 sudo route add -host $JUJU_CONTROLLER_IP gw $VCA_CONTAINER_IP
282 sudo sed -i "$ i route add -host $JUJU_CONTROLLER_IP gw $VCA_CONTAINER_IP" /etc/rc.local
283 # make journaling persistent
284 lxc exec SO-ub -- mkdir -p /var/log/journal
285 lxc exec SO-ub -- systemd-tmpfiles --create --prefix /var/log/journal
286 lxc exec SO-ub -- systemctl restart systemd-journald
287
288 echo RIFT_EXTERNAL_ADDRESS=$DEFAULT_IP | lxc exec SO-ub -- tee -a /usr/rift/etc/default/launchpad
289
290 lxc exec SO-ub -- systemctl restart launchpad
291
292 so_is_up $SO_CONTAINER_IP
293
294 #delete existing config agent (could be there on reconfigure)
295 curl -k --request DELETE \
296 --url https://$SO_CONTAINER_IP:8008/api/config/config-agent/account/osmjuju \
297 --header 'accept: application/vnd.yang.data+json' \
298 --header 'authorization: Basic YWRtaW46YWRtaW4=' \
299 --header 'cache-control: no-cache' \
300 --header 'content-type: application/vnd.yang.data+json' &> /dev/null
301
302 result=$(curl -k --request POST \
303 --url https://$SO_CONTAINER_IP:8008/api/config/config-agent \
304 --header 'accept: application/vnd.yang.data+json' \
305 --header 'authorization: Basic YWRtaW46YWRtaW4=' \
306 --header 'cache-control: no-cache' \
307 --header 'content-type: application/vnd.yang.data+json' \
308 --data '{"account": [ { "name": "osmjuju", "account-type": "juju", "juju": { "ip-address": "'$JUJU_CONTROLLER_IP'", "port": "17070", "user": "admin", "secret": "'$JUJU_PASSWD'" } } ]}')
309 [[ $result =~ .*success.* ]] || FATAL "Failed config-agent configuration: $result"
310
311 #R1/R2 config line
312 #result=$(curl -k --request PUT \
313 # --url https://$SO_CONTAINER_IP:8008/api/config/resource-orchestrator \
314 # --header 'accept: application/vnd.yang.data+json' \
315 # --header 'authorization: Basic YWRtaW46YWRtaW4=' \
316 # --header 'cache-control: no-cache' \
317 # --header 'content-type: application/vnd.yang.data+json' \
318 # --data '{ "openmano": { "host": "'$RO_CONTAINER_IP'", "port": "9090", "tenant-id": "'$RO_TENANT_ID'" }, "name": "osmopenmano", "account-type": "openmano" }')
319
320 result=$(curl -k --request PUT \
321 --url https://$SO_CONTAINER_IP:8008/api/config/project/default/ro-account/account \
322 --header 'accept: application/vnd.yang.data+json' \
323 --header 'authorization: Basic YWRtaW46YWRtaW4=' \
324 --header 'cache-control: no-cache' \
325 --header 'content-type: application/vnd.yang.data+json' \
326 --data '{"rw-ro-account:account": [ { "openmano": { "host": "'$RO_CONTAINER_IP'", "port": "9090", "tenant-id": "'$RO_TENANT_ID'"}, "name": "osmopenmano", "ro-account-type": "openmano" }]}')
327 [[ $result =~ .*success.* ]] || FATAL "Failed resource-orchestrator configuration: $result"
328
329 result=$(curl -k --request PATCH \
330 --url https://$SO_CONTAINER_IP:8008/v2/api/config/openidc-provider-config/rw-ui-client/redirect-uri \
331 --header 'accept: application/vnd.yang.data+json' \
332 --header 'authorization: Basic YWRtaW46YWRtaW4=' \
333 --header 'cache-control: no-cache' \
334 --header 'content-type: application/vnd.yang.data+json' \
335 --data '{"redirect-uri": "https://'$DEFAULT_IP':8443/callback" }')
336 [[ $result =~ .*success.* ]] || FATAL "Failed redirect-uri configuration: $result"
337
338 result=$(curl -k --request PATCH \
339 --url https://$SO_CONTAINER_IP:8008/v2/api/config/openidc-provider-config/rw-ui-client/post-logout-redirect-uri \
340 --header 'accept: application/vnd.yang.data+json' \
341 --header 'authorization: Basic YWRtaW46YWRtaW4=' \
342 --header 'cache-control: no-cache' \
343 --header 'content-type: application/vnd.yang.data+json' \
344 --data '{"post-logout-redirect-uri": "https://'$DEFAULT_IP':8443/?api_server=https://'$DEFAULT_IP'" }')
345 [[ $result =~ .*success.* ]] || FATAL "Failed post-logout-redirect-uri configuration: $result"
346
347 lxc exec SO-ub -- tee /etc/network/interfaces.d/60-rift.cfg <<EOF
348 auto lo:1
349 iface lo:1 inet static
350 address $DEFAULT_IP
351 netmask 255.255.255.255
352 EOF
353 lxc exec SO-ub ifup lo:1
354 }
355
356 #Configure RO, VCA, and SO with the initial configuration:
357 # RO -> tenant:osm, logs to be sent to SO
358 # VCA -> juju-password
359 # SO -> route to Juju Controller, add RO account, add VCA account
360 function configure(){
361 #Configure components
362 echo -e "\nConfiguring components"
363 configure_RO
364 configure_VCA
365 configure_SOUI
366 }
367
368 function install_lxd() {
369 sudo apt-get update
370 sudo apt-get install -y lxd
371 newgrp lxd
372 lxd init --auto
373 lxd waitready
374 lxc network create lxdbr0 ipv4.address=auto ipv4.nat=true ipv6.address=none ipv6.nat=false
375 DEFAULT_INTERFACE=$(route -n | awk '$1~/^0.0.0.0/ {print $8}')
376 DEFAULT_MTU=$( ip addr show $DEFAULT_INTERFACE | perl -ne 'if (/mtu\s(\d+)/) {print $1;}')
377 lxc profile device set default eth0 mtu $DEFAULT_MTU
378 #sudo systemctl stop lxd-bridge
379 #sudo systemctl --system daemon-reload
380 #sudo systemctl enable lxd-bridge
381 #sudo systemctl start lxd-bridge
382 }
383
384 function ask_user(){
385 # ask to the user and parse a response among 'y', 'yes', 'n' or 'no'. Case insensitive
386 # Params: $1 text to ask; $2 Action by default, can be 'y' for yes, 'n' for no, other or empty for not allowed
387 # Return: true(0) if user type 'yes'; false (1) if user type 'no'
388 read -e -p "$1" USER_CONFIRMATION
389 while true ; do
390 [ -z "$USER_CONFIRMATION" ] && [ "$2" == 'y' ] && return 0
391 [ -z "$USER_CONFIRMATION" ] && [ "$2" == 'n' ] && return 1
392 [ "${USER_CONFIRMATION,,}" == "yes" ] || [ "${USER_CONFIRMATION,,}" == "y" ] && return 0
393 [ "${USER_CONFIRMATION,,}" == "no" ] || [ "${USER_CONFIRMATION,,}" == "n" ] && return 1
394 read -e -p "Please type 'yes' or 'no': " USER_CONFIRMATION
395 done
396 }
397
398 function launch_container_from_lxd(){
399 export OSM_MDG=$1
400 OSM_load_config
401 export OSM_BASE_IMAGE=$2
402 if ! container_exists $OSM_BUILD_CONTAINER; then
403 CONTAINER_OPTS=""
404 [[ "$OSM_BUILD_CONTAINER_PRIVILEGED" == yes ]] && CONTAINER_OPTS="$CONTAINER_OPTS -c security.privileged=true"
405 [[ "$OSM_BUILD_CONTAINER_ALLOW_NESTED" == yes ]] && CONTAINER_OPTS="$CONTAINER_OPTS -c security.nesting=true"
406 create_container $OSM_BASE_IMAGE $OSM_BUILD_CONTAINER $CONTAINER_OPTS
407 wait_container_up $OSM_BUILD_CONTAINER
408 fi
409 }
410
411 function install_osmclient(){
412 CLIENT_RELEASE=${RELEASE#"-R "}
413 CLIENT_REPOSITORY_KEY="OSM%20ETSI%20Release%20Key.gpg"
414 CLIENT_REPOSITORY="stable"
415 [ -z "$REPOSITORY_BASE" ] && REPOSITORY_BASE="-u https://osm-download.etsi.org/repository/osm/debian"
416 CLIENT_REPOSITORY_BASE=${REPOSITORY_BASE#"-u "}
417 key_location=$CLIENT_REPOSITORY_BASE/$CLIENT_RELEASE/$CLIENT_REPOSITORY_KEY
418 curl $key_location | sudo apt-key add -
419 sudo add-apt-repository -y "deb [arch=amd64] $CLIENT_REPOSITORY_BASE/$CLIENT_RELEASE $CLIENT_REPOSITORY osmclient"
420 sudo apt-get update
421 sudo apt-get install -y python-osmclient
422 export OSM_HOSTNAME=`lxc list | awk '($2=="SO-ub"){print $6}'`
423 export OSM_RO_HOSTNAME=`lxc list | awk '($2=="RO"){print $6}'`
424 echo -e "\nOSM client installed"
425 echo -e "You might be interested in adding the following OSM client env variables to your .bashrc file:"
426 echo " export OSM_HOSTNAME=${OSM_HOSTNAME}"
427 echo " export OSM_RO_HOSTNAME=${OSM_RO_HOSTNAME}"
428 }
429
430 function install_from_lxdimages(){
431 LXD_RELEASE=${RELEASE#"-R "}
432 if [ -n "$LXD_REPOSITORY_PATH" ]; then
433 LXD_IMAGE_DIR="$LXD_REPOSITORY_PATH"
434 else
435 LXD_IMAGE_DIR="$(mktemp -d -q --tmpdir "osmimages.XXXXXX")"
436 trap 'rm -rf "$LXD_IMAGE_DIR"' EXIT
437 fi
438 echo -e "\nDeleting previous lxd images if they exist"
439 lxc image show osm-ro &>/dev/null && lxc image delete osm-ro
440 lxc image show osm-vca &>/dev/null && lxc image delete osm-vca
441 lxc image show osm-soui &>/dev/null && lxc image delete osm-soui
442 echo -e "\nImporting osm-ro"
443 [ -z "$LXD_REPOSITORY_PATH" ] && wget -O $LXD_IMAGE_DIR/osm-ro.tar.gz $LXD_REPOSITORY_BASE/$LXD_RELEASE/osm-ro.tar.gz
444 lxc image import $LXD_IMAGE_DIR/osm-ro.tar.gz --alias osm-ro
445 rm -f $LXD_IMAGE_DIR/osm-ro.tar.gz
446 echo -e "\nImporting osm-vca"
447 [ -z "$LXD_REPOSITORY_PATH" ] && wget -O $LXD_IMAGE_DIR/osm-vca.tar.gz $LXD_REPOSITORY_BASE/$LXD_RELEASE/osm-vca.tar.gz
448 lxc image import $LXD_IMAGE_DIR/osm-vca.tar.gz --alias osm-vca
449 rm -f $LXD_IMAGE_DIR/osm-vca.tar.gz
450 echo -e "\nImporting osm-soui"
451 [ -z "$LXD_REPOSITORY_PATH" ] && wget -O $LXD_IMAGE_DIR/osm-soui.tar.gz $LXD_REPOSITORY_BASE/$LXD_RELEASE/osm-soui.tar.gz
452 lxc image import $LXD_IMAGE_DIR/osm-soui.tar.gz --alias osm-soui
453 rm -f $LXD_IMAGE_DIR/osm-soui.tar.gz
454 launch_container_from_lxd RO osm-ro
455 ro_is_up && track RO
456 launch_container_from_lxd VCA osm-vca
457 vca_is_up && track VCA
458 launch_container_from_lxd MON osm-mon
459 mon_is_up && track MON
460 launch_container_from_lxd SO osm-soui
461 #so_is_up && track SOUI
462 track SOUI
463 }
464
465 function install_docker_ce() {
466 # installs and configures Docker CE
467 echo "Installing Docker CE ..."
468 sudo apt-get -qq update
469 sudo apt-get install -y apt-transport-https ca-certificates software-properties-common
470 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
471 sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
472 sudo apt-get -qq update
473 sudo apt-get install -y docker-ce
474 echo "Adding user to group 'docker'"
475 sudo groupadd -f docker
476 sudo usermod -aG docker $USER
477 echo "... Docker CE installation done"
478 sleep 2
479 sudo service docker restart
480 echo "... restarted Docker service"
481 }
482
483 function install_vimemu() {
484 # install Docker
485 install_docker_ce
486 # clone vim-emu repository (attention: branch is currently master only)
487 echo "Cloning vim-emu repository ..."
488 git clone https://osm.etsi.org/gerrit/osm/vim-emu.git
489 # build vim-emu docker
490 echo "Building vim-emu Docker container..."
491 sudo docker build -t vim-emu-img -f vim-emu/Dockerfile vim-emu/
492 # start vim-emu container as daemon
493 echo "Starting vim-emu Docker container 'vim-emu' ..."
494 sudo 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
495 echo "Waiting for 'vim-emu' container to start ..."
496 sleep 5
497 export VIMEMU_HOSTNAME=$(sudo docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' vim-emu)
498 echo "vim-emu running at $VIMEMU_HOSTNAME ..."
499 echo -e "You might be interested in adding the following OSM client env variables to your .bashrc file:"
500 echo " export OSM_HOSTNAME=${OSM_HOSTNAME}"
501 echo " export OSM_RO_HOSTNAME=${OSM_RO_HOSTNAME}"
502 echo -e "You might be interested in adding the following vim-emu env variables to your .bashrc file:"
503 echo " export VIMEMU_HOSTNAME=${VIMEMU_HOSTNAME}"
504 echo -e "\nTo add the emulated VIM to OSM you should do:"
505 echo " osm vim-create --name emu-vim1 --user username --password password --auth_url http://$VIMEMU_HOSTNAME:6001/v2.0 --tenant tenantName --account_type openstack"
506 }
507
508 function dump_vars(){
509 echo "DEVELOP=$DEVELOP"
510 echo "INSTALL_FROM_SOURCE=$INSTALL_FROM_SOURCE"
511 echo "UNINSTALL=$UNINSTALL"
512 echo "NAT=$NAT"
513 echo "UPDATE=$UPDATE"
514 echo "RECONFIGURE=$RECONFIGURE"
515 echo "TEST_INSTALLER=$TEST_INSTALLER"
516 echo "INSTALL_VIMEMU=$INSTALL_VIMEMU"
517 echo "INSTALL_LXD=$INSTALL_LXD"
518 echo "INSTALL_FROM_LXDIMAGES=$INSTALL_FROM_LXDIMAGES"
519 echo "LXD_REPOSITORY_BASE=$LXD_REPOSITORY_BASE"
520 echo "LXD_REPOSITORY_PATH=$LXD_REPOSITORY_PATH"
521 echo "RELEASE=$RELEASE"
522 echo "REPOSITORY=$REPOSITORY"
523 echo "REPOSITORY_BASE=$REPOSITORY_BASE"
524 echo "REPOSITORY_KEY=$REPOSITORY_KEY"
525 echo "NOCONFIGURE=$NOCONFIGURE"
526 echo "SHOWOPTS=$SHOWOPTS"
527 echo "Install from specific refspec (-b): $COMMIT_ID"
528 }
529
530 function track(){
531 ctime=`date +%s`
532 duration=$((ctime - SESSION_ID))
533 url="http://www.woopra.com/track/ce?project=osm.etsi.org&cookie=${SESSION_ID}"
534 #url="${url}&ce_campaign_name=${CAMPAIGN_NAME}"
535 event_name="bin"
536 [ -n "$INSTALL_FROM_SOURCE" ] && event_name="src"
537 [ -n "$INSTALL_FROM_LXDIMAGES" ] && event_name="lxd"
538 event_name="${event_name}_$1"
539 url="${url}&event=${event_name}&ce_duration=${duration}"
540 wget -q -O /dev/null $url
541 }
542
543 UNINSTALL=""
544 DEVELOP=""
545 NAT=""
546 UPDATE=""
547 RECONFIGURE=""
548 TEST_INSTALLER=""
549 INSTALL_LXD=""
550 SHOWOPTS=""
551 COMMIT_ID=""
552 ASSUME_YES=""
553 INSTALL_FROM_SOURCE=""
554 RELEASE="-R ReleaseTHREE"
555 INSTALL_VIMEMU=""
556 INSTALL_FROM_LXDIMAGES=""
557 LXD_REPOSITORY_BASE="https://osm-download.etsi.org/repository/osm/lxd"
558 LXD_REPOSITORY_PATH=""
559 NOCONFIGURE=""
560 RELEASE_DAILY=""
561 SESSION_ID=`date +%s`
562
563 while getopts ":hy-:b:r:k:u:R:l:p:" o; do
564 case "${o}" in
565 h)
566 usage && exit 0
567 ;;
568 b)
569 COMMIT_ID=${OPTARG}
570 ;;
571 r)
572 REPOSITORY="-r ${OPTARG}"
573 ;;
574 R)
575 RELEASE="-R ${OPTARG}"
576 ;;
577 k)
578 REPOSITORY_KEY="-k ${OPTARG}"
579 ;;
580 u)
581 REPOSITORY_BASE="-u ${OPTARG}"
582 ;;
583 l)
584 LXD_REPOSITORY_BASE="${OPTARG}"
585 ;;
586 p)
587 LXD_REPOSITORY_PATH="${OPTARG}"
588 ;;
589 -)
590 [ "${OPTARG}" == "help" ] && usage && exit 0
591 [ "${OPTARG}" == "source" ] && INSTALL_FROM_SOURCE="y" && continue
592 [ "${OPTARG}" == "develop" ] && DEVELOP="y" && continue
593 [ "${OPTARG}" == "uninstall" ] && UNINSTALL="y" && continue
594 [ "${OPTARG}" == "nat" ] && NAT="y" && continue
595 [ "${OPTARG}" == "update" ] && UPDATE="y" && continue
596 [ "${OPTARG}" == "reconfigure" ] && RECONFIGURE="y" && continue
597 [ "${OPTARG}" == "test" ] && TEST_INSTALLER="y" && continue
598 [ "${OPTARG}" == "lxdinstall" ] && INSTALL_LXD="y" && continue
599 [ "${OPTARG}" == "lxdimages" ] && INSTALL_FROM_LXDIMAGES="y" && continue
600 [ "${OPTARG}" == "vimemu" ] && INSTALL_VIMEMU="y" && continue
601 [ "${OPTARG}" == "noconfigure" ] && NOCONFIGURE="y" && continue
602 [ "${OPTARG}" == "showopts" ] && SHOWOPTS="y" && continue
603 [ "${OPTARG}" == "daily" ] && RELEASE_DAILY="y" && continue
604 echo -e "Invalid option: '--$OPTARG'\n" >&2
605 usage && exit 1
606 ;;
607 \?)
608 echo -e "Invalid option: '-$OPTARG'\n" >&2
609 usage && exit 1
610 ;;
611 y)
612 ASSUME_YES="y"
613 ;;
614 *)
615 usage && exit 1
616 ;;
617 esac
618 done
619
620 if [ -n "$SHOWOPTS" ]; then
621 dump_vars
622 exit 0
623 fi
624
625 [ -n "$RELEASE_DAILY" ] && echo -e "\nInstalling from daily build repo" && RELEASE="-R ReleaseTHREE-daily" && REPOSITORY="-r testing" && COMMIT_ID="master"
626
627 # if develop, we force master
628 [ -z "$COMMIT_ID" ] && [ -n "$DEVELOP" ] && COMMIT_ID="master"
629
630 # forcing source from master removed. Now only install from source when explicit
631 # [ -n "$COMMIT_ID" ] && [ "$COMMIT_ID" == "master" ] && INSTALL_FROM_SOURCE="y"
632
633 if [ -n "$TEST_INSTALLER" ]; then
634 echo -e "\nUsing local devops repo for OSM installation"
635 TEMPDIR="$(dirname $(realpath $(dirname $0)))"
636 else
637 echo -e "\nCreating temporary dir for OSM installation"
638 TEMPDIR="$(mktemp -d -q --tmpdir "installosm.XXXXXX")"
639 trap 'rm -rf "$TEMPDIR"' EXIT
640 fi
641
642 need_packages="git jq"
643 for package in $need_packages; do
644 echo -e "Checking required packages: $package"
645 dpkg -l $package &>/dev/null \
646 || ! echo -e " $package not installed.\nInstalling $package requires root privileges" \
647 || sudo apt-get install -y $package \
648 || FATAL "failed to install $package"
649 done
650
651 if [ -z "$TEST_INSTALLER" ]; then
652 echo -e "\nCloning devops repo temporarily"
653 git clone https://osm.etsi.org/gerrit/osm/devops.git $TEMPDIR
654 RC_CLONE=$?
655 fi
656
657 echo -e "\nGuessing the current stable release"
658 LATEST_STABLE_DEVOPS=`git -C $TEMPDIR tag -l v[0-9].* | sort -V | tail -n1`
659 [ -z "$COMMIT_ID" ] && [ -z "$LATEST_STABLE_DEVOPS" ] && echo "Could not find the current latest stable release" && exit 0
660 echo "Latest tag in devops repo: $LATEST_STABLE_DEVOPS"
661 [ -z "$COMMIT_ID" ] && [ -n "$LATEST_STABLE_DEVOPS" ] && COMMIT_ID="tags/$LATEST_STABLE_DEVOPS"
662
663 if [ -n "$RELEASE_DAILY" ]; then
664 echo "Using master/HEAD devops"
665 git -C $TEMPDIR checkout master
666 elif [ -z "$TEST_INSTALLER" ]; then
667 git -C $TEMPDIR checkout tags/$LATEST_STABLE_DEVOPS
668 fi
669
670 OSM_DEVOPS=$TEMPDIR
671 OSM_JENKINS="$TEMPDIR/jenkins"
672 . $OSM_JENKINS/common/all_funcs
673
674 [ -n "$UNINSTALL" ] && uninstall && echo -e "\nDONE" && exit 0
675 [ -n "$NAT" ] && nat && echo -e "\nDONE" && exit 0
676 [ -n "$UPDATE" ] && update && echo -e "\nDONE" && exit 0
677 [ -n "$RECONFIGURE" ] && configure && echo -e "\nDONE" && exit 0
678
679 #Installation starts here
680 echo -e "\nInstalling OSM from refspec: $COMMIT_ID"
681 if [ -n "$INSTALL_FROM_SOURCE" ] && [ -z "$ASSUME_YES" ]; then
682 ! ask_user "The installation will take about 75-90 minutes. Continue (Y/n)? " y && echo "Cancelled!" && exit 1
683 fi
684
685 echo -e "\nChecking required packages: wget, curl, tar"
686 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
687
688 echo -e "Checking required packages: lxd"
689 lxd --version &>/dev/null || FATAL "lxd not present, exiting."
690 [ -n "$INSTALL_LXD" ] && echo -e "\nInstalling and configuring lxd" && install_lxd
691
692 wget -q -O- https://osm-download.etsi.org/ftp/osm-3.0-three/README.txt &> /dev/null
693 track start
694
695 # use local devops for containers
696 export OSM_USE_LOCAL_DEVOPS=true
697 if [ -n "$INSTALL_FROM_SOURCE" ]; then #install from source
698 echo -e "\nCreating the containers and building from source ..."
699 $OSM_DEVOPS/jenkins/host/start_build RO --notest checkout $COMMIT_ID || FATAL "RO container build failed (refspec: '$COMMIT_ID')"
700 ro_is_up && track RO
701 $OSM_DEVOPS/jenkins/host/start_build VCA || FATAL "VCA container build failed"
702 vca_is_up && track VCA
703 $OSM_DEVOPS/jenkins/host/start_build MON || FATAL "MON install failed"
704 mon_is_up && track MON
705 $OSM_DEVOPS/jenkins/host/start_build SO checkout $COMMIT_ID || FATAL "SO container build failed (refspec: '$COMMIT_ID')"
706 $OSM_DEVOPS/jenkins/host/start_build UI checkout $COMMIT_ID || FATAL "UI container build failed (refspec: '$COMMIT_ID')"
707 #so_is_up && track SOUI
708 track SOUI
709 elif [ -n "$INSTALL_FROM_LXDIMAGES" ]; then #install from LXD images stored in OSM repo
710 echo -e "\nInstalling from lxd images ..."
711 install_from_lxdimages
712 else #install from binaries
713 echo -e "\nCreating the containers and installing from binaries ..."
714 $OSM_DEVOPS/jenkins/host/install RO $REPOSITORY $RELEASE $REPOSITORY_KEY $REPOSITORY_BASE || FATAL "RO install failed"
715 ro_is_up && track RO
716 $OSM_DEVOPS/jenkins/host/start_build VCA || FATAL "VCA install failed"
717 vca_is_up && track VCA
718 $OSM_DEVOPS/jenkins/host/install MON || FATAL "MON build failed"
719 mon_is_up && track MON
720 $OSM_DEVOPS/jenkins/host/install SO $REPOSITORY $RELEASE $REPOSITORY_KEY $REPOSITORY_BASE || FATAL "SO install failed"
721 $OSM_DEVOPS/jenkins/host/install UI $REPOSITORY $RELEASE $REPOSITORY_KEY $REPOSITORY_BASE || FATAL "UI install failed"
722 #so_is_up && track SOUI
723 track SOUI
724 fi
725
726 #Install iptables-persistent and configure NAT rules
727 [ -z "$NOCONFIGURE" ] && nat
728
729 #Configure components
730 [ -z "$NOCONFIGURE" ] && configure
731
732 #Install osmclient
733 [ -z "$NOCONFIGURE" ] && install_osmclient
734
735 #Install vim-emu (optional)
736 if [ -n "$INSTALL_VIMEMU" ]; then
737 echo -e "\nInstalling vim-emu ..."
738 install_vimemu
739 fi
740
741 wget -q -O- https://osm-download.etsi.org/ftp/osm-3.0-three/README2.txt &> /dev/null
742 track end
743 echo -e "\nDONE"
744