#!/bin/sh
#
# This script checks to make sure the database used by Neutron is at the
# correct version.  If the database is not at $NEUTRON_EXPECTED_DB_VERSION, as
# set in /etc/sysconfig/openstack-neutron, log an error and exit with an error
# code.
#
# This script is meant to be called from an init script or via ExecPreStart in
# a systemd unit.

RETCODE=0

[ -f /etc/sysconfig/openstack-neutron ] && . /etc/sysconfig/openstack-neutron

[ "$NEUTRON_EXPECTED_DB_VERSION" ] || exit 0

neutron_db_version=$(
		neutron-db-manage \
			--config-file /etc/neutron/neutron.conf \
			--config-file /etc/neutron/plugin.ini current 2>/dev/null |
		awk '{print $NF}')

if [ "x$neutron_db_version" = "x" ]; then
	logger -s -p daemon.error -t neutron-db-check \
		"ERROR: unable to find version information for the neutron database."\
		"Check your logs for additional information."

	RETCODE=1
elif [ "x$neutron_db_version" = "xNone" ]; then
	logger -s -p daemon.error -t neutron-db-check \
		"ERROR: neutron database does not have version information."\
		"You must run the \"neutron-db-manage stamp\" command before"\
		"starting neutron services."

	cat <<-EOF >&2
	
	If you are upgrading from a Grizzly installation, the command would
	look like this:

	neutron-db-manage \\
	    --config-file /usr/share/neutron/neutron-dist.conf \\
	    --config-file /etc/neutron/neutron.conf \\
	    --config-file /etc/neutron/plugin.ini stamp grizzly

	You will then need to upgrade the database by running:

	openstack-db --service neutron --update

	If you have an existing Havana installation, you would run:

	neutron-db-manage \\
	    --config-file /usr/share/neutron/neutron-dist.conf \\
	    --config-file /etc/neutron/neutron.conf \\
	    --config-file /etc/neutron/plugin.ini stamp havana
	EOF

	RETCODE=1
elif [ "x$neutron_db_version" != "x$NEUTRON_EXPECTED_DB_VERSION" ]; then
	logger -s -p daemon.error -t neutron-db-check \
		"ERROR: you must upgrade the neutron database before starting"\
		"neutron services."

	cat <<-EOF >&2
	
	You can upgrade the neutron database by running:

	openstack-db --service neutron --update
	EOF

	RETCODE=1
fi

exit $RETCODE
