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