| tierno | f7aa8c4 | 2016-09-06 16:43:04 +0200 | [diff] [blame] | 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 openvim |
| 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 [db-user [db-passwd]]\n Install source code in ./openvim" |
| 32 | } |
| 33 | |
| 34 | function install_packages(){ |
| 35 | [ -x /usr/bin/apt-get ] && apt-get install -y $* |
| 36 | [ -x /usr/bin/yum ] && yum install -y $* |
| 37 | |
| 38 | #check properly installed |
| 39 | for PACKAGE in $* |
| 40 | do |
| 41 | PACKAGE_INSTALLED="no" |
| 42 | [ -x /usr/bin/apt-get ] && dpkg -l $PACKAGE &>> /dev/null && PACKAGE_INSTALLED="yes" |
| 43 | [ -x /usr/bin/yum ] && yum list installed $PACKAGE &>> /dev/null && PACKAGE_INSTALLED="yes" |
| 44 | if [ "$PACKAGE_INSTALLED" = "no" ] |
| 45 | then |
| 46 | echo "failed to install package '$PACKAGE'. Revise network connectivity and try again" |
| 47 | exit -1 |
| 48 | fi |
| 49 | done |
| 50 | } |
| 51 | |
| 52 | #check root privileges and non a root user behind |
| 53 | [ "$1" == "-h" -o "$1" == "--help" ] && usage && exit 0 |
| 54 | [ "$USER" != "root" ] && echo "Needed root privileges" >&2 && usage >&2 && exit -1 |
| 55 | [ -z "$SUDO_USER" -o "$SUDO_USER" = "root" ] && echo "Must be runned with sudo from a non root user" >&2 && usage >&2 && exit -1 |
| 56 | |
| 57 | |
| 58 | #Discover Linux distribution |
| 59 | #try redhat type |
| 60 | [ -f /etc/redhat-release ] && _DISTRO=$(cat /etc/redhat-release 2>/dev/null | cut -d" " -f1) |
| 61 | #if not assuming ubuntu type |
| 62 | [ -f /etc/redhat-release ] || _DISTRO=$(lsb_release -is 2>/dev/null) |
| 63 | if [ "$_DISTRO" == "Ubuntu" ] |
| 64 | then |
| 65 | _RELEASE="14" |
| 66 | if ! lsb_release -rs | grep -q "14." |
| 67 | then |
| 68 | read -e -p "WARNING! Not tested Ubuntu version. Continue assuming a '$_RELEASE' type? (y/N)" KK |
| 69 | [ "$KK" != "y" -a "$KK" != "yes" ] && echo "Cancelled" && exit 0 |
| 70 | fi |
| 71 | elif [ "$_DISTRO" == "CentOS" ] |
| 72 | then |
| 73 | _RELEASE="7" |
| 74 | if ! cat /etc/redhat-release | grep -q "7." |
| 75 | then |
| 76 | read -e -p "WARNING! Not tested CentOS version. Continue assuming a '_RELEASE' type? (y/N)" KK |
| 77 | [ "$KK" != "y" -a "$KK" != "yes" ] && echo "Cancelled" && exit 0 |
| 78 | fi |
| 79 | elif [ "$_DISTRO" == "Red" ] |
| 80 | then |
| 81 | _RELEASE="7" |
| 82 | if ! cat /etc/redhat-release | grep -q "7." |
| 83 | then |
| 84 | read -e -p "WARNING! Not tested Red Hat OS version. Continue assuming a '_RELEASE' type? (y/N)" KK |
| 85 | [ "$KK" != "y" -a "$KK" != "yes" ] && echo "Cancelled" && exit 0 |
| 86 | fi |
| 87 | else #[ "$_DISTRO" != "Ubuntu" -a "$_DISTRO" != "CentOS" -a "$_DISTRO" != "Red" ] |
| 88 | _DISTRO_DISCOVER=$_DISTRO |
| 89 | [ -x /usr/bin/apt-get ] && _DISTRO="Ubuntu" && _RELEASE="14" |
| 90 | [ -x /usr/bin/yum ] && _DISTRO="CentOS" && _RELEASE="7" |
| 91 | read -e -p "WARNING! Not tested Linux distribution '$_DISTRO_DISCOVER '. Continue assuming a '$_DISTRO $_RELEASE' type? (y/N)" KK |
| 92 | [ "$KK" != "y" -a "$KK" != "yes" ] && echo "Cancelled" && exit 0 |
| 93 | fi |
| 94 | |
| 95 | |
| 96 | |
| 97 | echo ' |
| 98 | ################################################################# |
| 99 | ##### UPDATE REPOSITORIES ##### |
| 100 | #################################################################' |
| 101 | [ "$_DISTRO" == "Ubuntu" ] && apt-get update -y |
| 102 | |
| 103 | [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && yum check-update -y |
| 104 | [ "$_DISTRO" == "CentOS" ] && sudo yum install -y epel-release |
| 105 | [ "$_DISTRO" == "Red" ] && wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm \ |
| 106 | && sudo rpm -ivh epel-release-7-5.noarch.rpm && sudo yum install -y epel-release && rm -f epel-release-7-5.noarch.rpm |
| 107 | [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && sudo yum repolist |
| 108 | |
| 109 | |
| 110 | echo ' |
| 111 | ################################################################# |
| 112 | ##### INSTALL REQUIRED PACKAGES ##### |
| 113 | #################################################################' |
| 114 | [ "$_DISTRO" == "Ubuntu" ] && install_packages "mysql-server" |
| 115 | [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && install_packages "mariadb mariadb-server" |
| 116 | |
| 117 | if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] |
| 118 | then |
| 119 | #start services. By default CentOS does not start services |
| 120 | service mariadb start |
| 121 | service httpd start |
| 122 | systemctl enable mariadb |
| 123 | systemctl enable httpd |
| 124 | read -e -p "Do you want to configure mariadb (recomended if not done before) (Y/n)" KK |
| 125 | [ "$KK" != "n" -a "$KK" != "no" ] && mysql_secure_installation |
| 126 | |
| 127 | read -e -p "Do you want to set firewall to grant web access port 80,443 (Y/n)" KK |
| 128 | [ "$KK" != "n" -a "$KK" != "no" ] && |
| 129 | firewall-cmd --permanent --zone=public --add-service=http && |
| 130 | firewall-cmd --permanent --zone=public --add-service=https && |
| 131 | firewall-cmd --reload |
| 132 | fi |
| 133 | |
| 134 | #check and ask for database user password. Must be done after database instalation |
| 135 | [ -n "$1" ] && DBUSER=$1 |
| 136 | [ -z "$1" ] && DBUSER=root |
| 137 | [ -n "$2" ] && DBPASSWD="-p$2" |
| 138 | [ -z "$2" ] && DBPASSWD="" |
| 139 | echo -e "\nCheking database connection and ask for credentials" |
| 140 | while ! echo "" | mysql -u$DBUSER $DBPASSWD |
| 141 | do |
| 142 | [ -n "$logintry" ] && echo -e "\nInvalid database credentials!!!. Try again (Ctrl+c to abort)" |
| 143 | [ -z "$logintry" ] && echo -e "\nProvide database credentials" |
| 144 | read -e -p "database user? ($DBUSER) " DBUSER_ |
| 145 | [ -n "$DBUSER_" ] && DBUSER=$DBUSER_ |
| 146 | read -e -s -p "database password? (Enter for not using password) " DBPASSWD_ |
| 147 | [ -n "$DBPASSWD_" ] && DBPASSWD="-p$DBPASSWD_" |
| 148 | [ -z "$DBPASSWD_" ] && DBPASSWD="" |
| 149 | logintry="yes" |
| 150 | done |
| 151 | |
| 152 | echo ' |
| 153 | ################################################################# |
| 154 | ##### INSTALL PYTHON PACKAGES ##### |
| 155 | #################################################################' |
| 156 | [ "$_DISTRO" == "Ubuntu" ] && install_packages "python-yaml python-libvirt python-bottle python-mysqldb python-jsonschema python-paramiko python-argcomplete python-requests git screen wget" |
| 157 | [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && install_packages "PyYAML libvirt-python MySQL-python python-jsonschema python-paramiko python-argcomplete python-requests git screen wget" |
| 158 | |
| 159 | #The only way to install python-bottle on Centos7 is with easy_install or pip |
| 160 | [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && easy_install -U bottle |
| 161 | |
| 162 | echo ' |
| 163 | ################################################################# |
| 164 | ##### DOWNLOAD SOURCE ##### |
| 165 | #################################################################' |
| 166 | su $SUDO_USER -c 'git clone https://github.com/nfvlabs/openvim.git openvim' |
| 167 | #Unncoment to use a concrete branch, if not main branch |
| 168 | #pushd openvim |
| 169 | #su $SUDO_USER -c 'git checkout v0.4' |
| 170 | #popd |
| 171 | |
| 172 | echo ' |
| 173 | ################################################################# |
| 174 | ##### CREATE DATABASE ##### |
| 175 | #################################################################' |
| 176 | mysqladmin -u$DBUSER $DBPASSWD create vim_db |
| 177 | |
| 178 | echo "CREATE USER 'vim'@'localhost' identified by 'vimpw';" | mysql -u$DBUSER $DBPASSWD |
| 179 | echo "GRANT ALL PRIVILEGES ON vim_db.* TO 'vim'@'localhost';" | mysql -u$DBUSER $DBPASSWD |
| 180 | |
| 181 | echo "vim database" |
| 182 | su $SUDO_USER -c './openvim/database_utils/init_vim_db.sh -u vim -p vimpw' |
| 183 | |
| 184 | |
| 185 | if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] |
| 186 | then |
| 187 | echo ' |
| 188 | ################################################################# |
| 189 | ##### CONFIGURE firewalld ##### |
| 190 | #################################################################' |
| 191 | read -e -p "Configure firewalld for openvimd port 9080? (Y/n)" KK |
| 192 | if [ "$KK" != "n" -a "$KK" != "no" ] |
| 193 | then |
| 194 | #Creates a service file for openvim |
| 195 | echo '<?xml version="1.0" encoding="utf-8"?> |
| 196 | <service> |
| 197 | <short>openvimd</short> |
| 198 | <description>openvimd service</description> |
| 199 | <port protocol="tcp" port="9080"/> |
| 200 | </service>' > /etc/firewalld/services/openvimd.xml |
| 201 | #put proper permissions |
| 202 | pushd /etc/firewalld/services > /dev/null |
| 203 | restorecon openvim |
| 204 | chmod 640 openvim |
| 205 | popd > /dev/null |
| 206 | #Add the openvim service to the default zone permanently and reload the firewall configuration |
| 207 | firewall-cmd --permanent --add-service=openvim > /dev/null |
| 208 | firewall-cmd --reload > /dev/null |
| 209 | echo "done." |
| 210 | else |
| 211 | echo "skipping." |
| 212 | fi |
| 213 | fi |
| 214 | |
| 215 | echo ' |
| 216 | ################################################################# |
| 217 | ##### CONFIGURE openvim CLIENTS ##### |
| 218 | #################################################################' |
| 219 | #creates a link at ~/bin |
| 220 | su $SUDO_USER -c 'mkdir -p ~/bin' |
| 221 | rm -f /home/${SUDO_USER}/bin/openvim |
| 222 | rm -f /home/${SUDO_USER}/bin/openflow |
| 223 | rm -f /home/${SUDO_USER}/bin/service-openvim |
| 224 | rm -f /home/${SUDO_USER}/bin/initopenvim |
| 225 | rm -f /home/${SUDO_USER}/bin/service-floodlight |
| 226 | rm -f /home/${SUDO_USER}/bin/service-opendaylight |
| 227 | rm -f /home/${SUDO_USER}/bin/get_dhcp_lease.sh |
| 228 | ln -s ${PWD}/openvim/openvim /home/${SUDO_USER}/bin/openvim |
| 229 | ln -s ${PWD}/openvim/openflow /home/${SUDO_USER}/bin/openflow |
| 230 | ln -s ${PWD}/openvim/scripts/service-openvim.sh /home/${SUDO_USER}/bin/service-openvim |
| 231 | ln -s ${PWD}/openvim/scripts/initopenvim.sh /home/${SUDO_USER}/bin/initopenvim |
| 232 | ln -s ${PWD}/openvim/scripts/service-floodlight.sh /home/${SUDO_USER}/bin/service-floodlight |
| 233 | ln -s ${PWD}/openvim/scripts/service-opendaylight.sh /home/${SUDO_USER}/bin/service-opendaylight |
| 234 | ln -s ${PWD}/openvim/scripts/get_dhcp_lease.sh /home/${SUDO_USER}/bin/get_dhcp_lease.sh |
| 235 | |
| 236 | #insert /home/<user>/bin in the PATH |
| 237 | #skiped because normally this is done authomatically when ~/bin exist |
| 238 | #if ! su $SUDO_USER -c 'echo $PATH' | grep -q "/home/${SUDO_USER}/bin" |
| 239 | #then |
| 240 | # echo " inserting /home/$SUDO_USER/bin in the PATH at .bashrc" |
| 241 | # su $SUDO_USER -c 'echo "PATH=\$PATH:/home/\${USER}/bin" >> ~/.bashrc' |
| 242 | #fi |
| 243 | |
| 244 | #configure arg-autocomplete for this user |
| 245 | #in case of minmal instalation this package is not installed by default |
| 246 | [[ "$_DISTRO" == "CentOS" || "$_DISTRO" == "Red" ]] && yum install -y bash-completion |
| 247 | #su $SUDO_USER -c 'mkdir -p ~/.bash_completion.d' |
| 248 | su $SUDO_USER -c 'activate-global-python-argcomplete --user' |
| 249 | if ! grep -q bash_completion.d/python-argcomplete.sh /home/${SUDO_USER}/.bashrc |
| 250 | then |
| 251 | echo " inserting .bash_completion.d/python-argcomplete.sh execution at .bashrc" |
| 252 | su $SUDO_USER -c 'echo ". /home/${USER}/.bash_completion.d/python-argcomplete.sh" >> ~/.bashrc' |
| 253 | fi |
| 254 | |
| 255 | echo |
| 256 | echo "Done! you may need to logout and login again for loading the configuration" |
| 257 | echo " Run './openvim/scripts/service-openvim.sh start' for starting openvim in a screen" |
| 258 | |
| 259 | |
| 260 | |