Alon Bar-Lev has uploaded a new change for review. Change subject: db: cleanup: move public interface to top ......................................................................
db: cleanup: move public interface to top Change-Id: I9174019cce81acf7e2085a9dcf8070f5df78c929 Signed-off-by: Alon Bar-Lev <[email protected]> --- M packaging/dbscripts/dbfunc-common.sh 1 file changed, 260 insertions(+), 261 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/18/24918/1 diff --git a/packaging/dbscripts/dbfunc-common.sh b/packaging/dbscripts/dbfunc-common.sh index 31e87ae..9a3c78f 100644 --- a/packaging/dbscripts/dbfunc-common.sh +++ b/packaging/dbscripts/dbfunc-common.sh @@ -45,6 +45,131 @@ dbfunc_common_hook_init_insert_data } +dbfunc_common_upgrade() { + local res="$(find upgrade/ -name '*.sql' -or -name '*.sh' | wc -l)" + local CMD + if [ "${res}" -gt 0 ]; then + local state="FAILED" + local comment="" + local updated=0 + _dbfunc_common_validate_version_uniqueness + if [ -z "${DBFUNC_MD5DIR}" ] || ! _dbfunc_common_is_view_or_sp_changed; then + echo "upgrade script detected a change in Config, View or Stored Procedure..." + _dbfunc_common_run_pre_upgrade + updated=1 + fi + + # get current version + local current="$(_dbfunc_common_get_current_version)" + # we should remove leading blank (from select result) and zero in order not to treat number as octal + local last="$(expr substr "${current}" 3 7)" + local files="$(_dbfunc_common_get_files "upgrade" 1)" + local file + for file in $(ls ${files} | sort); do + local before="$(_dbfunc_common_get_db_time)" + local checksum="$(md5sum "${file}" | cut -d " " -f1)" + # upgrade/dd_dd_dddd* => dddddddd + local ver="$(echo "${file}" | sed -e 's#upgrade/\(..........\).*#\1#' -e 's/_//g')" + if [ "${ver}" -gt "${current}" ] ; then + # we should remove leading zero in order not to treat number as octal + local xver="$(expr substr "${ver}" 2 7)" + # taking major revision , i.e 03010000=>301 + local xverMajor="$(expr substr "${xver}" 1 3)" + local lastMajor="$(expr substr "${last}" 1 3)" + + # check for gaps in upgrade + # check gaps only for identical major revisions + if [ "${xverMajor}" -eq "${lastMajor}" ]; then + if [ $((${xver} - ${last})) -gt 10 ]; then + _dbfunc_common_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 + fi + fi + # check if script was already installed with other version name. + local installed_version="$(_dbfunc_common_get_installed_version "${checksum}")" + if [ -n "${installed_version}" ]; then + echo "Skipping upgrade script ${file}, already installed by ${installed_version}" + state="SKIPPED" + after="$(_dbfunc_common_get_db_time)" + last="${xver}" + comment="Installed already by ${installed_version}" + else + # force pre upgrade to run in case no md5 change was + # found but we still upgrade, like in db restore. + if [ "${updated}" = 0 ]; then + _dbfunc_common_run_pre_upgrade + updated=1 + fi + _dbfunc_common_run_required_scripts "${file}" + _dbfunc_common_run_file "${file}" + code=$? + if [ "${code}" -eq 0 ]; then + state="INSTALLED" + after=$(_dbfunc_common_get_db_time) + last=$xver + comment="" + else + _dbfunc_common_set_last_version + exit "${code}" + fi + fi + # TODO: figure out why not die here VVV + dbfunc_psql_statement " + insert into schema_version( + version, + script, + checksum, + installed_by, + started_at, + ended_at, + state, + current, + comment + ) + values ( + trim('${ver}'), + '${file}', + '${checksum}', + '${DBFUNC_DB_USER}', + cast(trim('${before}') as timestamp), + cast(trim('${after}') as timestamp), + '${state}', + false, + '${comment}' + ); + " > /dev/null + fi + done + _dbfunc_common_set_last_version + + # restore views & SPs if dropped + if [ "${updated}" -eq 1 ]; then + _dbfunc_common_run_post_upgrade + else + echo "database is up to date." + fi + fi +} + +# gets the configuration value of the given option name and version. +# usage: <some variable>=get_config_value <name> <version> +dbfunc_common_config_get_value() { + local option_name="${1}" + local version="${2}" + + dbfunc_psql_statement_parse_line "$( + dbfunc_psql_statement_parsable " + select option_value + from vdc_options + where + option_name='${option_name}' and + version='${version}' + " + )" +} + #drops views before upgrade or refresh operations dbfunc_common_views_drop() { local file="${DBFUNC_TMPDIR}/drop_views.sql" @@ -77,6 +202,141 @@ dbfunc_psql_die --file="${sql}" > /dev/null done dbfunc_psql_die --file="common_sp.sql" > /dev/null +} + +#unlocks the given VM/Template and its disks or a given disk +#in case of VM/Template the id is the name, in case of a disk, the id is the disk UUID +dbfunc_common_entity_unlock() { + local object_type="${1}" + local id="${2}" + local user="${3}" + local recursive="${4}" + [ -z "${recursive}" ] && recursive=false || recursive=true + local CMD="" + if [ "${object_type}" = "vm" -o "${object_type}" = "template" ]; then + CMD="select fn_db_unlock_entity('${object_type}', '${id}', ${recursive});" + elif [ "${object_type}" = "disk" ]; then + CMD="select fn_db_unlock_disk('${id}');" + elif [ "${object_type}" = "snapshot" ]; then + CMD="select fn_db_unlock_snapshot('${id}');" + else + printf "Error: $* " + fi + + if [ -n "${CMD}" ]; then + echo "${CMD}" + if dbfunc_psql_statement_die "${CMD}"; then + _dbfunc_common_log_unlock_entity ${object_type} ${id} ${user} + echo "unlock ${object_type} ${id} completed successfully." + else + echo "unlock ${object_type} ${id} completed with errors." + fi + fi +} + +#Displays locked entities +# +# TODO: figure out why can't we concat statements. +# +dbfunc_common_entity_query() { + local object_type="${1}" + local LOCKED=2 + local TEMPLATE_LOCKED=1 + local IMAGE_LOCKED=15; + local SNAPSHOT_LOCKED=LOCKED + local CMD + if [ "${object_type}" = "vm" ]; then + dbfunc_psql_statement_die " + select + vm_name as vm_name + from + vm_static a, + vm_dynamic b + where + a.vm_guid = b.vm_guid and + status = ${IMAGE_LOCKED}; + " + dbfunc_psql_statement_die " + select + vm_name as vm_name, + image_group_id as disk_id + from + images a, + vm_static b, + vm_device c + where + a.image_group_id = c.device_id and + b.vm_guid = c.vm_id and + imagestatus = ${LOCKED} and + entity_type ilike 'VM' and + image_group_id in ( + select device_id + from vm_device + where is_plugged + ); + " + dbfunc_psql_statement_die " + select + vm_name as vm_name, + snapshot_id as snapshot_id + from + vm_static a, + snapshots b + where + a.vm_guid = b.vm_id and + status ilike '${SNAPSHOT_LOCKED}'; + " + elif [ "${object_type}" = "template" ]; then + dbfunc_psql_statement_die " + select vm_name as template_name + from vm_static + where template_status = ${TEMPLATE_LOCKED}; + " + dbfunc_psql_statement_die " + select + vm_name as template_name, + image_group_id as disk_id + from + images a, + vm_static b, + vm_device c + where + a.image_group_id = c.device_id and + b.vm_guid = c.vm_id and + imagestatus = ${LOCKED} and + entity_type ilike 'TEMPLATE' and + image_group_id in ( + select device_id + from vm_device + where is_plugged + ); + " + elif [ "${object_type}" = "disk" ]; then + dbfunc_psql_statement_die " + select + vm_id as entity_id, + disk_id + from + base_disks a, + images b, + vm_device c + where + a.disk_id = b.image_group_id and + b.image_group_id = c.device_id and + imagestatus = ${LOCKED} and + is_plugged; + " + elif [ "${object_type}" = "snapshot" ]; then + dbfunc_psql_statement_die " + select + vm_id as entity_id, + snapshot_id + from + snapshots a + where + status ilike '${SNAPSHOT_LOCKED}'; + " + fi } _dbfunc_common_install_common_func() { @@ -253,131 +513,6 @@ done } -dbfunc_common_upgrade() { - local res="$(find upgrade/ -name '*.sql' -or -name '*.sh' | wc -l)" - local CMD - if [ "${res}" -gt 0 ]; then - local state="FAILED" - local comment="" - local updated=0 - _dbfunc_common_validate_version_uniqueness - if [ -z "${DBFUNC_MD5DIR}" ] || ! _dbfunc_common_is_view_or_sp_changed; then - echo "upgrade script detected a change in Config, View or Stored Procedure..." - _dbfunc_common_run_pre_upgrade - updated=1 - fi - - # get current version - local current="$(_dbfunc_common_get_current_version)" - # we should remove leading blank (from select result) and zero in order not to treat number as octal - local last="$(expr substr "${current}" 3 7)" - local files="$(_dbfunc_common_get_files "upgrade" 1)" - local file - for file in $(ls ${files} | sort); do - local before="$(_dbfunc_common_get_db_time)" - local checksum="$(md5sum "${file}" | cut -d " " -f1)" - # upgrade/dd_dd_dddd* => dddddddd - local ver="$(echo "${file}" | sed -e 's#upgrade/\(..........\).*#\1#' -e 's/_//g')" - if [ "${ver}" -gt "${current}" ] ; then - # we should remove leading zero in order not to treat number as octal - local xver="$(expr substr "${ver}" 2 7)" - # taking major revision , i.e 03010000=>301 - local xverMajor="$(expr substr "${xver}" 1 3)" - local lastMajor="$(expr substr "${last}" 1 3)" - - # check for gaps in upgrade - # check gaps only for identical major revisions - if [ "${xverMajor}" -eq "${lastMajor}" ]; then - if [ $((${xver} - ${last})) -gt 10 ]; then - _dbfunc_common_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 - fi - fi - # check if script was already installed with other version name. - local installed_version="$(_dbfunc_common_get_installed_version "${checksum}")" - if [ -n "${installed_version}" ]; then - echo "Skipping upgrade script ${file}, already installed by ${installed_version}" - state="SKIPPED" - after="$(_dbfunc_common_get_db_time)" - last="${xver}" - comment="Installed already by ${installed_version}" - else - # force pre upgrade to run in case no md5 change was - # found but we still upgrade, like in db restore. - if [ "${updated}" = 0 ]; then - _dbfunc_common_run_pre_upgrade - updated=1 - fi - _dbfunc_common_run_required_scripts "${file}" - _dbfunc_common_run_file "${file}" - code=$? - if [ "${code}" -eq 0 ]; then - state="INSTALLED" - after=$(_dbfunc_common_get_db_time) - last=$xver - comment="" - else - _dbfunc_common_set_last_version - exit "${code}" - fi - fi - # TODO: figure out why not die here VVV - dbfunc_psql_statement " - insert into schema_version( - version, - script, - checksum, - installed_by, - started_at, - ended_at, - state, - current, - comment - ) - values ( - trim('${ver}'), - '${file}', - '${checksum}', - '${DBFUNC_DB_USER}', - cast(trim('${before}') as timestamp), - cast(trim('${after}') as timestamp), - '${state}', - false, - '${comment}' - ); - " > /dev/null - fi - done - _dbfunc_common_set_last_version - - # restore views & SPs if dropped - if [ "${updated}" -eq 1 ]; then - _dbfunc_common_run_post_upgrade - else - echo "database is up to date." - fi - fi -} - -# gets the configuration value of the given option name and version. -# usage: <some variable>=get_config_value <name> <version> -dbfunc_common_config_get_value() { - local option_name="${1}" - local version="${2}" - - dbfunc_psql_statement_parse_line "$( - dbfunc_psql_statement_parsable " - select option_value - from vdc_options - where - option_name='${option_name}' and - version='${version}' - " - )" -} - #adds a record to audit_log in case of calling unlock_entity _dbfunc_common_log_unlock_entity() { local object_type="${1}" @@ -400,140 +535,4 @@ 'System user ${user} run unlock_entity script on ${object_type} ${id} with db user ${DBUTILS_DB_USER}}' ) " -} - - -#unlocks the given VM/Template and its disks or a given disk -#in case of VM/Template the id is the name, in case of a disk, the id is the disk UUID -dbfunc_common_entity_unlock() { - local object_type="${1}" - local id="${2}" - local user="${3}" - local recursive="${4}" - [ -z "${recursive}" ] && recursive=false || recursive=true - local CMD="" - if [ "${object_type}" = "vm" -o "${object_type}" = "template" ]; then - CMD="select fn_db_unlock_entity('${object_type}', '${id}', ${recursive});" - elif [ "${object_type}" = "disk" ]; then - CMD="select fn_db_unlock_disk('${id}');" - elif [ "${object_type}" = "snapshot" ]; then - CMD="select fn_db_unlock_snapshot('${id}');" - else - printf "Error: $* " - fi - - if [ -n "${CMD}" ]; then - echo "${CMD}" - if dbfunc_psql_statement_die "${CMD}"; then - _dbfunc_common_log_unlock_entity ${object_type} ${id} ${user} - echo "unlock ${object_type} ${id} completed successfully." - else - echo "unlock ${object_type} ${id} completed with errors." - fi - fi -} - -#Displays locked entities -# -# TODO: figure out why can't we concat statements. -# -dbfunc_common_entity_query() { - local object_type="${1}" - local LOCKED=2 - local TEMPLATE_LOCKED=1 - local IMAGE_LOCKED=15; - local SNAPSHOT_LOCKED=LOCKED - local CMD - if [ "${object_type}" = "vm" ]; then - dbfunc_psql_statement_die " - select - vm_name as vm_name - from - vm_static a, - vm_dynamic b - where - a.vm_guid = b.vm_guid and - status = ${IMAGE_LOCKED}; - " - dbfunc_psql_statement_die " - select - vm_name as vm_name, - image_group_id as disk_id - from - images a, - vm_static b, - vm_device c - where - a.image_group_id = c.device_id and - b.vm_guid = c.vm_id and - imagestatus = ${LOCKED} and - entity_type ilike 'VM' and - image_group_id in ( - select device_id - from vm_device - where is_plugged - ); - " - dbfunc_psql_statement_die " - select - vm_name as vm_name, - snapshot_id as snapshot_id - from - vm_static a, - snapshots b - where - a.vm_guid = b.vm_id and - status ilike '${SNAPSHOT_LOCKED}'; - " - elif [ "${object_type}" = "template" ]; then - dbfunc_psql_statement_die " - select vm_name as template_name - from vm_static - where template_status = ${TEMPLATE_LOCKED}; - " - dbfunc_psql_statement_die " - select - vm_name as template_name, - image_group_id as disk_id - from - images a, - vm_static b, - vm_device c - where - a.image_group_id = c.device_id and - b.vm_guid = c.vm_id and - imagestatus = ${LOCKED} and - entity_type ilike 'TEMPLATE' and - image_group_id in ( - select device_id - from vm_device - where is_plugged - ); - " - elif [ "${object_type}" = "disk" ]; then - dbfunc_psql_statement_die " - select - vm_id as entity_id, - disk_id - from - base_disks a, - images b, - vm_device c - where - a.disk_id = b.image_group_id and - b.image_group_id = c.device_id and - imagestatus = ${LOCKED} and - is_plugged; - " - elif [ "${object_type}" = "snapshot" ]; then - dbfunc_psql_statement_die " - select - vm_id as entity_id, - snapshot_id - from - snapshots a - where - status ilike '${SNAPSHOT_LOCKED}'; - " - fi } -- To view, visit http://gerrit.ovirt.org/24918 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I9174019cce81acf7e2085a9dcf8070f5df78c929 Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Alon Bar-Lev <[email protected]> _______________________________________________ Engine-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/engine-patches
