X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=database_utils%2Finstall-db-server.sh;h=c3a02487666e0f2b315f522d35a54f2094001b7a;hb=24ca7a8ae3b5f5c235a244c77163c71d1990b94f;hp=24116b5734a35f4a3448bb153d1241d2aac95470;hpb=fdbdb09960fdf66bb37330d804a2a909c1f987e4;p=osm%2Fopenvim.git diff --git a/database_utils/install-db-server.sh b/database_utils/install-db-server.sh index 24116b5..c3a0248 100755 --- a/database_utils/install-db-server.sh +++ b/database_utils/install-db-server.sh @@ -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