From 89b3d840dfa2ae1aae544338fad79cf7703f4af4 Mon Sep 17 00:00:00 2001 From: garciadeblas Date: Mon, 19 Sep 2016 15:18:33 +0200 Subject: [PATCH] Changes in several installation and init scripts to remove mysql warnings and become idempotent and usable from a cloned repository Change-Id: I4b4740f0125cb01ea6fbeff24684e48ed0ed6788 Signed-off-by: garciadeblas --- database_utils/init_mano_db.sh | 34 +++++++---- database_utils/migrate_mano_db.sh | 25 ++++++-- openmanod.py | 2 +- scripts/install-openmano-service.sh | 4 +- scripts/install-openmano.sh | 92 +++++++++++++++++++++++------ 5 files changed, 121 insertions(+), 36 deletions(-) diff --git a/database_utils/init_mano_db.sh b/database_utils/init_mano_db.sh index 8afd9c22..4b73441d 100755 --- a/database_utils/init_mano_db.sh +++ b/database_utils/init_mano_db.sh @@ -91,35 +91,49 @@ done shift $((OPTIND-1)) #check and ask for database user password -DBUSER_="-u$DBUSER" -DBPASS_="" -[ -n "$DBPASS" ] && DBPASS_="-p$DBPASS" +#DBUSER_="-u$DBUSER" +#DBPASS_="" +#[ -n "$DBPASS" ] && DBPASS_="-p$DBPASS" DBHOST_="-h$DBHOST" DBPORT_="-P$DBPORT" -while ! echo ";" | mysql $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ >/dev/null 2>&1 + +TEMPFILE="$(mktemp -q --tmpdir "initmanodb.XXXXXX")" +trap 'rm -f "$TEMPFILE"' EXIT +chmod 0600 "$TEMPFILE" +cat >"$TEMPFILE" </dev/null 2>&1 do [ -n "$logintry" ] && echo -e "\nInvalid database credentials!!!. Try again (Ctrl+c to abort)" [ -z "$logintry" ] && echo -e "\nProvide database credentials" # read -e -p "mysql database name($DBNAME): " KK # [ -n "$KK" ] && DBNAME="$KK" read -e -p "mysql user($DBUSER): " KK - [ -n "$KK" ] && DBUSER="$KK" && DBUSER_="-u$DBUSER" + [ -n "$KK" ] && DBUSER="$KK" read -e -s -p "mysql password: " DBPASS - [ -n "$DBPASS" ] && DBPASS_="-p$DBPASS" - [ -z "$DBPASS" ] && DBPASS_="" + cat >"$TEMPFILE" <"$TEMPFILE" </dev/null 2>&1 +while ! mysql $DEF_EXTRA_FILE_PARAM $DBHOST_ $DBPORT_ $DBNAME -e "quit" >/dev/null 2>&1 do [ -n "$logintry" ] && echo -e "\nInvalid database credentials!!!. Try again (Ctrl+c to abort)" [ -z "$logintry" ] && echo -e "\nProvide database name and credentials" read -e -p "mysql database name($DBNAME): " KK [ -n "$KK" ] && DBNAME="$KK" read -e -p "mysql user($DBUSER): " KK - [ -n "$KK" ] && DBUSER="$KK" && DBUSER_="-u$DBUSER" + [ -n "$KK" ] && DBUSER="$KK" read -e -s -p "mysql password: " DBPASS - [ -n "$DBPASS" ] && DBPASS_="-p$DBPASS" - [ -z "$DBPASS" ] && DBPASS_="" + cat >"$TEMPFILE" < /etc/systemd/system/openmano.service << EOF [Unit] Description=openmano server diff --git a/scripts/install-openmano.sh b/scripts/install-openmano.sh index cdbf9110..48e2a81d 100755 --- a/scripts/install-openmano.sh +++ b/scripts/install-openmano.sh @@ -21,7 +21,7 @@ # contact with: nfvlabs@tid.es ## -#ONLY TESTED for Ubuntu 14.10 14.04, CentOS7 and RHEL7 +#ONLY TESTED in Ubuntu 16.04 #Get needed packages, source code and configure to run openmano #Ask for database user and password if not provided # $1: database user @@ -34,9 +34,11 @@ function usage(){ echo -e " OPTIONS" echo -e " -u USER: database admin user. 'root' by default. Prompts if needed" echo -e " -p PASS: database admin password to be used or installed. Prompts if needed" - echo -e " -q --quiet: install in an unattended mode" + echo -e " -q --quiet: install in unattended mode" 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 and creating a new one" + echo -e " --noclone: assumes that openmano was cloned previously and that this script is run from the local repo" } function install_packages(){ @@ -52,17 +54,29 @@ function install_packages(){ if [ "$PACKAGE_INSTALLED" = "no" ] then echo "failed to install package '$PACKAGE'. Revise network connectivity and try again" >&2 - exit -1 + exit 1 fi 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 DBUSER="root" DBPASSWD="" DBPASSWD_PARAM="" QUIET_MODE="" DEVELOP="" +FORCEDB="" +NOCLONE="" while getopts ":u:p:hiq-:" o; do case "${o}" in u) @@ -82,6 +96,8 @@ while getopts ":u:p:hiq-:" o; do -) [ "${OPTARG}" == "help" ] && usage && exit 0 [ "${OPTARG}" == "develop" ] && DEVELOP="y" && continue + [ "${OPTARG}" == "forcedb" ] && FORCEDB="y" && continue + [ "${OPTARG}" == "noclone" ] && NOCLONE="y" && continue [ "${OPTARG}" == "quiet" ] && export QUIET_MODE=yes && export DEBIAN_FRONTEND=noninteractive && continue echo -e "Invalid option: '--$OPTARG'\nTry $0 --help for more information" >&2 exit 1 @@ -96,13 +112,13 @@ while getopts ":u:p:hiq-:" o; do ;; *) usage >&2 - exit -1 + exit 1 ;; esac done #check root privileges and non a root user behind -[ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit -1 +[ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit 1 if [[ -z "$SUDO_USER" ]] || [[ "$SUDO_USER" = "root" ]] then [[ -z $QUIET_MODE ]] && read -e -p "Install in the root user (y/N)?" KK @@ -199,7 +215,7 @@ fi if [[ -n $QUIET_MODE ]] then echo -e "\nCheking database connection and ask for credentials" - while ! mysqladmin -s -u$DBUSER $DBPASSWD_PARAM ping + 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" @@ -226,24 +242,63 @@ echo ' [ "$_DISTRO" == "Ubuntu" ] && install_packages "python-novaclient python-keystoneclient python-glanceclient python-neutronclient" [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && install_packages "python-devel" && easy_install python-novaclient python-keystoneclient python-glanceclient python-neutronclient #TODO revise if gcc python-pip is needed -echo ' +if [[ -z $NOCLONE ]]; then + echo ' ################################################################# ##### DOWNLOAD SOURCE ##### #################################################################' -su $SUDO_USER -c 'git clone '"${GIT_URL}"' openmano' -#[[ -z $DEVELOP ]] && su $SUDO_USER -c 'git checkout ' + su $SUDO_USER -c 'git clone '"${GIT_URL}"' openmano' + #[[ -z $DEVELOP ]] && su $SUDO_USER -c 'git checkout ' +fi echo ' ################################################################# ##### CREATE DATABASE ##### #################################################################' -mysqladmin -u$DBUSER $DBPASSWD_PARAM -s create mano_db || exit 1 +echo -e "\nCreating temporary file form MYSQL installation and initialization" +TEMPFILE="$(mktemp -q --tmpdir "installopenmano.XXXXXX")" +trap 'rm -f "$TEMPFILE"' EXIT +chmod 0600 "$TEMPFILE" +cat >"$TEMPFILE" </bin in the PATH #skiped because normally this is done authomatically when ~/bin exist @@ -324,7 +380,7 @@ echo ' ##### CONFIGURE OPENMANO SERVICE ##### #################################################################' - ./openmano/scripts/install-openmano-service.sh -f openmano #-u $SUDO_USER + ${OPENMANO_BASEFOLDER}/scripts/install-openmano-service.sh -f ${OPENMANO_BASEFOLDER} #-u $SUDO_USER # alias service-openmano="service openmano" # echo 'alias service-openmano="service openmano"' >> ${HOME}/.bashrc -- 2.25.1