Updated installer to use osm-ro service and to use service-openmano instead of servic...
[osm/RO.git] / scripts / install-openmano.sh
index db678d1..de8a892 100755 (executable)
@@ -36,9 +36,11 @@ function usage(){
     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 if exists and creating a new one"
     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 if exists and creating a new one"
-    echo -e "     --force:    makes idenpotent, delete previous installations folders if needed"
+    echo -e "     --updatedb: do not reinstall mano_db DB if exist, just update database"
+    echo -e "     --force:    makes idenpotent, delete previous installations folders if needed. It assumes --updatedb if --forcedb option is not provided"
     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"
     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"
+    echo -e "     --no-db: do not install mysql server"
 }
 
 function install_packages(){
 }
 
 function install_packages(){
@@ -59,26 +61,19 @@ function install_packages(){
     done
 }
 
     done
 }
 
-function db_exists() {
-    RESULT=`mysqlshow --defaults-extra-file="$2" | grep -v Wildcard | grep -o $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/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=""
 DBUSER="root"
 DBPASSWD=""
 DBPASSWD_PARAM=""
 QUIET_MODE=""
 DEVELOP=""
-FORCEDB=""
+DB_FORCE_UPDATE=""
+UPDATEDB=""
 FORCE=""
 NOCLONE=""
 NO_PACKAGES=""
 FORCE=""
 NOCLONE=""
 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)
@@ -98,11 +93,13 @@ 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}" == "forcedb" ] && FORCEDB="y" && continue
-            [ "${OPTARG}" == "force" ]   && FORCEDB="y" && FORCE="y" && continue
+            [ "${OPTARG}" == "forcedb" ] && DB_FORCE_UPDATE="${DB_FORCE_UPDATE}--forcedb" && continue
+            [ "${OPTARG}" == "updatedb" ] && DB_FORCE_UPDATE="${DB_FORCE_UPDATE}--updatedb" && continue
+            [ "${OPTARG}" == "force" ]   &&  FORCE="y" && 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
             [ "${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
+            [ "${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
             ;; 
@@ -121,6 +118,13 @@ while getopts ":u:p:hiq-:" o; do
     esac
 done
 
     esac
 done
 
+if [ "$DB_FORCE_UPDATE" == "--forcedb--updatedb" ] || [ "$DB_FORCE_UPDATE" == "--updatedb--forcedb" ] ; then
+    echo "Error: options --forcedb and --updatedb are mutually exclusive" >&2
+    exit 1
+elif [ -n "$FORCE" ] && [ -z "$DB_FORCE_UPDATE" ] ; then
+    DB_FORCE_UPDATE="--updatedb"
+fi
+
 #check root privileges and non a root user behind
 [ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit 1
 if [[ -z "$SUDO_USER" ]] || [[ "$SUDO_USER" = "root" ]]
 #check root privileges and non a root user behind
 [ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit 1
 if [[ -z "$SUDO_USER" ]] || [[ "$SUDO_USER" = "root" ]]
@@ -133,7 +137,7 @@ fi
 #Discover Linux distribution
 #try redhat type
 [ -f /etc/redhat-release ] && _DISTRO=$(cat /etc/redhat-release 2>/dev/null | cut  -d" " -f1) 
 #Discover Linux distribution
 #try redhat type
 [ -f /etc/redhat-release ] && _DISTRO=$(cat /etc/redhat-release 2>/dev/null | cut  -d" " -f1) 
-#if not assuming ubuntu type
+#else assuming ubuntu type
 [ -f /etc/redhat-release ] || _DISTRO=$(lsb_release -is  2>/dev/null)            
 if [ "$_DISTRO" == "Ubuntu" ]
 then
 [ -f /etc/redhat-release ] || _DISTRO=$(lsb_release -is  2>/dev/null)            
 if [ "$_DISTRO" == "Ubuntu" ]
 then
@@ -184,8 +188,7 @@ else
     OPENMANO_BASEFOLDER=$(dirname $HERE)
 fi
 
     OPENMANO_BASEFOLDER=$(dirname $HERE)
 fi
 
-
-if [[ -z "$NO_PACKAGES" ]]
+if [[ -z "$NO_PACKAGES" ]]  #if (UPDATE REPOS)
 then
 echo '
 #################################################################
 then
 echo '
 #################################################################
@@ -199,61 +202,19 @@ 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
+fi #if (UPDATE REPOS)
 
 
-if [[ -z "$NO_PACKAGES" ]]
+if [[ -z "$NO_PACKAGES" ]] #if (INSTALL DEPENDENCIES)
 then
 echo '
 #################################################################
 #####               INSTALL REQUIRED PACKAGES               #####
 #################################################################'
 then
 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-client"
+[ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && install_packages "git make screen wget mariadb-client"
+fi #if (INSTALL DEPENDENCIES)
 
 
-if [[ "$_DISTRO" == "Ubuntu" ]]
-then
-    #start services. By default CentOS does not start services
-    service mysql start >> /dev/null
-    # try to set admin password, ignore if fails
-    [[ -n $DBPASSWD ]] && mysqladmin -u $DBUSER -s password $DBPASSWD
-fi
-
-if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ]
-then
-    #start services. By default CentOS does not start services
-    service mariadb start
-    service httpd   start
-    systemctl enable mariadb
-    systemctl enable httpd
-    read -e -p "Do you want to configure mariadb (recommended if not done before) (Y/n)" KK
-    [ "$KK" != "n" -a  "$KK" != "no" ] && mysql_secure_installation
-
-    read -e -p "Do you want to set firewall to grant web access port 80,443  (Y/n)" KK
-    [ "$KK" != "n" -a  "$KK" != "no" ] && 
-        firewall-cmd --permanent --zone=public --add-service=http &&
-        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 ]]
-then 
-    echo -e "\nCheking database connection and ask for credentials"
-    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"
-        read -e -p "database user? ($DBUSER) " DBUSER_
-        [ -n "$DBUSER_" ] && DBUSER=$DBUSER_
-        read -e -s -p "database password? (Enter for not using password) " DBPASSWD_
-        [ -n "$DBPASSWD_" ] && DBPASSWD="$DBPASSWD_" && DBPASSWD_PARAM="-p$DBPASSWD_"
-        [ -z "$DBPASSWD_" ] && DBPASSWD=""           && DBPASSWD_PARAM=""
-        logintry="yes"
-    done
-fi
-
-if [[ -z "$NO_PACKAGES" ]]
+if [[ -z "$NO_PACKAGES" ]] #if (PYTHON PACKAGES AND PIP PACKAGES)
 then
 echo '
 #################################################################
 then
 echo '
 #################################################################
@@ -271,14 +232,14 @@ sudo pip install progressbar
 sudo pip install prettytable
 sudo pip install pyvmomi
 
 sudo pip install prettytable
 sudo pip install pyvmomi
 
-#requiered for AWS connector
+#required 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
 [ "$_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
 [ "$_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
 [ "$_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" ]]
+fi #if (PYTHON PACKAGES AND PIP PACKAGES)
 
 if [[ -z $NOCLONE ]]; then
     echo '
 
 if [[ -z $NOCLONE ]]; then
     echo '
@@ -287,49 +248,22 @@ if [[ -z $NOCLONE ]]; then
 #################################################################'
     su $SUDO_USER -c "git clone ${GIT_URL} ${OPENMANO_BASEFOLDER}"
     su $SUDO_USER -c "cp ${OPENMANO_BASEFOLDER}/.gitignore-common ${OPENMANO_BASEFOLDER}/.gitignore"
 #################################################################'
     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.0.2"
+    [[ -z $DEVELOP ]] && su $SUDO_USER -c "git -C ${OPENMANO_BASEFOLDER} checkout v2.0"
 fi
 
 echo '
 #################################################################
 fi
 
 echo '
 #################################################################
-#####        CREATE DATABASE                                #####
+#####        INSTALLING OVIM LIBRARY                        #####
 #################################################################'
 #################################################################'
-echo -e "\nCreating temporary file form MYSQL installation and initialization"
-TEMPFILE="$(mktemp -q --tmpdir "installopenmano.XXXXXX")"
-trap 'rm -f "$TEMPFILE"' EXIT
-chmod 0600 "$TEMPFILE"
-echo -e "[client]\n user='$DBUSER'\n password='$DBPASSWD'">"$TEMPFILE"
-
-if db_exists "mano_db" $TEMPFILE ; then
-    if [[ -n $FORCEDB ]]; then
-        echo "   Deleting previous database mano_db"
-        DBDELETEPARAM=""
-        [[ -n $QUIET_MODE ]] && DBDELETEPARAM="-f"
-        mysqladmin --defaults-extra-file=$TEMPFILE -s drop mano_db $DBDELETEPARAM || ! echo "Could not delete mano_db database" || exit 1
-        #echo "REVOKE ALL PRIVILEGES ON mano_db.* FROM 'mano'@'localhost';" | mysql --defaults-extra-file=$TEMPFILE -s || ! echo "Failed while creating user mano at database" || exit 1
-        #echo "DELETE USER 'mano'@'localhost';"   | mysql --defaults-extra-file=$TEMPFILE -s || ! echo "Failed while creating user mano at database" || exit 1
-        mysqladmin --defaults-extra-file=$TEMPFILE -s create mano_db || ! echo "Error creating mano_db database" || exit 1
-        echo "DROP USER 'mano'@'localhost';"   | mysql --defaults-extra-file=$TEMPFILE -s || ! echo "Failed while creating user mano at database" || exit 1
-        echo "CREATE USER 'mano'@'localhost' identified by 'manopw';"   | mysql --defaults-extra-file=$TEMPFILE -s || ! echo "Failed while creating user mano at database" || exit 1
-        echo "GRANT ALL PRIVILEGES ON mano_db.* TO 'mano'@'localhost';" | mysql --defaults-extra-file=$TEMPFILE -s || ! echo "Failed while creating user mano at database" || exit 1
-        echo " Database 'mano_db' created, user 'mano' password 'manopw'"
-    else
-        echo "Database exists. Use option '--forcedb' to force the deletion of the existing one" && exit 1
-    fi
-else
-    mysqladmin -u$DBUSER $DBPASSWD_PARAM -s create mano_db || ! echo "Error creating mano_db database" || exit 1 
-    echo "CREATE USER 'mano'@'localhost' identified by 'manopw';"   | mysql --defaults-extra-file=$TEMPFILE -s || ! echo "Failed while creating user mano at database" || exit 1
-    echo "GRANT ALL PRIVILEGES ON mano_db.* TO 'mano'@'localhost';" | mysql --defaults-extra-file=$TEMPFILE -s || ! echo "Failed while creating user mano at database" || exit 1
-    echo " Database 'mano_db' created, user 'mano' password 'manopw'"
-fi
-
-
-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 "git -C ${OPENMANO_BASEFOLDER} clone ${GIT_OVIM_URL} openvim"
+[[ -z $DEVELOP ]] && su $SUDO_USER -c "git -C ${OPENMANO_BASEFOLDER}/openvim checkout v2.0"
+trap 'rm -rf "${OPENMANO_BASEFOLDER}/openvim"' EXIT
 
 
+# Install debian dependencies before setup.py
+[ "$_DISTRO" == "Ubuntu" ] && install_packages "libmysqlclient-dev"
+[ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && install_packages "libmysqlclient-dev"  #TODO check if that's the name in CentOS and RedHat
+make -C ${OPENMANO_BASEFOLDER}/openvim lite
+OSMLIBOVIM_PATH=`python -c 'import lib_osm_openvim; print lib_osm_openvim.__path__[0]'`
 
 if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ]
 then
 
 if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ]
 then
@@ -375,7 +309,7 @@ then
     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 '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'
+    su $SUDO_USER -c "ln -s '${OPENMANO_BASEFOLDER}/scripts/service-openmano'  "'${HOME}/bin/service-openmano'
 
     #insert /home/<user>/bin in the PATH
     #skiped because normally this is done authomatically when ~/bin exist
 
     #insert /home/<user>/bin in the PATH
     #skiped because normally this is done authomatically when ~/bin exist
@@ -404,8 +338,24 @@ then
     su $SUDO_USER -c 'echo ". ${HOME}/.bash_completion.d/python-argcomplete.sh" >> ~/.bashrc'
 fi
 
     su $SUDO_USER -c 'echo ". ${HOME}/.bash_completion.d/python-argcomplete.sh" >> ~/.bashrc'
 fi
 
+if [ -z "$NO_DB" ]; then
+echo '
+#################################################################
+#####               INSTALL DATABASE SERVER                 #####
+#################################################################'
 
 
+    if [ -n "$QUIET_MODE" ]; then
+        DB_QUIET='-q'
+    fi
+    ${OPENMANO_BASEFOLDER}/database_utils/install-db-server.sh -U $DBUSER ${DBPASSWD_PARAM/p/P} $DB_QUIET $DB_FORCE_UPDATE || exit 1
+echo '
+#################################################################
+#####        CREATE AND INIT MANO_VIM DATABASE              #####
+#################################################################'
+# Install mano_vim_db after setup
+    ${OSMLIBOVIM_PATH}/database_utils/install-db-server.sh -U $DBUSER ${DBPASSWD_PARAM/p/P} -u mano -p manopw -d mano_vim_db $DB_QUIET $DB_FORCE_UPDATE || exit 1
 
 
+fi
 
 if [[ -n "$INSTALL_AS_A_SERVICE"  ]]
 then
 
 if [[ -n "$INSTALL_AS_A_SERVICE"  ]]
 then
@@ -421,16 +371,14 @@ echo '
 
     echo
     echo "Done!  installed at /opt/openmano"
 
     echo
     echo "Done!  installed at /opt/openmano"
-    echo " Manage server with 'sudo service openmano start|stop|status|...' "
+    echo " Manage server with 'sudo service osm-ro 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_BASEFOLDER}/scripts/service-openmano.sh start' for starting openmano in a screen"
+    echo " Run './${OPENMANO_BASEFOLDER}/scripts/service-openmano start' for starting openmano in a screen"
 
 fi
 
 
 fi
 
-
-