install_osm.sh: echo proper env variables when installing osmclient
[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-osmclient
424 #sed 's,OSM_SOL005=[^$]*,OSM_SOL005=True,' -i ${HOME}/.bashrc
425 #echo 'export OSM_HOSTNAME=localhost' >> ${HOME}/.bashrc
426 #echo 'export OSM_SOL005=True' >> ${HOME}/.bashrc
427 [ -z "$INSTALL_LIGHTWEIGHT" ] && export OSM_HOSTNAME=`lxc list | awk '($2=="SO-ub"){print $6}'`
428 [ -z "$INSTALL_LIGHTWEIGHT" ] && export OSM_RO_HOSTNAME=`lxc list | awk '($2=="RO"){print $6}'`
429 [ -n "$INSTALL_LIGHTWEIGHT" ] && export OSM_HOSTNAME=localhost
430 echo -e "\nOSM client installed"
431 echo -e "You might be interested in adding the following OSM client env variables to your .bashrc file:"
432 echo " export OSM_HOSTNAME=${OSM_HOSTNAME}"
433 [ -n "$INSTALL_LIGHTWEIGHT" ] && echo " export OSM_SOL005=True"
434 [ -z "$INSTALL_LIGHTWEIGHT" ] && echo " export OSM_RO_HOSTNAME=${OSM_RO_HOSTNAME}"
435 return 0
436 }
437
438 function install_from_lxdimages(){
439 LXD_RELEASE=${RELEASE#"-R "}
440 if [ -n "$LXD_REPOSITORY_PATH" ]; then
441 LXD_IMAGE_DIR="$LXD_REPOSITORY_PATH"
442 else
443 LXD_IMAGE_DIR="$(mktemp -d -q --tmpdir "osmimages.XXXXXX")"
444 trap 'rm -rf "$LXD_IMAGE_DIR"' EXIT
445 fi
446 echo -e "\nDeleting previous lxd images if they exist"
447 lxc image show osm-ro &>/dev/null && lxc image delete osm-ro
448 lxc image show osm-vca &>/dev/null && lxc image delete osm-vca
449 lxc image show osm-soui &>/dev/null && lxc image delete osm-soui
450 echo -e "\nImporting osm-ro"
451 [ -z "$LXD_REPOSITORY_PATH" ] && wget -O $LXD_IMAGE_DIR/osm-ro.tar.gz $LXD_REPOSITORY_BASE/$LXD_RELEASE/osm-ro.tar.gz
452 lxc image import $LXD_IMAGE_DIR/osm-ro.tar.gz --alias osm-ro
453 rm -f $LXD_IMAGE_DIR/osm-ro.tar.gz
454 echo -e "\nImporting osm-vca"
455 [ -z "$LXD_REPOSITORY_PATH" ] && wget -O $LXD_IMAGE_DIR/osm-vca.tar.gz $LXD_REPOSITORY_BASE/$LXD_RELEASE/osm-vca.tar.gz
456 lxc image import $LXD_IMAGE_DIR/osm-vca.tar.gz --alias osm-vca
457 rm -f $LXD_IMAGE_DIR/osm-vca.tar.gz
458 echo -e "\nImporting osm-soui"
459 [ -z "$LXD_REPOSITORY_PATH" ] && wget -O $LXD_IMAGE_DIR/osm-soui.tar.gz $LXD_REPOSITORY_BASE/$LXD_RELEASE/osm-soui.tar.gz
460 lxc image import $LXD_IMAGE_DIR/osm-soui.tar.gz --alias osm-soui
461 rm -f $LXD_IMAGE_DIR/osm-soui.tar.gz
462 launch_container_from_lxd RO osm-ro
463 ro_is_up && track RO
464 launch_container_from_lxd VCA osm-vca
465 vca_is_up && track VCA
466 launch_container_from_lxd MON osm-mon
467 mon_is_up && track MON
468 launch_container_from_lxd SO osm-soui
469 #so_is_up && track SOUI
470 track SOUI
471 }
472
473 function install_docker_ce() {
474 # installs and configures Docker CE
475 echo "Installing Docker CE ..."
476 sudo apt-get -qq update
477 sudo apt-get install -y apt-transport-https ca-certificates software-properties-common
478 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
479 sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
480 sudo apt-get -qq update
481 sudo apt-get install -y docker-ce
482 echo "Adding user to group 'docker'"
483 sudo groupadd -f docker
484 sudo usermod -aG docker $USER
485 echo "... Docker CE installation done"
486 sleep 2
487 sudo service docker restart
488 echo "... restarted Docker service"
489 }
490
491 function install_juju() {
492 echo "Installing juju"
493 sudo snap install juju --classic
494 sudo dpkg-reconfigure -p medium lxd
495 juju bootstrap localhost osm
496 echo "Finished installation of juju"
497 }
498
499 function generate_docker_images() {
500 echo "Pulling and generating docker images"
501 newgrp docker << EONG
502 docker pull wurstmeister/kafka
503 docker pull wurstmeister/zookeeper
504 docker pull mongo
505 docker pull mysql
506 git -C ${LWTEMPDIR} clone https://osm.etsi.org/gerrit/osm/MON
507 docker build ${LWTEMPDIR}/MON -f ${LWTEMPDIR}/MON/docker/Dockerfile -t osm/mon || ! echo "cannot build MON docker image" >&2
508 docker build ${LWTEMPDIR}/MON/policy_module -f ${LWTEMPDIR}/MON/policy_module/Dockerfile -t osm/pm || ! echo "cannot build PM docker image" >&2
509 git -C ${LWTEMPDIR} clone https://osm.etsi.org/gerrit/osm/NBI
510 docker build ${LWTEMPDIR}/NBI -f ${LWTEMPDIR}/NBI/Dockerfile.local -t osm/nbi || ! echo "cannot build NBI docker image" >&2
511 git -C ${LWTEMPDIR} clone https://osm.etsi.org/gerrit/osm/RO
512 docker build ${LWTEMPDIR}/RO -f ${LWTEMPDIR}/RO/docker/Dockerfile-local -t osm/ro || ! echo "cannot build RO docker image" >&2
513 docker build ${LWTEMPDIR}/RO/lcm -t osm/lcm || ! echo "cannot build LCM docker image" >&2
514 git -C ${LWTEMPDIR} clone https://github.com/superfluidity/osm-light-ui.git
515 docker build ${LWTEMPDIR}/osm-light-ui -t osm/light-ui -f ${LWTEMPDIR}/osm-light-ui/code/docker/Dockerfile
516 EONG
517 echo "Finished generation of docker images"
518 }
519
520 function generate_docker_env_files() {
521 echo "Generating docker env files"
522 OSMLCM_VCA_HOST=`juju show-controller|grep api-endpoints|awk -F\' '{print $2}'|awk -F\: '{print $1}'`
523 OSMLCM_VCA_SECRET=`grep password /home/ubuntu/.local/share/juju/accounts.yaml |awk '{print $2}'`
524 MYSQL_ROOT_PASSWORD=`date +%s | sha256sum | base64 | head -c 32`
525 echo "OSMLCM_VCA_HOST=${OSMLCM_VCA_HOST}" > ${OSM_DEVOPS}/installers/docker/lcm.env
526 echo "OSMLCM_VCA_SECRET=${OSMLCM_VCA_SECRET}" >> ${OSM_DEVOPS}/installers/docker/lcm.env
527 echo "MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}" > ${OSM_DEVOPS}/installers/docker/ro-db.env
528 echo "RO_DB_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}" > ${OSM_DEVOPS}/installers/docker/ro.env
529 echo "OS_NOTIFIER_URI=http://${DEFAULT_IP}:8662" > ${OSM_DEVOPS}/installers/docker/mon.env
530 echo "Finished generation of docker env files"
531 }
532
533 function deploy_lightweight() {
534 echo "Deploying lightweight build"
535 newgrp docker << EONG
536 docker swarm init --advertise-addr ${DEFAULT_IP}
537 docker network create --driver=overlay --attachable netOSM
538 docker stack deploy -c ${OSM_DEVOPS}/installers/docker/docker-compose.yaml osm
539 EONG
540 echo "Finished deployment of lightweight build"
541 }
542
543 function install_osmclient_sol005() {
544 sudo apt-get update
545 sudo apt-get install -y python-pip libcurl4-gnutls-dev libgnutls-dev
546 git -C ${LWTEMPDIR} clone https://osm.etsi.org/gerrit/osm/osmclient
547 sudo -H pip install -U pip
548 sudo -H pip install -U setuptools
549 pushd ${LWTEMPDIR}/osmclient
550 sudo -H python setup.py install
551 popd
552 #sed 's,OSM_SOL005=[^$]*,OSM_SOL005=True,' -i ~/.bashrc
553 echo 'export OSM_HOSTNAME=localhost' >> ${HOME}/.bashrc
554 echo 'export OSM_SOL005=True' >> ${HOME}/.bashrc
555 }
556
557 function install_lightweight() {
558 echo "Installing lightweight build of OSM"
559 LWTEMPDIR="$(mktemp -d -q --tmpdir "installosmlight.XXXXXX")"
560 trap 'rm -rf "${LWTEMPDIR}"' EXIT
561 DEFAULT_IF=`route -n |awk '$1~/^0.0.0.0/ {print $8}'`
562 DEFAULT_IP=`ip -o -4 a |grep ${DEFAULT_IF}|awk '{split($4,a,"/"); print a[1]}'`
563 install_juju
564 install_docker_ce
565 generate_docker_images
566 generate_docker_env_files
567 deploy_lightweight
568 install_osmclient
569 return 0
570 }
571
572 function install_vimemu() {
573 # install Docker
574 install_docker_ce
575 # clone vim-emu repository (attention: branch is currently master only)
576 echo "Cloning vim-emu repository ..."
577 git clone https://osm.etsi.org/gerrit/osm/vim-emu.git
578 # build vim-emu docker
579 echo "Building vim-emu Docker container..."
580 sudo docker build -t vim-emu-img -f vim-emu/Dockerfile vim-emu/
581 # start vim-emu container as daemon
582 echo "Starting vim-emu Docker container 'vim-emu' ..."
583 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
584 echo "Waiting for 'vim-emu' container to start ..."
585 sleep 5
586 export VIMEMU_HOSTNAME=$(sudo docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' vim-emu)
587 echo "vim-emu running at ${VIMEMU_HOSTNAME} ..."
588 echo -e "You might be interested in adding the following OSM client env variables to your .bashrc file:"
589 echo " export OSM_HOSTNAME=${OSM_HOSTNAME}"
590 echo " export OSM_RO_HOSTNAME=${OSM_RO_HOSTNAME}"
591 echo -e "You might be interested in adding the following vim-emu env variables to your .bashrc file:"
592 echo " export VIMEMU_HOSTNAME=${VIMEMU_HOSTNAME}"
593 echo -e "\nTo add the emulated VIM to OSM you should do:"
594 echo " osm vim-create --name emu-vim1 --user username --password password --auth_url http://${VIMEMU_HOSTNAME}:6001/v2.0 --tenant tenantName --account_type openstack"
595 }
596
597 function dump_vars(){
598 echo "DEVELOP=$DEVELOP"
599 echo "INSTALL_FROM_SOURCE=$INSTALL_FROM_SOURCE"
600 echo "UNINSTALL=$UNINSTALL"
601 echo "NAT=$NAT"
602 echo "UPDATE=$UPDATE"
603 echo "RECONFIGURE=$RECONFIGURE"
604 echo "TEST_INSTALLER=$TEST_INSTALLER"
605 echo "INSTALL_VIMEMU=$INSTALL_VIMEMU"
606 echo "INSTALL_LXD=$INSTALL_LXD"
607 echo "INSTALL_FROM_LXDIMAGES=$INSTALL_FROM_LXDIMAGES"
608 echo "LXD_REPOSITORY_BASE=$LXD_REPOSITORY_BASE"
609 echo "LXD_REPOSITORY_PATH=$LXD_REPOSITORY_PATH"
610 echo "INSTALL_LIGHTWEIGHT=$INSTALL_LIGHTWEIGHT"
611 echo "RELEASE=$RELEASE"
612 echo "REPOSITORY=$REPOSITORY"
613 echo "REPOSITORY_BASE=$REPOSITORY_BASE"
614 echo "REPOSITORY_KEY=$REPOSITORY_KEY"
615 echo "NOCONFIGURE=$NOCONFIGURE"
616 echo "SHOWOPTS=$SHOWOPTS"
617 echo "Install from specific refspec (-b): $COMMIT_ID"
618 }
619
620 function track(){
621 ctime=`date +%s`
622 duration=$((ctime - SESSION_ID))
623 url="http://www.woopra.com/track/ce?project=osm.etsi.org&cookie=${SESSION_ID}"
624 #url="${url}&ce_campaign_name=${CAMPAIGN_NAME}"
625 event_name="bin"
626 [ -n "$INSTALL_FROM_SOURCE" ] && event_name="src"
627 [ -n "$INSTALL_FROM_LXDIMAGES" ] && event_name="lxd"
628 event_name="${event_name}_$1"
629 url="${url}&event=${event_name}&ce_duration=${duration}"
630 wget -q -O /dev/null $url
631 }
632
633 UNINSTALL=""
634 DEVELOP=""
635 NAT=""
636 UPDATE=""
637 RECONFIGURE=""
638 TEST_INSTALLER=""
639 INSTALL_LXD=""
640 SHOWOPTS=""
641 COMMIT_ID=""
642 ASSUME_YES=""
643 INSTALL_FROM_SOURCE=""
644 RELEASE="-R ReleaseTHREE"
645 REPOSITORY="-r stable"
646 INSTALL_VIMEMU=""
647 INSTALL_FROM_LXDIMAGES=""
648 LXD_REPOSITORY_BASE="https://osm-download.etsi.org/repository/osm/lxd"
649 LXD_REPOSITORY_PATH=""
650 INSTALL_LIGHTWEIGHT=""
651 NOCONFIGURE=""
652 RELEASE_DAILY=""
653 SESSION_ID=`date +%s`
654 OSM_DEVOPS=
655
656 while getopts ":hy-:b:r:k:u:R:l:p:D:" o; do
657 case "${o}" in
658 h)
659 usage && exit 0
660 ;;
661 b)
662 COMMIT_ID=${OPTARG}
663 ;;
664 r)
665 REPOSITORY="-r ${OPTARG}"
666 ;;
667 R)
668 RELEASE="-R ${OPTARG}"
669 ;;
670 k)
671 REPOSITORY_KEY="-k ${OPTARG}"
672 ;;
673 u)
674 REPOSITORY_BASE="-u ${OPTARG}"
675 ;;
676 l)
677 LXD_REPOSITORY_BASE="${OPTARG}"
678 ;;
679 p)
680 LXD_REPOSITORY_PATH="${OPTARG}"
681 ;;
682 D)
683 OSM_DEVOPS="${OPTARG}"
684 ;;
685 -)
686 [ "${OPTARG}" == "help" ] && usage && exit 0
687 [ "${OPTARG}" == "source" ] && INSTALL_FROM_SOURCE="y" && continue
688 [ "${OPTARG}" == "develop" ] && DEVELOP="y" && continue
689 [ "${OPTARG}" == "uninstall" ] && UNINSTALL="y" && continue
690 [ "${OPTARG}" == "nat" ] && NAT="y" && continue
691 [ "${OPTARG}" == "update" ] && UPDATE="y" && continue
692 [ "${OPTARG}" == "reconfigure" ] && RECONFIGURE="y" && continue
693 [ "${OPTARG}" == "test" ] && TEST_INSTALLER="y" && continue
694 [ "${OPTARG}" == "lxdinstall" ] && INSTALL_LXD="y" && continue
695 [ "${OPTARG}" == "lxdimages" ] && INSTALL_FROM_LXDIMAGES="y" && continue
696 [ "${OPTARG}" == "lightweight" ] && INSTALL_LIGHTWEIGHT="y" && continue
697 [ "${OPTARG}" == "vimemu" ] && INSTALL_VIMEMU="y" && continue
698 [ "${OPTARG}" == "noconfigure" ] && NOCONFIGURE="y" && continue
699 [ "${OPTARG}" == "showopts" ] && SHOWOPTS="y" && continue
700 [ "${OPTARG}" == "daily" ] && RELEASE_DAILY="y" && continue
701 echo -e "Invalid option: '--$OPTARG'\n" >&2
702 usage && exit 1
703 ;;
704 \?)
705 echo -e "Invalid option: '-$OPTARG'\n" >&2
706 usage && exit 1
707 ;;
708 y)
709 ASSUME_YES="y"
710 ;;
711 *)
712 usage && exit 1
713 ;;
714 esac
715 done
716
717 if [ -n "$SHOWOPTS" ]; then
718 dump_vars
719 exit 0
720 fi
721
722 [ -n "$RELEASE_DAILY" ] && echo -e "\nInstalling from daily build repo" && RELEASE="-R ReleaseTHREE-daily" && REPOSITORY="-r testing" && COMMIT_ID="master"
723
724 # if develop, we force master
725 [ -z "$COMMIT_ID" ] && [ -n "$DEVELOP" ] && COMMIT_ID="master"
726
727 # forcing source from master removed. Now only install from source when explicit
728 # [ -n "$COMMIT_ID" ] && [ "$COMMIT_ID" == "master" ] && INSTALL_FROM_SOURCE="y"
729
730 if [ -z "$OSM_DEVOPS" ]; then
731 if [ -n "$TEST_INSTALLER" ]; then
732 echo -e "\nUsing local devops repo for OSM installation"
733 TEMPDIR="$(dirname $(realpath $(dirname $0)))"
734 else
735 echo -e "\nCreating temporary dir for OSM installation"
736 TEMPDIR="$(mktemp -d -q --tmpdir "installosm.XXXXXX")"
737 trap 'rm -rf "$TEMPDIR"' EXIT
738 fi
739 fi
740
741 need_packages="git jq wget curl tar"
742 echo -e "Checking required packages: $need_packages"
743 dpkg -l $need_packages &>/dev/null \
744 || ! echo -e "One or several required packages are not installed. Updating apt cache requires root privileges." \
745 || sudo apt-get update \
746 || FATAL "failed to run apt-get update"
747 dpkg -l $need_packages &>/dev/null \
748 || ! echo -e "Installing $need_packages requires root privileges." \
749 || sudo apt-get install -y $need_packages \
750 || FATAL "failed to install $need_packages"
751
752 if [ -z "$OSM_DEVOPS" ]; then
753 if [ -z "$TEST_INSTALLER" ]; then
754 echo -e "\nCloning devops repo temporarily"
755 git clone https://osm.etsi.org/gerrit/osm/devops.git $TEMPDIR
756 RC_CLONE=$?
757 fi
758
759 echo -e "\nGuessing the current stable release"
760 LATEST_STABLE_DEVOPS=`git -C $TEMPDIR tag -l v[0-9].* | sort -V | tail -n1`
761 [ -z "$COMMIT_ID" ] && [ -z "$LATEST_STABLE_DEVOPS" ] && echo "Could not find the current latest stable release" && exit 0
762 echo "Latest tag in devops repo: $LATEST_STABLE_DEVOPS"
763 [ -z "$COMMIT_ID" ] && [ -n "$LATEST_STABLE_DEVOPS" ] && COMMIT_ID="tags/$LATEST_STABLE_DEVOPS"
764
765 if [ -n "$RELEASE_DAILY" ]; then
766 echo "Using master/HEAD devops"
767 git -C $TEMPDIR checkout master
768 elif [ -z "$TEST_INSTALLER" ]; then
769 git -C $TEMPDIR checkout tags/$LATEST_STABLE_DEVOPS
770 fi
771 OSM_DEVOPS=$TEMPDIR
772 fi
773
774 OSM_JENKINS="$OSM_DEVOPS/jenkins"
775 . $OSM_JENKINS/common/all_funcs
776
777 [ -n "$UNINSTALL" ] && uninstall && echo -e "\nDONE" && exit 0
778 [ -n "$NAT" ] && nat && echo -e "\nDONE" && exit 0
779 [ -n "$UPDATE" ] && update && echo -e "\nDONE" && exit 0
780 [ -n "$RECONFIGURE" ] && configure && echo -e "\nDONE" && exit 0
781
782 #Installation starts here
783 [ -n "$INSTALL_LIGHTWEIGHT" ] && install_lightweight && echo -e "\nDONE" && exit 0
784 echo -e "\nInstalling OSM from refspec: $COMMIT_ID"
785 if [ -n "$INSTALL_FROM_SOURCE" ] && [ -z "$ASSUME_YES" ]; then
786 ! ask_user "The installation will take about 75-90 minutes. Continue (Y/n)? " y && echo "Cancelled!" && exit 1
787 fi
788
789 echo -e "Checking required packages: lxd"
790 lxd --version &>/dev/null || FATAL "lxd not present, exiting."
791 [ -n "$INSTALL_LXD" ] && echo -e "\nInstalling and configuring lxd" && install_lxd
792
793 wget -q -O- https://osm-download.etsi.org/ftp/osm-4.0-four/README.txt &> /dev/null
794 track start
795
796 # use local devops for containers
797 export OSM_USE_LOCAL_DEVOPS=true
798 if [ -n "$INSTALL_FROM_SOURCE" ]; then #install from source
799 echo -e "\nCreating the containers and building from source ..."
800 $OSM_DEVOPS/jenkins/host/start_build RO --notest checkout $COMMIT_ID || FATAL "RO container build failed (refspec: '$COMMIT_ID')"
801 ro_is_up && track RO
802 $OSM_DEVOPS/jenkins/host/start_build VCA || FATAL "VCA container build failed"
803 vca_is_up && track VCA
804 $OSM_DEVOPS/jenkins/host/start_build MON || FATAL "MON install failed"
805 mon_is_up && track MON
806 $OSM_DEVOPS/jenkins/host/start_build SO checkout $COMMIT_ID || FATAL "SO container build failed (refspec: '$COMMIT_ID')"
807 $OSM_DEVOPS/jenkins/host/start_build UI checkout $COMMIT_ID || FATAL "UI container build failed (refspec: '$COMMIT_ID')"
808 #so_is_up && track SOUI
809 track SOUI
810 elif [ -n "$INSTALL_FROM_LXDIMAGES" ]; then #install from LXD images stored in OSM repo
811 echo -e "\nInstalling from lxd images ..."
812 install_from_lxdimages
813 else #install from binaries
814 echo -e "\nCreating the containers and installing from binaries ..."
815 $OSM_DEVOPS/jenkins/host/install RO $REPOSITORY $RELEASE $REPOSITORY_KEY $REPOSITORY_BASE || FATAL "RO install failed"
816 ro_is_up && track RO
817 $OSM_DEVOPS/jenkins/host/start_build VCA || FATAL "VCA install failed"
818 vca_is_up && track VCA
819 $OSM_DEVOPS/jenkins/host/install MON || FATAL "MON build failed"
820 mon_is_up && track MON
821 $OSM_DEVOPS/jenkins/host/install SO $REPOSITORY $RELEASE $REPOSITORY_KEY $REPOSITORY_BASE || FATAL "SO install failed"
822 $OSM_DEVOPS/jenkins/host/install UI $REPOSITORY $RELEASE $REPOSITORY_KEY $REPOSITORY_BASE || FATAL "UI install failed"
823 #so_is_up && track SOUI
824 track SOUI
825 fi
826
827 #Install iptables-persistent and configure NAT rules
828 [ -z "$NOCONFIGURE" ] && nat
829
830 #Configure components
831 [ -z "$NOCONFIGURE" ] && configure
832
833 #Install osmclient
834 [ -z "$NOCONFIGURE" ] && install_osmclient
835
836 #Install vim-emu (optional)
837 if [ -n "$INSTALL_VIMEMU" ]; then
838 echo -e "\nInstalling vim-emu ..."
839 install_vimemu
840 fi
841
842 wget -q -O- https://osm-download.etsi.org/ftp/osm-4.0-four/README2.txt &> /dev/null
843 track end
844 echo -e "\nDONE"
845