Changes in MANO DB init scripts to allow db_name as input parameter
[osm/RO.git] / scripts / install-openmano.sh
1 #!/bin/bash
2
3 ##
4 # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5 # This file is part of openmano
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, CentOS7 and RHEL7
25 #Get needed packages, source code and configure to run openmano
26 #Ask for database user and password if not provided
27 # $1: database user
28 # $2: database password
29
30 function usage(){
31 echo -e "usage: sudo $0 [OPTIONS]"
32 echo -e "Install source code in ./openmano and the needed packages"
33 echo -e " OPTIONS"
34 echo -e " -u USER database admin user. 'root' by default. Prompts if needed"
35 echo -e " -p PASS database admin password to be used or installed.Prompts if needed"
36 echo -e " -q: install in an unattended mode"
37 echo -e " -h: show this help"
38 }
39
40 function install_packages(){
41 [ -x /usr/bin/apt-get ] && apt-get install -y $*
42 [ -x /usr/bin/yum ] && yum install -y $*
43
44 #check properly installed
45 for PACKAGE in $*
46 do
47 PACKAGE_INSTALLED="no"
48 [ -x /usr/bin/apt-get ] && dpkg -l $PACKAGE &>> /dev/null && PACKAGE_INSTALLED="yes"
49 [ -x /usr/bin/yum ] && yum list installed $PACKAGE &>> /dev/null && PACKAGE_INSTALLED="yes"
50 if [ "$PACKAGE_INSTALLED" = "no" ]
51 then
52 echo "failed to install package '$PACKAGE'. Revise network connectivity and try again" >&2
53 exit -1
54 fi
55 done
56 }
57
58 DBUSER="root"
59 DBPASSWD=""
60 DBPASSWD_PARAM=""
61 QUIET_MODE=""
62 while getopts ":u:p:hiq-:" o; do
63 case "${o}" in
64 u)
65 export DBUSER="$OPTARG"
66 ;;
67 p)
68 export DBPASSWD="$OPTARG"
69 export DBPASSWD_PARAM="-p$OPTARG"
70 ;;
71 q)
72 export QUIET_MODE=yes
73 export DEBIAN_FRONTEND=noninteractive
74 ;;
75 h)
76 usage && exit 0
77 ;;
78 -)
79 [ "${OPTARG}" == "help" ] && usage && exit 0
80 echo -e "Invalid option: '--$OPTARG'\nTry $0 --help for more information" >&2
81 exit 1
82 ;;
83 \?)
84 echo -e "Invalid option: '-$OPTARG'\nTry $0 --help for more information" >&2
85 exit 1
86 ;;
87 :)
88 echo -e "Option '-$OPTARG' requires an argument\nTry $0 --help for more information" >&2
89 exit 1
90 ;;
91 *)
92 usage >&2
93 exit -1
94 ;;
95 esac
96 done
97
98 #check root privileges and non a root user behind
99 [ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit -1
100 if [[ -z "$SUDO_USER" ]] || [[ "$SUDO_USER" = "root" ]]
101 then
102 [[ -z $QUIET_MODE ]] && read -e -p "Install in the root user (y/N)?" KK
103 [[ -z $QUIET_MODE ]] && [[ "$KK" != "y" ]] && [[ "$KK" != "yes" ]] && echo "Cancelled" && exit 1
104 export SUDO_USER=root
105 fi
106
107 #Discover Linux distribution
108 #try redhat type
109 [ -f /etc/redhat-release ] && _DISTRO=$(cat /etc/redhat-release 2>/dev/null | cut -d" " -f1)
110 #if not assuming ubuntu type
111 [ -f /etc/redhat-release ] || _DISTRO=$(lsb_release -is 2>/dev/null)
112 if [ "$_DISTRO" == "Ubuntu" ]
113 then
114 _RELEASE="14"
115 if ! lsb_release -rs | grep -q "14."
116 then
117 [[ -z $QUIET_MODE ]] && read -e -p "WARNING! Not tested Ubuntu version. Continue assuming a '$_RELEASE' type? (y/N)" KK
118 [[ -z $QUIET_MODE ]] && [[ "$KK" != "y" ]] && [[ "$KK" != "yes" ]] && echo "Cancelled" && exit 1
119 fi
120 elif [ "$_DISTRO" == "CentOS" ]
121 then
122 _RELEASE="7"
123 if ! cat /etc/redhat-release | grep -q "7."
124 then
125 [[ -z $QUIET_MODE ]] && read -e -p "WARNING! Not tested CentOS version. Continue assuming a '_RELEASE' type? (y/N)" KK
126 [[ -z $QUIET_MODE ]] && [[ "$KK" != "y" ]] && [[ "$KK" != "yes" ]] && echo "Cancelled" && exit 1
127 fi
128 elif [ "$_DISTRO" == "Red" ]
129 then
130 _RELEASE="7"
131 if ! cat /etc/redhat-release | grep -q "7."
132 then
133 [[ -z $QUIET_MODE ]] && read -e -p "WARNING! Not tested Red Hat OS version. Continue assuming a '_RELEASE' type? (y/N)" KK
134 [[ -z $QUIET_MODE ]] && [[ "$KK" != "y" ]] && [[ "$KK" != "yes" ]] && echo "Cancelled" && exit 1
135 fi
136 else #[ "$_DISTRO" != "Ubuntu" -a "$_DISTRO" != "CentOS" -a "$_DISTRO" != "Red" ]
137 _DISTRO_DISCOVER=$_DISTRO
138 [ -x /usr/bin/apt-get ] && _DISTRO="Ubuntu" && _RELEASE="14"
139 [ -x /usr/bin/yum ] && _DISTRO="CentOS" && _RELEASE="7"
140 [[ -z $QUIET_MODE ]] && read -e -p "WARNING! Not tested Linux distribution '$_DISTRO_DISCOVER '. Continue assuming a '$_DISTRO $_RELEASE' type? (y/N)" KK
141 [[ -z $QUIET_MODE ]] && [[ "$KK" != "y" ]] && [[ "$KK" != "yes" ]] && echo "Cancelled" && exit 1
142 fi
143
144
145
146 echo '
147 #################################################################
148 ##### UPDATE REPOSITORIES #####
149 #################################################################'
150 [ "$_DISTRO" == "Ubuntu" ] && apt-get update -y
151
152 [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && yum check-update -y
153 [ "$_DISTRO" == "CentOS" ] && sudo yum install -y epel-release
154 [ "$_DISTRO" == "Red" ] && wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm \
155 && sudo rpm -ivh epel-release-7-5.noarch.rpm && sudo yum install -y epel-release && rm -f epel-release-7-5.noarch.rpm
156 [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && sudo yum repolist
157
158
159 echo '
160 #################################################################
161 ##### INSTALL REQUIRED PACKAGES #####
162 #################################################################'
163 [ "$_DISTRO" == "Ubuntu" ] && install_packages "git screen wget mysql-server"
164 [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && install_packages "git screen wget mariadb mariadb-server"
165
166 if [[ "$_DISTRO" == "Ubuntu" ]]
167 then
168 #start services. By default CentOS does not start services
169 service mysql start >> /dev/null
170 # try to set admin password, ignore if fails
171 [[ -n $DBPASSWD ]] && mysqladmin -u $DBUSER -s password $DBPASSWD
172 fi
173
174 if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ]
175 then
176 #start services. By default CentOS does not start services
177 service mariadb start
178 service httpd start
179 systemctl enable mariadb
180 systemctl enable httpd
181 read -e -p "Do you want to configure mariadb (recommended if not done before) (Y/n)" KK
182 [ "$KK" != "n" -a "$KK" != "no" ] && mysql_secure_installation
183
184 read -e -p "Do you want to set firewall to grant web access port 80,443 (Y/n)" KK
185 [ "$KK" != "n" -a "$KK" != "no" ] &&
186 firewall-cmd --permanent --zone=public --add-service=http &&
187 firewall-cmd --permanent --zone=public --add-service=https &&
188 firewall-cmd --reload
189 fi
190
191 #check and ask for database user password. Must be done after database installation
192 if [[ -n $QUIET_MODE ]]
193 then
194 echo -e "\nCheking database connection and ask for credentials"
195 while ! mysqladmin -s -u$DBUSER $DBPASSWD_PARAM ping
196 do
197 [ -n "$logintry" ] && echo -e "\nInvalid database credentials!!!. Try again (Ctrl+c to abort)"
198 [ -z "$logintry" ] && echo -e "\nProvide database credentials"
199 read -e -p "database user? ($DBUSER) " DBUSER_
200 [ -n "$DBUSER_" ] && DBUSER=$DBUSER_
201 read -e -s -p "database password? (Enter for not using password) " DBPASSWD_
202 [ -n "$DBPASSWD_" ] && DBPASSWD="$DBPASSWD_" && DBPASSWD_PARAM="-p$DBPASSWD_"
203 [ -z "$DBPASSWD_" ] && DBPASSWD="" && DBPASSWD_PARAM=""
204 logintry="yes"
205 done
206 fi
207
208 echo '
209 #################################################################
210 ##### INSTALL PYTHON PACKAGES #####
211 #################################################################'
212 [ "$_DISTRO" == "Ubuntu" ] && install_packages "python-yaml python-bottle python-mysqldb python-jsonschema python-paramiko python-argcomplete python-requests"
213 [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && install_packages "PyYAML MySQL-python python-jsonschema python-paramiko python-argcomplete python-requests"
214
215 #The only way to install python-bottle on Centos7 is with easy_install or pip
216 [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && easy_install -U bottle
217
218 #install openstack client needed for using openstack as a VIM
219 [ "$_DISTRO" == "Ubuntu" ] && install_packages "python-novaclient python-keystoneclient python-glanceclient python-neutronclient"
220 [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && install_packages "python-devel" && easy_install python-novaclient python-keystoneclient python-glanceclient python-neutronclient #TODO revise if gcc python-pip is needed
221
222 echo '
223 #################################################################
224 ##### DOWNLOAD SOURCE #####
225 #################################################################'
226 su $SUDO_USER -c 'git clone https://osm.etsi.org/gerrit/osm/openmano.git openmano'
227
228 echo '
229 #################################################################
230 ##### CREATE DATABASE #####
231 #################################################################'
232 mysqladmin -u$DBUSER $DBPASSWD_PARAM -s create mano_db || exit 1
233
234 echo "CREATE USER 'mano'@'localhost' identified by 'manopw';" | mysql -u$DBUSER $DBPASSWD_PARAM -s || ! echo "Failed while creating user mano at dabase" || exit 1
235 echo "GRANT ALL PRIVILEGES ON mano_db.* TO 'mano'@'localhost';" | mysql -u$DBUSER $DBPASSWD_PARAM -s || ! echo "Failed while creating user mano at dabase" || exit 1
236 echo " Database 'mano_db' created, user 'mano' password 'manopw'"
237
238 su $SUDO_USER -c 'openmano/database_utils/init_mano_db.sh -u mano -p manopw -d mano_db' || ! echo "Failed while creating user mano at dabase" || exit 1
239
240 if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ]
241 then
242 echo '
243 #################################################################
244 ##### CONFIGURE firewalld #####
245 #################################################################'
246 KK=yes
247 [[ -z $QUIET_MODE ]] && read -e -p "Configure firewalld for openmanod port 9090? (Y/n)" KK
248 if [ "$KK" != "n" -a "$KK" != "no" ]
249 then
250 #Creates a service file for openmano
251 echo '<?xml version="1.0" encoding="utf-8"?>
252 <service>
253 <short>openmanod</short>
254 <description>openmanod service</description>
255 <port protocol="tcp" port="9090"/>
256 </service>' > /etc/firewalld/services/openmanod.xml
257 #put proper permissions
258 pushd /etc/firewalld/services > /dev/null
259 restorecon openmanod.xml
260 chmod 640 openmanod.xml
261 popd > /dev/null
262 #Add the openmanod service to the default zone permanently and reload the firewall configuration
263 firewall-cmd --permanent --add-service=openmanod > /dev/null
264 firewall-cmd --reload > /dev/null
265 echo "done."
266 else
267 echo "skipping."
268 fi
269 fi
270
271 echo '
272 #################################################################
273 ##### CONFIGURE OPENMANO CLIENT #####
274 #################################################################'
275 #creates a link at ~/bin
276 su $SUDO_USER -c 'mkdir -p ${HOME}/bin'
277 su $SUDO_USER -c 'rm -f ${HOME}/bin/openmano'
278 su $SUDO_USER -c 'rm -f ${HOME}/bin/service-openmano'
279 su $SUDO_USER -c 'ln -s ${PWD}/openmano/openmano ${HOME}/bin/openmano'
280 su $SUDO_USER -c 'ln -s '${PWD}'/openmano/scripts/openmano-report.sh ${HOME}/bin/openmano-report'
281 su $SUDO_USER -c 'ln -s '${PWD}'/openmano/scripts/service-openmano.sh ${HOME}/bin/service-openmano'
282
283 #insert /home/<user>/bin in the PATH
284 #skiped because normally this is done authomatically when ~/bin exist
285 #if ! su $SUDO_USER -c 'echo $PATH' | grep -q "${HOME}/bin"
286 #then
287 # echo " inserting /home/$SUDO_USER/bin in the PATH at .bashrc"
288 # su $SUDO_USER -c 'echo "PATH=\$PATH:\${HOME}/bin" >> ~/.bashrc'
289 #fi
290 if [[ $SUDO_USER == root ]]
291 then
292 if ! echo $PATH | grep -q "${HOME}/bin"
293 then
294 echo "PATH=\$PATH:\${HOME}/bin" >> ${HOME}/.bashrc
295 fi
296 fi
297
298 #configure arg-autocomplete for this user
299 #in case of minimal instalation this package is not installed by default
300 [[ "$_DISTRO" == "CentOS" || "$_DISTRO" == "Red" ]] && yum install -y bash-completion
301 #su $SUDO_USER -c 'mkdir -p ~/.bash_completion.d'
302 su $SUDO_USER -c 'activate-global-python-argcomplete --user'
303 if ! su $SUDO_USER -c 'grep -q bash_completion.d/python-argcomplete.sh ${HOME}/.bashrc'
304 then
305 echo " inserting .bash_completion.d/python-argcomplete.sh execution at .bashrc"
306 su $SUDO_USER -c 'echo ". ${HOME}/.bash_completion.d/python-argcomplete.sh" >> ~/.bashrc'
307 fi
308
309 echo
310 echo "Done! you may need to logout and login again for loading the configuration"
311 echo " Run './openmano/scripts/service-openmano.sh start' for starting openmano in a screen"
312
313
314