ONAP log files consolidation
[policy/engine.git] / packages / base / src / files / install / mysql / bin / db_upgrade_remote.sh
1 #!/bin/bash 
2 ###
3 # ============LICENSE_START=======================================================
4 # ONAP Policy Engine
5 # ================================================================================
6 # Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
7 # ================================================================================
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
11 #
12 #      http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 # ============LICENSE_END=========================================================
20
21 ###
22 #
23 # db_upgrade_remote.sh: This script is to perform database schema upgrade on remote db, 
24 #                       no shecma downgrade will be performed in case db_version is higher then target_version
25
26 # Logic: 1. Get target schema version from db scripts in $POLICY_HOME/data/mysql
27 #        2. Get current db schema version from support.db_version table (of target system)
28 #        3. Apply db upgrade script in order if target_version is HIGHER than db_version
29 #        4. Print out warning message        if target_version is LOWER than db_version
30 #        4. Print out message                if target_version is EQUAL to db_version
31 #
32 #
33 # Usage  : db_upgrade_remote.sh db_user_id  db_user_password hostname
34 # Example: db_upgrade_remote.sh policy_user password         localhost.com
35 #
36 # Assumption: 1. DB schema upgrade script in $POLICY_HOME/data/mysql folder with read permission
37 #             2. DB user has privilege to create/drop/alter database table
38 #
39 # Note: The default location for db schema upgrade script is $POLICY_HOME/data/mysql
40 #       The release level is represented as Two-digit-Year+Two-digit-Month+two-digit-Sub-release (151000, 151001)
41 #
42 #
43
44 TARGET_SCHEMA_VERSION=""
45 CURRENT_SCHEMA_VERSION=""
46 DB_UPGRADE_USER=""
47 DB_UPGRADE_PASSWORD=""
48 DB_HOSTNAME=""
49 DB_UPGRADE_DIR=$POLICY_HOME/data/mysql
50 DATE=`date +"%Y%m%d%H%M%S"`
51 LOG=""
52 ERR=""
53
54 function get_current_schema_version
55 {
56   echo "Get current schema version from [${DB_HOSTNAME}] started ...@`date`" | tee -a $LOG
57   # display output vertical
58   query="select version from support.db_version where the_key = 'VERSION' \G"
59   CURRENT_SCHEMA_VERSION=`${MYSQL} --skip-column-names --execute "${query}" 2>$ERR | grep -v "*"`
60   error_msg=`cat $ERR | grep "doesn't exist"`
61   if [ "${error_msg}" != "" ]; then 
62     echo "Create support.db_version table ..." | tee -a $LOG
63     sql="create database if not exists support;"
64     ${MYSQL} --execute "${sql}"
65     sql="create table support.db_version(the_key varchar(20) not null, version varchar(20), primary key(the_key));"
66     ${MYSQL} --execute "${sql}" 
67     CURRENT_SCHEMA_VERSION="00"
68   fi
69   echo "CURRENT_SCHEMA_VERSION: [$CURRENT_SCHEMA_VERSION]" | tee -a $LOG
70   echo "Get current schema version from [${DB_HOSTNAME}] completed ...@`date`" | tee -a $LOG
71 }
72
73 function get_target_schema_version
74 {
75   UPGRADE_LIST=/tmp/db_upgrade_list.$$
76   find ${DB_UPGRADE_DIR} -name "*_upgrade_script.sql" 2>/dev/null | grep -v "droolspdp" | sort -r | head -1 > $UPGRADE_LIST
77   while read -r file
78   do
79     TARGET_SCHEMA_VERSION=`basename $file | cut -d'_' -f1`
80     echo "TARGET_SCHEMA_VERSION: [$TARGET_SCHEMA_VERSION]" | tee -a $LOG
81     break
82   done < $UPGRADE_LIST
83   rm -f $UPGRADE_LIST
84 }
85
86 function evaluate_upgrade_downgrade
87 {
88   echo "CURRENT_SCHEMA_VERSION --> [$CURRENT_SCHEMA_VERSION]" | tee -a $LOG
89   echo "TARGET_SCHEMA_VERSION  --> [$TARGET_SCHEMA_VERSION] " | tee -a $LOG
90   if [[ "${CURRENT_SCHEMA_VERSION}" < "${TARGET_SCHEMA_VERSION}" ]]; then 
91     # perform db upgrade
92     UPGRADE_LIST=/tmp/db_upgrade_list.$$
93     find ${DB_UPGRADE_DIR} -name "*_upgrade_script.sql" 2>/dev/null | grep -v "droolspdp" | sort > $UPGRADE_LIST
94     while read -r file
95     do
96       DB_VERSION=`basename $file | cut -d'_' -f1`
97       #echo "[$DB_VERSION] [$TARGET_SCHEMA_VERSION]" | tee -a $LOG
98       if [ "${DB_VERSION}" -gt "${CURRENT_SCHEMA_VERSION}" ] && [ "${DB_VERSION}" -le "${TARGET_SCHEMA_VERSION}" ]; then
99         run_script "UPGRADE" "${file}" 2>&1 | tee -a $LOG
100       fi
101     done < $UPGRADE_LIST
102     rm -f $UPGRADE_LIST
103     set_current_release_level $TARGET_SCHEMA_VERSION
104   elif [[ "${CURRENT_SCHEMA_VERSION}" > "${TARGET_SCHEMA_VERSION}" ]]; then 
105     # db downgrade
106     echo "WARNING: Target db schema version is LOWER than current db scema version, please run downgrade script manually." | tee -a $LOG | tee -a $ERR
107   else
108     echo "CURRENT SCHEMA VERSION THE SAME AS TARGET SCHEMA VERSION, NO ACTION TAKEN ..." | tee -a $LOG
109   fi
110 }
111
112 function run_script
113 {
114   action="${1}"
115   script="${2}"
116   echo "Perform DB $action on [${DB_HOSTNAME}] use $script ..." | tee -a $LOG
117   echo "--" | tee -a $LOG
118   ${MYSQL} --verbose < "${script}" 2>$ERR | tee -a $LOG
119   echo "--" | tee -a $LOG
120 }
121
122 function set_current_release_level
123 {
124   DB_VERSION="${1}"
125   echo "Set current release level on [${DB_HOSTNAME}] to [$DB_VERSION] started ...@`date`" | tee -a $LOG
126   update_statement="insert into support.db_version (the_key, version) values ('VERSION', '${DB_VERSION}') on duplicate key update version='${DB_VERSION}';"
127   ${MYSQL} --execute "${update_statement}" 
128
129   echo "" | tee -a $LOG
130   echo "CURRENT_SCHEMA_VERSION set to: [$DB_VERSION]" | tee -a $LOG
131   echo "" | tee -a $LOG
132   echo "Set current release level on [${DB_HOSTNAME}] to [$DB_VERSION] completed ...@`date`" | tee -a $LOG
133 }
134
135 function check_directory
136 {
137   if [ ! -d $DB_UPGRADE_DIR ]; then
138     echo "ERROR, DIRECTORY NOT EXIST: $DB_UPGRADE_DIR, PROCESS EXIT ..."
139     exit;
140   else
141     if [ ! -d $DB_UPGRADE_DIR/logs ]; then
142       mkdir $DB_UPGRADE_DIR/logs
143     fi
144   fi
145 }
146
147 # MAIN
148 #check_directory
149 if [ -z ${POLICY_LOGS} ]; then
150   POLICY_LOGS=/var/log/onap
151 fi
152 mkdir -p $POLICY_LOGS/policy/db
153 LOG=$POLICY_LOGS/policy/db/db_upgrade_remote_$DATE.log
154 ERR=$POLICY_LOGS/policy/db/db_upgrade_remote_$DATE.err
155 echo "db_upgrade_remote.sh started ..." | tee -a $LOG
156 if [ $# -eq 3 ]; then 
157   DB_UPGRADE_USER="${1}"
158   DB_UPGRADE_PASSWORD="${2}"
159   DB_HOSTNAME="${3}"
160   echo "DB_UPGRADE_USER: $DB_UPGRADE_USER" | tee -a $LOG
161   echo "DB_UPGRADE_DIR : $DB_UPGRADE_DIR"  | tee -a $LOG
162   echo "DB_HOSTNAME    : $DB_HOSTNAME"     | tee -a $LOG
163   #
164   typeset -r MYSQL="mysql -u${DB_UPGRADE_USER} -p${DB_UPGRADE_PASSWORD} -h ${DB_HOSTNAME}";
165   get_target_schema_version
166   if [ ${#TARGET_SCHEMA_VERSION} -ne 6 ]; then 
167     echo "ERROR, TARGET_SCHEMA_VERSION MUST BE 6 DIGITS: $TARGET_SCHEMA_VERSION" | tee -a $LOG | tee -a $ERR
168   else
169     get_current_schema_version
170     evaluate_upgrade_downgrade
171   fi
172 else
173   echo "Usage  : db_upgrade_remote.sh db_user_id   db_user_password db_hostname" | tee -a $LOG
174   echo "Example: db_upgrade_remote.sh policy_user  password         localhost.com" | tee -a $LOG
175 fi
176
177 echo "db_upgrade_remote.sh completed ..." | tee -a $LOG