blob: c4f05e05b888663bc4fecf16527b8273b5274a50 [file] [log] [blame]
garciadeblas16304ba2016-05-11 14:47:39 +02001#!/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
30function usage(){
tiernoc50e5da2016-07-06 17:49:45 +020031 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"
garciadeblas16304ba2016-05-11 14:47:39 +020038}
39
40function install_packages(){
41 [ -x /usr/bin/apt-get ] && apt-get install -y $*
tiernoc50e5da2016-07-06 17:49:45 +020042 [ -x /usr/bin/yum ] && yum install -y $*
garciadeblas16304ba2016-05-11 14:47:39 +020043
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
tiernoc50e5da2016-07-06 17:49:45 +020052 echo "failed to install package '$PACKAGE'. Revise network connectivity and try again" >&2
garciadeblas16304ba2016-05-11 14:47:39 +020053 exit -1
54 fi
55 done
56}
57
tiernoc50e5da2016-07-06 17:49:45 +020058DBUSER="root"
59DBPASSWD=""
60DBPASSWD_PARAM=""
61QUIET_MODE=""
62while 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
96done
garciadeblas16304ba2016-05-11 14:47:39 +020097
tiernoc50e5da2016-07-06 17:49:45 +020098#check root privileges and non a root user behind
99[ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit -1
100if [[ -z "$SUDO_USER" ]] || [[ "$SUDO_USER" = "root" ]]
101then
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
105fi
garciadeblas16304ba2016-05-11 14:47:39 +0200106
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)
112if [ "$_DISTRO" == "Ubuntu" ]
113then
114 _RELEASE="14"
115 if ! lsb_release -rs | grep -q "14."
116 then
tiernoc50e5da2016-07-06 17:49:45 +0200117 [[ -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
garciadeblas16304ba2016-05-11 14:47:39 +0200119 fi
120elif [ "$_DISTRO" == "CentOS" ]
121then
122 _RELEASE="7"
123 if ! cat /etc/redhat-release | grep -q "7."
124 then
tiernoc50e5da2016-07-06 17:49:45 +0200125 [[ -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
garciadeblas16304ba2016-05-11 14:47:39 +0200127 fi
128elif [ "$_DISTRO" == "Red" ]
129then
130 _RELEASE="7"
131 if ! cat /etc/redhat-release | grep -q "7."
132 then
tiernoc50e5da2016-07-06 17:49:45 +0200133 [[ -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
garciadeblas16304ba2016-05-11 14:47:39 +0200135 fi
136else #[ "$_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"
tiernoc50e5da2016-07-06 17:49:45 +0200140 [[ -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
garciadeblas16304ba2016-05-11 14:47:39 +0200142fi
143
144
145
146echo '
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
159echo '
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
tiernoc50e5da2016-07-06 17:49:45 +0200166if [[ "$_DISTRO" == "Ubuntu" ]]
167then
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
172fi
173
garciadeblas16304ba2016-05-11 14:47:39 +0200174if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ]
175then
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
189fi
190
191#check and ask for database user password. Must be done after database installation
tiernoc50e5da2016-07-06 17:49:45 +0200192if [[ -n $QUIET_MODE ]]
193then
194 echo -e "\nCheking database connection and ask for credentials"
195 while ! mysqladmin -s -u$DBUSER $DBPASSWD_PARAM ping
196 do
garciadeblas16304ba2016-05-11 14:47:39 +0200197 [ -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_
tiernoc50e5da2016-07-06 17:49:45 +0200202 [ -n "$DBPASSWD_" ] && DBPASSWD="$DBPASSWD_" && DBPASSWD_PARAM="-p$DBPASSWD_"
203 [ -z "$DBPASSWD_" ] && DBPASSWD="" && DBPASSWD_PARAM=""
garciadeblas16304ba2016-05-11 14:47:39 +0200204 logintry="yes"
tiernoc50e5da2016-07-06 17:49:45 +0200205 done
206fi
garciadeblas16304ba2016-05-11 14:47:39 +0200207
208echo '
209#################################################################
210##### INSTALL PYTHON PACKAGES #####
211#################################################################'
tiernoc50e5da2016-07-06 17:49:45 +0200212[ "$_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"
garciadeblas16304ba2016-05-11 14:47:39 +0200214
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
222echo '
223#################################################################
224##### DOWNLOAD SOURCE #####
225#################################################################'
226su $SUDO_USER -c 'git clone https://osm.etsi.org/gerrit/osm/openmano.git openmano'
227
228echo '
229#################################################################
230##### CREATE DATABASE #####
231#################################################################'
tiernoc50e5da2016-07-06 17:49:45 +0200232mysqladmin -u$DBUSER $DBPASSWD_PARAM -s create mano_db || exit 1
garciadeblas16304ba2016-05-11 14:47:39 +0200233
tiernoc50e5da2016-07-06 17:49:45 +0200234echo "CREATE USER 'mano'@'localhost' identified by 'manopw';" | mysql -u$DBUSER $DBPASSWD_PARAM -s || ! echo "Failed while creating user mano at dabase" || exit 1
235echo "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
236echo " Database 'mano_db' created, user 'mano' password 'manopw'"
garciadeblas16304ba2016-05-11 14:47:39 +0200237
tiernoc50e5da2016-07-06 17:49:45 +0200238su $SUDO_USER -c 'openmano/database_utils/init_mano_db.sh -u mano -p manopw' || ! echo "Failed while creating user mano at dabase" || exit 1
garciadeblas16304ba2016-05-11 14:47:39 +0200239
240if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ]
241then
242 echo '
243#################################################################
244##### CONFIGURE firewalld #####
245#################################################################'
tiernoc50e5da2016-07-06 17:49:45 +0200246 KK=yes
247 [[ -z $QUIET_MODE ]] && read -e -p "Configure firewalld for openmanod port 9090? (Y/n)" KK
garciadeblas16304ba2016-05-11 14:47:39 +0200248 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
269fi
270
271echo '
272#################################################################
273##### CONFIGURE OPENMANO CLIENT #####
274#################################################################'
275#creates a link at ~/bin
tiernoc50e5da2016-07-06 17:49:45 +0200276su $SUDO_USER -c 'mkdir -p ${HOME}/bin'
277su $SUDO_USER -c 'rm -f ${HOME}/bin/openmano'
278su $SUDO_USER -c 'rm -f ${HOME}/bin/service-openmano'
279su $SUDO_USER -c 'ln -s ${PWD}/openmano/openmano ${HOME}/bin/openmano'
280su $SUDO_USER -c 'ln -s '${PWD}'/openmano/scripts/openmano-report.sh ${HOME}/bin/openmano-report'
281su $SUDO_USER -c 'ln -s '${PWD}'/openmano/scripts/service-openmano.sh ${HOME}/bin/service-openmano'
garciadeblas16304ba2016-05-11 14:47:39 +0200282
283#insert /home/<user>/bin in the PATH
284#skiped because normally this is done authomatically when ~/bin exist
tiernoc50e5da2016-07-06 17:49:45 +0200285#if ! su $SUDO_USER -c 'echo $PATH' | grep -q "${HOME}/bin"
garciadeblas16304ba2016-05-11 14:47:39 +0200286#then
287# echo " inserting /home/$SUDO_USER/bin in the PATH at .bashrc"
tiernoc50e5da2016-07-06 17:49:45 +0200288# su $SUDO_USER -c 'echo "PATH=\$PATH:\${HOME}/bin" >> ~/.bashrc'
garciadeblas16304ba2016-05-11 14:47:39 +0200289#fi
tiernoc50e5da2016-07-06 17:49:45 +0200290if [[ $SUDO_USER == root ]]
291then
292 if ! echo $PATH | grep -q "${HOME}/bin"
293 then
294 echo "PATH=\$PATH:\${HOME}/bin" >> ${HOME}/.bashrc
295 fi
296fi
garciadeblas16304ba2016-05-11 14:47:39 +0200297
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'
302su $SUDO_USER -c 'activate-global-python-argcomplete --user'
tiernoc50e5da2016-07-06 17:49:45 +0200303if ! su $SUDO_USER -c 'grep -q bash_completion.d/python-argcomplete.sh ${HOME}/.bashrc'
garciadeblas16304ba2016-05-11 14:47:39 +0200304then
305 echo " inserting .bash_completion.d/python-argcomplete.sh execution at .bashrc"
tiernoc50e5da2016-07-06 17:49:45 +0200306 su $SUDO_USER -c 'echo ". ${HOME}/.bash_completion.d/python-argcomplete.sh" >> ~/.bashrc'
garciadeblas16304ba2016-05-11 14:47:39 +0200307fi
308
309echo
310echo "Done! you may need to logout and login again for loading the configuration"
311echo " Run './openmano/scripts/service-openmano.sh start' for starting openmano in a screen"
312
313
314