installation script enhancement. Make idempotent
[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 " --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"
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 -o $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 FORCEDB=""
82 FORCE=""
83 NOCLONE=""
84 NO_PACKAGES=""
85 while getopts ":u:p:hiq-:" o; do
86 case "${o}" in
87 u)
88 export DBUSER="$OPTARG"
89 ;;
90 p)
91 export DBPASSWD="$OPTARG"
92 export DBPASSWD_PARAM="-p$OPTARG"
93 ;;
94 q)
95 export QUIET_MODE=yes
96 export DEBIAN_FRONTEND=noninteractive
97 ;;
98 h)
99 usage && exit 0
100 ;;
101 -)
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
110 exit 1
111 ;;
112 \?)
113 echo -e "Invalid option: '-$OPTARG'\nTry $0 --help for more information" >&2
114 exit 1
115 ;;
116 :)
117 echo -e "Option '-$OPTARG' requires an argument\nTry $0 --help for more information" >&2
118 exit 1
119 ;;
120 *)
121 usage >&2
122 exit -1
123 ;;
124 esac
125 done
126
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" ]]
130 then
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
134 fi
135
136 #Discover Linux distribution
137 #try redhat type
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" ]
142 then
143 _RELEASE=$(lsb_release -rs)
144 if [[ ${_RELEASE%%.*} != 14 ]] && [[ ${_RELEASE%%.*} != 16 ]]
145 then
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
148 _RELEASE = 14
149 fi
150 elif [ "$_DISTRO" == "CentOS" ]
151 then
152 _RELEASE="7"
153 if ! cat /etc/redhat-release | grep -q "7."
154 then
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
157 fi
158 elif [ "$_DISTRO" == "Red" ]
159 then
160 _RELEASE="7"
161 if ! cat /etc/redhat-release | grep -q "7."
162 then
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
165 fi
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
172 fi
173
174 #check if installed as a service
175 INSTALL_AS_A_SERVICE=""
176 [[ "$_DISTRO" == "Ubuntu" ]] && [[ ${_RELEASE%%.*} == 16 ]] && [[ -z $DEVELOP ]] && INSTALL_AS_A_SERVICE="y"
177
178 #Next operations require knowing BASEFOLDER
179 if [[ -z "$NOCLONE" ]]; then
180 if [[ -n "$INSTALL_AS_A_SERVICE" ]] ; then
181 OPENVIM_BASEFOLDER=__openvim__${RANDOM}
182 else
183 OPENVIM_BASEFOLDER="${PWD}/openvim"
184 fi
185 [[ -n "$FORCE" ]] && rm -rf $OPENVIM_BASEFOLDER #make idempotent
186 else
187 HERE=$(realpath $(dirname $0))
188 OPENVIM_BASEFOLDER=$(dirname $HERE)
189 fi
190
191
192 if [[ -z "$NO_PACKAGES" ]]
193 then
194 echo '
195 #################################################################
196 ##### UPDATE REPOSITORIES #####
197 #################################################################'
198 [ "$_DISTRO" == "Ubuntu" ] && apt-get update -y
199
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
205 fi
206
207 if [[ -z "$NO_PACKAGES" ]]
208 then
209 echo '
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"
215
216 if [[ "$_DISTRO" == "Ubuntu" ]]
217 then
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
222 fi
223
224 if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ]
225 then
226 #start services. By default CentOS does not start services
227 service mariadb start
228 service httpd 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
233
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
239 fi
240 fi #[[ -z "$NO_PACKAGES" ]]
241
242 #check and ask for database user password. Must be done after database installation
243 if [[ -n $QUIET_MODE ]]
244 then
245 echo -e "\nCheking database connection and ask for credentials"
246 while ! mysqladmin -s -u$DBUSER $DBPASSWD_PARAM status >/dev/null
247 do
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=""
255 logintry="yes"
256 done
257 fi
258
259 if [[ -z "$NO_PACKAGES" ]]
260 then
261 echo '
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"
267
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
270
271 fi #[[ -z "$NO_PACKAGES" ]]
272
273 if [[ -z $NOCLONE ]]; then
274 echo '
275 #################################################################
276 ##### DOWNLOAD SOURCE #####
277 #################################################################'
278 if [[ -d "${OPENVIM_BASEFOLDER}" ]]
279 then
280 if [[ -n "$FORCE" ]]
281 then
282 echo "deleting '${OPENVIM_BASEFOLDER}' folder"
283 rm -rf "$OPENVIM_BASEFOLDER" #make idempotent
284 elif [[ -z "$QUIET_MODE" ]]
285 then
286 read -e -p "${OPENVIM_BASEFOLDER} folder exist, overwrite? (y/N)" KK
287 if [[ "$KK" == "y" ]] || [[ "$KK" == "yes" ]]
288 then rm -rf "$OPENVIM_BASEFOLDER"
289 else
290 echo "canceled"
291 exit 1
292 fi
293 else
294 echo "'${OPENVIM_BASEFOLDER}' folder exist" >&2 && exit 1
295 fi
296 fi
297
298
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.1"
302 fi
303
304 echo '
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"
313
314 if db_exists "vim_db" $TEMPFILE ; then
315 if [[ -n $FORCEDB ]]; then
316 echo " Deleting previous database vim_db"
317 DBDELETEPARAM=""
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'"
327 else
328 echo "Database exists. Use option '--forcedb' to force the deletion of the existing one" && exit 1
329 fi
330 else
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'"
335 fi
336
337 echo '
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
342
343
344 if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ]
345 then
346 echo '
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" ]
352 then
353 #Creates a service file for openvim
354 echo '<?xml version="1.0" encoding="utf-8"?>
355 <service>
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
362 restorecon openvim
363 chmod 640 openvim
364 popd > /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
368 echo "done."
369 else
370 echo "skipping."
371 fi
372 fi
373
374 echo '
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" ]]
380 then
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'
396
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"
400 #then
401 # echo " inserting /home/$SUDO_USER/bin in the PATH at .bashrc"
402 # su $SUDO_USER -c 'echo "PATH=\$PATH:/home/\${USER}/bin" >> ~/.bashrc'
403 #fi
404
405 if [[ $SUDO_USER == root ]]
406 then
407 if ! echo $PATH | grep -q "${HOME}/bin"
408 then
409 echo "PATH=\$PATH:\${HOME}/bin" >> ${HOME}/.bashrc
410 fi
411 fi
412 fi
413
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
420 then
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'
423 fi
424
425
426
427 if [[ -n "$INSTALL_AS_A_SERVICE" ]]
428 then
429 echo '
430 #################################################################
431 ##### CONFIGURE OPENVIM SERVICE #####
432 #################################################################'
433
434 ${OPENVIM_BASEFOLDER}/scripts/install-openvim-service.sh -f ${OPENVIM_BASEFOLDER} && `[[ -z "$NOCLONE" ]] && echo "-d"`
435 # alias service-openvim="service openvim"
436 # echo 'alias service-openvim="service openvim"' >> ${HOME}/.bashrc
437
438 echo
439 echo
440 echo "Done! installed at /opt/openvim"
441 echo " Manage server with 'service openvim start|stop|status|...' "
442
443
444 else
445
446 echo
447 echo "Done! you may need to logout and login again for loading client configuration"
448 echo " Run 'service-openvim start' for starting openvim in a screen"
449
450 fi