Capture succesful installation
[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 " -b master"
23 echo -e " -b v1.0"
24 echo -e " ..."
25 echo -e " --develop: (deprecated, use '-b master') install OSM from source code using the master branch"
26 echo -e " --nat: install only NAT rules"
27 # echo -e " --update: update to the latest stable release or to the latest commit if using a specific branch"
28 echo -e " --showopts: print chosen options and exit (only for debugging)"
29 echo -e " -y: do not prompt for confirmation, assumes yes"
30 echo -e " -h / --help: print this help"
31 }
32
33 #Uninstall OSM: remove containers
34 function uninstall(){
35 if [ $RC_CLONE ] || [ -n "$TEST_INSTALLER" ]; then
36 $OSM_DEVOPS/jenkins/host/clean_container RO
37 $OSM_DEVOPS/jenkins/host/clean_container VCA
38 $OSM_DEVOPS/jenkins/host/clean_container SO
39 #$OSM_DEVOPS/jenkins/host/clean_container UI
40 else
41 lxc stop RO && lxc delete RO
42 lxc stop VCA && lxc delete VCA
43 lxc stop SO-ub && lxc delete SO-ub
44 fi
45 }
46
47 #Configure NAT rules, based on the current IP addresses of containers
48 function nat(){
49 echo -e "\nChecking required packages: iptables-persistent"
50 dpkg -l iptables-persistent &>/dev/null || ! echo -e " Not installed.\nInstalling iptables-persistent requires root privileges" || \
51 sudo DEBIAN_FRONTEND=noninteractive apt-get -yq install iptables-persistent
52 echo -e "\nConfiguring NAT rules"
53 echo -e " Required root privileges"
54 sudo $OSM_DEVOPS/installers/nat_osm
55 }
56
57 #Update RO, SO and UI:
58 function update(){
59 echo -e "\nUpdating components"
60
61 echo -e " Updating RO"
62 CONTAINER="RO"
63 MDG="RO"
64 INSTALL_FOLDER="/opt/openmano"
65 echo -e " Fetching the repo"
66 lxc exec $CONTAINER -- git -C $INSTALL_FOLDER fetch --all
67 BRANCH=""
68 BRANCH=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER status -sb | head -n1 | sed -n 's/^## \(.*\).*/\1/p'|awk '{print $1}' |sed 's/\(.*\)\.\.\..*/\1/'`
69 [ -z "$BRANCH" ] && echo " Could not find the current branch in use in the $MDG" && exit 1
70 CURRENT=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER status |head -n1`
71 CURRENT_COMMIT_ID=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER rev-parse HEAD`
72 echo " FROM: $CURRENT ($CURRENT_COMMIT_ID)"
73 # COMMIT_ID either was previously set with -b option, or is an empty string
74 CHECKOUT_ID=$COMMIT_ID
75 [ -z "$CHECKOUT_ID" ] && [ "$BRANCH" == "HEAD" ] && CHECKOUT_ID="tags/$LATEST_STABLE_DEVOPS"
76 [ -z "$CHECKOUT_ID" ] && [ "$BRANCH" != "HEAD" ] && CHECKOUT_ID="$BRANCH"
77 if [[ $CHECKOUT_ID == "tags/"* ]]; then
78 REMOTE_COMMIT_ID=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER rev-list -n 1 $CHECKOUT_ID`
79 else
80 REMOTE_COMMIT_ID=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER rev-parse origin/$CHECKOUT_ID`
81 fi
82 echo " TO: $CHECKOUT_ID ($REMOTE_COMMIT_ID)"
83 if [ "$CURRENT_COMMIT_ID" == "$REMOTE_COMMIT_ID" ]; then
84 echo " Nothing to be done."
85 else
86 echo " Update required."
87 lxc exec $CONTAINER -- service openmano stop
88 lxc exec $CONTAINER -- git -C /opt/openmano stash
89 lxc exec $CONTAINER -- git -C /opt/openmano pull --rebase
90 lxc exec $CONTAINER -- git -C /opt/openmano checkout $CHECKOUT_ID
91 lxc exec $CONTAINER -- git -C /opt/openmano stash pop
92 lxc exec $CONTAINER -- /opt/openmano/database_utils/migrate_mano_db.sh
93 lxc exec $CONTAINER -- service openmano start
94 fi
95 echo
96
97 echo -e " Updating SO and UI"
98 CONTAINER="SO-ub"
99 MDG="SO"
100 INSTALL_FOLDER="" # To be filled in
101 echo -e " Fetching the repo"
102 lxc exec $CONTAINER -- git -C $INSTALL_FOLDER fetch --all
103 BRANCH=""
104 BRANCH=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER status -sb | head -n1 | sed -n 's/^## \(.*\).*/\1/p'|awk '{print $1}' |sed 's/\(.*\)\.\.\..*/\1/'`
105 [ -z "$BRANCH" ] && echo " Could not find the current branch in use in the $MDG" && exit 1
106 CURRENT=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER status |head -n1`
107 CURRENT_COMMIT_ID=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER rev-parse HEAD`
108 echo " FROM: $CURRENT ($CURRENT_COMMIT_ID)"
109 # COMMIT_ID either was previously set with -b option, or is an empty string
110 CHECKOUT_ID=$COMMIT_ID
111 [ -z "$CHECKOUT_ID" ] && [ "$BRANCH" == "HEAD" ] && CHECKOUT_ID="tags/$LATEST_STABLE_DEVOPS"
112 [ -z "$CHECKOUT_ID" ] && [ "$BRANCH" != "HEAD" ] && CHECKOUT_ID="$BRANCH"
113 if [[ $CHECKOUT_ID == "tags/"* ]]; then
114 REMOTE_COMMIT_ID=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER rev-list -n 1 $CHECKOUT_ID`
115 else
116 REMOTE_COMMIT_ID=`lxc exec $CONTAINER -- git -C $INSTALL_FOLDER rev-parse origin/$CHECKOUT_ID`
117 fi
118 echo " TO: $CHECKOUT_ID ($REMOTE_COMMIT_ID)"
119 if [ "$CURRENT_COMMIT_ID" == "$REMOTE_COMMIT_ID" ]; then
120 echo " Nothing to be done."
121 else
122 echo " Update required."
123 # Instructions to be added
124 # lxc exec SO-ub -- ...
125 fi
126 echo
127 }
128
129 #Configure VCA, SO and RO with the initial configuration:
130 # RO -> tenant:osm, logs to be sent to SO
131 # VCA -> juju-password
132 # SO -> route to Juju Controller, add RO account, add VCA account
133 function configure(){
134 #Configure components
135 echo -e "\nConfiguring components"
136 . $OSM_DEVOPS/installers/export_ips
137
138 echo -e " Configuring RO"
139 lxc exec RO -- sed -i -e "s/^\#\?log_socket_host:.*/log_socket_host: $SO_CONTAINER_IP/g" /opt/openmano/openmanod.cfg
140 lxc exec RO -- service openmano restart
141 time=0; step=2; timelength=20; while [ $time -le $timelength ]; do sleep $step; echo -n "."; time=$((time+step)); done; echo
142 lxc exec RO -- openmano tenant-delete -f osm >/dev/null
143 RO_TENANT_ID=`lxc exec RO -- openmano tenant-create osm |awk '{print $1}'`
144
145 echo -e " Configuring VCA"
146 JUJU_PASSWD=`date +%s | sha256sum | base64 | head -c 32`
147 echo -e "$JUJU_PASSWD\n$JUJU_PASSWD" | lxc exec VCA -- juju change-user-password
148 JUJU_CONTROLLER_IP=`lxc exec VCA -- lxc list -c 4 |grep eth0 |awk '{print $2}'`
149
150 echo -e " Configuring SO"
151 sudo route add -host $JUJU_CONTROLLER_IP gw $VCA_CONTAINER_IP
152 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 &
153 time=0; step=30; timelength=300; while [ $time -le $timelength ]; do sleep $step; echo -n "."; time=$((time+step)); done; echo
154
155 curl -k --request POST \
156 --url https://$SO_CONTAINER_IP:8008/api/config/config-agent \
157 --header 'accept: application/vnd.yang.data+json' \
158 --header 'authorization: Basic YWRtaW46YWRtaW4=' \
159 --header 'cache-control: no-cache' \
160 --header 'content-type: application/vnd.yang.data+json' \
161 --data '{"account": [ { "name": "osmjuju", "account-type": "juju", "juju": { "ip-address": "'$JUJU_CONTROLLER_IP'", "port": "17070", "user": "admin", "secret": "'$JUJU_PASSWD'" } } ]}'
162
163 curl -k --request PUT \
164 --url https://$SO_CONTAINER_IP:8008/api/config/resource-orchestrator \
165 --header 'accept: application/vnd.yang.data+json' \
166 --header 'authorization: Basic YWRtaW46YWRtaW4=' \
167 --header 'cache-control: no-cache' \
168 --header 'content-type: application/vnd.yang.data+json' \
169 --data '{ "openmano": { "host": "'$RO_CONTAINER_IP'", "port": "9090", "tenant-id": "'$RO_TENANT_ID'" }, "name": "osmopenmano", "account-type": "openmano" }'
170
171 }
172
173 function install_lxd() {
174 lxd init --auto
175 lxd waitready
176 systemctl stop lxd-bridge
177 systemctl --system daemon-reload
178 systemctl enable lxd-bridge
179 systemctl start lxd-bridge
180 }
181
182
183 UNINSTALL=""
184 DEVELOP=""
185 NAT=""
186 UPDATE=""
187 RECONFIGURE=""
188 TEST_INSTALLER=""
189 LXD=""
190 SHOWOPTS=""
191 COMMIT_ID=""
192 ASSUME_YES=""
193
194 while getopts ":hy-:b:" o; do
195 case "${o}" in
196 h)
197 usage && exit 0
198 ;;
199 b)
200 COMMIT_ID=${OPTARG}
201 ;;
202 -)
203 [ "${OPTARG}" == "help" ] && usage && exit 0
204 [ "${OPTARG}" == "develop" ] && DEVELOP="y" && continue
205 [ "${OPTARG}" == "uninstall" ] && UNINSTALL="y" && continue
206 [ "${OPTARG}" == "nat" ] && NAT="y" && continue
207 [ "${OPTARG}" == "update" ] && UPDATE="y" && continue
208 [ "${OPTARG}" == "reconfigure" ] && RECONFIGURE="y" && continue
209 [ "${OPTARG}" == "test" ] && TEST_INSTALLER="y" && continue
210 [ "${OPTARG}" == "lxd" ] && LXD="y" && continue
211 [ "${OPTARG}" == "showopts" ] && SHOWOPTS="y" && continue
212 echo -e "Invalid option: '--$OPTARG'\n" >&2
213 usage && exit 1
214 ;;
215 \?)
216 echo -e "Invalid option: '-$OPTARG'\n" >&2
217 usage && exit 1
218 ;;
219 y)
220 ASSUME_YES="y"
221 ;;
222 *)
223 usage && exit 1
224 ;;
225 esac
226 done
227
228 [ -z "$COMMIT_ID" ] && [ -n "$DEVELOP" ] && COMMIT_ID="master"
229 [ -n "$TEST_INSTALLER" ] && [ -z "$COMMIT_ID" ] && echo "Use -b option to specify the branch to use for the test (e.g.: v1.0)" && exit 0
230
231 if [ -n "$TEST_INSTALLER" ]; then
232 echo -e "\nUsing local devops repo for OSM installation"
233 TEMPDIR="$(dirname $(realpath $(dirname $0)))"
234 else
235 echo -e "\nCreating temporary dir for OSM installation"
236 TEMPDIR="$(mktemp -d -q --tmpdir "installosm.XXXXXX")"
237 trap 'rm -rf "$TEMPDIR"' EXIT
238 fi
239
240 echo -e "Checking required packages: git"
241 dpkg -l git &>/dev/null || ! echo -e " git not installed.\nInstalling git requires root privileges" || sudo apt install -y git
242 if [ -z "$TEST_INSTALLER" ]; then
243 echo -e "\nCloning devops repo temporarily"
244 git clone https://osm.etsi.org/gerrit/osm/devops.git $TEMPDIR
245 RC_CLONE=$?
246 fi
247
248 echo -e "\nGuessing the current stable release"
249 LATEST_STABLE_DEVOPS=`git -C $TEMPDIR tag -l v[0-9].* | tail -n1`
250 [ -z "$COMMIT_ID" ] && [ -z "$LATEST_STABLE_DEVOPS" ] && echo "Could not find the current latest stable release" && exit 0
251
252 if [ -n "$SHOWOPTS" ]; then
253 echo "DEVELOP=$DEVELOP"
254 echo "UNINSTALL=$UNINSTALL"
255 echo "NAT=$NAT"
256 echo "UPDATE=$UPDATE"
257 echo "RECONFIGURE=$RECONFIGURE"
258 echo "TEST_INSTALLER=$TEST_INSTALLER"
259 echo "LXD=$LXD"
260 echo "SHOWOPTS=$SHOWOPTS"
261 echo "Latest tag in devops repo: $LATEST_STABLE_DEVOPS"
262 echo "Commit to be installed (-b): $COMMIT_ID"
263 exit 0
264 fi
265
266 OSM_DEVOPS=$TEMPDIR
267 OSM_JENKINS="$TEMPDIR/jenkins"
268 . $OSM_JENKINS/common/all_funcs
269
270 [ -n "$UNINSTALL" ] && uninstall && echo -e "\nDONE" && exit 0
271 [ -n "$NAT" ] && nat && echo -e "\nDONE" && exit 0
272 [ -n "$UPDATE" ] && update && echo -e "\nDONE" && exit 0
273 [ -n "$RECONFIGURE" ] && configure && echo -e "\nDONE" && exit 0
274
275 #Installation starts here
276 if [ -z "$ASSUME_YES" ]; then
277 read -e -p "The installation will take about 75-90 minutes. Continue (Y/n)?" USER_CONFIRMATION
278 [ -n "$USER_CONFIRMATION" ] && [ "$USER_CONFIRMATION" != "yes" ] && \
279 [ "$USER_CONFIRMATION" != "y" ] && echo "Cancelled!" && exit 0
280 fi
281
282 [ -z "$COMMIT_ID" ] && [ -n "$LATEST_STABLE_DEVOPS" ] && COMMIT_ID="tags/$LATEST_STABLE_DEVOPS"
283 echo -e "\n Installing OSM from refspec: $COMMIT_ID"
284
285 wget -q -O- https://osm-download.etsi.org/ftp/osm-1.0-one/README.txt &> /dev/null
286
287 [ -n "$LXD" ] && echo -e "\nConfiguring lxd" && install_lxd
288
289 echo -e "\nChecking required packages: wget, curl, tar"
290 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
291
292 echo -e "\nCreating the containers and building ..."
293 $OSM_DEVOPS/jenkins/host/start_build RO --notest checkout $COMMIT_ID
294 $OSM_DEVOPS/jenkins/host/start_build VCA
295 $OSM_DEVOPS/jenkins/host/start_build SO checkout $COMMIT_ID
296 $OSM_DEVOPS/jenkins/host/start_build UI checkout $COMMIT_ID
297
298 #Install iptables-persistent and configure NAT rules
299 nat
300
301 #Configure components
302 configure
303
304 echo -e "\nDONE"
305 wget -q -O- https://osm-download.etsi.org/ftp/osm-1.0-one/README2.txt &> /dev/null