#!/bin/bash
#
# Copyright (C) 2012, Red Hat, Inc.
#
# 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.
#

# The script supports the plugins below
declare -a SUPPORTED_PLUGINS=(cisco linuxbridge nicira openvswitch ryu)

#
# Print --help output and exit.
#
usage() {

cat << EOF
The helper script will install the necessary support for the selected plugin.

Usage: quantum-node-setup [options]
Options:
	--help        | -h
		Print usage information.
        --qpw <pw>    | -q <pw>
                Specify the password for the 'quantum' MySQL user that quantum will
                use to connect to the 'quantum' MySQL database.  By default,
                the password 'quantum' will be used.
	--yes         | -y
		In cases where the script would normally ask for confirmation
		before doing something, such as installing mysql-server,
		just assume yes.  This is useful if you want to run the script
		non-interactively.
        --user        | -u
		The quantum user. 
        --plugin      | -p
                The quantum plugin. Supported plugins:-
                    ${SUPPORTED_PLUGINS[*]}
        --dbhost        | -d
		The quantum database host. 
EOF

	exit 0
}

is_valid_plugin() {
	local i=
	for i in "${SUPPORTED_PLUGINS[@]}"; do
		if [ "$i" == "$1" ]; then
			return 0 
		fi
	done
	return 1
}

QUANTUM_USER=quantum
MYSQL_Q_PW=quantum
CISCO_CONF=/etc/quantum/plugins/cisco/db_conn.ini
LB_CONF=/etc/quantum/plugins/linuxbridge/linuxbridge_conf.ini
OVS_CONF=/etc/quantum/plugins/openvswitch/ovs_quantum_plugin.ini
# Nova specific
NOVA_CONF=/etc/nova/nova.conf

while [ $# -gt 0 ]
do
	case "$1" in
		-h|--help)
			usage
			;;
		-q|--qpw)
			shift
			MYSQL_Q_PW==${1}
			;;
		-y|--yes)
			ASSUME_YES="yes"
			;;
                -u|--user)
                        shift
                        QUANTUM_USER=${1}
                        ;;
                -p|--plugin)
                        shift
                        QUANTUM_PLUGIN=${1}
                        ;;
                -d|--dbhost)
                        shift
                        QUANTUM_HOST=${1}
                        ;;
		*)
			# ignore
			shift
			;;
	esac
	shift
done

# if the plugin is not defined
if [ -z ${QUANTUM_PLUGIN} ] ; then
        echo "Please select a plugin from: ${SUPPORTED_PLUGINS[*]}"
        echo "Choice:"
        read QUANTUM_PLUGIN
fi

# check that the plugin is valid
is_valid_plugin ${QUANTUM_PLUGIN}
if [ $? -ne 0 ]; then
        echo "Plugin '${QUANTUM_PLUGIN}' not supported. Supported plugins:-"
        echo "    ${SUPPORTED_PLUGINS[*]}"
        exit 0
fi

echo "Quantum plugin: ${QUANTUM_PLUGIN}"

if ! [ -e "/etc/quantum/plugins/${QUANTUM_PLUGIN}" ]; then
	echo "Please install the ${QUANTUM_PLUGIN} quantum plugin"
        exit 0
fi

#if the database hostname and is not defined and is required
if [ -z ${QUANTUM_HOST} ] ; then
	case "${QUANTUM_PLUGIN}" in
	"cisco"|"linuxbridge"|"openvswitch")
		echo "Please enter the Quantum database hostname:"
		read QUANTUM_HOST
		;;
	esac
fi

case "${QUANTUM_PLUGIN}" in
"cisco")
        openstack-config --set ${CISCO_CONF} DATABASE user ${QUANTUM_USER}
        openstack-config --set ${CISCO_CONF} DATABASE pass ${MYSQL_Q_PW}
        openstack-config --set ${CISCO_CONF} DATABASE host ${QUANTUM_HOST}
        SCHEDULER_DRIVER=quantum.plugins.cisco.nova.quantum_port_aware_scheduler.QuantumPortAwareScheduler
        LIBVIRT_VIF_DRIVER=quantum.plugins.cisco.nova.vifdirect.Libvirt802dot1QbhDriver
        LIBVIRT_VIF_TYPE=802.1Qbh
;;

"linuxbridge")
	openstack-config --set ${LB_CONF} DATABASE connection mysql
        openstack-config --set ${LB_CONF} DATABASE user ${QUANTUM_USER}
        openstack-config --set ${LB_CONF} DATABASE pass ${MYSQL_Q_PW}
	openstack-config --set ${LB_CONF} DATABASE host ${QUANTUM_HOST}
        LIBVIRT_VIF_TYPE=ethernet
        LIBVIRT_VIF_DRIVER=nova.virt.libvirt.vif.QuantumLinuxBridgeVIFDriver
        LINUX_INTERFACE_DRIVER=nova.network.linux_net.QuantumLinuxBridgeInterfaceDriver

        echo "Please enter network device for VLAN trunking:"
        read NETWORK_DEVICE
        openstack-config --set ${LB_CONF} LINUX_BRIDGE physical_interface ${NETWORK_DEVICE}
;;

"nicira")
	echo "Complete!"
        exit 0;
;;

"openvswitch")
        if ! rpm -q openvswitch > /dev/null
        then
                echo "Please install openvswitch"
                exit 0
        fi
        openstack-config --set ${OVS_CONF} DATABASE sql_connection mysql://${QUANTUM_USER}:${MYSQL_Q_PW}@${QUANTUM_HOST}/ovs_quantum
        LIBVIRT_VIF_TYPE=ethernet
        LIBVIRT_VIF_DRIVER=nova.virt.libvirt.vif.LibvirtOpenVswitchDriver
        LINUX_INTERFACE_DRIVER=nova.network.linux_net.LinuxOVSInterfaceDriver
;;

"ryu")
	echo "Complete!"
        exit 0;
;;

esac

echo "Would you like to update the nova configuration files? (y/n): "
read response
case "$response" in
y|Y)
        ;;
*)
        echo "Complete!"
        exit 0
esac

# If OpenStack is installed then configure nova.conf
if ! [ -e "${NOVA_CONF}" ]; then
        echo "Please install OpenStack compute and then set the value in /etc/nova/nova.conf DEFAULT section"
        echo "network_manager=nova.network.quantum.manager.QuantumManager"
        echo "quantum_connection_host=${QUANTUM_HOST}"
        echo "quantum_connection_port 9696"
        echo "quantum_use_dhcp True"
        echo "scheduler_driver ${SCHEDULER_DRIVER}"
        echo "libvirt_vif_driver ${LIBVIRT_VIF_DRIVER}"
        echo "libvirt_vif_type ${LIBVIRT_VIF_TYPE}"
        echo "linuxnet_interface_driver ${LINUX_INTERFACE_DRIVER}"
else
        openstack-config --set ${NOVA_CONF} DEFAULT network_manager nova.network.quantum.manager.QuantumManager
        openstack-config --set ${NOVA_CONF} DEFAULT quantum_connection_host ${QUANTUM_HOST}
        openstack-config --set ${NOVA_CONF} DEFAULT quantum_connection_port 9696
        openstack-config --set ${NOVA_CONF} DEFAULT quantum_use_dhcp True
	if [ ${SCHEDULER_DRIVER} ]; then
	        openstack-config --set ${NOVA_CONF} DEFAULT scheduler_driver ${SCHEDULER_DRIVER}
	fi
        if [ ${LIBVIRT_VIF_DRIVER} ]; then
	        openstack-config --set ${NOVA_CONF} DEFAULT libvirt_vif_driver ${LIBVIRT_VIF_DRIVER}
	fi
        if [ ${LIBVIRT_VIF_TYPE} ]; then
	        openstack-config --set ${NOVA_CONF} DEFAULT libvirt_vif_type ${LIBVIRT_VIF_TYPE}
	fi
        if [ ${LINUX_INTERFACE_DRIVER} ]; then
	        openstack-config --set ${NOVA_CONF} DEFAULT linuxnet_interface_driver ${LINUX_INTERFACE_DRIVER}
	fi
fi

echo "Configuration updates complete!"
