fix issue updating a vim network in a multisite deployment
[osm/RO.git] / database_utils / install-db-server.sh
index bf1a88a..36b8003 100755 (executable)
@@ -10,6 +10,7 @@ DB_HOST=""
 DB_HOST_PARAM=""
 QUIET_MODE=""
 FORCEDB=""
+UPDATEDB=""
 NO_PACKAGES=""
 UNINSTALL=""
 
@@ -27,11 +28,26 @@ function usage(){
     echo -e "     -T: PORT  database port. '$DB_PORT' by default"
     echo -e "     -q --quiet: install in unattended mode"
     echo -e "     -h --help:  show this help"
-    echo -e "     --forcedb:  reinstall database, deleting previous database if exists and creating a new one"
+    echo -e "     --forcedb:  if database exists, it is dropped and a new one is created"
+    echo -e "     --updatedb: if database exists, it preserves the content and it is updated to the needed version"
     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 "     --unistall: delete database"
 }
 
+function ask_user(){
+    # ask to the user and parse a response among 'y', 'yes', 'n' or 'no'. Case insensitive
+    # Params: $1 text to ask;   $2 Action by default, can be 'y' for yes, 'n' for no, other or empty for not allowed
+    # Return: true(0) if user type 'yes'; false (1) if user type 'no'
+    read -e -p "$1" USER_CONFIRMATION
+    while true ; do
+        [ -z "$USER_CONFIRMATION" ] && [ "$2" == 'y' ] && return 0
+        [ -z "$USER_CONFIRMATION" ] && [ "$2" == 'n' ] && return 1
+        [ "${USER_CONFIRMATION,,}" == "yes" ] || [ "${USER_CONFIRMATION,,}" == "y" ] && return 0
+        [ "${USER_CONFIRMATION,,}" == "no" ]  || [ "${USER_CONFIRMATION,,}" == "n" ] && return 1
+        read -e -p "Please type 'yes' or 'no': " USER_CONFIRMATION
+    done
+}
+
 function install_packages(){
     [ -x /usr/bin/apt-get ] && apt-get install -y $*
     [ -x /usr/bin/yum ]     && yum install     -y $*   
@@ -73,11 +89,10 @@ function _install_mysql_package(){
         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
+        ask_user "Do you want to configure mariadb (recommended if not done before) (Y/n)? " y &&
+            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" ] &&
+        ask_user "Do you want to set firewall to grant web access port 80,443  (Y/n)? " y &&
             firewall-cmd --permanent --zone=public --add-service=http &&
             firewall-cmd --permanent --zone=public --add-service=https &&
             firewall-cmd --reload
@@ -87,36 +102,45 @@ function _install_mysql_package(){
 function _create_db(){
     echo '
     #################################################################
-    #####               CREATE DATABASE                         #####
+    #####        CREATE AND INIT DATABASE                       #####
     #################################################################'
-
-    if db_exists $DB_NAME $TEMPFILE ; then
-        if [[ -n $FORCEDB ]]; then
-            DBDELETEPARAM=""
-            [[ -n $QUIET_MODE ]] && DBDELETEPARAM="-f"
-            mysqladmin --defaults-extra-file="$TEMPFILE" -s drop ${DB_NAME} $DBDELETEPARAM || ! echo "Could not delete ${DB_NAME} database" || exit 1
-        elif [[ -z $QUIET_MODE ]] ; then
-            read -e -p "Drop exiting database '$DB_NAME'. All the content will be lost (y/N)? " KK_
-            [ "$KK_" != "yes" ] && [ "$KK_" != "y" ] && echo "Aborted!" && exit 1
-            mysqladmin --defaults-extra-file="$TEMPFILE" -s drop ${DB_NAME} -f || ! echo "Could not delete ${DB_NAME} database" || exit 1
-        else
-            echo "Database '$DB_NAME' exists. Use option '--forcedb' to force the deletion of the existing one" && exit 1
-        fi
-    fi
     echo "mysqladmin --defaults-extra-file="$TEMPFILE" -s create ${DB_NAME}"
-    mysqladmin --defaults-extra-file="$TEMPFILE" -s create ${DB_NAME} || ! echo "1 Error creating ${DB_NAME} database" || exit 1
-    echo "CREATE USER $DB_USER@'localhost' IDENTIFIED BY '$DB_PASS';"   | mysql --defaults-extra-file="$TEMPFILE" -s 2>/dev/null || echo "Warning: User '$DB_USER' cannot be created at database. Probably exist"
-    echo "GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO '$DB_USER'@'localhost';" | mysql --defaults-extra-file="$TEMPFILE" -s || ! echo "Error: Granting privileges to user '$DB_USER' at database" || exit 1
+    mysqladmin --defaults-extra-file="$TEMPFILE" -s create ${DB_NAME} \
+        || ! echo "Error creating ${DB_NAME} database" >&2 \
+        || exit 1
+    echo "CREATE USER $DB_USER@'localhost' IDENTIFIED BY '$DB_PASS';"   | mysql --defaults-extra-file="$TEMPFILE" -s 2>/dev/null \
+        || echo "Warning: User '$DB_USER' cannot be created at database. Probably exist" >&2
+    echo "GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO '$DB_USER'@'localhost';" | mysql --defaults-extra-file="$TEMPFILE" -s \
+        || ! echo "Error: Granting privileges to user '$DB_USER' at database" >&2 \
+        || exit 1
     echo " Database '${DB_NAME}' created, user '$DB_USER' password '$DB_PASS'"
+    DIRNAME=$(dirname $(readlink -f ${BASH_SOURCE[0]}))
+    ${DIRNAME}/init_mano_db.sh -u"$DB_USER" -p"$DB_PASS" -d"$DB_NAME" -P"$DB_PORT" $DB_HOST_PARAM \
+        || ! echo "Error initializing database '$DB_NAME'" >&2 \
+        || exit 1
+}
+
+function _delete_db(){
+   mysqladmin --defaults-extra-file="$TEMPFILE" -s drop "${DB_NAME}" $DBDELETEPARAM \
+       || ! echo "Error: Could not delete '${DB_NAME}' database" >&2 \
+       || exit 1
 }
 
-function _init_db(){
+function _update_db(){
     echo '
     #################################################################
-    #####        INIT DATABASE                                  #####
+    #####        UPDATE DATABASE                                #####
     #################################################################'
+    echo "CREATE USER $DB_USER@'localhost' IDENTIFIED BY '$DB_PASS';" | mysql --defaults-extra-file="$TEMPFILE" -s 2>/dev/null \
+        || echo "Warning: User '$DB_USER' cannot be created at database. Probably exist" >&2
+    echo "GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO '$DB_USER'@'localhost';" | mysql --defaults-extra-file="$TEMPFILE" -s \
+        || ! echo "Error: Granting privileges to user '$DB_USER' at database" >&2 \
+        || exit 1
+    echo " Granted privileges to user '$DB_USER' password '$DB_PASS' to existing database '${DB_NAME}'"
     DIRNAME=$(dirname $(readlink -f ${BASH_SOURCE[0]}))
-    ${DIRNAME}/init_mano_db.sh -u"$DB_USER" -p"$DB_PASS" -d"$DB_NAME" -P"$DB_PORT" $DB_HOST_PARAM || ! echo "Error initializing database '$DB_NAME'" || exit 1
+    ${DIRNAME}/migrate_mano_db.sh -u"$DB_USER" -p"$DB_PASS" -d"$DB_NAME" -P"$DB_PORT" $DB_HOST_PARAM \
+        || ! echo "Error updating database '$DB_NAME'" >&2 \
+        || exit 1
 }
 
 function _uninstall_db(){
@@ -126,13 +150,14 @@ echo '
     #################################################################'
     DBDELETEPARAM=""
     [[ -n $QUIET_MODE ]] && DBDELETEPARAM="-f"
-    mysqladmin  --defaults-extra-file="$TEMPFILE" -s drop "${DB_NAME}" $DBDELETEPARAM || ! echo "Error: Could not delete '${DB_NAME}' database" || exit 1
-
+    _delete_db
 }
 
 function db_exists(){  # (db_name, credential_file)
-    RESULT=`mysqlshow --defaults-extra-file="$2" | grep -v Wildcard | grep -o $1`
-    if [ "$RESULT" == "$1" ]; then
+    # check credentials
+    mysqlshow --defaults-extra-file="$2" >/dev/null  || exit 1
+    if mysqlshow --defaults-extra-file="$2" | grep -v Wildcard | grep -w -q $1
+    then
         # echo " DB $1 exists"
         return 0
     fi
@@ -174,6 +199,7 @@ while getopts ":U:P:d:u:p:H:T:hiq-:" o; do
         -)
             [ "${OPTARG}" == "help" ] && usage && exit 0
             [ "${OPTARG}" == "forcedb" ] && FORCEDB="y" && continue
+            [ "${OPTARG}" == "updatedb" ] && UPDATEDB="y" && continue
             [ "${OPTARG}" == "quiet" ] && export QUIET_MODE=yes && export DEBIAN_FRONTEND=noninteractive && continue
             [ "${OPTARG}" == "no-install-packages" ] && export NO_PACKAGES=yes && continue
             [ "${OPTARG}" == "uninstall" ] &&  UNINSTALL="y" && continue
@@ -194,6 +220,10 @@ while getopts ":U:P:d:u:p:H:T:hiq-:" o; do
             ;;
     esac
 done
+if [ -n "$FORCEDB" ] && [ -n "$UPDATEDB" ] ; then
+    echo "Error: options --forcedb and --updatedb are mutually exclusive" >&2
+    exit 1
+fi
 
 # Discover Linux distribution
 # try redhat type
@@ -201,6 +231,12 @@ done
 # if not assuming ubuntu type
 [ -f /etc/redhat-release ] || _DISTRO=$(lsb_release -is  2>/dev/null)
 
+if [[ -z "$NO_PACKAGES" ]]
+then
+    [ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit 1
+    _install_mysql_package || exit 1
+fi
+
 # Creating temporary file for MYSQL installation and initialization"
 TEMPFILE="$(mktemp -q --tmpdir "installdb.XXXXXX")"
 trap 'rm -f "$TEMPFILE"' EXIT
@@ -232,11 +268,29 @@ then
     exit
 fi
 
-if [[ -z "$NO_PACKAGES" ]]
-then
-    [ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit 1
-    _install_mysql_package || exit 1
+# Create or update database
+if db_exists $DB_NAME $TEMPFILE ; then
+    if [[ -n $FORCEDB ]] ; then
+        # DBDELETEPARAM=""
+        # [[ -n $QUIET_MODE ]] && DBDELETEPARAM="-f"
+        DBDELETEPARAM="-f"
+        _delete_db
+        _create_db
+    elif [[ -n $UPDATEDB ]] ; then
+        _update_db
+    elif [[ -z $QUIET_MODE ]] ; then
+        echo "database '$DB_NAME' exist. Reinstall it?"
+        if ask_user "Type 'y' to drop and reinstall existing database (content will be lost), Type 'n' to update existing database (y/N)? " n ; then
+            _delete_db
+            _create_db
+        else
+            _update_db
+        fi
+    else
+        echo "Database '$DB_NAME' exists. Use option '--forcedb' to force the deletion of the existing one, or '--updatedb' to use existing one and update it"
+        exit 1
+    fi
+else
+    _create_db
 fi
 
-_create_db || exit 1
-_init_db   || exit 1