Alon Bar-Lev has uploaded a new change for review. Change subject: db: cleanup: add die and usage ......................................................................
db: cleanup: add die and usage Change-Id: I5d708d1855c93f88fcba97c04660b2e4f5f47a6e Signed-off-by: Alon Bar-Lev <alo...@redhat.com> --- M packaging/dbscripts/dbcustomfunctions.sh M packaging/dbscripts/dbfunctions.sh M packaging/dbscripts/unlock_entity.sh M packaging/setup/dbutils/changedbowner.sh M packaging/setup/dbutils/common.sh M packaging/setup/dbutils/encodingvalidator.sh M packaging/setup/dbutils/fkvalidator.sh M packaging/setup/dbutils/taskcleaner.sh 8 files changed, 52 insertions(+), 59 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/15/25215/1 diff --git a/packaging/dbscripts/dbcustomfunctions.sh b/packaging/dbscripts/dbcustomfunctions.sh index 005cc52..14137fb 100644 --- a/packaging/dbscripts/dbcustomfunctions.sh +++ b/packaging/dbscripts/dbcustomfunctions.sh @@ -1,4 +1,10 @@ +die() { + local m="$1" + echo "FATAL: ${m}" >&2 + exit 1 +} + insert_initial_data() { echo "Inserting data..." execute_file "insert_data.sql" "${DATABASE}" "${SERVERNAME}" "${PORT}" > /dev/null diff --git a/packaging/dbscripts/dbfunctions.sh b/packaging/dbscripts/dbfunctions.sh index 782b888..529698d 100644 --- a/packaging/dbscripts/dbfunctions.sh +++ b/packaging/dbscripts/dbfunctions.sh @@ -213,7 +213,8 @@ expr="$(echo "${line}" | cut -d " " -f1 | grep "\-\-#source")" [ -z "${expr}" ] && break local sql="$(echo "${line}" | cut -d " " -f2)" - valid="$(echo "${sql}" | grep "_sp.sql")" + echo "${sql}" | grep -q "_sp.sql" || \ + die "invalid source file ${sql} in ${file}, source files must end with '_sp.sql'" if [ -z "${valid}" ]; then echo "invalid source file ${sql} in ${file}, source files must end with '_sp.sql'" exit 1 @@ -308,10 +309,7 @@ local file for file in $(ls ${files} | sort) ; do local ver="$(_dbfunc_common_get_file_version "${file}")" - if [ "${ver}" = "${prev}" ]; then - echo "Operation aborted, found duplicate version: ${ver}" - exit 1 - fi + [ "${ver}" != "${prev}" ] || die "Operation aborted, found duplicate version: ${ver}" prev="${ver}" done } @@ -359,9 +357,8 @@ if [ "${xverMajor}" -eq "${lastMajor}" ]; then if [ $((${xver} - ${last})) -gt 10 ]; then set_last_version - echo "Illegal script version number ${ver},version should be in max 10 gap from last installed version: 0${last}" - echo "Please fix numbering to interval 0$(( ${last} + 1)) to 0$(( ${last} + 10)) and run the upgrade script." - exit 1 + die "Illegal script version number ${ver},version should be in max 10 gap from last installed version: 0${last} +Please fix numbering to interval 0$(( ${last} + 1)) to 0$(( ${last} + 10)) and run the upgrade script." fi fi # check if script was already installed with other version name. diff --git a/packaging/dbscripts/unlock_entity.sh b/packaging/dbscripts/unlock_entity.sh index 55b5a76..281c7b6 100755 --- a/packaging/dbscripts/unlock_entity.sh +++ b/packaging/dbscripts/unlock_entity.sh @@ -45,23 +45,19 @@ shift $(( $OPTIND - 1 )) IDS="$@" +[ -n "${TYPE}" ] || die "Please specify type" +[ -z "${IDS}" -a -z "${QUERY}" ] && die "Please specify ids or query" +[ -n "${IDS}" -a -n "${QUERY}" ] && die "Please specify one ids or query" -if [ -n "${TYPE}" -a -n "${IDS}" ]; then +if [ -n "${IDS}" ]; then echo "Caution, this operation may lead to data corruption and should be used with care. Please contact support prior to running this command" echo "Are you sure you want to proceed? [y/n]" read answer - - if [ "${answer}" = "n" ]; then - echo "Please contact support for further assistance." - exit 1 - fi + [ "${answer}" = "y" ] || die "Please contact support for further assistance." for ID in ${IDS} ; do unlock_entity "${TYPE}" "${ID}" "$(whoami)" ${RECURSIVE} done -elif [ -n "${TYPE}" -a -n "${QUERY}" ]; then +elif [ -n "${QUERY}" ]; then query_locked_entities "${TYPE}" -else - echo "Please specify type and ids or query" - exit 1 fi diff --git a/packaging/setup/dbutils/changedbowner.sh b/packaging/setup/dbutils/changedbowner.sh index 46ea309..21f5247 100755 --- a/packaging/setup/dbutils/changedbowner.sh +++ b/packaging/setup/dbutils/changedbowner.sh @@ -37,10 +37,9 @@ esac done -if [ ! -n "${FROM_USER}" -o ! -n "${TO_USER}" ]; then - echo "Please specify users" - exit 1 -fi +[ -n "${FROM_USER}" ] || die "Please specify from user" +[ -n "${TO_USER}" ] || die "Please specify to user" +[ -n "${DATABASE}" ] || die "Please specify database" tempfile="$(mktemp)" cleanup() { @@ -54,17 +53,13 @@ grep -i 'owner to' | sed "s/OWNER TO ${FROM_USER};/OWNER TO ${TO_USER};/i" | \ ( psql -h "${SERVERNAME}" -p "${PORT}" -U "${FROM_USER}" "${DATABASE}" && echo ok >> "${tempfile}" ) -if [ "$(wc -l < "${tempfile}")" -ne 2 ]; then - echo "Failed to change DB ${DATABASE} objects ownership." - exit 1 -fi +[ "$(wc -l < "${tempfile}")" -eq 2 ] || die "Failed to change DB ${DATABASE} objects ownership." #change the DB ownership echo "Changing database ${DATABASE} ownership" cmd="ALTER DATABASE ${DATABASE} OWNER TO ${TO_USER};" if ! psql -w -h "${SERVERNAME}" -p "${PORT}" --pset=tuples_only=on --set ON_ERROR_STOP=1 -c "${cmd}" -U "${FROM_USER}" -d "${DATABASE}"; then - echo "Failed to change DB ${DATABASE} ownership." - exit 2 + die "Failed to change DB ${DATABASE} ownership." fi echo "Changing database ${DATABASE} ownership from ${FROM_USER} to ${TO_USER} completed successfully." diff --git a/packaging/setup/dbutils/common.sh b/packaging/setup/dbutils/common.sh index 08d8a56..580ca20 100644 --- a/packaging/setup/dbutils/common.sh +++ b/packaging/setup/dbutils/common.sh @@ -1,4 +1,10 @@ +die() { + local m="$1" + echo "FATAL: ${m}" >&2 + exit 1 +} + set_defaults() { SERVERNAME="localhost" PORT="5432" diff --git a/packaging/setup/dbutils/encodingvalidator.sh b/packaging/setup/dbutils/encodingvalidator.sh index a975a6c..66597ce 100755 --- a/packaging/setup/dbutils/encodingvalidator.sh +++ b/packaging/setup/dbutils/encodingvalidator.sh @@ -47,6 +47,8 @@ esac done +[ -n "${USERNAME}" ] || die "Please specify user" + run() { local command="${1}" local db="${2}" @@ -81,25 +83,23 @@ run "${CMD}" template1 } -if [ -n "${FIXIT}" -a -z "${QUIET}" ]; then - echo "Caution, this operation should be used with care. Please contact support prior to running this command" - echo "Are you sure you want to proceed? [y/n]" - read answer - - if [ "${answer}" != "y" ]; then - echo "Please contact support for further assistance." - exit 1 - fi -fi - encoding="$(get)" if [ "${encoding}" = "UTF8" -o "${encoding}" = "utf8" ]; then echo "Database template1 has already UTF8 default encoding configured. nothing to do, exiting..." exit 0 -elif [ -z "${FIXIT}" ]; then - echo "Database template1 is configured with an incompatible encoding: ${encoding}" - exit 1 +fi + +echo "Database template1 is configured with an incompatible encoding: ${encoding}" + +[ -n "${FIXIT}" ] || die "Database is incompatible" + +if [ -z "${QUIET}" ]; then + echo "Caution, this operation should be used with care. Please contact support prior to running this command" + echo "Are you sure you want to proceed? [y/n]" + read answer + + [ "${answer}" = "y" ] || die "Please contact support for further assistance." fi fix_template1_encoding diff --git a/packaging/setup/dbutils/fkvalidator.sh b/packaging/setup/dbutils/fkvalidator.sh index 8e9aae2..cab3f3b 100755 --- a/packaging/setup/dbutils/fkvalidator.sh +++ b/packaging/setup/dbutils/fkvalidator.sh @@ -92,18 +92,18 @@ esac done -# Install fkvalidator procedures -psql -w -U "${USERNAME}" -h "${SERVERNAME}" -p "${PORT}" -f ./fkvalidator_sp.sql "${DATABASE}" > /dev/null +[ -n "${USERNAME}" ] || die "Please specify user name" +[ -n "${DATABASE}" ] || die "Please specify database" if [ -n "${FIXIT}" -a -z "${QUIET}" ]; then echo "Caution, this operation should be used with care. Please contact support prior to running this command" echo "Are you sure you want to proceed? [y/n]" read answer - if [ "${answer}" = "n" ]; then - echo "Please contact support for further assistance." - exit 1 - fi + [ "${answer}" = "y" ] || die "Please contact support for further assistance." fi +# Install fkvalidator procedures +psql -w -U "${USERNAME}" -h "${SERVERNAME}" -p "${PORT}" -f ./fkvalidator_sp.sql "${DATABASE}" > /dev/null +# Execute validate_db_fks "${FIXIT}" "${VERBOSE}" diff --git a/packaging/setup/dbutils/taskcleaner.sh b/packaging/setup/dbutils/taskcleaner.sh index f61cc62..017b1db 100755 --- a/packaging/setup/dbutils/taskcleaner.sh +++ b/packaging/setup/dbutils/taskcleaner.sh @@ -91,18 +91,12 @@ Are you sure you want to proceed? [y/n] __EOF__ read answer - if [ "${answer}" = "n" ]; then - echo "Please contact support for further assistance." - exit 1 - fi + [ "${answer}" = "y" ] || die "Please contact support for further assistance." fi } -if [ -z "${USERNAME}" ]; then - echo "Please specify user name" - exit 1 -fi - +[ -n "${USERNAME}" ] || die "Please specify user name" +[ -n "${DATABASE}" ] || die "Please specify database" # Install taskcleaner procedures psql -w -U "${USERNAME}" -h "${SERVERNAME}" -p "${PORT}" -f ./taskcleaner_sp.sql "${DATABASE}" > /dev/null @@ -237,8 +231,7 @@ fi fi else - echo "Please specify task" - exit 1 + die "Please specify task" fi elif [ -n "${ZOMBIES_ONLY}" ]; then #only display operations block CMD1="SELECT ${FIELDS} FROM GetAsyncTasksZombies();" -- To view, visit http://gerrit.ovirt.org/25215 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I5d708d1855c93f88fcba97c04660b2e4f5f47a6e Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Alon Bar-Lev <alo...@redhat.com> _______________________________________________ Engine-patches mailing list Engine-patches@ovirt.org http://lists.ovirt.org/mailman/listinfo/engine-patches