Enhance install-openvim script to add same options as in RO
[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 in Ubuntu 16.04 partially tested in 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 function usage(){
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"
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 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 " --forcedb: reinstall vim_db DB, deleting previous database if exists and creating a new one"
39 echo -e " --updatedb: do not reinstall vim_db DB if it exists, just update database"
40 echo -e " --force: makes idenpotent, delete previous installations folders if needed. It assumes --updatedb if --forcedb option is not provided"
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 install 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" >&2
59 exit 1
60 fi
61 done
62 }
63
64 function ask_user(){
65 # ask to the user and parse a response among 'y', 'yes', 'n' or 'no'. Case insensitive
66 # Params: $1 text to ask; $2 Action by default, can be 'y' for yes, 'n' for no, other or empty for not allowed
67 # Return: true(0) if user type 'yes'; false (1) if user type 'no'
68 read -e -p "$1" USER_CONFIRMATION
69 while true ; do
70 [ -z "$USER_CONFIRMATION" ] && [ "$2" == 'y' ] && return 0
71 [ -z "$USER_CONFIRMATION" ] && [ "$2" == 'n' ] && return 1
72 [ "${USER_CONFIRMATION,,}" == "yes" ] || [ "${USER_CONFIRMATION,,}" == "y" ] && return 0
73 [ "${USER_CONFIRMATION,,}" == "no" ] || [ "${USER_CONFIRMATION,,}" == "n" ] && return 1
74 read -e -p "Please type 'yes' or 'no': " USER_CONFIRMATION
75 done
76 }
77
78
79 GIT_URL=https://osm.etsi.org/gerrit/osm/openvim.git
80 DBUSER="root"
81 DBPASSWD=""
82 DBPASSWD_PARAM=""
83 QUIET_MODE=""
84 DEVELOP=""
85 DB_FORCE_UPDATE=""
86 UPDATEDB=""
87 FORCE=""
88 NOCLONE=""
89 NO_PACKAGES=""
90 NO_DB=""
91
92 while getopts ":u:p:hiq-:" o; do
93 case "${o}" in
94 u)
95 export DBUSER="$OPTARG"
96 ;;
97 p)
98 export DBPASSWD="$OPTARG"
99 export DBPASSWD_PARAM="-p$OPTARG"
100 ;;
101 q)
102 export QUIET_MODE=yes
103 export DEBIAN_FRONTEND=noninteractive
104 ;;
105 h)
106 usage && exit 0
107 ;;
108 -)
109 [ "${OPTARG}" == "help" ] && usage && exit 0
110 [ "${OPTARG}" == "develop" ] && DEVELOP="y" && continue
111 [ "${OPTARG}" == "forcedb" ] && DB_FORCE_UPDATE="${DB_FORCE_UPDATE}--forcedb" && continue
112 [ "${OPTARG}" == "updatedb" ] && DB_FORCE_UPDATE="${DB_FORCE_UPDATE}--updatedb" && continue
113 [ "${OPTARG}" == "force" ] && FORCE="y" && continue
114 [ "${OPTARG}" == "noclone" ] && NOCLONE="y" && continue
115 [ "${OPTARG}" == "quiet" ] && export QUIET_MODE=yes && export DEBIAN_FRONTEND=noninteractive && continue
116 [ "${OPTARG}" == "no-install-packages" ] && export NO_PACKAGES=yes && continue
117 [ "${OPTARG}" == "no-db" ] && NO_DB="y" && continue
118 echo -e "Invalid option: '--$OPTARG'\nTry $0 --help for more information" >&2
119 exit 1
120 ;;
121 \?)
122 echo -e "Invalid option: '-$OPTARG'\nTry $0 --help for more information" >&2
123 exit 1
124 ;;
125 :)
126 echo -e "Option '-$OPTARG' requires an argument\nTry $0 --help for more information" >&2
127 exit 1
128 ;;
129 *)
130 usage >&2
131 exit 1
132 ;;
133 esac
134 done
135
136 if [ "$DB_FORCE_UPDATE" == "--forcedb--updatedb" ] || [ "$DB_FORCE_UPDATE" == "--updatedb--forcedb" ] ; then
137 echo "Error: options --forcedb and --updatedb are mutually exclusive" >&2
138 exit 1
139 elif [ -n "$FORCE" ] && [ -z "$DB_FORCE_UPDATE" ] ; then
140 DB_FORCE_UPDATE="--updatedb"
141 fi
142
143 #check root privileges and non a root user behind
144 [ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit 1
145 if [[ -z "$SUDO_USER" ]] || [[ "$SUDO_USER" = "root" ]]
146 then
147 [[ -z $QUIET_MODE ]] && ! ask_user "Install in the root user (y/N)? " n && echo "Cancelled" && exit 1
148 export SUDO_USER=root
149 fi
150
151 # Discover Linux distribution
152 # try redhat type
153 [ -f /etc/redhat-release ] && _DISTRO=$(cat /etc/redhat-release 2>/dev/null | cut -d" " -f1)
154 # if not assuming ubuntu type
155 [ -f /etc/redhat-release ] || _DISTRO=$(lsb_release -is 2>/dev/null)
156 if [ "$_DISTRO" == "Ubuntu" ]
157 then
158 _RELEASE=$(lsb_release -rs)
159 if [[ ${_RELEASE%%.*} != 14 ]] && [[ ${_RELEASE%%.*} != 16 ]]
160 then
161 [[ -z $QUIET_MODE ]] &&
162 ! ask_user "WARNING! Not tested Ubuntu version. Continue assuming a trusty (14.XX)' (y/N)? " n &&
163 echo "Cancelled" && exit 1
164 _RELEASE = 14
165 fi
166 elif [ "$_DISTRO" == "CentOS" ]
167 then
168 _RELEASE="7"
169 if ! cat /etc/redhat-release | grep -q "7."
170 then
171 [[ -z $QUIET_MODE ]] &&
172 ! ask_user "WARNING! Not tested CentOS version. Continue assuming a '$_RELEASE' type (y/N)? " n &&
173 echo "Cancelled" && exit 1
174 fi
175 elif [ "$_DISTRO" == "Red" ]
176 then
177 _RELEASE="7"
178 if ! cat /etc/redhat-release | grep -q "7."
179 then
180 [[ -z $QUIET_MODE ]] &&
181 ! ask_user "WARNING! Not tested Red Hat OS version. Continue assuming a '$_RELEASE' type (y/N)? " n &&
182 echo "Cancelled" && exit 1
183 fi
184 else #[ "$_DISTRO" != "Ubuntu" -a "$_DISTRO" != "CentOS" -a "$_DISTRO" != "Red" ]
185 _DISTRO_DISCOVER=$_DISTRO
186 [ -x /usr/bin/apt-get ] && _DISTRO="Ubuntu" && _RELEASE="14"
187 [ -x /usr/bin/yum ] && _DISTRO="CentOS" && _RELEASE="7"
188 [[ -z $QUIET_MODE ]] &&
189 ! ask_user "WARNING! Not tested Linux distribution '$_DISTRO_DISCOVER '. Continue assuming a '$_DISTRO $_RELEASE' type (y/N)? " n &&
190 echo "Cancelled" && exit 1
191 fi
192
193 #check if installed as a service
194 INSTALL_AS_A_SERVICE=""
195 [[ "$_DISTRO" == "Ubuntu" ]] && [[ ${_RELEASE%%.*} == 16 ]] && [[ -z $DEVELOP ]] && INSTALL_AS_A_SERVICE="y"
196
197 # Next operations require knowing BASEFOLDER
198 if [[ -z "$NOCLONE" ]]; then
199 if [[ -n "$INSTALL_AS_A_SERVICE" ]] ; then
200 BASEFOLDER=__openvim__${RANDOM}
201 else
202 BASEFOLDER="${PWD}/openvim"
203 fi
204 [[ -n "$FORCE" ]] && rm -rf $BASEFOLDER #make idempotent
205 else
206 HERE=$(dirname $(readlink -f ${BASH_SOURCE[0]}))
207 BASEFOLDER=$(dirname $HERE)
208 fi
209
210 if [[ -z "$NO_PACKAGES" ]]
211 then
212 echo -e "\n"\
213 "#################################################################\n"\
214 "##### UPDATE REPOSITORIES #####\n"\
215 "#################################################################"
216 [ "$_DISTRO" == "Ubuntu" ] && apt-get update -y
217
218 [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && yum check-update -y
219 [ "$_DISTRO" == "CentOS" ] && sudo yum install -y epel-release
220 [ "$_DISTRO" == "Red" ] && wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm \
221 && sudo rpm -ivh epel-release-7-5.noarch.rpm && sudo yum install -y epel-release && rm -f epel-release-7-5.noarch.rpm
222 [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && sudo yum repolist
223
224 echo -e "\n"\
225 "#################################################################\n"\
226 "##### INSTALL REQUIRED PACKAGES #####\n"\
227 "#################################################################"
228 [ "$_DISTRO" == "Ubuntu" ] && install_packages "git make screen wget mysql-client"
229 [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && install_packages "git make screen wget mariadb-client"
230
231 echo -e "\n"\
232 "#################################################################\n"\
233 "##### INSTALL PYTHON PACKAGES #####\n"\
234 "#################################################################"
235 [ "$_DISTRO" == "Ubuntu" ] && install_packages "python-yaml python-libvirt python-bottle python-mysqldb python-jsonschema python-paramiko python-argcomplete python-requests"
236 [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && install_packages "PyYAML libvirt-python MySQL-python python-jsonschema python-paramiko python-argcomplete python-requests"
237 # The only way to install python-bottle on Centos7 is with easy_install or pip
238 [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && easy_install -U bottle
239
240 fi # [[ -z "$NO_PACKAGES" ]]
241
242 if [[ -z $NOCLONE ]]; then
243 echo -e "\n"\
244 "#################################################################\n"\
245 "##### DOWNLOAD SOURCE #####\n"\
246 "#################################################################"
247 if [[ -d "${BASEFOLDER}" ]] ; then
248 if [[ -n "$FORCE" ]] ; then
249 echo "deleting '${BASEFOLDER}' folder"
250 rm -rf "$BASEFOLDER" #make idempotent
251 elif [[ -z "$QUIET_MODE" ]] ; then
252 ! ask_user "folder '${BASEFOLDER}' exists, overwrite (y/N)? " n && echo "Cancelled!" && exit 1
253 rm -rf "$BASEFOLDER"
254 else
255 echo "'${BASEFOLDER}' folder exists. Use "--force" to overwrite" >&2 && exit 1
256 fi
257 fi
258 su $SUDO_USER -c "git clone ${GIT_URL} ${BASEFOLDER}"
259 su $SUDO_USER -c "cp ${BASEFOLDER}/.gitignore-common ${BASEFOLDER}/.gitignore"
260 [[ -z $DEVELOP ]] && su $SUDO_USER -c "git -C ${BASEFOLDER} checkout v2.0"
261 fi
262
263
264
265 if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ]
266 then
267 echo -e "\n"\
268 "#################################################################\n"\
269 "##### CONFIGURE firewalld #####\n"\
270 "#################################################################"
271 if [[ -z $QUIET_MODE ]] || ask_user "Configure firewalld for openvimd port 9080 (Y/n)? " y
272 then
273 #Creates a service file for openvim
274 echo '<?xml version="1.0" encoding="utf-8"?>
275 <service>
276 <short>openvimd</short>
277 <description>openvimd service</description>
278 <port protocol="tcp" port="9080"/>
279 </service>' > /etc/firewalld/services/openvimd.xml
280 #put proper permissions
281 pushd /etc/firewalld/services > /dev/null
282 restorecon openvim
283 chmod 640 openvim
284 popd > /dev/null
285 #Add the openvim service to the default zone permanently and reload the firewall configuration
286 firewall-cmd --permanent --add-service=openvim > /dev/null
287 firewall-cmd --reload > /dev/null
288 echo "done."
289 else
290 echo "skipping."
291 fi
292 fi
293
294 echo -e "\n"\
295 "#################################################################n"\
296 "##### CONFIGURE OPENVIM CLIENT #####n"\
297 "#################################################################"
298 #creates a link at ~/bin if not configured as a service
299 if [[ -z "$INSTALL_AS_A_SERVICE" ]]
300 then
301 su $SUDO_USER -c 'mkdir -p ${HOME}/bin'
302 su $SUDO_USER -c 'rm -f ${HOME}/bin/openvim'
303 su $SUDO_USER -c 'rm -f ${HOME}/bin/openflow'
304 su $SUDO_USER -c 'rm -f ${HOME}/bin/service-openvim'
305 su $SUDO_USER -c 'rm -f ${HOME}/bin/initopenvim'
306 su $SUDO_USER -c 'rm -f ${HOME}/bin/service-floodlight'
307 su $SUDO_USER -c 'rm -f ${HOME}/bin/service-opendaylight'
308 su $SUDO_USER -c 'rm -f ${HOME}/bin/get_dhcp_lease.sh'
309 su $SUDO_USER -c "ln -s '${BASEFOLDER}/openvim' "'${HOME}/bin/openvim'
310 su $SUDO_USER -c "ln -s '${BASEFOLDER}/openflow' "'${HOME}/bin/openflow'
311 su $SUDO_USER -c "ln -s '${BASEFOLDER}/scripts/service-openvim' "'${HOME}/bin/service-openvim'
312 su $SUDO_USER -c "ln -s '${BASEFOLDER}/scripts/initopenvim' "'${HOME}/bin/initopenvim'
313 su $SUDO_USER -c "ln -s '${BASEFOLDER}/scripts/service-floodlight' "'${HOME}/bin/service-floodlight'
314 su $SUDO_USER -c "ln -s '${BASEFOLDER}/scripts/service-opendaylight' "'${HOME}/bin/service-opendaylight'
315 su $SUDO_USER -c "ln -s '${BASEFOLDER}/scripts/get_dhcp_lease.sh' "'${HOME}/bin/get_dhcp_lease.sh'
316
317 #insert /home/<user>/bin in the PATH
318 #skiped because normally this is done authomatically when ~/bin exists
319 #if ! su $SUDO_USER -c 'echo $PATH' | grep -q "${HOME}/bin"
320 #then
321 # echo " inserting /home/$SUDO_USER/bin in the PATH at .bashrc"
322 # su $SUDO_USER -c 'echo "PATH=\$PATH:\${HOME}/bin" >> ~/.bashrc'
323 #fi
324
325 if [[ $SUDO_USER == root ]]
326 then
327 if ! echo $PATH | grep -q "${HOME}/bin"
328 then
329 echo "PATH=\$PATH:\${HOME}/bin" >> ${HOME}/.bashrc
330 fi
331 fi
332 fi
333
334 #configure arg-autocomplete for this user
335 #in case of minimal instalation this package is not installed by default
336 [[ "$_DISTRO" == "CentOS" || "$_DISTRO" == "Red" ]] && yum install -y bash-completion
337 #su $SUDO_USER -c 'mkdir -p ~/.bash_completion.d'
338 su $SUDO_USER -c 'activate-global-python-argcomplete --user'
339 if ! su $SUDO_USER -c 'grep -q bash_completion.d/python-argcomplete.sh ${HOME}/.bashrc'
340 then
341 echo " inserting .bash_completion.d/python-argcomplete.sh execution at .bashrc"
342 su $SUDO_USER -c 'echo ". ${HOME}/.bash_completion.d/python-argcomplete.sh" >> ~/.bashrc'
343 fi
344
345 if [ -z "$NO_DB" ]; then
346 echo -e "\n"\
347 "#################################################################"\n"\
348 "##### INSTALL DATABASE SERVER #####"\n"\
349 "#################################################################"
350
351 if [ -n "$QUIET_MODE" ]; then
352 DB_QUIET='-q'
353 fi
354 ${BASEFOLDER}/database_utils/install-db-server.sh -U $DBUSER ${DBPASSWD_PARAM/p/P} $DB_QUIET $DB_FORCE_UPDATE || exit 1
355 fi # [ -z "$NO_DB" ]
356
357 if [[ -n "$INSTALL_AS_A_SERVICE" ]]
358 then
359 echo -e "\n"\
360 "#################################################################"\n"\
361 "##### CONFIGURE OPENVIM SERVICE #####"\n"\
362 "#################################################################"
363
364 ${BASEFOLDER}/scripts/install-openvim-service.sh -f ${BASEFOLDER} `[[ -z "$NOCLONE" ]] && echo "-d"`
365 # rm -rf ${BASEFOLDER}
366 # alias service-openvim="service openvim"
367 # echo 'alias service-openvim="service openvim"' >> ${HOME}/.bashrc
368 echo
369 echo "Done! installed at /opt/openvim"
370 echo " Manage server with 'sudo service osm-openvim start|stop|status|...' "
371 else
372 echo
373 echo "Done! you may need to logout and login again for loading client configuration"
374 echo " Run './${BASEFOLDER}/scripts/service-openvim start' for starting openvim in a screen"
375 fi