diff options
Diffstat (limited to 'packages/base/src/files/bin')
-rw-r--r-- | packages/base/src/files/bin/db_upgrade_droolspdp.sh | 154 | ||||
-rw-r--r-- | packages/base/src/files/bin/db_upgrade_droolspdp_remote.sh | 151 | ||||
-rw-r--r-- | packages/base/src/files/bin/monitor.sh | 148 | ||||
-rw-r--r-- | packages/base/src/files/bin/policy.sh | 154 |
4 files changed, 607 insertions, 0 deletions
diff --git a/packages/base/src/files/bin/db_upgrade_droolspdp.sh b/packages/base/src/files/bin/db_upgrade_droolspdp.sh new file mode 100644 index 00000000..738317de --- /dev/null +++ b/packages/base/src/files/bin/db_upgrade_droolspdp.sh @@ -0,0 +1,154 @@ +#!/bin/bash + +### +# ============LICENSE_START======================================================= +# Base Package +# ================================================================================ +# Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============LICENSE_END========================================================= +### + +# +# db_upgrade_droolspdp.sh: Run this script to upgrade drools database to a given release level, +# it is recommended that you switch to the policy user to run this script +# +# Usage : db_upgrade_droolspdp.sh target_db_release_level db_user_id db_user_password +# Example: db_upgrade_droolspdp.sh 151000 policy_user password +# +# Assumption: 1. DB upgrade sql 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 release script is $POLICY_HOME/data/mysql +# The release level is represented as Two-digit-Year+Two-digit-Month+two-digit-Sub-release (151000, 151001) +# Drools database version is represented by record with the_key of 'DROOLS_VERSION' +# + +TARGET_RELEASE="" +CURRENT_RELEASE="" +DB_UPGRADE_USER="" +DB_UPGRADE_PASSWORD="" +DB_UPGRADE_DIR=$POLICY_HOME/data/mysql +DATE=`date +"%Y%m%d%H%M%S"` +LOG="" +ERR="" + +function get_current_release_level +{ + echo "Get current release level started ...@`date`" | tee -a $LOG + # display output vertical + query="select version from support.db_version where the_key = 'DROOLS_VERSION' \G" + CURRENT_RELEASE=`${MYSQL} --skip-column-names --execute "${query}" 2>$ERR | grep -v "*"` + echo "CURRENT_RELEASE: [$CURRENT_RELEASE]" | tee -a $LOG + echo "Get current release level completed ...@`date`" | tee -a $LOG +} + +function evaluate_upgrade_downgrade +{ + echo "CURRENT_RELEASE --> [$CURRENT_RELEASE]" | tee -a $LOG + echo "TARGET_RELEASE --> [$TARGET_RELEASE] " | tee -a $LOG + if [[ "${CURRENT_RELEASE}" < "${TARGET_RELEASE}" ]]; then + # perform db upgrade + UPGRADE_LIST=/tmp/db_upgrade_droolspdp_list.$$ + find ${DB_UPGRADE_DIR} -name "*_upgrade_script.sql" 2>/dev/null | grep "droolspdp" | sort > $UPGRADE_LIST + while read -r file + do + RELEASE=`basename $file | cut -d'_' -f1` + #echo "[$RELEASE] [$TARGET_RELEASE]" | tee -a $LOG + if [ "${RELEASE}" -gt "${CURRENT_RELEASE}" ] && [ "${RELEASE}" -le "${TARGET_RELEASE}" ]; then + run_script "UPGRADE" "${file}" 2>&1 | tee -a $LOG + fi + done < $UPGRADE_LIST + rm -f $UPGRADE_LIST + set_current_release_level $TARGET_RELEASE + elif [[ "${CURRENT_RELEASE}" > "${TARGET_RELEASE}" ]]; then + # perform db downgrade + DOWNGRADE_LIST=/tmp/db_downgrade_list.$$ + find ${DB_UPGRADE_DIR} -name "*_downgrade_script.sql" 2>/dev/null | grep "droolspdp" | sort -r > $DOWNGRADE_LIST + while read -r file + do + RELEASE=`basename $file | cut -d'_' -f1` + #echo "[$RELEASE] [$TARGET_RELEASE]" | tee -a $LOG + if [ "${RELEASE}" -le "${CURRENT_RELEASE}" ] && [ "${RELEASE}" -gt "${TARGET_RELEASE}" ]; then + run_script "DOWNGRADE" "${file}" + fi + done < $DOWNGRADE_LIST + rm -f $DOWNGRADE_LIST + set_current_release_level $TARGET_RELEASE + else + echo "CURRENT DB RELEASE LEVEL THE SAME AS TARGET RELEASE LEVEL, NO ACTION TAKEN ..." | tee -a $LOG + fi +} + +function run_script +{ + action="${1}" + script="${2}" + echo "Perform DB $action 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 +{ + RELEASE="${1}" + echo "Set current release level to [$RELEASE] started ...@`date`" | tee -a $LOG + update_statement="insert into support.db_version (the_key, version) values ('DROOLS_VERSION', '${RELEASE}') on duplicate key update version='${RELEASE}';" + ${MYSQL} --execute "${update_statement}" + + echo "" | tee -a $LOG + echo "CURRENT_RELEASE set to: [$RELEASE]" | tee -a $LOG + echo "" | tee -a $LOG + echo "Set current release level 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_$DATE.log +ERR=$POLICY_HOME/logs/db_upgrade_droolspdp_$DATE.err +echo "db_upgrade_droolspdp.sh started ..." | tee -a $LOG +if [ $# -eq 3 ]; then + TARGET_RELEASE="${1}" + DB_UPGRADE_USER="${2}" + DB_UPGRADE_PASSWORD="${3}" + echo "TARGET_RELEASE : $TARGET_RELEASE" | tee -a $LOG + echo "DB_UPGRADE_USER: $DB_UPGRADE_USER" | tee -a $LOG + echo "DB_UPGRADE_DIR : $DB_UPGRADE_DIR" | tee -a $LOG + # + if [ ${#TARGET_RELEASE} -ne 6 ]; then + echo "ERROR, TARGET_RELEASE MUST BE 6 DIGITS: $TARGET_RELEASE" | tee -a $LOG | tee -a $ERR + else + typeset -r MYSQL="mysql -u${DB_UPGRADE_USER} -p${DB_UPGRADE_PASSWORD} "; + get_current_release_level + evaluate_upgrade_downgrade + fi +else + echo "Usage : db_upgrade_droolspdp.sh target_release_level db_user_id db_user_password" | tee -a $LOG + echo "Example: db_upgrade_droolspdp.sh 151000 policy_user password" | tee -a $LOG +fi + +echo "db_upgrade_droolspdp.sh completed ..." | tee -a $LOG 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 diff --git a/packages/base/src/files/bin/monitor.sh b/packages/base/src/files/bin/monitor.sh new file mode 100644 index 00000000..f4fad486 --- /dev/null +++ b/packages/base/src/files/bin/monitor.sh @@ -0,0 +1,148 @@ +#!/bin/bash + +### +# ============LICENSE_START======================================================= +# Base Package +# ================================================================================ +# Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============LICENSE_END========================================================= +### + +function usage() { + echo -n "syntax: $(basename $0) " + echo "[--debug]" +} + +function log() { + echo "$(date +"%Y-%m-%d_%H-%M-%S") $1" >> ${POLICY_HOME}/logs/monitor.log +} + +function monitor() { + if [[ $DEBUG == y ]]; then + echo "-- ${FUNCNAME[0]} --" + set -x + fi + + CONTROLLER=$1 + STATUS=$2 + + if [[ -z ${CONTROLLER} ]]; then + log "WARNING: invalid invocation: no component provided" + return + fi + + if [[ -z ${STATUS} ]]; then + log "WARNING: invalid invocation: no on/off/uninstalled switch provided for ${CONTROLLER}" + return + fi + + if [[ "${STATUS}" == "off" ]]; then + off ${CONTROLLER} + else + if [[ "${STATUS}" == "on" ]]; then + on ${CONTROLLER} + fi + fi +} + +function on() { + if [[ $DEBUG == y ]]; then + echo "-- ${FUNCNAME[0]} --" + set -x + fi + + CONTROLLER=$1 + NAGIOS_COMPONENT_SERVICE="Check_${CONTROLLER}-AliveStatus_AP_24094" + + ${POLICY_HOME}/bin/${CONTROLLER} status + if [[ $? != 0 ]]; then + log "starting ${CONTROLLER}" + + # need to make sure we don't pass the lock file descriptor + ${POLICY_HOME}/bin/${CONTROLLER} umstart {cfg}>&- + else + log "OK: ${CONTROLLER} (UP)" + fi +} + +function off() { + if [[ $DEBUG == y ]]; then + echo "-- ${FUNCNAME[0]} --" + set -x + fi + + CONTROLLER=$1 + NAGIOS_COMPONENT_SERVICE="Check_${CONTROLLER}-AliveStatus_AP_24094" + + ${POLICY_HOME}/bin/${CONTROLLER} status + if [[ $? != 0 ]]; then + log "OK: ${CONTROLLER} (DOWN)" + + else + log "stopping ${CONTROLLER}" + ${POLICY_HOME}/bin/${CONTROLLER} umstop + fi +} + +function process_config() { + if [[ $DEBUG == y ]]; then + echo "-- ${FUNCNAME[0]} --" + set -x + fi + + CONF_FILE=${POLICY_HOME}/etc/monitor/monitor.cfg + while read line || [ -n "${line}" ]; do + if [[ -n ${line} ]] && [[ ${line} != *#* ]]; then + controller=$(echo "${line}" | awk -F = '{print $1;}') + status=$(echo "${line}" | awk -F = '{print $2;}') + if [[ -n ${controller} ]] && [[ -n ${status} ]]; then + monitor ${controller} ${status} + fi + fi + done < "${CONF_FILE}" + return 0 +} + +log "Enter monitor" + +DEBUG=n +until [[ -z "$1" ]]; do + case $1 in + -d|--debug|debug) DEBUG=y + set -x + ;; + *) usage + exit 1 + ;; + esac + shift +done + +if pidof -o %PPID -x $(basename $0) > /dev/null 2>&1; then + log "WARNING: $(basename $0) from the previous iteration still running. Exiting." + exit 1 +fi + +. ${POLICY_HOME}/etc/profile.d/env.sh + +if [[ ${NAGIOS_NRDP_DISABLED} == true ]]; then + log "Nagios NRDS is disabled." +fi + +if flock ${cfg} ; then + process_config +fi {cfg}>>${POLICY_HOME}/etc/monitor/monitor.cfg.lock + + diff --git a/packages/base/src/files/bin/policy.sh b/packages/base/src/files/bin/policy.sh new file mode 100644 index 00000000..67f56caf --- /dev/null +++ b/packages/base/src/files/bin/policy.sh @@ -0,0 +1,154 @@ +#!/bin/bash + +### +# ============LICENSE_START======================================================= +# Base Package +# ================================================================================ +# Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============LICENSE_END========================================================= +### + +function usage() { + echo -n "syntax: $(basename $0) " + echo -n "[--debug] " + echo "status|start|stop" +} + +function check_x_file() { + if [[ $DEBUG == y ]]; then + echo "-- ${FUNCNAME[0]} --" + set -x + fi + + FILE=$1 + if [[ ! -f ${FILE} || ! -x ${FILE} ]]; then + return 1 + fi + + return 0 +} + +function policy_op() { + if [[ $DEBUG == y ]]; then + echo "-- ${FUNCNAME[0]} --" + set -x + fi + + operation=$1 + + cd $POLICY_HOME + echo "[drools-pdp-controllers]" + for binScript in bin/*-controller; do + if check_x_file "${binScript}"; then + trap "rm -f /tmp/out$$" EXIT + ${binScript} ${operation} >/tmp/out$$ + echo " L [${controller}]: $(sed ':a;N;$!ba;s/\n/ /g' /tmp/out$$)" + else + echo " L [${controller}]: -" + fi + done +} + +function policy_status() { + if [[ $DEBUG == y ]]; then + echo "-- ${FUNCNAME[0]} --" + set -x + fi + + policy_op "status" + + NUM_CRONS=$(crontab -l 2> /dev/null | wc -l) + echo " ${NUM_CRONS} cron jobs installed." + +} + +function policy_start() { + if [[ $DEBUG == y ]]; then + echo "-- ${FUNCNAME[0]} --" + set -x + fi + + policy_op "start" +} + + +function policy_stop() { + if [[ $DEBUG == y ]]; then + echo "-- ${FUNCNAME[0]} --" + set -x + fi + + policy_op "stop" +} + +######################################################################### +## +## script execution body +## +######################################################################### + +DEBUG=n +OPERATION=none + +until [[ -z "$1" ]]; do + case $1 in + -d|--debug|debug) DEBUG=y + set -x + ;; + -i|--status|status) OPERATION=status + ;; + -s|--start|start) OPERATION=start + ;; + -h|--stop|stop|--halt|halt) OPERATION=halt + ;; + *) usage + exit 1 + ;; + esac + shift +done + +# operation validation +case $OPERATION in + status) ;; + start) ;; + halt) ;; + *) echo "invalid operation \(${OPERATION}\): must be in {status|start|stop}"; + usage + exit 1 + ;; +esac + +if [[ -z ${POLICY_HOME} ]]; then + echo "error: POLICY_HOME is unset." + exit 1 +fi + +# operation validation +case $OPERATION in + status) + policy_status + ;; + start) + policy_start + ;; + halt) + policy_stop + ;; + *) echo "invalid operation \(${OPERATION}\): must be in {status|start|stop}"; + usage + exit 1 + ;; +esac |