Added openvim client at path when installing openvim service
[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 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 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"
39 }
40
41 function install_packages(){
42 [ -x /usr/bin/apt-get ] && apt-get install -y $*
43 [ -x /usr/bin/yum ] && yum install -y $*
44
45 #check properly installed
46 for PACKAGE in $*
47 do
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" ]
52 then
53 echo "failed to install package '$PACKAGE'. Revise network connectivity and try again"
54 exit -1
55 fi
56 done
57 }
58
59
60 GIT_URL=https://osm.etsi.org/gerrit/osm/openvim.git
61 DBUSER="root"
62 DBPASSWD=""
63 DBPASSWD_PARAM=""
64 QUIET_MODE=""
65 DEVELOP=""
66 NO_PACKAGES=""
67 while getopts ":u:p:hiq-:" o; do
68 case "${o}" in
69 u)
70 export DBUSER="$OPTARG"
71 ;;
72 p)
73 export DBPASSWD="$OPTARG"
74 export DBPASSWD_PARAM="-p$OPTARG"
75 ;;
76 q)
77 export QUIET_MODE=yes
78 export DEBIAN_FRONTEND=noninteractive
79 ;;
80 h)
81 usage && exit 0
82 ;;
83 -)
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
89 exit 1
90 ;;
91 \?)
92 echo -e "Invalid option: '-$OPTARG'\nTry $0 --help for more information" >&2
93 exit 1
94 ;;
95 :)
96 echo -e "Option '-$OPTARG' requires an argument\nTry $0 --help for more information" >&2
97 exit 1
98 ;;
99 *)
100 usage >&2
101 exit -1
102 ;;
103 esac
104 done
105
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" ]]
109 then
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
113 fi
114
115 #Discover Linux distribution
116 #try redhat type
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" ]
121 then
122 _RELEASE=$(lsb_release -rs)
123 if [[ ${_RELEASE%%.*} != 14 ]] && [[ ${_RELEASE%%.*} != 16 ]]
124 then
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
127 _RELEASE = 14
128 fi
129 elif [ "$_DISTRO" == "CentOS" ]
130 then
131 _RELEASE="7"
132 if ! cat /etc/redhat-release | grep -q "7."
133 then
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
136 fi
137 elif [ "$_DISTRO" == "Red" ]
138 then
139 _RELEASE="7"
140 if ! cat /etc/redhat-release | grep -q "7."
141 then
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
144 fi
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
151 fi
152
153
154 if [[ -z "$NO_PACKAGES" ]]
155 then
156 echo '
157 #################################################################
158 ##### UPDATE REPOSITORIES #####
159 #################################################################'
160 [ "$_DISTRO" == "Ubuntu" ] && apt-get update -y
161
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
167 fi
168
169 if [[ -z "$NO_PACKAGES" ]]
170 then
171 echo '
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"
177
178 if [[ "$_DISTRO" == "Ubuntu" ]]
179 then
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
184 fi
185
186 if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ]
187 then
188 #start services. By default CentOS does not start services
189 service mariadb start
190 service httpd 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
195
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
201 fi
202 fi #[[ -z "$NO_PACKAGES" ]]
203
204 #check and ask for database user password. Must be done after database installation
205 if [[ -n $QUIET_MODE ]]
206 then
207 echo -e "\nCheking database connection and ask for credentials"
208 while ! mysqladmin -s -u$DBUSER $DBPASSWD_PARAM ping
209 do
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=""
217 logintry="yes"
218 done
219 fi
220
221 if [[ -z "$NO_PACKAGES" ]]
222 then
223 echo '
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"
229
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
232
233 fi #[[ -z "$NO_PACKAGES" ]]
234
235 echo '
236 #################################################################
237 ##### DOWNLOAD SOURCE #####
238 #################################################################'
239 su $SUDO_USER -c 'git clone '"${GIT_URL}"' openvim'
240 #[[ -z $DEVELOP ]] && su $SUDO_USER -c 'git checkout <tag version>'
241
242 #Unncoment to use a concrete branch, if not main branch
243 #pushd openvim
244 #su $SUDO_USER -c 'git checkout v0.4'
245 #popd
246
247 echo '
248 #################################################################
249 ##### CREATE DATABASE #####
250 #################################################################'
251 mysqladmin -u$DBUSER $DBPASSWD_PARAM -s create vim_db || ! echo "Cannot create database vim_db" || exit 1
252
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'"
256
257
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
259
260
261 if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ]
262 then
263 echo '
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" ]
269 then
270 #Creates a service file for openvim
271 echo '<?xml version="1.0" encoding="utf-8"?>
272 <service>
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
279 restorecon openvim
280 chmod 640 openvim
281 popd > /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
285 echo "done."
286 else
287 echo "skipping."
288 fi
289 fi
290
291 echo '
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'
311
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"
315 #then
316 # echo " inserting /home/$SUDO_USER/bin in the PATH at .bashrc"
317 # su $SUDO_USER -c 'echo "PATH=\$PATH:/home/\${USER}/bin" >> ~/.bashrc'
318 #fi
319
320 if [[ $SUDO_USER == root ]]
321 then
322 if ! echo $PATH | grep -q "${HOME}/bin"
323 then
324 echo "PATH=\$PATH:\${HOME}/bin" >> ${HOME}/.bashrc
325 fi
326 fi
327
328
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
335 then
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'
338 fi
339
340
341
342 if [[ "$_DISTRO" == "Ubuntu" ]] && [[ ${_RELEASE%%.*} == 16 ]] && [[ -z $DEVELOP ]]
343 then
344 echo '
345 #################################################################
346 ##### CONFIGURE OPENVIM SERVICE #####
347 #################################################################'
348
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
352
353 echo
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|...' "
356
357
358 else
359
360 echo
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"
363
364 fi