Install ovim library as part of install-openmano
[osm/RO.git] / scripts / install-openmano.sh
index 4a2ff72..e40b570 100755 (executable)
 # contact with: nfvlabs@tid.es
 ##
 
 # contact with: nfvlabs@tid.es
 ##
 
-#ONLY TESTED in Ubuntu 16.04
+#ONLY TESTED in Ubuntu 16.04   partially tested in Ubuntu 14.10 14.04 16.04, CentOS7 and RHEL7
 #Get needed packages, source code and configure to run openmano
 #Ask for database user and password if not provided
 #Get needed packages, source code and configure to run openmano
 #Ask for database user and password if not provided
-#        $1: database user
-#        $2: database password 
 
 function usage(){
     echo -e "usage: sudo $0 [OPTIONS]"
 
 function usage(){
     echo -e "usage: sudo $0 [OPTIONS]"
@@ -37,8 +35,10 @@ function usage(){
     echo -e "     -q --quiet: install in unattended mode"
     echo -e "     -h --help:  show this help"
     echo -e "     --develop:  install last version for developers, and do not configure as a service"
     echo -e "     -q --quiet: install in unattended mode"
     echo -e "     -h --help:  show this help"
     echo -e "     --develop:  install last version for developers, and do not configure as a service"
-    echo -e "     --forcedb:  reinstall mano_db DB, deleting previous database and creating a new one"
+    echo -e "     --forcedb:  reinstall mano_db DB, deleting previous database if exists and creating a new one"
+    echo -e "     --force:    makes idenpotent, delete previous installations folders if needed"
     echo -e "     --noclone:  assumes that openmano was cloned previously and that this script is run from the local repo"
     echo -e "     --noclone:  assumes that openmano was cloned previously and that this script is run from the local repo"
+    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"
 }
 
 function install_packages(){
 }
 
 function install_packages(){
@@ -70,13 +70,16 @@ function db_exists() {
 }
 
 GIT_URL=https://osm.etsi.org/gerrit/osm/RO.git
 }
 
 GIT_URL=https://osm.etsi.org/gerrit/osm/RO.git
+GIT_OVIM_URL=https://osm.etsi.org/gerrit/osm/openvim.git
 DBUSER="root"
 DBPASSWD=""
 DBPASSWD_PARAM=""
 QUIET_MODE=""
 DEVELOP=""
 FORCEDB=""
 DBUSER="root"
 DBPASSWD=""
 DBPASSWD_PARAM=""
 QUIET_MODE=""
 DEVELOP=""
 FORCEDB=""
+FORCE=""
 NOCLONE=""
 NOCLONE=""
+NO_PACKAGES=""
 while getopts ":u:p:hiq-:" o; do
     case "${o}" in
         u)
 while getopts ":u:p:hiq-:" o; do
     case "${o}" in
         u)
@@ -97,8 +100,10 @@ while getopts ":u:p:hiq-:" o; do
             [ "${OPTARG}" == "help" ] && usage && exit 0
             [ "${OPTARG}" == "develop" ] && DEVELOP="y" && continue
             [ "${OPTARG}" == "forcedb" ] && FORCEDB="y" && continue
             [ "${OPTARG}" == "help" ] && usage && exit 0
             [ "${OPTARG}" == "develop" ] && DEVELOP="y" && continue
             [ "${OPTARG}" == "forcedb" ] && FORCEDB="y" && continue
+            [ "${OPTARG}" == "force" ]   && FORCEDB="y" && FORCE="y" && continue
             [ "${OPTARG}" == "noclone" ] && NOCLONE="y" && continue
             [ "${OPTARG}" == "quiet" ] && export QUIET_MODE=yes && export DEBIAN_FRONTEND=noninteractive && continue
             [ "${OPTARG}" == "noclone" ] && NOCLONE="y" && continue
             [ "${OPTARG}" == "quiet" ] && export QUIET_MODE=yes && export DEBIAN_FRONTEND=noninteractive && continue
+            [ "${OPTARG}" == "no-install-packages" ] && export NO_PACKAGES=yes && continue
             echo -e "Invalid option: '--$OPTARG'\nTry $0 --help for more information" >&2 
             exit 1
             ;; 
             echo -e "Invalid option: '--$OPTARG'\nTry $0 --help for more information" >&2 
             exit 1
             ;; 
@@ -164,8 +169,25 @@ else  #[ "$_DISTRO" != "Ubuntu" -a "$_DISTRO" != "CentOS" -a "$_DISTRO" != "Red"
     [[ -z $QUIET_MODE ]] && [[ "$KK" != "y" ]] && [[ "$KK" != "yes" ]] && echo "Cancelled" && exit 1
 fi
 
     [[ -z $QUIET_MODE ]] && [[ "$KK" != "y" ]] && [[ "$KK" != "yes" ]] && echo "Cancelled" && exit 1
 fi
 
+#check if installed as a service
+INSTALL_AS_A_SERVICE=""
+[[ "$_DISTRO" == "Ubuntu" ]] &&  [[ ${_RELEASE%%.*} == 16 ]] && [[ -z $DEVELOP ]] && INSTALL_AS_A_SERVICE="y"
+#Next operations require knowing OPENMANO_BASEFOLDER
+if [[ -z "$NOCLONE" ]]; then
+    if [[ -n "$INSTALL_AS_A_SERVICE" ]] ; then
+        OPENMANO_BASEFOLDER=__openmano__${RANDOM}
+    else
+        OPENMANO_BASEFOLDER="${PWD}/openmano"
+    fi
+    [[ -n "$FORCE" ]] && rm -rf $OPENMANO_BASEFOLDER #make idempotent
+else
+    HERE=$(realpath $(dirname $0))
+    OPENMANO_BASEFOLDER=$(dirname $HERE)
+fi
 
 
 
 
+if [[ -z "$NO_PACKAGES" ]]
+then
 echo '
 #################################################################
 #####        UPDATE REPOSITORIES                            #####
 echo '
 #################################################################
 #####        UPDATE REPOSITORIES                            #####
@@ -178,13 +200,16 @@ echo '
   && sudo rpm -ivh epel-release-7-5.noarch.rpm && sudo yum install -y epel-release && rm -f epel-release-7-5.noarch.rpm
 [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && sudo yum repolist
 
   && sudo rpm -ivh epel-release-7-5.noarch.rpm && sudo yum install -y epel-release && rm -f epel-release-7-5.noarch.rpm
 [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && sudo yum repolist
 
+fi
 
 
+if [[ -z "$NO_PACKAGES" ]]
+then
 echo '
 #################################################################
 #####               INSTALL REQUIRED PACKAGES               #####
 #################################################################'
 echo '
 #################################################################
 #####               INSTALL REQUIRED PACKAGES               #####
 #################################################################'
-[ "$_DISTRO" == "Ubuntu" ] && install_packages "git screen wget mysql-server"
-[ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && install_packages "git screen wget mariadb mariadb-server"
+[ "$_DISTRO" == "Ubuntu" ] && install_packages "git make screen wget mysql-server"
+[ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && install_packages "git make screen wget mariadb mariadb-server"
 
 if [[ "$_DISTRO" == "Ubuntu" ]]
 then
 
 if [[ "$_DISTRO" == "Ubuntu" ]]
 then
@@ -210,6 +235,7 @@ then
         firewall-cmd --permanent --zone=public --add-service=https &&
         firewall-cmd --reload
 fi
         firewall-cmd --permanent --zone=public --add-service=https &&
         firewall-cmd --reload
 fi
+fi  #[[ -z "$NO_PACKAGES" ]]
 
 #check and ask for database user password. Must be done after database installation
 if [[ -n $QUIET_MODE ]]
 
 #check and ask for database user password. Must be done after database installation
 if [[ -n $QUIET_MODE ]]
@@ -228,29 +254,54 @@ then
     done
 fi
 
     done
 fi
 
+if [[ -z "$NO_PACKAGES" ]]
+then
 echo '
 #################################################################
 #####        INSTALL PYTHON PACKAGES                        #####
 #################################################################'
 echo '
 #################################################################
 #####        INSTALL PYTHON PACKAGES                        #####
 #################################################################'
-[ "$_DISTRO" == "Ubuntu" ] && install_packages "python-yaml python-bottle python-mysqldb python-jsonschema python-paramiko python-argcomplete python-requests python-logutils"
-[ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && install_packages "PyYAML MySQL-python python-jsonschema python-paramiko python-argcomplete python-requests python-logutils"
-
+[ "$_DISTRO" == "Ubuntu" ] && install_packages "python-yaml python-bottle python-mysqldb python-jsonschema python-paramiko python-argcomplete python-requests python-logutils libxml2-dev libxslt-dev python-dev python-pip"
+[ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && install_packages "PyYAML MySQL-python python-jsonschema python-paramiko python-argcomplete python-requests python-logutils libxslt-devel libxml2-devel python-devel python-pip"
 #The only way to install python-bottle on Centos7 is with easy_install or pip
 [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && easy_install -U bottle
 
 #The only way to install python-bottle on Centos7 is with easy_install or pip
 [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && easy_install -U bottle
 
+#required for vmware connector TODO move that to separete opt in install script
+sudo pip install --upgrade pip
+sudo pip install pyvcloud
+sudo pip install progressbar
+sudo pip install prettytable
+sudo pip install pyvmomi
+
+#requiered for AWS connector
+[ "$_DISTRO" == "Ubuntu" ] && install_packages "python-boto"
+[ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && install_packages "python-boto"  #TODO check if at Centos it exist with this name, or PIP should be used
+
 #install openstack client needed for using openstack as a VIM
 #install openstack client needed for using openstack as a VIM
-[ "$_DISTRO" == "Ubuntu" ] && install_packages "python-novaclient python-keystoneclient python-glanceclient python-neutronclient"
-[ "$_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
+[ "$_DISTRO" == "Ubuntu" ] && install_packages "python-novaclient python-keystoneclient python-glanceclient python-neutronclient python-cinderclient"
+[ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && install_packages "python-devel" && easy_install python-novaclient python-keystoneclient python-glanceclient python-neutronclient python-cinderclient #TODO revise if gcc python-pip is needed
+fi  #[[ -z "$NO_PACKAGES" ]]
 
 if [[ -z $NOCLONE ]]; then
     echo '
 #################################################################
 #####        DOWNLOAD SOURCE                                #####
 #################################################################'
 
 if [[ -z $NOCLONE ]]; then
     echo '
 #################################################################
 #####        DOWNLOAD SOURCE                                #####
 #################################################################'
-    su $SUDO_USER -c 'git clone '"${GIT_URL}"' openmano'
-    #[[ -z $DEVELOP ]] && su $SUDO_USER -c 'git checkout <tag version>'
+    su $SUDO_USER -c "git clone ${GIT_URL} ${OPENMANO_BASEFOLDER}"
+    su $SUDO_USER -c "cp ${OPENMANO_BASEFOLDER}/.gitignore-common ${OPENMANO_BASEFOLDER}/.gitignore"
+    [[ -z $DEVELOP ]] && su $SUDO_USER -c "git -C ${OPENMANO_BASEFOLDER} checkout tags/v1.1.0"
 fi
 
 fi
 
+echo '
+#################################################################
+#####        INSTALLING OVIM LIBRARY                        #####
+#################################################################'
+su $SUDO_USER -c "git -C ${OPENMANO_BASEFOLDER} clone ${GIT_OVIM_URL} openvim"
+[[ -z $DEVELOP ]] && su $SUDO_USER -c "git -C ${OPENMANO_BASEFOLDER}/openvim checkout master"
+# Install debian dependencies before setup.py
+#[ "$_DISTRO" == "Ubuntu" ] && install_packages "git"
+#[ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && install_packages "git"
+make -C ${OPENMANO_BASEFOLDER}/openvim lite
+
 echo '
 #################################################################
 #####        CREATE DATABASE                                #####
 echo '
 #################################################################
 #####        CREATE DATABASE                                #####
@@ -259,11 +310,7 @@ echo -e "\nCreating temporary file form MYSQL installation and initialization"
 TEMPFILE="$(mktemp -q --tmpdir "installopenmano.XXXXXX")"
 trap 'rm -f "$TEMPFILE"' EXIT
 chmod 0600 "$TEMPFILE"
 TEMPFILE="$(mktemp -q --tmpdir "installopenmano.XXXXXX")"
 trap 'rm -f "$TEMPFILE"' EXIT
 chmod 0600 "$TEMPFILE"
-cat >"$TEMPFILE" <<EOF
-[client]
-user=$DBUSER
-password=$DBPASSWD
-EOF
+echo -e "[client]\n user='$DBUSER'\n password='$DBPASSWD'">"$TEMPFILE"
 
 if db_exists "mano_db" $TEMPFILE ; then
     if [[ -n $FORCEDB ]]; then
 
 if db_exists "mano_db" $TEMPFILE ; then
     if [[ -n $FORCEDB ]]; then
@@ -289,21 +336,18 @@ else
 fi
 
 
 fi
 
 
-#Next operations require knowing OPENMANO_BASEFOLDER
-HERE=$(realpath $(dirname $0))
-if [[ -z $NOCLONE ]]; then
-    OPENMANO_BASEFOLDER="${HERE}/openmano"
-else
-    OPENMANO_BASEFOLDER=$(dirname $HERE)
-fi
-
-
 echo '
 #################################################################
 #####        INIT DATABASE                                  #####
 #################################################################'
 echo '
 #################################################################
 #####        INIT DATABASE                                  #####
 #################################################################'
-su $SUDO_USER -c "${OPENMANO_BASEFOLDER}"'/database_utils/init_mano_db.sh -u mano -p manopw -d mano_db' || ! echo "Failed while initializing database" || exit 1
+su $SUDO_USER -c "${OPENMANO_BASEFOLDER}/database_utils/init_mano_db.sh -u mano -p manopw -d mano_db" || ! echo "Failed while initializing main database" || exit 1
 
 
+echo '
+#################################################################
+#####        CREATE AND INIT MANO_VIM DATABASE              #####
+#################################################################'
+# Install mano_vim_db after setup
+su $SUDO_USER -c "${OPENMANO_BASEFOLDER}/openvim/database_utils/install-db-server.sh -U $DBUSER ${DBPASSWD_PARAM/p/P} -u mano -p manopw -d mano_vim_db" || ! echo "Failed while installing ovim database" || exit 1
 
 if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ]
 then
 
 if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ]
 then
@@ -340,29 +384,32 @@ echo '
 #################################################################
 #####             CONFIGURE OPENMANO CLIENT                 #####
 #################################################################'
 #################################################################
 #####             CONFIGURE OPENMANO CLIENT                 #####
 #################################################################'
-#creates a link at ~/bin
-su $SUDO_USER -c 'mkdir -p ${HOME}/bin'
-su $SUDO_USER -c 'rm -f ${HOME}/bin/openmano'
-su $SUDO_USER -c 'rm -f ${HOME}/bin/openmano-report'
-su $SUDO_USER -c 'rm -f ${HOME}/bin/service-openmano'
-su $SUDO_USER -c 'ln -s '${OPENMANO_BASEFOLDER}'/openmano ${HOME}/bin/openmano'
-su $SUDO_USER -c 'ln -s '${OPENMANO_BASEFOLDER}'/scripts/openmano-report.sh   ${HOME}/bin/openmano-report'
-su $SUDO_USER -c 'ln -s '${OPENMANO_BASEFOLDER}'/scripts/service-openmano.sh  ${HOME}/bin/service-openmano'
-
-#insert /home/<user>/bin in the PATH
-#skiped because normally this is done authomatically when ~/bin exist
-#if ! su $SUDO_USER -c 'echo $PATH' | grep -q "${HOME}/bin"
-#then
-#    echo "    inserting /home/$SUDO_USER/bin in the PATH at .bashrc"
-#    su $SUDO_USER -c 'echo "PATH=\$PATH:\${HOME}/bin" >> ~/.bashrc'
-#fi
-if [[ $SUDO_USER == root ]] 
+#creates a link at ~/bin if not configured as a service
+if [[ -z "$INSTALL_AS_A_SERVICE" ]]
 then
 then
-    if ! echo $PATH | grep -q "${HOME}/bin"
+    su $SUDO_USER -c 'mkdir -p ${HOME}/bin'
+    su $SUDO_USER -c 'rm -f ${HOME}/bin/openmano'
+    su $SUDO_USER -c 'rm -f ${HOME}/bin/openmano-report'
+    su $SUDO_USER -c 'rm -f ${HOME}/bin/service-openmano'
+    su $SUDO_USER -c "ln -s '${OPENMANO_BASEFOLDER}/openmano' "'${HOME}/bin/openmano'
+    su $SUDO_USER -c "ln -s '${OPENMANO_BASEFOLDER}/scripts/openmano-report.sh'   "'${HOME}/bin/openmano-report'
+    su $SUDO_USER -c "ln -s '${OPENMANO_BASEFOLDER}/scripts/service-openmano.sh'  "'${HOME}/bin/service-openmano'
+
+    #insert /home/<user>/bin in the PATH
+    #skiped because normally this is done authomatically when ~/bin exist
+    #if ! su $SUDO_USER -c 'echo $PATH' | grep -q "${HOME}/bin"
+    #then
+    #    echo "    inserting /home/$SUDO_USER/bin in the PATH at .bashrc"
+    #    su $SUDO_USER -c 'echo "PATH=\$PATH:\${HOME}/bin" >> ~/.bashrc'
+    #fi
+    if [[ $SUDO_USER == root ]] 
     then
     then
-        echo "PATH=\$PATH:\${HOME}/bin" >> ${HOME}/.bashrc
-    fi
-fi 
+        if ! echo $PATH | grep -q "${HOME}/bin"
+        then
+            echo "PATH=\$PATH:\${HOME}/bin" >> ${HOME}/.bashrc
+        fi
+    fi 
+fi
 
 #configure arg-autocomplete for this user
 #in case of minimal instalation this package is not installed by default
 
 #configure arg-autocomplete for this user
 #in case of minimal instalation this package is not installed by default
@@ -378,27 +425,28 @@ fi
 
 
 
 
 
 
-if [[ "$_DISTRO" == "Ubuntu" ]] &&  [[ ${_RELEASE%%.*} == 16 ]] && [[ -z $DEVELOP ]]
+if [[ -n "$INSTALL_AS_A_SERVICE"  ]]
 then
 echo '
 #################################################################
 #####             CONFIGURE OPENMANO SERVICE                #####
 #################################################################'
 
 then
 echo '
 #################################################################
 #####             CONFIGURE OPENMANO SERVICE                #####
 #################################################################'
 
-    ${OPENMANO_BASEFOLDER}/scripts/install-openmano-service.sh -f ${OPENMANO_BASEFOLDER} #-u $SUDO_USER
+    ${OPENMANO_BASEFOLDER}/scripts/install-openmano-service.sh -f ${OPENMANO_BASEFOLDER} `[[ -z "$NOCLONE" ]] && echo "-d"`
+#    rm -rf ${OPENMANO_BASEFOLDER}
 #    alias service-openmano="service openmano"
 #    echo 'alias service-openmano="service openmano"' >> ${HOME}/.bashrc
 
     echo
 #    alias service-openmano="service openmano"
 #    echo 'alias service-openmano="service openmano"' >> ${HOME}/.bashrc
 
     echo
-    echo "Done!  you may need to logout and login again for loading client configuration"
-    echo " Manage server with 'service openmano start|stop|status|...' "
+    echo "Done!  installed at /opt/openmano"
+    echo " Manage server with 'sudo service openmano start|stop|status|...' "
 
 
 else
 
     echo
     echo "Done!  you may need to logout and login again for loading client configuration"
 
 
 else
 
     echo
     echo "Done!  you may need to logout and login again for loading client configuration"
-    echo " Run './openmano/scripts/service-openmano.sh start' for starting openmano in a screen"
+    echo " Run './${OPENMANO_BASEFOLDER}/scripts/service-openmano.sh start' for starting openmano in a screen"
 
 fi
 
 
 fi