4 # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5 # This file is part of openvim
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
12 # http://www.apache.org/licenses/LICENSE-2.0
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
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact with: nfvlabs@tid.es
24 #ONLY TESTED for Ubuntu 14.10 14.04 16.04, CentOS7 and RHEL7
25 #Get needed packages, source code and configure to run openvim
26 #Ask for database user and password if not provided
29 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"
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"
41 function install_packages
(){
42 [ -x /usr
/bin
/apt-get
] && apt-get
install -y $
*
43 [ -x /usr
/bin
/yum
] && yum
install -y $
*
45 #check properly installed
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" ]
53 echo "failed to install package '$PACKAGE'. Revise network connectivity and try again"
60 GIT_URL
=https
://osm.etsi.org
/gerrit
/osm
/openvim.git
67 while getopts ":u:p:hiq-:" o
; do
70 export DBUSER
="$OPTARG"
73 export DBPASSWD
="$OPTARG"
74 export DBPASSWD_PARAM
="-p$OPTARG"
78 export DEBIAN_FRONTEND
=noninteractive
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
92 echo -e "Invalid option: '-$OPTARG'\nTry $0 --help for more information" >&2
96 echo -e "Option '-$OPTARG' requires an argument\nTry $0 --help for more information" >&2
106 #check root privileges and non a root user behind
107 [ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit -1
108 if [[ -z "$SUDO_USER" ]] ||
[[ "$SUDO_USER" = "root" ]]
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
115 #Discover Linux distribution
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
)
120 if [ "$_DISTRO" == "Ubuntu" ]
122 _RELEASE
=$
(lsb_release
-rs)
123 if [[ ${_RELEASE%%.*} != 14 ]] && [[ ${_RELEASE%%.*} != 16 ]]
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
129 elif [ "$_DISTRO" == "CentOS" ]
132 if ! cat /etc
/redhat-release |
grep -q "7."
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
137 elif [ "$_DISTRO" == "Red" ]
140 if ! cat /etc
/redhat-release |
grep -q "7."
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
145 else #[ "$_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
154 if [[ -z "$NO_PACKAGES" ]]
157 #################################################################
158 ##### UPDATE REPOSITORIES #####
159 #################################################################'
160 [ "$_DISTRO" == "Ubuntu" ] && apt-get update
-y
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
169 if [[ -z "$NO_PACKAGES" ]]
172 #################################################################
173 ##### INSTALL REQUIRED PACKAGES #####
174 #################################################################'
175 [ "$_DISTRO" == "Ubuntu" ] && install_packages
"git screen wget mysql-server"
176 [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && install_packages
"git screen wget mariadb mariadb-server"
178 if [[ "$_DISTRO" == "Ubuntu" ]]
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
186 if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ]
188 #start services. By default CentOS does not start services
189 service mariadb 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
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
202 fi #[[ -z "$NO_PACKAGES" ]]
204 #check and ask for database user password. Must be done after database installation
205 if [[ -n $QUIET_MODE ]]
207 echo -e "\nCheking database connection and ask for credentials"
208 while ! mysqladmin
-s -u$DBUSER $DBPASSWD_PARAM status
>/dev
/null
210 [ -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_
215 [ -n "$DBPASSWD_" ] && DBPASSWD
="$DBPASSWD_" && DBPASSWD_PARAM
="-p$DBPASSWD_"
216 [ -z "$DBPASSWD_" ] && DBPASSWD
="" && DBPASSWD_PARAM
=""
221 if [[ -z "$NO_PACKAGES" ]]
224 #################################################################
225 ##### INSTALL PYTHON PACKAGES #####
226 #################################################################'
227 [ "$_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"
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
233 fi #[[ -z "$NO_PACKAGES" ]]
236 #################################################################
237 ##### DOWNLOAD SOURCE #####
238 #################################################################'
239 su
$SUDO_USER -c 'git clone '"${GIT_URL}"' openvim'
240 [[ -z $DEVELOP ]] && su
$SUDO_USER -c 'git -C openvim checkout tags/v1.0.1'
242 #Unncoment to use a concrete branch, if not main branch
244 #su $SUDO_USER -c 'git checkout v0.4'
248 #################################################################
249 ##### CREATE DATABASE #####
250 #################################################################'
251 mysqladmin
-u$DBUSER $DBPASSWD_PARAM -s create vim_db ||
! echo "Cannot create database vim_db" ||
exit 1
253 echo "CREATE USER 'vim'@'localhost' identified by 'vimpw';" | mysql
-u$DBUSER $DBPASSWD_PARAM ||
! echo "Failed while creating user vim at dabase" ||
exit 1
254 echo "GRANT ALL PRIVILEGES ON vim_db.* TO 'vim'@'localhost';" | mysql
-u$DBUSER $DBPASSWD_PARAM ||
! echo "Failed while creating user vim at dabase" ||
exit 1
255 echo " Database 'vim_db' created, user 'vim' password 'vimpw'"
258 su
$SUDO_USER -c './openvim/database_utils/init_vim_db.sh -u vim -p vimpw' ||
! echo "Failed while creating user vim at dabase" ||
exit 1
261 if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ]
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" ]
270 #Creates a service file for openvim
271 echo '<?xml version="1.0" encoding="utf-8"?>
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
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
292 #################################################################
293 ##### CONFIGURE openvim CLIENT #####
294 #################################################################'
295 #creates a link at ~/bin
296 su
$SUDO_USER -c 'mkdir -p ~/bin'
297 su
$SUDO_USER -c 'rm -f ${HOME}/bin/openvim'
298 su
$SUDO_USER -c 'rm -f ${HOME}/bin/openflow'
299 su
$SUDO_USER -c 'rm -f ${HOME}/bin/service-openvim'
300 su
$SUDO_USER -c 'rm -f ${HOME}/bin/initopenvim'
301 su
$SUDO_USER -c 'rm -f ${HOME}/bin/service-floodlight'
302 su
$SUDO_USER -c 'rm -f ${HOME}/bin/service-opendaylight'
303 su
$SUDO_USER -c 'rm -f ${HOME}/bin/get_dhcp_lease.sh'
304 su
$SUDO_USER -c 'ln -s ${PWD}/openvim/openvim ${HOME}/bin/openvim'
305 su
$SUDO_USER -c 'ln -s ${PWD}/openvim/openflow ${HOME}/bin/openflow'
306 su
$SUDO_USER -c 'ln -s ${PWD}/openvim/scripts/service-openvim.sh ${HOME}/bin/service-openvim'
307 su
$SUDO_USER -c 'ln -s ${PWD}/openvim/scripts/initopenvim.sh ${HOME}/bin/initopenvim'
308 su
$SUDO_USER -c 'ln -s ${PWD}/openvim/scripts/service-floodlight.sh ${HOME}/bin/service-floodlight'
309 su
$SUDO_USER -c 'ln -s ${PWD}/openvim/scripts/service-opendaylight.sh ${HOME}/bin/service-opendaylight'
310 su
$SUDO_USER -c 'ln -s ${PWD}/openvim/scripts/get_dhcp_lease.sh ${HOME}/bin/get_dhcp_lease.sh'
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"
316 # echo " inserting /home/$SUDO_USER/bin in the PATH at .bashrc"
317 # su $SUDO_USER -c 'echo "PATH=\$PATH:/home/\${USER}/bin" >> ~/.bashrc'
320 if [[ $SUDO_USER == root
]]
322 if ! echo $PATH |
grep -q "${HOME}/bin"
324 echo "PATH=\$PATH:\${HOME}/bin" >> ${HOME}/.bashrc
329 #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'
333 su
$SUDO_USER -c 'activate-global-python-argcomplete --user'
334 if ! grep -q bash_completion.d
/python-argcomplete.sh
${HOME}/.bashrc
336 echo " inserting .bash_completion.d/python-argcomplete.sh execution at .bashrc"
337 su
$SUDO_USER -c 'echo ". ${HOME}/.bash_completion.d/python-argcomplete.sh" >> ~/.bashrc'
342 if [[ "$_DISTRO" == "Ubuntu" ]] && [[ ${_RELEASE%%.*} == 16 ]] && [[ -z $DEVELOP ]]
345 #################################################################
346 ##### CONFIGURE OPENVIM SERVICE #####
347 #################################################################'
349 .
/openvim
/scripts
/install-openvim-service.sh
-f openvim
#-u $SUDO_USER
350 # alias service-openvim="service openvim"
351 # echo 'alias service-openvim="service openvim"' >> ${HOME}/.bashrc
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|...' "
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"