Fix bug installing openvim and db generation
[osm/openvim.git] / scripts / install-openvim.sh
index 053b3e9..5f0867f 100755 (executable)
@@ -25,6 +25,8 @@
 #Get needed packages, source code and configure to run openvim
 #Ask for database user and password if not provided
 
 #Get needed packages, source code and configure to run openvim
 #Ask for database user and password if not provided
 
+
+
 function usage(){
     echo -e "usage: sudo $0 [OPTIONS]"
     echo -e "Install last stable source code in ./openvim and the needed packages"
 function usage(){
     echo -e "usage: sudo $0 [OPTIONS]"
     echo -e "Install last stable source code in ./openvim and the needed packages"
@@ -35,7 +37,10 @@ function usage(){
     echo -e "     -q --quiet: install in an 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 an 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 "     --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"
+    echo -e "     --force:    makes idenpotent, delete previous installations folders if needed"
+    echo -e "     --noclone:  assumes that openvim 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"
+    echo -e "     --no-db: do not insall mysql server"
 }
 
 function install_packages(){
 }
 
 function install_packages(){
@@ -56,6 +61,16 @@ function install_packages(){
     done
 }
 
     done
 }
 
+function db_exists() {
+    RESULT=`mysqlshow --defaults-extra-file="$2" | grep -v Wildcard | grep -w $1`
+    if [ "$RESULT" == "$1" ]; then
+        echo " DB $1 exists"
+        return 0
+    fi
+    echo " DB $1 does not exist"
+    return 1
+}
+
 
 GIT_URL=https://osm.etsi.org/gerrit/osm/openvim.git
 DBUSER="root"
 
 GIT_URL=https://osm.etsi.org/gerrit/osm/openvim.git
 DBUSER="root"
@@ -63,7 +78,11 @@ DBPASSWD=""
 DBPASSWD_PARAM=""
 QUIET_MODE=""
 DEVELOP=""
 DBPASSWD_PARAM=""
 QUIET_MODE=""
 DEVELOP=""
+FORCE=""
+NOCLONE=""
 NO_PACKAGES=""
 NO_PACKAGES=""
+NO_DB=""
+
 while getopts ":u:p:hiq-:" o; do
     case "${o}" in
         u)
 while getopts ":u:p:hiq-:" o; do
     case "${o}" in
         u)
@@ -83,8 +102,11 @@ while getopts ":u:p:hiq-:" o; do
         -)
             [ "${OPTARG}" == "help" ] && usage && exit 0
             [ "${OPTARG}" == "develop" ] && DEVELOP="y" && continue
         -)
             [ "${OPTARG}" == "help" ] && usage && exit 0
             [ "${OPTARG}" == "develop" ] && DEVELOP="y" && continue
+            [ "${OPTARG}" == "force" ]   && FORCE="y" && continue
+            [ "${OPTARG}" == "noclone" ] && NOCLONE="y" && continue
             [ "${OPTARG}" == "quiet" ] && export QUIET_MODE=yes && export DEBIAN_FRONTEND=noninteractive && continue
             [ "${OPTARG}" == "quiet" ] && export QUIET_MODE=yes && export DEBIAN_FRONTEND=noninteractive && continue
-            [ "${OPTARG}" == "skip-install-packages" ] && export NO_PACKAGES=yes && continue
+            [ "${OPTARG}" == "no-install-packages" ] && export NO_PACKAGES=yes && continue
+            [ "${OPTARG}" == "no-db" ] && NO_DB="y" && 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
             ;;
@@ -150,6 +172,23 @@ else  #[ "$_DISTRO" != "Ubuntu" -a "$_DISTRO" != "CentOS" -a "$_DISTRO" != "Red"
     [ "$KK" != "y" -a  "$KK" != "yes" ] && echo "Cancelled" && exit 0
 fi
 
     [ "$KK" != "y" -a  "$KK" != "yes" ] && echo "Cancelled" && exit 0
 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 BASEFOLDER
+if [[ -z "$NOCLONE" ]]; then
+    if [[ -n "$INSTALL_AS_A_SERVICE" ]] ; then
+        OPENVIM_BASEFOLDER=__openvim__${RANDOM}
+    else
+        OPENVIM_BASEFOLDER="${PWD}/openvim"
+    fi
+    [[ -n "$FORCE" ]] && rm -rf $OPENVIM_BASEFOLDER #make idempotent
+else
+    HERE=$(dirname $(readlink -f ${BASH_SOURCE[0]}))
+    OPENVIM_BASEFOLDER=$(dirname $HERE)
+fi
+
 
 if [[ -z "$NO_PACKAGES" ]]
 then
 
 if [[ -z "$NO_PACKAGES" ]]
 then
@@ -205,7 +244,7 @@ fi  #[[ -z "$NO_PACKAGES" ]]
 if [[ -n $QUIET_MODE ]]
 then
     echo -e "\nCheking database connection and ask for credentials"
 if [[ -n $QUIET_MODE ]]
 then
     echo -e "\nCheking database connection and ask for credentials"
-    while ! mysqladmin -s -u$DBUSER $DBPASSWD_PARAM ping
+    while ! mysqladmin -s -u$DBUSER $DBPASSWD_PARAM status >/dev/null
     do
         [ -n "$logintry" ] &&  echo -e "\nInvalid database credentials!!!. Try again (Ctrl+c to abort)"
         [ -z "$logintry" ] &&  echo -e "\nProvide database credentials"
     do
         [ -n "$logintry" ] &&  echo -e "\nInvalid database credentials!!!. Try again (Ctrl+c to abort)"
         [ -z "$logintry" ] &&  echo -e "\nProvide database credentials"
@@ -220,6 +259,7 @@ fi
 
 if [[ -z "$NO_PACKAGES" ]]
 then
 
 if [[ -z "$NO_PACKAGES" ]]
 then
+
 echo '
 #################################################################
 #####               INSTALL PYTHON PACKAGES                 #####
 echo '
 #################################################################
 #####               INSTALL PYTHON PACKAGES                 #####
@@ -232,30 +272,46 @@ echo '
 
 fi  #[[ -z "$NO_PACKAGES" ]]
 
 
 fi  #[[ -z "$NO_PACKAGES" ]]
 
-echo '
+if [[ -z $NOCLONE ]]; then
+    echo '
 #################################################################
 #####                 DOWNLOAD SOURCE                       #####
 #################################################################'
 #################################################################
 #####                 DOWNLOAD SOURCE                       #####
 #################################################################'
-su $SUDO_USER -c 'git clone '"${GIT_URL}"' openvim'
-#[[ -z $DEVELOP ]] && su $SUDO_USER -c 'git checkout <tag version>'
-
-#Unncoment to use a concrete branch, if not main branch 
-#pushd openvim
-#su $SUDO_USER -c 'git checkout v0.4'
-#popd
+    if [[ -d "${OPENVIM_BASEFOLDER}" ]]
+    then
+        if [[ -n "$FORCE" ]]
+        then
+            echo "deleting '${OPENVIM_BASEFOLDER}' folder"
+            rm -rf "$OPENVIM_BASEFOLDER" #make idempotent
+        elif [[ -z "$QUIET_MODE" ]]
+        then
+            read -e -p "${OPENVIM_BASEFOLDER} folder exist, overwrite? (y/N)" KK
+            if [[ "$KK" == "y" ]] || [[ "$KK" == "yes" ]]
+                then rm -rf "$OPENVIM_BASEFOLDER"
+            else
+                echo "canceled"
+                exit 1
+            fi
+        else
+            echo "'${OPENVIM_BASEFOLDER}' folder exist" >&2 && exit 1
+        fi
+    fi
 
 
-echo '
-#################################################################
-#####               CREATE DATABASE                         #####
-#################################################################'
-mysqladmin -u$DBUSER $DBPASSWD_PARAM -s create vim_db || ! echo "Cannot create database vim_db" || exit 1
 
 
-echo "CREATE USER 'vim'@'localhost' identified by 'vimpw';"     | mysql -u$DBUSER $DBPASSWD_PARAM || ! echo "Failed while creating user vim at dabase" || exit 1
-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
-echo " Database 'vim_db' created, user 'vim' password 'vimpw'"
+    su $SUDO_USER -c "git clone ${GIT_URL} ${OPENVIM_BASEFOLDER}"
+    su $SUDO_USER -c "cp ${OPENVIM_BASEFOLDER}/.gitignore-common ${OPENVIM_BASEFOLDER}/.gitignore"
+    [[ -z $DEVELOP ]] && su $SUDO_USER -c "git -C  ${OPENVIM_BASEFOLDER} checkout v2.0"
+fi
 
 
+DB_QUIET=''
+if [ -z "$NO_DB" ]; then
+    if [ -n "$QUIET_MODE" ]; then
+        DB_QUIET='-q'
+    fi
 
 
-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
+    echo "!!!! install-db-server.sh: ${OPENVIM_BASEFOLDER}/database_utils/install-db-server.sh -U $DBUSER $DBPASSWD_PARAM $DB_QUIET"
+    ${OPENVIM_BASEFOLDER}/database_utils/install-db-server.sh -U $DBUSER $DBPASSWD_PARAM  $DB_QUIET  || exit 1
+fi
 
 
 if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ]
 
 
 if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ]
@@ -292,66 +348,70 @@ echo '
 #################################################################
 #####        CONFIGURE openvim CLIENT                       #####
 #################################################################'
 #################################################################
 #####        CONFIGURE openvim CLIENT                       #####
 #################################################################'
-#creates a link at ~/bin
-su $SUDO_USER -c 'mkdir -p ~/bin'
-su $SUDO_USER -c 'rm -f ${HOME}/bin/openvim'
-su $SUDO_USER -c 'rm -f ${HOME}/bin/openflow'
-su $SUDO_USER -c 'rm -f ${HOME}/bin/service-openvim'
-su $SUDO_USER -c 'rm -f ${HOME}/bin/initopenvim'
-su $SUDO_USER -c 'rm -f ${HOME}/bin/service-floodlight'
-su $SUDO_USER -c 'rm -f ${HOME}/bin/service-opendaylight'
-su $SUDO_USER -c 'rm -f ${HOME}/bin/get_dhcp_lease.sh'
-su $SUDO_USER -c 'ln -s ${PWD}/openvim/openvim   ${HOME}/bin/openvim'
-su $SUDO_USER -c 'ln -s ${PWD}/openvim/openflow  ${HOME}/bin/openflow'
-su $SUDO_USER -c 'ln -s ${PWD}/openvim/scripts/service-openvim.sh  ${HOME}/bin/service-openvim'
-su $SUDO_USER -c 'ln -s ${PWD}/openvim/scripts/initopenvim.sh  ${HOME}/bin/initopenvim'
-su $SUDO_USER -c 'ln -s ${PWD}/openvim/scripts/service-floodlight.sh  ${HOME}/bin/service-floodlight'
-su $SUDO_USER -c 'ln -s ${PWD}/openvim/scripts/service-opendaylight.sh  ${HOME}/bin/service-opendaylight'
-su $SUDO_USER -c 'ln -s ${PWD}/openvim/scripts/get_dhcp_lease.sh  ${HOME}/bin/get_dhcp_lease.sh'
-
-#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/${SUDO_USER}/bin"
-#then
-#    echo "    inserting /home/$SUDO_USER/bin in the PATH at .bashrc"
-#    su $SUDO_USER -c 'echo "PATH=\$PATH:/home/\${USER}/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 ~/bin'
+    su $SUDO_USER -c 'rm -f ${HOME}/bin/openvim'
+    su $SUDO_USER -c 'rm -f ${HOME}/bin/openflow'
+    su $SUDO_USER -c 'rm -f ${HOME}/bin/service-openvim'
+    su $SUDO_USER -c 'rm -f ${HOME}/bin/initopenvim'
+    su $SUDO_USER -c 'rm -f ${HOME}/bin/service-floodlight'
+    su $SUDO_USER -c 'rm -f ${HOME}/bin/service-opendaylight'
+    su $SUDO_USER -c 'rm -f ${HOME}/bin/get_dhcp_lease.sh'
+    su $SUDO_USER -c "ln -s '${OPENVIM_BASEFOLDER}/openvim'   "'${HOME}/bin/openvim'
+    su $SUDO_USER -c "ln -s '${OPENVIM_BASEFOLDER}/openflow'  "'${HOME}/bin/openflow'
+    su $SUDO_USER -c "ln -s '${OPENVIM_BASEFOLDER}/scripts/service-openvim'  "'${HOME}/bin/service-openvim'
+    su $SUDO_USER -c "ln -s '${OPENVIM_BASEFOLDER}/scripts/initopenvim'  "'${HOME}/bin/initopenvim'
+    su $SUDO_USER -c "ln -s '${OPENVIM_BASEFOLDER}/scripts/service-floodlight'  "'${HOME}/bin/service-floodlight'
+    su $SUDO_USER -c "ln -s '${OPENVIM_BASEFOLDER}/scripts/service-opendaylight'  "'${HOME}/bin/service-opendaylight'
+    su $SUDO_USER -c "ln -s '${OPENVIM_BASEFOLDER}/scripts/get_dhcp_lease.sh'  "'${HOME}/bin/get_dhcp_lease.sh'
+    
+    #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/${SUDO_USER}/bin"
+    #then
+    #    echo "    inserting /home/$SUDO_USER/bin in the PATH at .bashrc"
+    #    su $SUDO_USER -c 'echo "PATH=\$PATH:/home/\${USER}/bin" >> ~/.bashrc'
+    #fi
+    
+    if [[ $SUDO_USER == root ]]
     then
     then
-        echo "PATH=\$PATH:\${HOME}/bin" >> ${HOME}/.bashrc
+        if ! echo $PATH | grep -q "${HOME}/bin"
+        then
+            echo "PATH=\$PATH:\${HOME}/bin" >> ${HOME}/.bashrc
+        fi
     fi
 fi
 
     fi
 fi
 
-
 #configure arg-autocomplete for this user
 #in case of minmal instalation this package is not installed by default
 [[ "$_DISTRO" == "CentOS" || "$_DISTRO" == "Red" ]] && yum install -y bash-completion
 #su $SUDO_USER -c 'mkdir -p ~/.bash_completion.d'
 su $SUDO_USER -c 'activate-global-python-argcomplete --user'
 #configure arg-autocomplete for this user
 #in case of minmal instalation this package is not installed by default
 [[ "$_DISTRO" == "CentOS" || "$_DISTRO" == "Red" ]] && yum install -y bash-completion
 #su $SUDO_USER -c 'mkdir -p ~/.bash_completion.d'
 su $SUDO_USER -c 'activate-global-python-argcomplete --user'
-if ! grep -q bash_completion.d/python-argcomplete.sh /home/${SUDO_USER}/.bashrc
+if ! grep -q bash_completion.d/python-argcomplete.sh ${HOME}/.bashrc
 then
     echo "    inserting .bash_completion.d/python-argcomplete.sh execution at .bashrc"
 then
     echo "    inserting .bash_completion.d/python-argcomplete.sh execution at .bashrc"
-    su $SUDO_USER -c 'echo ". /home/${USER}/.bash_completion.d/python-argcomplete.sh" >> ~/.bashrc'
+    su $SUDO_USER -c 'echo ". ${HOME}/.bash_completion.d/python-argcomplete.sh" >> ~/.bashrc'
 fi
 
 
 
 fi
 
 
 
-if [[ "$_DISTRO" == "Ubuntu" ]] &&  [[ ${_RELEASE%%.*} == 16 ]] && [[ -z $DEVELOP ]]
+if [[ -n "$INSTALL_AS_A_SERVICE"  ]]
 then
 echo '
 #################################################################
 then
 echo '
 #################################################################
-#####             CONFIGURE OPENMANO SERVICE                #####
+#####             CONFIGURE OPENVIM SERVICE                 #####
 #################################################################'
 
 #################################################################'
 
-    ./openvim/scripts/install-openvim-service.sh -f openvim #-u $SUDO_USER
+    DELETE_PARAM="" && [[ -z "$NOCLONE" ]] && DELETE_PARAM="-d"
+    ${OPENVIM_BASEFOLDER}/scripts/install-openvim-service.sh -f ${OPENVIM_BASEFOLDER} ${DELETE_PARAM}
 #    alias service-openvim="service openvim"
 #    echo 'alias service-openvim="service openvim"' >> ${HOME}/.bashrc
 
     echo
 #    alias service-openvim="service openvim"
 #    echo 'alias service-openvim="service openvim"' >> ${HOME}/.bashrc
 
     echo
-    echo "Done!  you may need to logout and login again for loading client configuration"
+    echo
+    echo "Done!  installed at /opt/openvim"
     echo " Manage server with 'service openvim start|stop|status|...' "
 
 
     echo " Manage server with 'service openvim start|stop|status|...' "
 
 
@@ -359,6 +419,6 @@ else
 
     echo
     echo "Done!  you may need to logout and login again for loading client configuration"
 
     echo
     echo "Done!  you may need to logout and login again for loading client configuration"
-    echo " Run './openvim/scripts/service-openvim.sh start' for starting openvim in a screen"
+    echo " Run 'service-openvim start' for starting openvim in a screen"
 
 fi
 
 fi