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