#!/bin/bash # do not rely on hostname(1), might return hostname of the # deployment system when initially deploying via chroot if [ -r /etc/hostname ] ; then hostname="$(cat /etc/hostname)" else hostname=$(hostname) fi if [ -z "${hostname}" ] ; then echo "Error: hostname could not be identified." >&2 exit 1 fi /etc/init.d/mysql start || true . /etc/mysql/sipwise.cnf if [ -z "${SIPWISE_DB_PASSWORD}" ] ; then echo 'Error: SIPWISE_DB_PASSWORD is unset (using /etc/mysql/sipwise.cnf).' >&2 exit 1 fi if ! mysql -e "GRANT ALL PRIVILEGES ON *.* TO sipwise@localhost IDENTIFIED BY '${SIPWISE_DB_PASSWORD}' WITH GRANT OPTION;" ; then echo 'Error: grants for user sipwise on localhost could not be applied.' >&2 exit 1 fi # support automated installation if [ -n "$AUTOMATED_INSTALL_MODE" ] ; then echo "Running in automated installation mode, ignoring check for empty db_schema." else if [ "$(mysql -usipwise -p${SIPWISE_DB_PASSWORD} -e 'use ngcp; select * from db_schema;' 2>/dev/null)" = "" ] ; then echo "=================================================================" echo "Warning: the db_schema table of the ngcp database is empty." echo "Are you sure you want to proceed with applying db-schema changes?" printf "Please type 'agree' to really continue: " unset AGREE read AGREE if [ "$AGREE" != "agree" ]; then echo "Exiting as requested." >&2 exit 1 fi fi fi if ! mysql -usipwise -p${SIPWISE_DB_PASSWORD} -e 'use ngcp;' 2>/dev/null ; then mysql -usipwise -p${SIPWISE_DB_PASSWORD} < /usr/share/ngcp-db-schema/db_scripts/init/0005_create_ngcp.up fi if ! mysql -usipwise -p${SIPWISE_DB_PASSWORD} -e 'use ngcp;' 2>/dev/null ; then echo 'Error: ngcp database does not exist.' >&2 exit 1 fi if ! mysql -usipwise -p${SIPWISE_DB_PASSWORD} ngcp -e 'describe db_schema' >/dev/null; then echo 'Error: db_schema table does not exit.' >&2 exit 1 fi if [ -x /usr/sbin/ngcp-check_active ] ; then if /usr/sbin/ngcp-check_active -q ; then echo "This seems to be the active node, nothing to do." exit 0 else # TODO - what about /usr/share/ngcp-upgrade-2.4/db/5670.up? echo "This seems to be the inactive node, registering revisions." fi fi revision_wrapper() { [ -n "$1" ] || return 1 for rev in $* ; do revname="$(basename $rev)" cd $(dirname $rev) || exit 1 # would fail if a script references a file (like language_strings.txt) in its CWD if ngcp-check-rev-applied --schema db_schema --revision "$revname" ; then echo "Revision $revname has been applied already, nothing to do." continue fi printf "Applying revision script %s: " "$rev" if mysql -usipwise -p${SIPWISE_DB_PASSWORD} < "$rev" ; then echo done else echo "failed. :(" exit 1 fi if mysql -usipwise -p${SIPWISE_DB_PASSWORD} ngcp -e "insert into db_schema values (0, '${revname}', \""${hostname}"\", CURRENT_TIMESTAMP);" ; then echo "Marked revision $rev for being applied." else echo "Error while executing DB commands using revision $rev for host $hostname" >&2 exit 1 fi done } revision_wrapper /usr/share/ngcp-db-schema/db_scripts/base/*.up revision_wrapper /usr/share/ngcp-db-schema/db_scripts/diff/*.up # TODO language_wrapper ## END OF FILE #################################################################