blob: 0899b93777cdba8581041458a7b40916d6b5f1ef [file] [log] [blame]
tiernof7aa8c42016-09-06 16:43:04 +02001#!/bin/bash
2
3##
4# Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
tierno9a61c6b2016-09-08 10:57:02 +02005# This file is part of openvim
tiernof7aa8c42016-09-06 16:43:04 +02006# All Rights Reserved.
7#
8# Licensed under the Apache License, Version 2.0 (the "License"); you may
9# not use this file except in compliance with the License. You may obtain
10# a copy of the License at
11#
12# http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing, software
15# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17# License for the specific language governing permissions and limitations
18# under the License.
19#
20# For those usages not covered by the Apache License, Version 2.0 please
21# contact with: nfvlabs@tid.es
22##
23
tierno9a61c6b2016-09-08 10:57:02 +020024#ONLY TESTED for Ubuntu 14.10 14.04 16.04, CentOS7 and RHEL7
tiernof7aa8c42016-09-06 16:43:04 +020025#Get needed packages, source code and configure to run openvim
26#Ask for database user and password if not provided
tiernof7aa8c42016-09-06 16:43:04 +020027
28function usage(){
tierno9a61c6b2016-09-08 10:57:02 +020029 echo -e "usage: sudo $0 [OPTIONS]"
30 echo -e "Install last stable source code in ./openvim and the needed packages"
31 echo -e "On a Ubuntu 16.04 it configures openvim as a service"
32 echo -e " OPTIONS"
33 echo -e " -u USER: database admin user. 'root' by default. Prompts if needed"
34 echo -e " -p PASS: database admin password to be used or installed. Prompts if needed"
35 echo -e " -q --quiet: install in an unattended mode"
36 echo -e " -h --help: show this help"
37 echo -e " --develop: install last version for developers, and do not configure as a service"
38 echo -e " --skip-install-packages: use this option to skip and update and install the requires packages. This avoid wasting time if you are sure requires packages are present for e.g. a previous installation"
tiernof7aa8c42016-09-06 16:43:04 +020039}
40
41function install_packages(){
42 [ -x /usr/bin/apt-get ] && apt-get install -y $*
43 [ -x /usr/bin/yum ] && yum install -y $*
44
45 #check properly installed
46 for PACKAGE in $*
47 do
48 PACKAGE_INSTALLED="no"
49 [ -x /usr/bin/apt-get ] && dpkg -l $PACKAGE &>> /dev/null && PACKAGE_INSTALLED="yes"
50 [ -x /usr/bin/yum ] && yum list installed $PACKAGE &>> /dev/null && PACKAGE_INSTALLED="yes"
51 if [ "$PACKAGE_INSTALLED" = "no" ]
52 then
53 echo "failed to install package '$PACKAGE'. Revise network connectivity and try again"
54 exit -1
55 fi
56 done
57}
58
tiernof7aa8c42016-09-06 16:43:04 +020059
tierno9a61c6b2016-09-08 10:57:02 +020060GIT_URL=https://osm.etsi.org/gerrit/osm/openvim.git
61DBUSER="root"
62DBPASSWD=""
63DBPASSWD_PARAM=""
64QUIET_MODE=""
65DEVELOP=""
66NO_PACKAGES=""
67while getopts ":u:p:hiq-:" o; do
68 case "${o}" in
69 u)
70 export DBUSER="$OPTARG"
71 ;;
72 p)
73 export DBPASSWD="$OPTARG"
74 export DBPASSWD_PARAM="-p$OPTARG"
75 ;;
76 q)
77 export QUIET_MODE=yes
78 export DEBIAN_FRONTEND=noninteractive
79 ;;
80 h)
81 usage && exit 0
82 ;;
83 -)
84 [ "${OPTARG}" == "help" ] && usage && exit 0
85 [ "${OPTARG}" == "develop" ] && DEVELOP="y" && continue
86 [ "${OPTARG}" == "quiet" ] && export QUIET_MODE=yes && export DEBIAN_FRONTEND=noninteractive && continue
87 [ "${OPTARG}" == "skip-install-packages" ] && export NO_PACKAGES=yes && continue
88 echo -e "Invalid option: '--$OPTARG'\nTry $0 --help for more information" >&2
89 exit 1
90 ;;
91 \?)
92 echo -e "Invalid option: '-$OPTARG'\nTry $0 --help for more information" >&2
93 exit 1
94 ;;
95 :)
96 echo -e "Option '-$OPTARG' requires an argument\nTry $0 --help for more information" >&2
97 exit 1
98 ;;
99 *)
100 usage >&2
101 exit -1
102 ;;
103 esac
104done
105
106#check root privileges and non a root user behind
107[ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit -1
108if [[ -z "$SUDO_USER" ]] || [[ "$SUDO_USER" = "root" ]]
109then
110 [[ -z $QUIET_MODE ]] && read -e -p "Install in the root user (y/N)?" KK
111 [[ -z $QUIET_MODE ]] && [[ "$KK" != "y" ]] && [[ "$KK" != "yes" ]] && echo "Cancelled" && exit 1
112 export SUDO_USER=root
113fi
tiernof7aa8c42016-09-06 16:43:04 +0200114
115#Discover Linux distribution
116#try redhat type
117[ -f /etc/redhat-release ] && _DISTRO=$(cat /etc/redhat-release 2>/dev/null | cut -d" " -f1)
118#if not assuming ubuntu type
119[ -f /etc/redhat-release ] || _DISTRO=$(lsb_release -is 2>/dev/null)
120if [ "$_DISTRO" == "Ubuntu" ]
121then
tierno9a61c6b2016-09-08 10:57:02 +0200122 _RELEASE=$(lsb_release -rs)
123 if [[ ${_RELEASE%%.*} != 14 ]] && [[ ${_RELEASE%%.*} != 16 ]]
124 then
125 [[ -z $QUIET_MODE ]] && read -e -p "WARNING! Not tested Ubuntu version. Continue assuming a trusty (14.XX)'? (y/N)" KK
126 [[ -z $QUIET_MODE ]] && [[ "$KK" != "y" ]] && [[ "$KK" != "yes" ]] && echo "Cancelled" && exit 1
127 _RELEASE = 14
tiernof7aa8c42016-09-06 16:43:04 +0200128 fi
129elif [ "$_DISTRO" == "CentOS" ]
130then
131 _RELEASE="7"
132 if ! cat /etc/redhat-release | grep -q "7."
133 then
134 read -e -p "WARNING! Not tested CentOS version. Continue assuming a '_RELEASE' type? (y/N)" KK
135 [ "$KK" != "y" -a "$KK" != "yes" ] && echo "Cancelled" && exit 0
136 fi
137elif [ "$_DISTRO" == "Red" ]
138then
139 _RELEASE="7"
140 if ! cat /etc/redhat-release | grep -q "7."
141 then
142 read -e -p "WARNING! Not tested Red Hat OS version. Continue assuming a '_RELEASE' type? (y/N)" KK
143 [ "$KK" != "y" -a "$KK" != "yes" ] && echo "Cancelled" && exit 0
144 fi
145else #[ "$_DISTRO" != "Ubuntu" -a "$_DISTRO" != "CentOS" -a "$_DISTRO" != "Red" ]
146 _DISTRO_DISCOVER=$_DISTRO
147 [ -x /usr/bin/apt-get ] && _DISTRO="Ubuntu" && _RELEASE="14"
148 [ -x /usr/bin/yum ] && _DISTRO="CentOS" && _RELEASE="7"
149 read -e -p "WARNING! Not tested Linux distribution '$_DISTRO_DISCOVER '. Continue assuming a '$_DISTRO $_RELEASE' type? (y/N)" KK
150 [ "$KK" != "y" -a "$KK" != "yes" ] && echo "Cancelled" && exit 0
151fi
152
153
tierno9a61c6b2016-09-08 10:57:02 +0200154if [[ -z "$NO_PACKAGES" ]]
155then
tiernof7aa8c42016-09-06 16:43:04 +0200156echo '
157#################################################################
158##### UPDATE REPOSITORIES #####
159#################################################################'
160[ "$_DISTRO" == "Ubuntu" ] && apt-get update -y
161
162[ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && yum check-update -y
163[ "$_DISTRO" == "CentOS" ] && sudo yum install -y epel-release
164[ "$_DISTRO" == "Red" ] && wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm \
165 && sudo rpm -ivh epel-release-7-5.noarch.rpm && sudo yum install -y epel-release && rm -f epel-release-7-5.noarch.rpm
166[ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && sudo yum repolist
tierno9a61c6b2016-09-08 10:57:02 +0200167fi
tiernof7aa8c42016-09-06 16:43:04 +0200168
tierno9a61c6b2016-09-08 10:57:02 +0200169if [[ -z "$NO_PACKAGES" ]]
170then
tiernof7aa8c42016-09-06 16:43:04 +0200171echo '
172#################################################################
173##### INSTALL REQUIRED PACKAGES #####
174#################################################################'
tierno9a61c6b2016-09-08 10:57:02 +0200175[ "$_DISTRO" == "Ubuntu" ] && install_packages "git screen wget mysql-server"
176[ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && install_packages "git screen wget mariadb mariadb-server"
177
178if [[ "$_DISTRO" == "Ubuntu" ]]
179then
180 #start services. By default CentOS does not start services
181 service mysql start >> /dev/null
182 # try to set admin password, ignore if fails
183 [[ -n $DBPASSWD ]] && mysqladmin -u $DBUSER -s password $DBPASSWD
184fi
tiernof7aa8c42016-09-06 16:43:04 +0200185
186if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ]
187then
188 #start services. By default CentOS does not start services
189 service mariadb start
190 service httpd start
191 systemctl enable mariadb
192 systemctl enable httpd
193 read -e -p "Do you want to configure mariadb (recomended if not done before) (Y/n)" KK
194 [ "$KK" != "n" -a "$KK" != "no" ] && mysql_secure_installation
195
196 read -e -p "Do you want to set firewall to grant web access port 80,443 (Y/n)" KK
197 [ "$KK" != "n" -a "$KK" != "no" ] &&
198 firewall-cmd --permanent --zone=public --add-service=http &&
199 firewall-cmd --permanent --zone=public --add-service=https &&
200 firewall-cmd --reload
201fi
tierno9a61c6b2016-09-08 10:57:02 +0200202fi #[[ -z "$NO_PACKAGES" ]]
tiernof7aa8c42016-09-06 16:43:04 +0200203
tierno9a61c6b2016-09-08 10:57:02 +0200204#check and ask for database user password. Must be done after database installation
205if [[ -n $QUIET_MODE ]]
206then
207 echo -e "\nCheking database connection and ask for credentials"
208 while ! mysqladmin -s -u$DBUSER $DBPASSWD_PARAM ping
209 do
tiernof7aa8c42016-09-06 16:43:04 +0200210 [ -n "$logintry" ] && echo -e "\nInvalid database credentials!!!. Try again (Ctrl+c to abort)"
211 [ -z "$logintry" ] && echo -e "\nProvide database credentials"
212 read -e -p "database user? ($DBUSER) " DBUSER_
213 [ -n "$DBUSER_" ] && DBUSER=$DBUSER_
214 read -e -s -p "database password? (Enter for not using password) " DBPASSWD_
tierno9a61c6b2016-09-08 10:57:02 +0200215 [ -n "$DBPASSWD_" ] && DBPASSWD="$DBPASSWD_" && DBPASSWD_PARAM="-p$DBPASSWD_"
216 [ -z "$DBPASSWD_" ] && DBPASSWD="" && DBPASSWD_PARAM=""
tiernof7aa8c42016-09-06 16:43:04 +0200217 logintry="yes"
tierno9a61c6b2016-09-08 10:57:02 +0200218 done
219fi
tiernof7aa8c42016-09-06 16:43:04 +0200220
tierno9a61c6b2016-09-08 10:57:02 +0200221if [[ -z "$NO_PACKAGES" ]]
222then
tiernof7aa8c42016-09-06 16:43:04 +0200223echo '
224#################################################################
225##### INSTALL PYTHON PACKAGES #####
226#################################################################'
tierno9a61c6b2016-09-08 10:57:02 +0200227[ "$_DISTRO" == "Ubuntu" ] && install_packages "python-yaml python-libvirt python-bottle python-mysqldb python-jsonschema python-paramiko python-argcomplete python-requests"
228[ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && install_packages "PyYAML libvirt-python MySQL-python python-jsonschema python-paramiko python-argcomplete python-requests"
tiernof7aa8c42016-09-06 16:43:04 +0200229
230#The only way to install python-bottle on Centos7 is with easy_install or pip
231[ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && easy_install -U bottle
232
tierno9a61c6b2016-09-08 10:57:02 +0200233fi #[[ -z "$NO_PACKAGES" ]]
234
tiernof7aa8c42016-09-06 16:43:04 +0200235echo '
236#################################################################
237##### DOWNLOAD SOURCE #####
238#################################################################'
tierno9a61c6b2016-09-08 10:57:02 +0200239su $SUDO_USER -c 'git clone '"${GIT_URL}"' openvim'
240#[[ -z $DEVELOP ]] && su $SUDO_USER -c 'git checkout <tag version>'
241
tiernof7aa8c42016-09-06 16:43:04 +0200242#Unncoment to use a concrete branch, if not main branch
243#pushd openvim
244#su $SUDO_USER -c 'git checkout v0.4'
245#popd
246
247echo '
248#################################################################
249##### CREATE DATABASE #####
250#################################################################'
tierno9a61c6b2016-09-08 10:57:02 +0200251mysqladmin -u$DBUSER $DBPASSWD_PARAM -s create vim_db || ! echo "Cannot create database vim_db" || exit 1
tiernof7aa8c42016-09-06 16:43:04 +0200252
tierno9a61c6b2016-09-08 10:57:02 +0200253echo "CREATE USER 'vim'@'localhost' identified by 'vimpw';" | mysql -u$DBUSER $DBPASSWD_PARAM || ! echo "Failed while creating user vim at dabase" || exit 1
254echo "GRANT ALL PRIVILEGES ON vim_db.* TO 'vim'@'localhost';" | mysql -u$DBUSER $DBPASSWD_PARAM || ! echo "Failed while creating user vim at dabase" || exit 1
255echo " Database 'vim_db' created, user 'vim' password 'vimpw'"
tiernof7aa8c42016-09-06 16:43:04 +0200256
tierno9a61c6b2016-09-08 10:57:02 +0200257
258su $SUDO_USER -c './openvim/database_utils/init_vim_db.sh -u vim -p vimpw' || ! echo "Failed while creating user vim at dabase" || exit 1
tiernof7aa8c42016-09-06 16:43:04 +0200259
260
261if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ]
262then
263 echo '
264#################################################################
265##### CONFIGURE firewalld #####
266#################################################################'
267 read -e -p "Configure firewalld for openvimd port 9080? (Y/n)" KK
268 if [ "$KK" != "n" -a "$KK" != "no" ]
269 then
270 #Creates a service file for openvim
271 echo '<?xml version="1.0" encoding="utf-8"?>
272<service>
273 <short>openvimd</short>
274 <description>openvimd service</description>
275 <port protocol="tcp" port="9080"/>
276</service>' > /etc/firewalld/services/openvimd.xml
277 #put proper permissions
278 pushd /etc/firewalld/services > /dev/null
279 restorecon openvim
280 chmod 640 openvim
281 popd > /dev/null
282 #Add the openvim service to the default zone permanently and reload the firewall configuration
283 firewall-cmd --permanent --add-service=openvim > /dev/null
284 firewall-cmd --reload > /dev/null
285 echo "done."
286 else
287 echo "skipping."
288 fi
289fi
290
291echo '
292#################################################################
tierno9a61c6b2016-09-08 10:57:02 +0200293##### CONFIGURE openvim CLIENT #####
tiernof7aa8c42016-09-06 16:43:04 +0200294#################################################################'
295#creates a link at ~/bin
296su $SUDO_USER -c 'mkdir -p ~/bin'
tierno9a61c6b2016-09-08 10:57:02 +0200297su $SUDO_USER -c 'rm -f ${HOME}/bin/openvim'
298su $SUDO_USER -c 'rm -f ${HOME}/bin/openflow'
299su $SUDO_USER -c 'rm -f ${HOME}/bin/service-openvim'
300su $SUDO_USER -c 'rm -f ${HOME}/bin/initopenvim'
301su $SUDO_USER -c 'rm -f ${HOME}/bin/service-floodlight'
302su $SUDO_USER -c 'rm -f ${HOME}/bin/service-opendaylight'
303su $SUDO_USER -c 'rm -f ${HOME}/bin/get_dhcp_lease.sh'
304su $SUDO_USER -c 'ln -s ${PWD}/openvim/openvim ${HOME}/bin/openvim'
305su $SUDO_USER -c 'ln -s ${PWD}/openvim/openflow ${HOME}/bin/openflow'
306su $SUDO_USER -c 'ln -s ${PWD}/openvim/scripts/service-openvim.sh ${HOME}/bin/service-openvim'
307su $SUDO_USER -c 'ln -s ${PWD}/openvim/scripts/initopenvim.sh ${HOME}/bin/initopenvim'
308su $SUDO_USER -c 'ln -s ${PWD}/openvim/scripts/service-floodlight.sh ${HOME}/bin/service-floodlight'
309su $SUDO_USER -c 'ln -s ${PWD}/openvim/scripts/service-opendaylight.sh ${HOME}/bin/service-opendaylight'
310su $SUDO_USER -c 'ln -s ${PWD}/openvim/scripts/get_dhcp_lease.sh ${HOME}/bin/get_dhcp_lease.sh'
tiernof7aa8c42016-09-06 16:43:04 +0200311
312#insert /home/<user>/bin in the PATH
313#skiped because normally this is done authomatically when ~/bin exist
314#if ! su $SUDO_USER -c 'echo $PATH' | grep -q "/home/${SUDO_USER}/bin"
315#then
316# echo " inserting /home/$SUDO_USER/bin in the PATH at .bashrc"
317# su $SUDO_USER -c 'echo "PATH=\$PATH:/home/\${USER}/bin" >> ~/.bashrc'
318#fi
319
tierno9a61c6b2016-09-08 10:57:02 +0200320if [[ $SUDO_USER == root ]]
321then
322 if ! echo $PATH | grep -q "${HOME}/bin"
323 then
324 echo "PATH=\$PATH:\${HOME}/bin" >> ${HOME}/.bashrc
325 fi
326fi
327
328
tiernof7aa8c42016-09-06 16:43:04 +0200329#configure arg-autocomplete for this user
330#in case of minmal instalation this package is not installed by default
331[[ "$_DISTRO" == "CentOS" || "$_DISTRO" == "Red" ]] && yum install -y bash-completion
332#su $SUDO_USER -c 'mkdir -p ~/.bash_completion.d'
333su $SUDO_USER -c 'activate-global-python-argcomplete --user'
334if ! grep -q bash_completion.d/python-argcomplete.sh /home/${SUDO_USER}/.bashrc
335then
336 echo " inserting .bash_completion.d/python-argcomplete.sh execution at .bashrc"
337 su $SUDO_USER -c 'echo ". /home/${USER}/.bash_completion.d/python-argcomplete.sh" >> ~/.bashrc'
338fi
339
tiernof7aa8c42016-09-06 16:43:04 +0200340
341
tierno9a61c6b2016-09-08 10:57:02 +0200342if [[ "$_DISTRO" == "Ubuntu" ]] && [[ ${_RELEASE%%.*} == 16 ]] && [[ -z $DEVELOP ]]
343then
344echo '
345#################################################################
346##### CONFIGURE OPENMANO SERVICE #####
347#################################################################'
tiernof7aa8c42016-09-06 16:43:04 +0200348
tierno9a61c6b2016-09-08 10:57:02 +0200349 ./openvim/scripts/install-service-openvim.sh -f openvim #-u $SUDO_USER
350# alias service-openvim="service openvim"
351# echo 'alias service-openvim="service openvim"' >> ${HOME}/.bashrc
352
353 echo
354 echo "Done! you may need to logout and login again for loading client configuration"
355 echo " Manage server with 'service openvim start|stop|status|...' "
356
357
358else
359
360 echo
361 echo "Done! you may need to logout and login again for loading client configuration"
362 echo " Run './openvim/scripts/service-openvim.sh start' for starting openvim in a screen"
363
364fi