#!/bin/sh
set -e

. /usr/share/debconf/confmodule

NOVA_CONF=/etc/nova/nova.conf

#PKGOS-INCLUDE#

manage_nova_my_ip () {
	pkgos_inifile get ${NOVA_CONF} DEFAULT my_ip
	if [ -n "${RET}" ] && [ ! "${RET}" = "NOT_FOUND" ] ; then
		db_set nova/my-ip "${RET}"
	else
		DEFROUTE_IF=`awk '{ if ( $2 == "00000000" ) print $1 }' /proc/net/route | head -n 1`
		if [ -n "${DEFROUTE_IF}" ] ; then
			DEFROUTE_IP=$(LC_ALL=C ip addr show "${DEFROUTE_IF}" | grep inet | head -n 1 | awk '{print $2}' | cut -d/ -f1 | grep -E '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$' || true)
		fi
		if [ -z "${DEFROUTE_IP}" ] ; then
			HOSTNAME_IP=$(hostname -i)
			if [ -n "${HOSTNAME_IP}" ] ; then
				DEFROUTE_IP=${HOSTNAME_IP}
			fi
		fi
		if [ -n "${DEFROUTE_IP}" ] ; then
			db_set nova/my-ip ${DEFROUTE_IP}
		fi
	fi
	db_input high nova/my-ip || true
	db_go
}

read_placement_config () {
	# If we want to setup placement correctly, we need
	# to have keystone_auth url specified from user correctly.
	# If user didn't configure ksat via debconf, we will not configure
	# placement as well
	db_get nova/configure_ksat
	if  [ "${RET}" = "true" ] ; then
		db_input high nova/configure_placement || true
		db_go
		db_get nova/configure_placement
		if  [ "${RET}" = "true" ] ; then
			pkgos_read_config -p high ${NOVA_CONF} placement project_name nova/placement_admin_tenant_name
			pkgos_read_config -p high ${NOVA_CONF} placement region_name nova/placement_os_region_name
			pkgos_read_config -p high ${NOVA_CONF} placement username nova/placement_admin_username
			pkgos_read_config -p high ${NOVA_CONF} placement password nova/placement_admin_password
		fi
	fi
}

read_neutron_config () {
	# If we want to setup neutron correctly, we need
	# to have keystone_auth url specified from user correctly.
	# If user didn't configure ksat via debconf, we will not configure
	# neutron as well
	db_get nova/configure_ksat
	if  [ "${RET}" = "true" ] ; then
		db_input high nova/configure_neutron || true
		db_go
		db_get nova/configure_neutron
		if  [ "${RET}" = "true" ] ; then
			# Keystone auth credential to talk to Neutron
			pkgos_read_config -p high ${NOVA_CONF} neutron endpoint_override nova/neutron_url
			pkgos_read_config -p medium ${NOVA_CONF} neutron project_name nova/neutron_admin_tenant_name
			pkgos_read_config -p medium ${NOVA_CONF} neutron username nova/neutron_admin_username
			pkgos_read_config -p high ${NOVA_CONF} neutron password nova/neutron_admin_password

			# The metadata_proxy_shared_secret
			pkgos_read_config -p high ${NOVA_CONF} neutron metadata_proxy_shared_secret nova/metadata_secret
		fi
	fi
}

pkgos_var_user_group nova /bin/sh
pkgos_dbc_read_conf -pkg nova-common ${NOVA_CONF} database connection nova $@
pkgos_rabbit_read_conf ${NOVA_CONF} oslo_messaging_rabbit nova
pkgos_read_admin_creds ${NOVA_CONF} keystone_authtoken nova
read_neutron_config
read_placement_config

manage_nova_my_ip

# Cinder os_region_name config
pkgos_read_config -p high ${NOVA_CONF} cinder os_region_name nova/cinder_os_region_name
# Glance api_servers config
pkgos_read_config -p high ${NOVA_CONF} glance api_servers nova/glance_api_servers

exit 0
