fix issue updating a vim network in a multisite deployment
[osm/RO.git] / database_utils / install-db-server.sh
index 49ab7ea..36b8003 100755 (executable)
@@ -34,6 +34,20 @@ function usage(){
     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 $*   
@@ -75,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
@@ -141,10 +154,10 @@ echo '
 }
 
 function db_exists(){  # (db_name, credential_file)
-    RESULT=`mysqlshow --defaults-extra-file="$2" | grep -v Wildcard | grep -o $1` \
-        || ! echo "$RESULT" >&2 \
-        || exit 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
@@ -218,6 +231,12 @@ fi
 # 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
@@ -249,12 +268,6 @@ then
     exit
 fi
 
-if [[ -z "$NO_PACKAGES" ]]
-then
-    [ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit 1
-    _install_mysql_package || exit 1
-fi
-
 # Create or update database
 if db_exists $DB_NAME $TEMPFILE ; then
     if [[ -n $FORCEDB ]] ; then
@@ -267,8 +280,7 @@ if db_exists $DB_NAME $TEMPFILE ; then
         _update_db
     elif [[ -z $QUIET_MODE ]] ; then
         echo "database '$DB_NAME' exist. Reinstall it?"
-        read -e -p "Type 'y' to drop and reinstall exiting database (content will be lost), Type 'n' to update existing database (y/N)? " KK_
-        if [ "$KK_" == "yes" ] || [ "$KK_" != "y" ] ; then
+        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