installer_from_source: refactored uninstall, nat and configure code; new tag v1.0.1
[osm/devops.git] / installers / install_from_source.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 source code"
19 echo -e " OPTIONS"
20 echo -e " --uninstall: uninstall OSM: remove the containers and delete NAT rules"
21 echo -e " --develop: install OSM from source code using the master branch"
22 echo -e " --nat: install only NAT rules"
23 echo -e " -h / --help: print this help"
24 }
25
26 #Uninstall OSM: remove containers
27 function uninstall(){
28 if [ $RC_CLONE ] || [ -n "$TEST_INSTALLER" ]; then
29 $OSM_DEVOPS/jenkins/host/clean_container RO
30 $OSM_DEVOPS/jenkins/host/clean_container VCA
31 $OSM_DEVOPS/jenkins/host/clean_container SO
32 #$OSM_DEVOPS/jenkins/host/clean_container UI
33 else
34 lxc stop RO && lxc delete RO
35 lxc stop VCA && lxc delete VCA
36 lxc stop SO-ub && lxc delete SO-ub
37 fi
38 }
39
40 #Configure NAT rules, based on the current IP addresses of containers
41 function nat(){
42 echo -e "\nChecking required packages: iptables-persistent"
43 dpkg -l iptables-persistent &>/dev/null || ! echo -e " Not installed.\nInstalling iptables-persistent requires root privileges" || \
44 sudo DEBIAN_FRONTEND=noninteractive apt-get -yq install iptables-persistent
45 echo -e "\nConfiguring NAT rules"
46 echo -e " Required root privileges"
47 sudo $OSM_DEVOPS/installers/nat_osm
48 }
49
50 #Configure VCA, SO and RO with the initial configuration:
51 # RO -> tenant:osm, logs to be sent to SO
52 # VCA -> juju-password
53 # SO -> route to Juju Controller, add RO account, add VCA account
54 function configure(){
55 #Configure components
56 echo -e "\nConfiguring components"
57 . $OSM_DEVOPS/installers/export_ips
58
59 echo -e " Configuring RO"
60 lxc exec RO -- sed -i -e "s/^\#\?log_socket_host:.*/log_socket_host: $SO_CONTAINER_IP/g" /opt/openmano/openmanod.cfg
61 lxc exec RO -- service openmano restart
62 time=0; step=1; timelength=10; while [ $time -le $timelength ]; do sleep $step; echo -n "."; time=$((time+step)); done; echo
63 RO_TENANT_ID=`lxc exec RO -- openmano tenant-create osm |awk '{print $1}'`
64
65 echo -e " Configuring VCA"
66 JUJU_PASSWD=`date +%s | sha256sum | base64 | head -c 32`
67 echo -e "$JUJU_PASSWD\n$JUJU_PASSWD" | lxc exec VCA -- juju change-user-password
68 JUJU_CONTROLLER_IP=`lxc exec VCA -- lxc list -c 4 |grep eth0 |awk '{print $2}'`
69
70 echo -e " Configuring SO"
71 sudo route add -host $JUJU_CONTROLLER_IP gw $VCA_CONTAINER_IP
72 lxc exec SO-ub -- nohup sudo -b -H /usr/rift/rift-shell -r -i /usr/rift -a /usr/rift/.artifacts -- ./demos/launchpad.py --use-xml-mode
73 time=0; step=20; timelength=200; while [ $time -le $timelength ]; do sleep $step; echo -n "."; time=$((time+step)); done; echo
74
75 curl -k --request POST \
76 --url https://$SO_CONTAINER_IP:8008/api/config/config-agent \
77 --header 'accept: application/vnd.yang.data+json' \
78 --header 'authorization: Basic YWRtaW46YWRtaW4=' \
79 --header 'cache-control: no-cache' \
80 --header 'content-type: application/vnd.yang.data+json' \
81 --data '{"account": [ { "name": "osmjuju", "account-type": "juju", "juju": { "ip-address": "'$JUJU_CONTROLLER_IP'", "port": "17070", "user": "admin", "secret": "'$JUJU_PASSWD'" } } ]}'
82
83 curl -k --request PUT \
84 --url https://$SO_CONTAINER_IP:8008/api/config/resource-orchestrator \
85 --header 'accept: application/vnd.yang.data+json' \
86 --header 'authorization: Basic YWRtaW46YWRtaW4=' \
87 --header 'cache-control: no-cache' \
88 --header 'content-type: application/vnd.yang.data+json' \
89 --data '{ "openmano": { "host": "'$RO_CONTAINER_IP'", "port": "9090", "tenant-id": "'$RO_TENANT_ID'" }, "name": "osmopenmano", "account-type": "openmano" }'
90
91 }
92
93 UNINSTALL=""
94 DEVELOP=""
95 NAT=""
96 RECONFIGURE=""
97 TEST_INSTALLER=""
98 while getopts ":h-:" o; do
99 case "${o}" in
100 h)
101 usage && exit 0
102 ;;
103 -)
104 [ "${OPTARG}" == "help" ] && usage && exit 0
105 [ "${OPTARG}" == "develop" ] && DEVELOP="y" && continue
106 [ "${OPTARG}" == "uninstall" ] && UNINSTALL="y" && continue
107 [ "${OPTARG}" == "nat" ] && NAT="y" && continue
108 [ "${OPTARG}" == "reconfigure" ] && RECONFIGURE="y" && continue
109 [ "${OPTARG}" == "test" ] && TEST_INSTALLER="y" && continue
110 echo -e "Invalid option: '--$OPTARG'\n" >&2
111 usage && exit 1
112 ;;
113 \?)
114 echo -e "Invalid option: '-$OPTARG'\n" >&2
115 usage && exit 1
116 ;;
117 *)
118 usage && exit 1
119 ;;
120 esac
121 done
122
123 if [ -n "$TEST_INSTALLER" ]; then
124 echo -e "\nUsing local devops repo for OSM installation"
125 TEMPDIR="$(dirname $(realpath $(dirname $0)))"
126 else
127 echo -e "\nCreating temporary dir for OSM installation"
128 TEMPDIR="$(mktemp -d -q --tmpdir "installosm.XXXXXX")"
129 trap 'rm -rf "$TEMPDIR"' EXIT
130 fi
131
132 echo -e "Checking required packages: git"
133 dpkg -l git &>/dev/null || ! echo -e " git not installed.\nInstalling git requires root privileges" || sudo apt install -y git
134 if [ -z "$TEST_INSTALLER" ]; then
135 echo -e "\nCloning devops repo temporarily"
136 git clone https://osm.etsi.org/gerrit/osm/devops.git $TEMPDIR
137 RC_CLONE=$?
138 DEVOPS_COMMITID="tags/v1.0.1"
139 git -C $TEMPDIR checkout $DEVOPS_COMMITID
140 fi
141 OSM_DEVOPS=$TEMPDIR
142 OSM_JENKINS="$TEMPDIR/jenkins"
143 . $OSM_JENKINS/common/all_funcs
144
145 [ -n "$UNINSTALL" ] && uninstall && exit 0
146 [ -n "$NAT" ] && nat && exit 0
147 [ -n "$RECONFIGURE" ] && configure && exit 0
148
149 #Installation starts here
150 wget -q -O- https://osm-download.etsi.org/ftp/osm-1.0-one/README.txt &> /dev/null
151
152 echo -e "\nChecking required packages: wget, curl, tar"
153 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 install -y wget curl tar
154
155 echo -e "\nCreating the containers and building ..."
156 COMMIT_ID="tags/v1.0.1"
157 [ -n "$DEVELOP" ] && COMMIT_ID="master"
158 $OSM_DEVOPS/jenkins/host/start_build RO checkout $COMMIT_ID
159 $OSM_DEVOPS/jenkins/host/start_build VCA
160 $OSM_DEVOPS/jenkins/host/start_build SO checkout $COMMIT_ID
161 $OSM_DEVOPS/jenkins/host/start_build UI checkout $COMMIT_ID
162
163 #Install iptables-persistent and configure NAT rules
164 nat
165
166 #Configure components
167 configure
168
169 echo -e "\nDONE"
170
171