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
31 echo -e "usage: sudo $0 [OPTIONS]"
32 echo -e "Install last stable source code in ./openvim and the needed packages"
33 echo -e "On a Ubuntu 16.04 it configures openvim as a service"
35 echo -e " -u USER: database admin user. 'root' by default. Prompts if needed"
36 echo -e " -p PASS: database admin password to be used or installed. Prompts if needed"
37 echo -e " -q --quiet: install in an unattended mode"
38 echo -e " -h --help: show this help"
39 echo -e " --develop: install last version for developers, and do not configure as a service"
40 echo -e " --forcedb: reinstall vim_db DB, deleting previous database if exists and creating a new one"
41 echo -e " --force: makes idenpotent, delete previous installations folders if needed"
42 echo -e " --noclone: assumes that openvim was cloned previously and that this script is run from the local repo"
43 echo -e " --no-install-packages: use this option to skip updating and installing the requires packages. This avoid wasting time if you are sure requires packages are present e.g. because of a previous installation"
46 function install_packages
(){
47 [ -x /usr
/bin
/apt-get
] && apt-get
install -y $
*
48 [ -x /usr
/bin
/yum
] && yum
install -y $
*
50 #check properly installed
53 PACKAGE_INSTALLED
="no"
54 [ -x /usr
/bin
/apt-get
] && dpkg
-l $PACKAGE &>> /dev
/null
&& PACKAGE_INSTALLED
="yes"
55 [ -x /usr
/bin
/yum
] && yum list installed
$PACKAGE &>> /dev
/null
&& PACKAGE_INSTALLED
="yes"
56 if [ "$PACKAGE_INSTALLED" = "no" ]
58 echo "failed to install package '$PACKAGE'. Revise network connectivity and try again"
64 function db_exists
() {
65 RESULT
=`mysqlshow --defaults-extra-file="$2" | grep -v Wildcard | grep -o $1`
66 if [ "$RESULT" == "$1" ]; then
70 echo " DB $1 does not exist"
75 GIT_URL
=https
://osm.etsi.org
/gerrit
/osm
/openvim.git
85 while getopts ":u:p:hiq-:" o
; do
88 export DBUSER
="$OPTARG"
91 export DBPASSWD
="$OPTARG"
92 export DBPASSWD_PARAM
="-p$OPTARG"
96 export DEBIAN_FRONTEND
=noninteractive
102 [ "${OPTARG}" == "help" ] && usage
&& exit 0
103 [ "${OPTARG}" == "develop" ] && DEVELOP
="y" && continue
104 [ "${OPTARG}" == "forcedb" ] && FORCEDB
="y" && continue
105 [ "${OPTARG}" == "force" ] && FORCEDB
="y" && FORCE
="y" && continue
106 [ "${OPTARG}" == "noclone" ] && NOCLONE
="y" && continue
107 [ "${OPTARG}" == "quiet" ] && export QUIET_MODE
=yes && export DEBIAN_FRONTEND
=noninteractive
&& continue
108 [ "${OPTARG}" == "no-install-packages" ] && export NO_PACKAGES
=yes && continue
109 echo -e "Invalid option: '--$OPTARG'\nTry $0 --help for more information" >&2
113 echo -e "Invalid option: '-$OPTARG'\nTry $0 --help for more information" >&2
117 echo -e "Option '-$OPTARG' requires an argument\nTry $0 --help for more information" >&2
127 #check root privileges and non a root user behind
128 [ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit -1
129 if [[ -z "$SUDO_USER" ]] ||
[[ "$SUDO_USER" = "root" ]]
131 [[ -z $QUIET_MODE ]] && read -e -p "Install in the root user (y/N)?" KK
132 [[ -z $QUIET_MODE ]] && [[ "$KK" != "y" ]] && [[ "$KK" != "yes" ]] && echo "Cancelled" && exit 1
133 export SUDO_USER
=root
136 #Discover Linux distribution
138 [ -f /etc
/redhat-release
] && _DISTRO
=$
(cat /etc
/redhat-release
2>/dev
/null | cut
-d" " -f1)
139 #if not assuming ubuntu type
140 [ -f /etc
/redhat-release
] || _DISTRO
=$
(lsb_release
-is 2>/dev
/null
)
141 if [ "$_DISTRO" == "Ubuntu" ]
143 _RELEASE
=$
(lsb_release
-rs)
144 if [[ ${_RELEASE%%.*} != 14 ]] && [[ ${_RELEASE%%.*} != 16 ]]
146 [[ -z $QUIET_MODE ]] && read -e -p "WARNING! Not tested Ubuntu version. Continue assuming a trusty (14.XX)'? (y/N)" KK
147 [[ -z $QUIET_MODE ]] && [[ "$KK" != "y" ]] && [[ "$KK" != "yes" ]] && echo "Cancelled" && exit 1
150 elif [ "$_DISTRO" == "CentOS" ]
153 if ! cat /etc
/redhat-release |
grep -q "7."
155 read -e -p "WARNING! Not tested CentOS version. Continue assuming a '_RELEASE' type? (y/N)" KK
156 [ "$KK" != "y" -a "$KK" != "yes" ] && echo "Cancelled" && exit 0
158 elif [ "$_DISTRO" == "Red" ]
161 if ! cat /etc
/redhat-release |
grep -q "7."
163 read -e -p "WARNING! Not tested Red Hat OS version. Continue assuming a '_RELEASE' type? (y/N)" KK
164 [ "$KK" != "y" -a "$KK" != "yes" ] && echo "Cancelled" && exit 0
166 else #[ "$_DISTRO" != "Ubuntu" -a "$_DISTRO" != "CentOS" -a "$_DISTRO" != "Red" ]
167 _DISTRO_DISCOVER
=$_DISTRO
168 [ -x /usr
/bin
/apt-get
] && _DISTRO
="Ubuntu" && _RELEASE
="14"
169 [ -x /usr
/bin
/yum
] && _DISTRO
="CentOS" && _RELEASE
="7"
170 read -e -p "WARNING! Not tested Linux distribution '$_DISTRO_DISCOVER '. Continue assuming a '$_DISTRO $_RELEASE' type? (y/N)" KK
171 [ "$KK" != "y" -a "$KK" != "yes" ] && echo "Cancelled" && exit 0
174 #check if installed as a service
175 INSTALL_AS_A_SERVICE
=""
176 [[ "$_DISTRO" == "Ubuntu" ]] && [[ ${_RELEASE%%.*} == 16 ]] && [[ -z $DEVELOP ]] && INSTALL_AS_A_SERVICE
="y"
178 #Next operations require knowing BASEFOLDER
179 if [[ -z "$NOCLONE" ]]; then
180 if [[ -n "$INSTALL_AS_A_SERVICE" ]] ; then
181 OPENVIM_BASEFOLDER
=__openvim__
${RANDOM}
183 OPENVIM_BASEFOLDER
="${PWD}/openvim"
185 [[ -n "$FORCE" ]] && rm -rf $OPENVIM_BASEFOLDER #make idempotent
187 HERE
=$
(realpath $
(dirname $0))
188 OPENVIM_BASEFOLDER
=$
(dirname $HERE)
192 if [[ -z "$NO_PACKAGES" ]]
195 #################################################################
196 ##### UPDATE REPOSITORIES #####
197 #################################################################'
198 [ "$_DISTRO" == "Ubuntu" ] && apt-get update
-y
200 [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && yum check-update
-y
201 [ "$_DISTRO" == "CentOS" ] && sudo yum
install -y epel-release
202 [ "$_DISTRO" == "Red" ] && wget http
://dl.fedoraproject.org
/pub
/epel
/7/x86_64
/e
/epel-release-7-5.noarch.rpm \
203 && sudo rpm
-ivh epel-release-7-5.noarch.rpm
&& sudo yum
install -y epel-release
&& rm -f epel-release-7-5.noarch.rpm
204 [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && sudo yum repolist
207 if [[ -z "$NO_PACKAGES" ]]
210 #################################################################
211 ##### INSTALL REQUIRED PACKAGES #####
212 #################################################################'
213 [ "$_DISTRO" == "Ubuntu" ] && install_packages
"git screen wget mysql-server"
214 [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && install_packages
"git screen wget mariadb mariadb-server"
216 if [[ "$_DISTRO" == "Ubuntu" ]]
218 #start services. By default CentOS does not start services
219 service mysql start
>> /dev
/null
220 # try to set admin password, ignore if fails
221 [[ -n $DBPASSWD ]] && mysqladmin
-u $DBUSER -s password
$DBPASSWD
224 if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ]
226 #start services. By default CentOS does not start services
227 service mariadb start
229 systemctl
enable mariadb
230 systemctl
enable httpd
231 read -e -p "Do you want to configure mariadb (recomended if not done before) (Y/n)" KK
232 [ "$KK" != "n" -a "$KK" != "no" ] && mysql_secure_installation
234 read -e -p "Do you want to set firewall to grant web access port 80,443 (Y/n)" KK
235 [ "$KK" != "n" -a "$KK" != "no" ] &&
236 firewall-cmd
--permanent --zone=public
--add-service=http
&&
237 firewall-cmd
--permanent --zone=public
--add-service=https
&&
238 firewall-cmd
--reload
240 fi #[[ -z "$NO_PACKAGES" ]]
242 #check and ask for database user password. Must be done after database installation
243 if [[ -n $QUIET_MODE ]]
245 echo -e "\nCheking database connection and ask for credentials"
246 while ! mysqladmin
-s -u$DBUSER $DBPASSWD_PARAM status
>/dev
/null
248 [ -n "$logintry" ] && echo -e "\nInvalid database credentials!!!. Try again (Ctrl+c to abort)"
249 [ -z "$logintry" ] && echo -e "\nProvide database credentials"
250 read -e -p "database user? ($DBUSER) " DBUSER_
251 [ -n "$DBUSER_" ] && DBUSER
=$DBUSER_
252 read -e -s -p "database password? (Enter for not using password) " DBPASSWD_
253 [ -n "$DBPASSWD_" ] && DBPASSWD
="$DBPASSWD_" && DBPASSWD_PARAM
="-p$DBPASSWD_"
254 [ -z "$DBPASSWD_" ] && DBPASSWD
="" && DBPASSWD_PARAM
=""
259 if [[ -z "$NO_PACKAGES" ]]
262 #################################################################
263 ##### INSTALL PYTHON PACKAGES #####
264 #################################################################'
265 [ "$_DISTRO" == "Ubuntu" ] && install_packages
"python-yaml python-libvirt python-bottle python-mysqldb python-jsonschema python-paramiko python-argcomplete python-requests"
266 [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && install_packages
"PyYAML libvirt-python MySQL-python python-jsonschema python-paramiko python-argcomplete python-requests"
268 #The only way to install python-bottle on Centos7 is with easy_install or pip
269 [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && easy_install
-U bottle
271 fi #[[ -z "$NO_PACKAGES" ]]
273 if [[ -z $NOCLONE ]]; then
275 #################################################################
276 ##### DOWNLOAD SOURCE #####
277 #################################################################'
278 if [[ -d "${OPENVIM_BASEFOLDER}" ]]
282 echo "deleting '${OPENVIM_BASEFOLDER}' folder"
283 rm -rf "$OPENVIM_BASEFOLDER" #make idempotent
284 elif [[ -z "$QUIET_MODE" ]]
286 read -e -p "${OPENVIM_BASEFOLDER} folder exist, overwrite? (y/N)" KK
287 if [[ "$KK" == "y" ]] ||
[[ "$KK" == "yes" ]]
288 then rm -rf "$OPENVIM_BASEFOLDER"
294 echo "'${OPENVIM_BASEFOLDER}' folder exist" >&2 && exit 1
299 su
$SUDO_USER -c "git clone ${GIT_URL} ${OPENVIM_BASEFOLDER}"
300 su
$SUDO_USER -c "cp ${OPENVIM_BASEFOLDER}/.gitignore-common ${OPENVIM_BASEFOLDER}/.gitignore"
301 [[ -z $DEVELOP ]] && su
$SUDO_USER -c "git -C ${OPENVIM_BASEFOLDER} checkout tags/v1.0.2"
305 #################################################################
306 ##### CREATE DATABASE #####
307 #################################################################'
308 echo -e "\nCreating temporary file form MYSQL installation and initialization"
309 TEMPFILE
="$(mktemp -q --tmpdir "installopenvim.XXXXXX
")"
310 trap 'rm -f "$TEMPFILE"' EXIT
311 chmod 0600 "$TEMPFILE"
312 echo -e "[client]\n user='$DBUSER'\n password='$DBPASSWD'">"$TEMPFILE"
314 if db_exists
"vim_db" $TEMPFILE ; then
315 if [[ -n $FORCEDB ]]; then
316 echo " Deleting previous database vim_db"
318 [[ -n $QUIET_MODE ]] && DBDELETEPARAM
="-f"
319 mysqladmin
--defaults-extra-file=$TEMPFILE -s drop vim_db
$DBDELETEPARAM ||
! echo "Could not delete vim_db database" ||
exit 1
320 #echo "REVOKE ALL PRIVILEGES ON vim_db.* FROM 'vim'@'localhost';" | mysql --defaults-extra-file=$TEMPFILE -s || ! echo "Failed while creating user vim at database" || exit 1
321 #echo "DELETE USER 'vim'@'localhost';" | mysql --defaults-extra-file=$TEMPFILE -s || ! echo "Failed while creating user vim at database" || exit 1
322 mysqladmin
--defaults-extra-file=$TEMPFILE -s create vim_db ||
! echo "Error creating vim_db database" ||
exit 1
323 echo "DROP USER 'vim'@'localhost';" | mysql
--defaults-extra-file=$TEMPFILE -s ||
! echo "Failed while creating user vim at database" ||
exit 1
324 echo "CREATE USER 'vim'@'localhost' identified by 'vimpw';" | mysql
--defaults-extra-file=$TEMPFILE -s ||
! echo "Failed while creating user vim at database" ||
exit 1
325 echo "GRANT ALL PRIVILEGES ON vim_db.* TO 'vim'@'localhost';" | mysql
--defaults-extra-file=$TEMPFILE -s ||
! echo "Failed while creating user vim at database" ||
exit 1
326 echo " Database 'vim_db' created, user 'vim' password 'vimpw'"
328 echo "Database exists. Use option '--forcedb' to force the deletion of the existing one" && exit 1
331 mysqladmin
-u$DBUSER $DBPASSWD_PARAM -s create vim_db ||
! echo "Error creating vim_db database" ||
exit 1
332 echo "CREATE USER 'vim'@'localhost' identified by 'vimpw';" | mysql
--defaults-extra-file=$TEMPFILE -s ||
! echo "Failed while creating user vim at database" ||
exit 1
333 echo "GRANT ALL PRIVILEGES ON vim_db.* TO 'vim'@'localhost';" | mysql
--defaults-extra-file=$TEMPFILE -s ||
! echo "Failed while creating user vim at database" ||
exit 1
334 echo " Database 'vim_db' created, user 'vim' password 'vimpw'"
338 #################################################################
339 ##### INIT DATABASE #####
340 #################################################################'
341 su
$SUDO_USER -c "${OPENVIM_BASEFOLDER}/database_utils/init_vim_db.sh -u vim -p vimpw -d vim_db" ||
! echo "Failed while initializing database" ||
exit 1
344 if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ]
347 #################################################################
348 ##### CONFIGURE firewalld #####
349 #################################################################'
350 read -e -p "Configure firewalld for openvimd port 9080? (Y/n)" KK
351 if [ "$KK" != "n" -a "$KK" != "no" ]
353 #Creates a service file for openvim
354 echo '<?xml version="1.0" encoding="utf-8"?>
356 <short>openvimd</short>
357 <description>openvimd service</description>
358 <port protocol="tcp" port="9080"/>
359 </service>' > /etc
/firewalld
/services
/openvimd.xml
360 #put proper permissions
361 pushd /etc
/firewalld
/services
> /dev
/null
365 #Add the openvim service to the default zone permanently and reload the firewall configuration
366 firewall-cmd
--permanent --add-service=openvim
> /dev
/null
367 firewall-cmd
--reload > /dev
/null
375 #################################################################
376 ##### CONFIGURE openvim CLIENT #####
377 #################################################################'
378 #creates a link at ~/bin if not configured as a service
379 if [[ -z "$INSTALL_AS_A_SERVICE" ]]
381 su
$SUDO_USER -c 'mkdir -p ~/bin'
382 su
$SUDO_USER -c 'rm -f ${HOME}/bin/openvim'
383 su
$SUDO_USER -c 'rm -f ${HOME}/bin/openflow'
384 su
$SUDO_USER -c 'rm -f ${HOME}/bin/service-openvim'
385 su
$SUDO_USER -c 'rm -f ${HOME}/bin/initopenvim'
386 su
$SUDO_USER -c 'rm -f ${HOME}/bin/service-floodlight'
387 su
$SUDO_USER -c 'rm -f ${HOME}/bin/service-opendaylight'
388 su
$SUDO_USER -c 'rm -f ${HOME}/bin/get_dhcp_lease.sh'
389 su
$SUDO_USER -c "ln -s '${OPENVIM_BASEFOLDER}/openvim' "'${HOME}/bin/openvim'
390 su
$SUDO_USER -c "ln -s '${OPENVIM_BASEFOLDER}/openflow' "'${HOME}/bin/openflow'
391 su
$SUDO_USER -c "ln -s '${OPENVIM_BASEFOLDER}/scripts/service-openvim.sh' "'${HOME}/bin/service-openvim'
392 su
$SUDO_USER -c "ln -s '${OPENVIM_BASEFOLDER}/scripts/initopenvim.sh' "'${HOME}/bin/initopenvim'
393 su
$SUDO_USER -c "ln -s '${OPENVIM_BASEFOLDER}/scripts/service-floodlight.sh' "'${HOME}/bin/service-floodlight'
394 su
$SUDO_USER -c "ln -s '${OPENVIM_BASEFOLDER}/scripts/service-opendaylight.sh' "'${HOME}/bin/service-opendaylight'
395 su
$SUDO_USER -c "ln -s '${OPENVIM_BASEFOLDER}/scripts/get_dhcp_lease.sh' "'${HOME}/bin/get_dhcp_lease.sh'
397 #insert /home/<user>/bin in the PATH
398 #skiped because normally this is done authomatically when ~/bin exist
399 #if ! su $SUDO_USER -c 'echo $PATH' | grep -q "/home/${SUDO_USER}/bin"
401 # echo " inserting /home/$SUDO_USER/bin in the PATH at .bashrc"
402 # su $SUDO_USER -c 'echo "PATH=\$PATH:/home/\${USER}/bin" >> ~/.bashrc'
405 if [[ $SUDO_USER == root
]]
407 if ! echo $PATH |
grep -q "${HOME}/bin"
409 echo "PATH=\$PATH:\${HOME}/bin" >> ${HOME}/.bashrc
414 #configure arg-autocomplete for this user
415 #in case of minmal instalation this package is not installed by default
416 [[ "$_DISTRO" == "CentOS" ||
"$_DISTRO" == "Red" ]] && yum
install -y bash-completion
417 #su $SUDO_USER -c 'mkdir -p ~/.bash_completion.d'
418 su
$SUDO_USER -c 'activate-global-python-argcomplete --user'
419 if ! grep -q bash_completion.d
/python-argcomplete.sh
${HOME}/.bashrc
421 echo " inserting .bash_completion.d/python-argcomplete.sh execution at .bashrc"
422 su
$SUDO_USER -c 'echo ". ${HOME}/.bash_completion.d/python-argcomplete.sh" >> ~/.bashrc'
427 if [[ -n "$INSTALL_AS_A_SERVICE" ]]
430 #################################################################
431 ##### CONFIGURE OPENVIM SERVICE #####
432 #################################################################'
434 DELETE_PARAM
="" && [[ -z "$NOCLONE" ]] && DELETE_PARAM
="-d"
435 ${OPENVIM_BASEFOLDER}/scripts/install-openvim-service.sh -f ${OPENVIM_BASEFOLDER} ${DELETE_PARAM}
436 # alias service-openvim="service openvim"
437 # echo 'alias service-openvim="service openvim"' >> ${HOME}/.bashrc
441 echo "Done! installed at /opt/openvim"
442 echo " Manage server with 'service openvim start|stop|status|...' "
448 echo "Done! you may need to logout and login again for loading client configuration"
449 echo " Run 'service-openvim start' for starting openvim in a screen"