X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=database_utils%2Finstall-db-server.sh;h=8ef780c9826e98932d4236b6f240ceb094156d72;hb=HEAD;hp=bf1a88a70fd0ccad1a6bdfdf252ea98a20c9b124;hpb=ab8cb54f3c83b05b00b82419fcccb947916dc7e5;p=osm%2FRO.git diff --git a/database_utils/install-db-server.sh b/database_utils/install-db-server.sh deleted file mode 100755 index bf1a88a7..00000000 --- a/database_utils/install-db-server.sh +++ /dev/null @@ -1,242 +0,0 @@ -#!/usr/bin/env bash - -DB_NAME='mano_db' -DB_ADMIN_USER="root" -DB_USER="mano" -DB_PASS="manopw" -DB_ADMIN_PASSWD="" -DB_PORT="3306" -DB_HOST="" -DB_HOST_PARAM="" -QUIET_MODE="" -FORCEDB="" -NO_PACKAGES="" -UNINSTALL="" - - -function usage(){ - echo -e "usage: sudo $0 [OPTIONS]" - echo -e "Install openmano database server and the needed packages" - echo -e " OPTIONS" - echo -e " -U USER: database admin user. '$DB_ADMIN_USER' by default. Prompts if needed" - echo -e " -P PASS: database admin password to be used or installed. Prompts if needed" - echo -e " -d: database name, '$DB_NAME' by default" - echo -e " -u: database user, '$DB_USER' by default" - echo -e " -p: database pass, '$DB_PASS' by default" - echo -e " -H: HOST database host. 'localhost' by default" - 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 " --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 install_packages(){ - [ -x /usr/bin/apt-get ] && apt-get install -y $* - [ -x /usr/bin/yum ] && yum install -y $* - - #check properly installed - for PACKAGE in $* - do - PACKAGE_INSTALLED="no" - [ -x /usr/bin/apt-get ] && dpkg -l $PACKAGE &>> /dev/null && PACKAGE_INSTALLED="yes" - [ -x /usr/bin/yum ] && yum list installed $PACKAGE &>> /dev/null && PACKAGE_INSTALLED="yes" - if [ "$PACKAGE_INSTALLED" = "no" ] - then - echo "failed to install package '$PACKAGE'. Revise network connectivity and try again" >&2 - exit 1 - fi - done -} - -function _install_mysql_package(){ - echo ' - ################################################################# - ##### INSTALL REQUIRED PACKAGES ##### - #################################################################' - [ "$_DISTRO" == "Ubuntu" ] && ! install_packages "mysql-server" && exit 1 - [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && ! install_packages "mariadb mariadb-server" && exit 1 - - 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 $DB_ADMIN_USER -s password $DB_ADMIN_PASSWD - 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 -} - -function _create_db(){ - echo ' - ################################################################# - ##### CREATE 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 - echo " Database '${DB_NAME}' created, user '$DB_USER' password '$DB_PASS'" -} - -function _init_db(){ - echo ' - ################################################################# - ##### INIT DATABASE ##### - #################################################################' - 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 -} - -function _uninstall_db(){ -echo ' - ################################################################# - ##### DELETE DATABASE ##### - #################################################################' - 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 - -} - -function db_exists(){ # (db_name, credential_file) - 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 -} - -while getopts ":U:P:d:u:p:H:T:hiq-:" o; do - case "${o}" in - U) - export DB_ADMIN_USER="$OPTARG" - ;; - P) - export DB_ADMIN_PASSWD="$OPTARG" - ;; - d) - export DB_NAME="$OPTARG" - ;; - u) - export DB_USER="$OPTARG" - ;; - p) - export DB_PASS="$OPTARG" - ;; - H) - export DB_HOST="$OPTARG" - export DB_HOST_PARAM="-h$DB_HOST" - ;; - T) - export DB_PORT="$OPTARG" - ;; - q) - export QUIET_MODE=yes - export DEBIAN_FRONTEND=noninteractive - ;; - h) - usage && exit 0 - ;; - -) - [ "${OPTARG}" == "help" ] && usage && exit 0 - [ "${OPTARG}" == "forcedb" ] && FORCEDB="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 - 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 - ;; - :) - echo -e "Option '-$OPTARG' requires an argument\nTry $0 --help for more information" >&2 - exit 1 - ;; - *) - usage >&2 - exit 1 - ;; - esac -done - -# 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 -[ -f /etc/redhat-release ] || _DISTRO=$(lsb_release -is 2>/dev/null) - -# Creating temporary file for MYSQL installation and initialization" -TEMPFILE="$(mktemp -q --tmpdir "installdb.XXXXXX")" -trap 'rm -f "$TEMPFILE"' EXIT -chmod 0600 "$TEMPFILE" -echo -e "[client]\n user='${DB_ADMIN_USER}'\n password='$DB_ADMIN_PASSWD'\n host='$DB_HOST'\n port='$DB_PORT'" > "$TEMPFILE" - -#check and ask for database user password. Must be done after database installation -if [[ -z $QUIET_MODE ]] -then - echo -e "\nCheking database connection and ask for credentials" - # echo "mysqladmin --defaults-extra-file=$TEMPFILE -s status >/dev/null" - while ! mysqladmin --defaults-extra-file="$TEMPFILE" -s 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 admin user? ($DB_ADMIN_USER) " DBUSER_ - [ -n "$DBUSER_" ] && DB_ADMIN_USER=$DBUSER_ - read -e -s -p "database admin password? (Enter for not using password) " DBPASSWD_ - [ -n "$DBPASSWD_" ] && DB_ADMIN_PASSWD="$DBPASSWD_" - [ -z "$DBPASSWD_" ] && DB_ADMIN_PASSWD="" - echo -e "[client]\n user='${DB_ADMIN_USER}'\n password='$DB_ADMIN_PASSWD'\n host='$DB_HOST'\n port='$DB_PORT'" > "$TEMPFILE" - logintry="yes" - done -fi - -if [[ ! -z "$UNINSTALL" ]] -then - _uninstall_db - exit -fi - -if [[ -z "$NO_PACKAGES" ]] -then - [ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit 1 - _install_mysql_package || exit 1 -fi - -_create_db || exit 1 -_init_db || exit 1