From 0e16acf4d8579fd82349d6cd24e236275735a818 Mon Sep 17 00:00:00 2001 From: Pamela Dragosh Date: Tue, 14 Feb 2017 19:45:48 -0500 Subject: Initial OpenECOMP policy/drools-pdp commit Change-Id: I0072ccab6f40ed32da39667f9f8523b6d6dad2e2 Signed-off-by: Pamela Dragosh --- .../src/files/bin/db_upgrade_droolspdp_remote.sh | 151 +++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 packages/base/src/files/bin/db_upgrade_droolspdp_remote.sh (limited to 'packages/base/src/files/bin/db_upgrade_droolspdp_remote.sh') diff --git a/packages/base/src/files/bin/db_upgrade_droolspdp_remote.sh b/packages/base/src/files/bin/db_upgrade_droolspdp_remote.sh new file mode 100644 index 00000000..96cfda1c --- /dev/null +++ b/packages/base/src/files/bin/db_upgrade_droolspdp_remote.sh @@ -0,0 +1,151 @@ +#!/bin/bash +# +# db_upgrade_droolspdp_remote.sh: This script is to perform database schema upgrade on remote db, +# no shecma downgrade will be performed in case db_version is higher then target_version +# +# Logic: 1. Get target schema version from db scripts in $POLICY_HOME/data/mysql +# 2. Get current db schema version from support.db_version table (of target system) +# 3. Apply db upgrade script in order if target_version is HIGHER than db_version +# 4. Print out warning message if target_version is LOWER than db_version +# 4. Print out message if target_version is EQUAL to db_version +# +# +# Usage : db_upgrade_droolspdp_remote.sh db_user_id db_user_password hostname +# Example: db_upgrade_droolspdp_remote.sh policy_user password policydb +# +# Assumption: 1. DB schema upgrade script in $POLICY_HOME/data/mysql folder with read permission +# 2. DB user has privilege to create/drop/alter database table +# +# Note: The default location for db schema upgrade script is $POLICY_HOME/data/mysql +# The release level is represented as Two-digit-Year+Two-digit-Month+two-digit-Sub-release (151000, 151001) +# +# + +TARGET_SCHEMA_VERSION="" +CURRENT_SCHEMA_VERSION="" +DB_UPGRADE_USER="" +DB_UPGRADE_PASSWORD="" +DB_HOSTNAME="" +DB_UPGRADE_DIR=$POLICY_HOME/data/mysql +DATE=`date +"%Y%m%d%H%M%S"` +LOG="" +ERR="" + +function get_current_schema_version +{ + echo "Get current schema version from [${DB_HOSTNAME}] started ...@`date`" | tee -a $LOG + # display output vertical + query="select version from support.db_version where the_key = 'DROOLS_VERSION' \G" + CURRENT_SCHEMA_VERSION=`${MYSQL} --skip-column-names --execute "${query}" 2>$ERR | grep -v "*"` + error_msg=`cat $ERR | grep "doesn't exist"` + if [ "${error_msg}" != "" ]; then + echo "Create support.db_version table ..." | tee -a $LOG + sql="create table support.db_version(the_key varchar(20) not null, version varchar(20), primary key(the_key));" + ${MYSQL} --execute "${sql}" + CURRENT_SCHEMA_VERSION="00" + fi + echo "CURRENT_SCHEMA_VERSION: [$CURRENT_SCHEMA_VERSION]" | tee -a $LOG + echo "Get current schema version from [${DB_HOSTNAME}] completed ...@`date`" | tee -a $LOG +} + +function get_target_schema_version +{ + UPGRADE_LIST=/tmp/db_upgrade_list.$$ + find ${DB_UPGRADE_DIR} -name "*_upgrade_script.sql" 2>/dev/null | grep "droolspdp" | sort -r | head -1 > $UPGRADE_LIST + while read -r file + do + TARGET_SCHEMA_VERSION=`basename $file | cut -d'_' -f1` + echo "TARGET_SCHEMA_VERSION: [$TARGET_SCHEMA_VERSION]" | tee -a $LOG + break + done < $UPGRADE_LIST + rm -f $UPGRADE_LIST +} + +function evaluate_upgrade_downgrade +{ + echo "CURRENT_SCHEMA_VERSION --> [$CURRENT_SCHEMA_VERSION]" | tee -a $LOG + echo "TARGET_SCHEMA_VERSION --> [$TARGET_SCHEMA_VERSION] " | tee -a $LOG + if [[ "${CURRENT_SCHEMA_VERSION}" < "${TARGET_SCHEMA_VERSION}" ]]; then + # perform db upgrade + UPGRADE_LIST=/tmp/db_upgrade_list.$$ + find ${DB_UPGRADE_DIR} -name "*_upgrade_script.sql" 2>/dev/null | grep "droolspdp" | sort > $UPGRADE_LIST + while read -r file + do + DB_VERSION=`basename $file | cut -d'_' -f1` + #echo "[$DB_VERSION] [$TARGET_SCHEMA_VERSION]" | tee -a $LOG + if [ "${DB_VERSION}" -gt "${CURRENT_SCHEMA_VERSION}" ] && [ "${DB_VERSION}" -le "${TARGET_SCHEMA_VERSION}" ]; then + run_script "UPGRADE" "${file}" 2>&1 | tee -a $LOG + fi + done < $UPGRADE_LIST + rm -f $UPGRADE_LIST + set_current_release_level $TARGET_SCHEMA_VERSION + elif [[ "${CURRENT_SCHEMA_VERSION}" > "${TARGET_SCHEMA_VERSION}" ]]; then + # db downgrade + echo "WARNING: Target db schema version is LOWER than current db scema version, please run downgrade script manually." | tee -a $LOG | tee -a $ERR + else + echo "CURRENT SCHEMA VERSION THE SAME AS TARGET SCHEMA VERSION, NO ACTION TAKEN ..." | tee -a $LOG + fi +} + +function run_script +{ + action="${1}" + script="${2}" + echo "Perform DB $action on [${DB_HOSTNAME}] use $script ..." | tee -a $LOG + echo "--" | tee -a $LOG + ${MYSQL} --verbose < "${script}" 2>$ERR | tee -a $LOG + echo "--" | tee -a $LOG +} + +function set_current_release_level +{ + DB_VERSION="${1}" + echo "Set current release level on [${DB_HOSTNAME}] to [$DB_VERSION] started ...@`date`" | tee -a $LOG + update_statement="insert into support.db_version (the_key, version) values ('DROOLS_VERSION', '${DB_VERSION}') on duplicate key update version='${DB_VERSION}';" + ${MYSQL} --execute "${update_statement}" + + echo "" | tee -a $LOG + echo "CURRENT_SCHEMA_VERSION set to: [$DB_VERSION]" | tee -a $LOG + echo "" | tee -a $LOG + echo "Set current release level on [${DB_HOSTNAME}] to [$DB_VERSION] completed ...@`date`" | tee -a $LOG +} + +function check_directory +{ + if [ ! -d $DB_UPGRADE_DIR ]; then + echo "ERROR, DIRECTORY NOT EXIST: $DB_UPGRADE_DIR, PROCESS EXIT ..." + exit; + else + if [ ! -d $DB_UPGRADE_DIR/logs ]; then + mkdir $DB_UPGRADE_DIR/logs + fi + fi +} + +# MAIN +#check_directory +LOG=$POLICY_HOME/logs/db_upgrade_droolspdp_remote_$DATE.log +ERR=$POLICY_HOME/logs/db_upgrade_droolspdp_remote_$DATE.err +echo "db_upgrade_droolspdp_remote.sh started ..." | tee -a $LOG +if [ $# -eq 3 ]; then + DB_UPGRADE_USER="${1}" + DB_UPGRADE_PASSWORD="${2}" + DB_HOSTNAME="${3}" + echo "DB_UPGRADE_USER: $DB_UPGRADE_USER" | tee -a $LOG + echo "DB_UPGRADE_DIR : $DB_UPGRADE_DIR" | tee -a $LOG + echo "DB_HOSTNAME : $DB_HOSTNAME" | tee -a $LOG + # + typeset -r MYSQL="mysql -u${DB_UPGRADE_USER} -p${DB_UPGRADE_PASSWORD} -h ${DB_HOSTNAME}"; + get_target_schema_version + if [ ${#TARGET_SCHEMA_VERSION} -ne 6 ]; then + echo "ERROR, TARGET_SCHEMA_VERSION MUST BE 6 DIGITS: $TARGET_SCHEMA_VERSION" | tee -a $LOG | tee -a $ERR + else + get_current_schema_version + evaluate_upgrade_downgrade + fi +else + echo "Usage : db_upgrade_droolspdp_remote.sh db_user_id db_user_password db_hostname" | tee -a $LOG + echo "Example: db_upgrade_droolspdp_remote.sh policy_user password policydb" | tee -a $LOG +fi + +echo "db_upgrade_droolspdp_remote.sh completed ..." | tee -a $LOG -- cgit 1.2.3-korg