Fix bug installing openvim and db generation
[osm/openvim.git] / scripts / install-openvim.sh
1 #!/bin/bash
2
3 ##
4 # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5 # This file is part of openvim
6 # 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
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
27
28
29
30 function usage(){
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"
34 echo -e " OPTIONS"
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 " --force: makes idenpotent, delete previous installations folders if needed"
41 echo -e " --noclone: assumes that openvim was cloned previously and that this script is run from the local repo"
42 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"
43 echo -e " --no-db: do not insall mysql server"
44 }
45
46 function install_packages(){
47 [ -x /usr/bin/apt-get ] && apt-get install -y $*
48 [ -x /usr/bin/yum ] && yum install -y $*
49
50 #check properly installed
51 for PACKAGE in $*
52 do
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" ]
57 then
58 echo "failed to install package '$PACKAGE'. Revise network connectivity and try again"
59 exit -1
60 fi
61 done
62 }
63
64 function db_exists() {
65 RESULT=`mysqlshow --defaults-extra-file="$2" | grep -v Wildcard | grep -w $1`
66 if [ "$RESULT" == "$1" ]; then
67 echo " DB $1 exists"
68 return 0
69 fi
70 echo " DB $1 does not exist"
71 return 1
72 }
73
74
75 GIT_URL=https://osm.etsi.org/gerrit/osm/openvim.git
76 DBUSER="root"
77 DBPASSWD=""
78 DBPASSWD_PARAM=""
79 QUIET_MODE=""
80 DEVELOP=""
81 FORCE=""
82 NOCLONE=""
83 NO_PACKAGES=""
84 NO_DB=""
85
86 while getopts ":u:p:hiq-:" o; do
87 case "${o}" in
88 u)
89 export DBUSER="$OPTARG"
90 ;;
91 p)
92 export DBPASSWD="$OPTARG"
93 export DBPASSWD_PARAM="-p$OPTARG"
94 ;;
95 q)
96 export QUIET_MODE=yes
97 export DEBIAN_FRONTEND=noninteractive
98 ;;
99 h)
100 usage && exit 0
101 ;;
102 -)
103 [ "${OPTARG}" == "help" ] && usage && exit 0
104 [ "${OPTARG}" == "develop" ] && DEVELOP="y" && continue
105 [ "${OPTARG}" == "force" ] && 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 [ "${OPTARG}" == "no-db" ] && NO_DB="y" && continue
110 echo -e "Invalid option: '--$OPTARG'\nTry $0 --help for more information" >&2
111 exit 1
112 ;;
113 \?)
114 echo -e "Invalid option: '-$OPTARG'\nTry $0 --help for more information" >&2
115 exit 1
116 ;;
117 :)
118 echo -e "Option '-$OPTARG' requires an argument\nTry $0 --help for more information" >&2
119 exit 1
120 ;;
121 *)
122 usage >&2
123 exit -1
124 ;;
125 esac
126 done
127
128 #check root privileges and non a root user behind
129 [ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit -1
130 if [[ -z "$SUDO_USER" ]] || [[ "$SUDO_USER" = "root" ]]
131 then
132 [[ -z $QUIET_MODE ]] && read -e -p "Install in the root user (y/N)?" KK
133 [[ -z $QUIET_MODE ]] && [[ "$KK" != "y" ]] && [[ "$KK" != "yes" ]] && echo "Cancelled" && exit 1
134 export SUDO_USER=root
135 fi
136
137 #Discover Linux distribution
138 #try redhat type
139 [ -f /etc/redhat-release ] && _DISTRO=$(cat /etc/redhat-release 2>/dev/null | cut -d" " -f1)
140 #if not assuming ubuntu type
141 [ -f /etc/redhat-release ] || _DISTRO=$(lsb_release -is 2>/dev/null)
142 if [ "$_DISTRO" == "Ubuntu" ]
143 then
144 _RELEASE=$(lsb_release -rs)
145 if [[ ${_RELEASE%%.*} != 14 ]] && [[ ${_RELEASE%%.*} != 16 ]]
146 then
147 [[ -z $QUIET_MODE ]] && read -e -p "WARNING! Not tested Ubuntu version. Continue assuming a trusty (14.XX)'? (y/N)" KK
148 [[ -z $QUIET_MODE ]] && [[ "$KK" != "y" ]] && [[ "$KK" != "yes" ]] && echo "Cancelled" && exit 1
149 _RELEASE = 14
150 fi
151 elif [ "$_DISTRO" == "CentOS" ]
152 then
153 _RELEASE="7"
154 if ! cat /etc/redhat-release | grep -q "7."
155 then
156 read -e -p "WARNING! Not tested CentOS version. Continue assuming a '_RELEASE' type? (y/N)" KK
157 [ "$KK" != "y" -a "$KK" != "yes" ] && echo "Cancelled" && exit 0
158 fi
159 elif [ "$_DISTRO" == "Red" ]
160 then
161 _RELEASE="7"
162 if ! cat /etc/redhat-release | grep -q "7."
163 then
164 read -e -p "WARNING! Not tested Red Hat OS version. Continue assuming a '_RELEASE' type? (y/N)" KK
165 [ "$KK" != "y" -a "$KK" != "yes" ] && echo "Cancelled" && exit 0
166 fi
167 else #[ "$_DISTRO" != "Ubuntu" -a "$_DISTRO" != "CentOS" -a "$_DISTRO" != "Red" ]
168 _DISTRO_DISCOVER=$_DISTRO
169 [ -x /usr/bin/apt-get ] && _DISTRO="Ubuntu" && _RELEASE="14"
170 [ -x /usr/bin/yum ] && _DISTRO="CentOS" && _RELEASE="7"
171 read -e -p "WARNING! Not tested Linux distribution '$_DISTRO_DISCOVER '. Continue assuming a '$_DISTRO $_RELEASE' type? (y/N)" KK
172 [ "$KK" != "y" -a "$KK" != "yes" ] && echo "Cancelled" && exit 0
173 fi
174
175 #check if installed as a service
176 INSTALL_AS_A_SERVICE=""
177 [[ "$_DISTRO" == "Ubuntu" ]] && [[ ${_RELEASE%%.*} == 16 ]] && [[ -z $DEVELOP ]] && INSTALL_AS_A_SERVICE="y"
178
179 #Next operations require knowing BASEFOLDER
180 if [[ -z "$NOCLONE" ]]; then
181 if [[ -n "$INSTALL_AS_A_SERVICE" ]] ; then
182 OPENVIM_BASEFOLDER=__openvim__${RANDOM}
183 else
184 OPENVIM_BASEFOLDER="${PWD}/openvim"
185 fi
186 [[ -n "$FORCE" ]] && rm -rf $OPENVIM_BASEFOLDER #make idempotent
187 else
188 HERE=$(dirname $(readlink -f ${BASH_SOURCE[0]}))
189 OPENVIM_BASEFOLDER=$(dirname $HERE)
190 fi
191
192
193 if [[ -z "$NO_PACKAGES" ]]
194 then
195 echo '
196 #################################################################
197 ##### UPDATE REPOSITORIES #####
198 #################################################################'
199 [ "$_DISTRO" == "Ubuntu" ] && apt-get update -y
200
201 [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && yum check-update -y
202 [ "$_DISTRO" == "CentOS" ] && sudo yum install -y epel-release
203 [ "$_DISTRO" == "Red" ] && wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm \
204 && sudo rpm -ivh epel-release-7-5.noarch.rpm && sudo yum install -y epel-release && rm -f epel-release-7-5.noarch.rpm
205 [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && sudo yum repolist
206 fi
207
208 if [[ -z "$NO_PACKAGES" ]]
209 then
210 echo '
211 #################################################################
212 ##### INSTALL REQUIRED PACKAGES #####
213 #################################################################'
214 [ "$_DISTRO" == "Ubuntu" ] && install_packages "git screen wget mysql-server"
215 [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && install_packages "git screen wget mariadb mariadb-server"
216
217 if [[ "$_DISTRO" == "Ubuntu" ]]
218 then
219 #start services. By default CentOS does not start services
220 service mysql start >> /dev/null
221 # try to set admin password, ignore if fails
222 [[ -n $DBPASSWD ]] && mysqladmin -u $DBUSER -s password $DBPASSWD
223 fi
224
225 if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ]
226 then
227 #start services. By default CentOS does not start services
228 service mariadb start
229 service httpd start
230 systemctl enable mariadb
231 systemctl enable httpd
232 read -e -p "Do you want to configure mariadb (recomended if not done before) (Y/n)" KK
233 [ "$KK" != "n" -a "$KK" != "no" ] && mysql_secure_installation
234
235 read -e -p "Do you want to set firewall to grant web access port 80,443 (Y/n)" KK
236 [ "$KK" != "n" -a "$KK" != "no" ] &&
237 firewall-cmd --permanent --zone=public --add-service=http &&
238 firewall-cmd --permanent --zone=public --add-service=https &&
239 firewall-cmd --reload
240 fi
241 fi #[[ -z "$NO_PACKAGES" ]]
242
243 #check and ask for database user password. Must be done after database installation
244 if [[ -n $QUIET_MODE ]]
245 then
246 echo -e "\nCheking database connection and ask for credentials"
247 while ! mysqladmin -s -u$DBUSER $DBPASSWD_PARAM status >/dev/null
248 do
249 [ -n "$logintry" ] && echo -e "\nInvalid database credentials!!!. Try again (Ctrl+c to abort)"
250 [ -z "$logintry" ] && echo -e "\nProvide database credentials"
251 read -e -p "database user? ($DBUSER) " DBUSER_
252 [ -n "$DBUSER_" ] && DBUSER=$DBUSER_
253 read -e -s -p "database password? (Enter for not using password) " DBPASSWD_
254 [ -n "$DBPASSWD_" ] && DBPASSWD="$DBPASSWD_" && DBPASSWD_PARAM="-p$DBPASSWD_"
255 [ -z "$DBPASSWD_" ] && DBPASSWD="" && DBPASSWD_PARAM=""
256 logintry="yes"
257 done
258 fi
259
260 if [[ -z "$NO_PACKAGES" ]]
261 then
262
263 echo '
264 #################################################################
265 ##### INSTALL PYTHON PACKAGES #####
266 #################################################################'
267 [ "$_DISTRO" == "Ubuntu" ] && install_packages "python-yaml python-libvirt python-bottle python-mysqldb python-jsonschema python-paramiko python-argcomplete python-requests"
268 [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && install_packages "PyYAML libvirt-python MySQL-python python-jsonschema python-paramiko python-argcomplete python-requests"
269
270 #The only way to install python-bottle on Centos7 is with easy_install or pip
271 [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && easy_install -U bottle
272
273 fi #[[ -z "$NO_PACKAGES" ]]
274
275 if [[ -z $NOCLONE ]]; then
276 echo '
277 #################################################################
278 ##### DOWNLOAD SOURCE #####
279 #################################################################'
280 if [[ -d "${OPENVIM_BASEFOLDER}" ]]
281 then
282 if [[ -n "$FORCE" ]]
283 then
284 echo "deleting '${OPENVIM_BASEFOLDER}' folder"
285 rm -rf "$OPENVIM_BASEFOLDER" #make idempotent
286 elif [[ -z "$QUIET_MODE" ]]
287 then
288 read -e -p "${OPENVIM_BASEFOLDER} folder exist, overwrite? (y/N)" KK
289 if [[ "$KK" == "y" ]] || [[ "$KK" == "yes" ]]
290 then rm -rf "$OPENVIM_BASEFOLDER"
291 else
292 echo "canceled"
293 exit 1
294 fi
295 else
296 echo "'${OPENVIM_BASEFOLDER}' folder exist" >&2 && exit 1
297 fi
298 fi
299
300
301 su $SUDO_USER -c "git clone ${GIT_URL} ${OPENVIM_BASEFOLDER}"
302 su $SUDO_USER -c "cp ${OPENVIM_BASEFOLDER}/.gitignore-common ${OPENVIM_BASEFOLDER}/.gitignore"
303 [[ -z $DEVELOP ]] && su $SUDO_USER -c "git -C ${OPENVIM_BASEFOLDER} checkout v2.0"
304 fi
305
306 DB_QUIET=''
307 if [ -z "$NO_DB" ]; then
308 if [ -n "$QUIET_MODE" ]; then
309 DB_QUIET='-q'
310 fi
311
312 echo "!!!! install-db-server.sh: ${OPENVIM_BASEFOLDER}/database_utils/install-db-server.sh -U $DBUSER $DBPASSWD_PARAM $DB_QUIET"
313 ${OPENVIM_BASEFOLDER}/database_utils/install-db-server.sh -U $DBUSER $DBPASSWD_PARAM $DB_QUIET || exit 1
314 fi
315
316
317 if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ]
318 then
319 echo '
320 #################################################################
321 ##### CONFIGURE firewalld #####
322 #################################################################'
323 read -e -p "Configure firewalld for openvimd port 9080? (Y/n)" KK
324 if [ "$KK" != "n" -a "$KK" != "no" ]
325 then
326 #Creates a service file for openvim
327 echo '<?xml version="1.0" encoding="utf-8"?>
328 <service>
329 <short>openvimd</short>
330 <description>openvimd service</description>
331 <port protocol="tcp" port="9080"/>
332 </service>' > /etc/firewalld/services/openvimd.xml
333 #put proper permissions
334 pushd /etc/firewalld/services > /dev/null
335 restorecon openvim
336 chmod 640 openvim
337 popd > /dev/null
338 #Add the openvim service to the default zone permanently and reload the firewall configuration
339 firewall-cmd --permanent --add-service=openvim > /dev/null
340 firewall-cmd --reload > /dev/null
341 echo "done."
342 else
343 echo "skipping."
344 fi
345 fi
346
347 echo '
348 #################################################################
349 ##### CONFIGURE openvim CLIENT #####
350 #################################################################'
351 #creates a link at ~/bin if not configured as a service
352 if [[ -z "$INSTALL_AS_A_SERVICE" ]]
353 then
354 su $SUDO_USER -c 'mkdir -p ~/bin'
355 su $SUDO_USER -c 'rm -f ${HOME}/bin/openvim'
356 su $SUDO_USER -c 'rm -f ${HOME}/bin/openflow'
357 su $SUDO_USER -c 'rm -f ${HOME}/bin/service-openvim'
358 su $SUDO_USER -c 'rm -f ${HOME}/bin/initopenvim'
359 su $SUDO_USER -c 'rm -f ${HOME}/bin/service-floodlight'
360 su $SUDO_USER -c 'rm -f ${HOME}/bin/service-opendaylight'
361 su $SUDO_USER -c 'rm -f ${HOME}/bin/get_dhcp_lease.sh'
362 su $SUDO_USER -c "ln -s '${OPENVIM_BASEFOLDER}/openvim' "'${HOME}/bin/openvim'
363 su $SUDO_USER -c "ln -s '${OPENVIM_BASEFOLDER}/openflow' "'${HOME}/bin/openflow'
364 su $SUDO_USER -c "ln -s '${OPENVIM_BASEFOLDER}/scripts/service-openvim' "'${HOME}/bin/service-openvim'
365 su $SUDO_USER -c "ln -s '${OPENVIM_BASEFOLDER}/scripts/initopenvim' "'${HOME}/bin/initopenvim'
366 su $SUDO_USER -c "ln -s '${OPENVIM_BASEFOLDER}/scripts/service-floodlight' "'${HOME}/bin/service-floodlight'
367 su $SUDO_USER -c "ln -s '${OPENVIM_BASEFOLDER}/scripts/service-opendaylight' "'${HOME}/bin/service-opendaylight'
368 su $SUDO_USER -c "ln -s '${OPENVIM_BASEFOLDER}/scripts/get_dhcp_lease.sh' "'${HOME}/bin/get_dhcp_lease.sh'
369
370 #insert /home/<user>/bin in the PATH
371 #skiped because normally this is done authomatically when ~/bin exist
372 #if ! su $SUDO_USER -c 'echo $PATH' | grep -q "/home/${SUDO_USER}/bin"
373 #then
374 # echo " inserting /home/$SUDO_USER/bin in the PATH at .bashrc"
375 # su $SUDO_USER -c 'echo "PATH=\$PATH:/home/\${USER}/bin" >> ~/.bashrc'
376 #fi
377
378 if [[ $SUDO_USER == root ]]
379 then
380 if ! echo $PATH | grep -q "${HOME}/bin"
381 then
382 echo "PATH=\$PATH:\${HOME}/bin" >> ${HOME}/.bashrc
383 fi
384 fi
385 fi
386
387 #configure arg-autocomplete for this user
388 #in case of minmal instalation this package is not installed by default
389 [[ "$_DISTRO" == "CentOS" || "$_DISTRO" == "Red" ]] && yum install -y bash-completion
390 #su $SUDO_USER -c 'mkdir -p ~/.bash_completion.d'
391 su $SUDO_USER -c 'activate-global-python-argcomplete --user'
392 if ! grep -q bash_completion.d/python-argcomplete.sh ${HOME}/.bashrc
393 then
394 echo " inserting .bash_completion.d/python-argcomplete.sh execution at .bashrc"
395 su $SUDO_USER -c 'echo ". ${HOME}/.bash_completion.d/python-argcomplete.sh" >> ~/.bashrc'
396 fi
397
398
399
400 if [[ -n "$INSTALL_AS_A_SERVICE" ]]
401 then
402 echo '
403 #################################################################
404 ##### CONFIGURE OPENVIM SERVICE #####
405 #################################################################'
406
407 DELETE_PARAM="" && [[ -z "$NOCLONE" ]] && DELETE_PARAM="-d"
408 ${OPENVIM_BASEFOLDER}/scripts/install-openvim-service.sh -f ${OPENVIM_BASEFOLDER} ${DELETE_PARAM}
409 # alias service-openvim="service openvim"
410 # echo 'alias service-openvim="service openvim"' >> ${HOME}/.bashrc
411
412 echo
413 echo
414 echo "Done! installed at /opt/openvim"
415 echo " Manage server with 'service openvim start|stop|status|...' "
416
417
418 else
419
420 echo
421 echo "Done! you may need to logout and login again for loading client configuration"
422 echo " Run 'service-openvim start' for starting openvim in a screen"
423
424 fi