#!/bin/bash

set -e

export PKGOS_VERBOSE=yes

ACTION=${1}

if ! [ -r /usr/share/openstack-deploy/preseed-lib ] ; then
	echo "Can't find /usr/share/openstack-deploy/pressed-lib: exiting"
	exit 1
fi
. /usr/share/openstack-deploy/preseed-lib

OSINSTALL_RC=osinstallrc

osinstall_gen_pass () {
	GENERATED_PASSWORD=$(dd if=/dev/random bs=64 count=1 2>|/dev/null | md5sum | awk '{print $1}')
}

osinstall_check_installed () {
	local PKG_NAME
	PKG_NAME=${1}
	INSTALL_STRING=`dpkg -l ${PKG_NAME} | grep ^ii | awk '{print $2}'`
	if [ "${INSTALL_STRING}" = ${PKG_NAME} ] ; then
		return 0
	else
		return 1
	fi
}

osinstall_install_if_not_installed () {
	local PKG_NAME
	PKG_NAME=${1}
	if ! osinstall_check_installed ${PKG_NAME} ; then
		DEBIAN_FRONTEND=noninteractive ${APTGET} install ${PKG_NAME}
	fi
}

mysql_version () {
	MYSQL_VERSION=$(apt-cache policy mysql-server  |grep Candidate |awk '{print $2}' | cut -c 1-3)
}

osinstall_rcvalue () {
	local RC_VARNAME RC_VARCONTENT RC_MESSAGE RC_ISPASS RC_DEFAULT
	RC_VARNAME=${1}
	RC_MESSAGE=${2}
	RC_ISPASS=${3}
	RC_DEFAULT=${4}
	RC_VARCONTENT=${!RC_VARNAME}	# TODO: find a way to work out this in ISO sh
	# using something like: eval "RC_VARCONTENT=\$$RC_VARNAME"
	if [ -z "${RC_VARCONTENT}" ] ; then
		if [ "${RC_ISPASS}" = "yes" ] && [ "${OS_INTERACTIVE}" = "no" ] ; then
			echo "Skipping entering pass in non-interactive mode"
		elif [ -n "${RC_DEFAULT}" ] && [ "${OS_INTERACTIVE}" = "no" ] ; then
			echo "Skipping user customization of default value in non-interactive mode"
		else
			echo -n "${RC_MESSAGE}"
			read RC_VARCONTENT
		fi
		if [ -z "${RC_VARCONTENT}" ]  && [ "${RC_ISPASS}" = "yes" ] ; then
			osinstall_gen_pass
			RC_VARCONTENT=${GENERATED_PASSWORD}
		elif [ -z "${RC_VARCONTENT}" ]  && [ -n "${RC_DEFAULT}" ] ; then
			RC_VARCONTENT=${RC_DEFAULT}
		else
			while [ -z "${RC_VARCONTENT}" ] ; do
				echo -n "${RC_MESSAGE}"
				read RC_VARCONTENT
			done
		fi
		echo "${RC_VARNAME}=${RC_VARCONTENT}" >>${OSINSTALL_RC}
		. ${OSINSTALL_RC}
	fi
}

usage () {
	echo "${0} [--non-interactive] <services|controller|compute|all-in-one|fuel-master-node>"
	exit 1
}

if [ -r ${OSINSTALL_RC} ] ; then
	. ${OSINSTALL_RC}
else
	echo "" >${OSINSTALL_RC}
fi

osinstall_mysql_server () {
	osinstall_rcvalue RC_MYSQL_SERVER_PKG_NAME "MySQL package name [mysql-server-${MYSQL_VERSION}]: " no mysql-server-${MYSQL_VERSION}
	osinstall_rcvalue RC_MYSQL_PASSWORD "MySQL server password [generated-password]: " yes
	if ! osinstall_check_installed ${RC_MYSQL_SERVER_PKG_NAME} ; then
echo "${RC_MYSQL_SERVER_PKG_NAME} mysql-server/root_password password ${RC_MYSQL_PASSWORD}
${RC_MYSQL_SERVER_PKG_NAME} mysql-server/root_password seen true
${RC_MYSQL_SERVER_PKG_NAME} mysql-server/root_password_again password ${RC_MYSQL_PASSWORD}
${RC_MYSQL_SERVER_PKG_NAME} mysql-server/root_password_again seen true
" | debconf-set-selections
		${APTGET} install ${RC_MYSQL_SERVER_PKG_NAME}
		# The default of 100 max connections will fail when installing
		# all of OpenStack on a single node, if that node has too many
		# cores (as OpenStack in general, spawns more threads if more cores).
		# So we increase max_connections from 100 to 1000, which is more
		# than enough for big servers.
		sed -i -e 's/^[#]*max_connections[ \t]*=.*/max_connections = 1000/' /etc/mysql/my.cnf
		invoke-rc.d mysql restart
	fi
}

osinstall_mysql_set_host () {
	osinstall_rcvalue RC_MYSQL_SERVER_HOSTNAME "MySQL server hostname []: " no
}

osinstall_mysql_set_localhost () {
	. ${OSINSTALL_RC}
	if [ -z "${RC_MYSQL_SERVER_HOSTNAME}" ] ; then
		echo "RC_MYSQL_SERVER_HOSTNAME=localhost" >>${OSINSTALL_RC}
	fi
	. ${OSINSTALL_RC}
}

osinstall_pgsql_server () {
	osinstall_rcvalue RC_PGSQL_SERVER_PKG_NAME "postgresql package name [postgresql-9.4]: " no postgresql-9.4
	if ! osinstall_check_installed ${RC_PGSQL_SERVER_PKG_NAME} ; then
		${APTGET} install ${RC_PGSQL_SERVER_PKG_NAME}
	fi
}

osinstall_install_services () {
	os_pressed_misc
	mysql_version
	osinstall_mysql_server
	osinstall_install_if_not_installed mongodb
	osinstall_install_if_not_installed rabbitmq-server
	osinstall_install_if_not_installed ntp
	osinstall_install_if_not_installed openstack-cloud-services
}

osinstall_install_fuel_services () {
	os_pressed_misc
	osinstall_mysql_server
	osinstall_pgsql_server
	osinstall_install_if_not_installed rabbitmq-server
	osinstall_install_if_not_installed fuel-master-node-services
}

osinstall_rabbit_host_and_pass () {
	osinstall_rcvalue RC_RABIT_HOST "Rabbit-MQ host [127.0.0.1]: " no 127.0.0.1
	osinstall_rcvalue RC_RABIT_USER "Rabbit-MQ user [guest]: " no guest
	osinstall_rcvalue RC_RABIT_PASS "Rabbit-MQ password [generated-password]: " yes
	if [ "${RC_RABIT_HOST}" = 127.0.0.1 ] && [ -x /usr/sbin/rabbitmqctl ] ; then
		rabbitmqctl change_password guest ${RC_RABIT_PASS}
	fi
}

osinstall_rcvalue_keystone_endpoint () {
	DEFROUTE_IF=`LC_ALL=C /sbin/route | grep default |awk -- '{ print $8 }' | cut -d" " -f1`
	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}$'`
	fi
	osinstall_rcvalue RC_KEYSTONE_ENDPOINT_IP "Keystone endpoint IP address [${DEFROUTE_IP}]: " no ${DEFROUTE_IP}
	osinstall_rcvalue RC_KEYSTONE_REGION "Keystone endpoint region [regionOne]: " no regionOne
}

osinstall_keystone () {
	if [ -z "${RC_KEYSTONE_ENDPOINT_IP}" ] ; then
		osinstall_rcvalue_keystone_endpoint
	fi
	osinstall_rcvalue RC_KEYSTONE_AUTHTOKEN "Keystone authtoken [generated-password]: " yes
	osinstall_rcvalue RC_KEYSTONE_ADMINPASS "Keystone admin tenant password [generated-password]: " yes
	osinstall_rcvalue RC_KEYSTONE_SQL_PASS "Keystone MySQL password [generated-password]: " yes
	os_install_keystone ${RC_KEYSTONE_AUTHTOKEN} ${RC_KEYSTONE_ADMINPASS} ${RC_KEYSTONE_ENDPOINT_IP} ${RC_KEYSTONE_REGION} \
		${RC_KEYSTONE_SQL_PASS} ${RC_MYSQL_PASSWORD} ${RC_MYSQL_SERVER_HOSTNAME}
}

osinstall_preseed_all () {
	osinstall_rcvalue RC_GLANCE_SQL_PASS "Glance MySQL password [generated-password]: " yes
	os_preseed_glance ${RC_GLANCE_SQL_PASS} ${RC_MYSQL_PASSWORD} ${RC_MYSQL_SERVER_HOSTNAME} \
		${RC_KEYSTONE_ENDPOINT_IP} ${RC_KEYSTONE_REGION} ${RC_KEYSTONE_ADMINPASS} ${RC_KEYSTONE_AUTHTOKEN} \
		${RC_RABIT_HOST} ${RC_RABIT_USER} ${RC_RABIT_PASS}
	osinstall_rcvalue RC_NEUTRON_SQL_PASS "Neutron MySQL password [generated-password]: " yes
	osinstall_rcvalue RC_METADATA_SHARED_SECRET "Metadata proxy shared secret [generated-password]: " yes

	# Get the tenant_id which is needed by neutron
	ADMIN_TENANT_ID=$(openstack --os-tenant-name admin --os-username admin --os-password ${RC_KEYSTONE_ADMINPASS} --os-auth-url http://${RC_KEYSTONE_ENDPOINT_IP}:35357/v2.0 project show admin | grep id | awk '{print $4}')
	# TODO: make it work with any IP for the nova-api server (eg: replace the 127.0.0.1 below)
	os_preseed_neutron ${RC_NEUTRON_SQL_PASS} ${RC_MYSQL_PASSWORD} ${RC_MYSQL_SERVER_HOSTNAME} \
		${RC_KEYSTONE_ENDPOINT_IP} ${RC_KEYSTONE_REGION} ${RC_KEYSTONE_ADMINPASS} ${RC_KEYSTONE_AUTHTOKEN} \
		${RC_RABIT_HOST} ${RC_RABIT_USER} ${RC_RABIT_PASS} ${RC_METADATA_SHARED_SECRET} http://127.0.0.1:8774/v2 ${ADMIN_TENANT_ID}
	osinstall_rcvalue RC_NOVA_SQL_PASS "Nova MySQL password [generated-password]: " yes
	osinstall_rcvalue RC_NOVAAPI_SQL_PASS "Nova API MySQL password [generated-password]: " yes
	os_preseed_nova ${RC_NOVA_SQL_PASS} ${RC_NOVAAPI_SQL_PASS} ${RC_MYSQL_PASSWORD} ${RC_MYSQL_SERVER_HOSTNAME} \
		${RC_KEYSTONE_ENDPOINT_IP} ${RC_KEYSTONE_REGION} ${RC_KEYSTONE_ADMINPASS} ${RC_KEYSTONE_AUTHTOKEN} \
		${RC_RABIT_HOST} ${RC_RABIT_USER} ${RC_RABIT_PASS} ${RC_METADATA_SHARED_SECRET}
	osinstall_rcvalue RC_CINDER_SQL_PASS "Cinder MySQL password [generated-password]: " yes
	os_preseed_cinder ${RC_CINDER_SQL_PASS} ${RC_MYSQL_PASSWORD} ${RC_MYSQL_SERVER_HOSTNAME} \
		${RC_KEYSTONE_ENDPOINT_IP} ${RC_KEYSTONE_REGION} ${RC_KEYSTONE_ADMINPASS} ${RC_KEYSTONE_AUTHTOKEN} \
		${RC_RABIT_HOST} ${RC_RABIT_USER} ${RC_RABIT_PASS}
	os_preseed_ceilometer ${RC_KEYSTONE_ENDPOINT_IP} ${RC_KEYSTONE_REGION} ${RC_KEYSTONE_ADMINPASS} ${RC_KEYSTONE_AUTHTOKEN} \
		${RC_RABIT_HOST} ${RC_RABIT_USER} ${RC_RABIT_PASS}
	osinstall_rcvalue RC_HEAT_SQL_PASS "Heat MySQL password [generated-password]: " yes
	os_preseed_heat ${RC_HEAT_SQL_PASS} ${RC_MYSQL_PASSWORD} ${RC_MYSQL_SERVER_HOSTNAME} \
		${RC_KEYSTONE_ENDPOINT_IP} ${RC_KEYSTONE_REGION} ${RC_KEYSTONE_ADMINPASS} ${RC_KEYSTONE_AUTHTOKEN} \
		${RC_RABIT_HOST} ${RC_RABIT_USER} ${RC_RABIT_PASS}
	osinstall_rcvalue RC_AODH_SQL_PASS "Aodh MySQL password [generated-password]: " yes
	os_preseed_aodh ${RC_AODH_SQL_PASS} ${RC_MYSQL_PASSWORD} ${RC_MYSQL_SERVER_HOSTNAME} \
		${RC_KEYSTONE_ENDPOINT_IP} ${RC_KEYSTONE_REGION} ${RC_KEYSTONE_ADMINPASS} ${RC_KEYSTONE_AUTHTOKEN} \
		${RC_RABIT_HOST} ${RC_RABIT_USER} ${RC_RABIT_PASS}
	os_pressed_horizon
}

osinstall_preseed_compute () {
	osinstall_rcvalue RC_NOVA_SQL_PASS "Nova MySQL password [generated-password]: " yes
	os_preseed_nova ${RC_NOVA_SQL_PASS} ${RC_MYSQL_PASSWORD} ${RC_MYSQL_SERVER_HOSTNAME} \
		${RC_KEYSTONE_ENDPOINT_IP} ${RC_KEYSTONE_REGION} ${RC_KEYSTONE_ADMINPASS} ${RC_KEYSTONE_AUTHTOKEN} \
		${RC_RABIT_HOST} ${RC_RABIT_USER} ${RC_RABIT_PASS}
	os_preseed_ceilometer ${RC_KEYSTONE_ENDPOINT_IP} ${RC_KEYSTONE_REGION} ${RC_KEYSTONE_ADMINPASS} ${RC_KEYSTONE_AUTHTOKEN} \
		${RC_RABIT_HOST} ${RC_RABIT_USER} ${RC_RABIT_PASS}
	osinstall_rcvalue RC_NEUTRON_SQL_PASS "Neutron MySQL password [generated-password]: " yes
	os_preseed_neutron ${RC_NEUTRON_SQL_PASS} ${RC_MYSQL_PASSWORD} ${RC_MYSQL_SERVER_HOSTNAME} \
		${RC_KEYSTONE_ENDPOINT_IP} ${RC_KEYSTONE_REGION} ${RC_KEYSTONE_ADMINPASS} ${RC_KEYSTONE_AUTHTOKEN} \
		${RC_RABIT_HOST} ${RC_RABIT_USER} ${RC_RABIT_PASS}
}

osinstall_preseed_more_services () {
	OS_OTHER_PRESEED_PARAMS="${RC_MYSQL_PASSWORD} ${RC_MYSQL_SERVER_HOSTNAME} ${RC_KEYSTONE_ENDPOINT_IP} ${RC_KEYSTONE_REGION} ${RC_KEYSTONE_ADMINPASS} ${RC_KEYSTONE_AUTHTOKEN}"
	OS_RABBIT_PRESEED_PARAMS="${RC_RABIT_HOST} ${RC_RABIT_USER} ${RC_RABIT_PASS}"
	OS_ALL_PRESEED_PARAMS="${OS_OTHER_PRESEED_PARAMS} ${OS_RABBIT_PRESEED_PARAMS}"

	osinstall_rcvalue RC_DESIGNATE_SQL_PASS "Designate MySQL password [generated-password]: " yes
	os_preseed_designate ${RC_DESIGNATE_SQL_PASS} ${OS_ALL_PRESEED_PARAMS}

	osinstall_rcvalue RC_BARBICAN_SQL_PASS "Barbican MySQL password [generated-password]: " yes
	os_preseed_barbican ${RC_BARBICAN_SQL_PASS} ${OS_ALL_PRESEED_PARAMS}

	osinstall_rcvalue RC_MANILA_SQL_PASS "Manila MySQL password [generated-password]: " yes
	os_preseed_manila ${RC_MANILA_SQL_PASS} ${OS_ALL_PRESEED_PARAMS}

	osinstall_rcvalue RC_MISTRAL_SQL_PASS "Mistral MySQL password [generated-password]: " yes
	os_preseed_mistral ${RC_MISTRAL_SQL_PASS} ${OS_ALL_PRESEED_PARAMS}

	osinstall_rcvalue RC_TROVE_SQL_PASS "Trove MySQL password [generated-password]: " yes
	os_preseed_trove ${RC_TROVE_SQL_PASS} ${OS_ALL_PRESEED_PARAMS}

	# Note: Congress doesn't use Rabbit (yet?)
	osinstall_rcvalue RC_CONGRESS_SQL_PASS "Congress MySQL password [generated-password]: " yes
	os_preseed_congress ${RC_CONGRESS_SQL_PASS} ${OS_OTHER_PRESEED_PARAMS}

	osinstall_rcvalue RC_SAHARA_SQL_PASS "Sahara MySQL password [generated-password]: " yes
	os_preseed_sahara ${RC_SAHARA_SQL_PASS} ${OS_ALL_PRESEED_PARAMS}

	osinstall_rcvalue RC_MURANO_SQL_PASS "Murano MySQL password [generated-password]: " yes
	os_preseed_murano ${RC_MURANO_SQL_PASS} ${OS_ALL_PRESEED_PARAMS}
}

osinstall_build_fuel_bootstrap_iso () {
	osinstall_install_if_not_installed fuel-bootstrap-iso
	if ! [ -d /var/lib/fuel-bootstrap-iso/fs/fuel-bootstrap-iso/live ] ; then
		build-fuel-bootstrap-iso
	fi
}

osinstall_preseed_master_node_values () {
#	osinstall_rcvalue RC_COBBLER_PASS "Cobbler admin password [generated-password]: " yes
	osinstall_rcvalue RC_COBBLER_PASS "Cobbler admin password [cobbler]: " no cobbler
	echo "cobbler cobbler/password password ${RC_COBBLER_PASS}
cobbler cobbler/password seen true
postgresql-common postgresql-common/ssl boolean false
postgresql-common postgresql-common/ssl seen true
nailgun-common fuel/configure_db boolean true
nailgun-common fuel/configure_db seen true
" | debconf-set-selections
}

osinstall_fix_master_node_setup () {
	# Make sure Apache is using SSL
	a2enmod ssl
	a2ensite default-ssl.conf
	# Make sure apache is up, otherwise, cobbler will fail
	apache2ctl start || true
	apache2ctl reload || true

	# TODO: fix this by packaging rabbitmqadmin within the rabbitmq-server package.
	# This is already commited to git on Alioth, though a backport should be done.
	echo "===> Downloading rabbitmqadmin to declare queues and exchanges"
	wget https://raw.githubusercontent.com/rabbitmq/rabbitmq-management/rabbitmq_v3_6_0/bin/rabbitmqadmin -O /usr/bin/rabbitmqadmin
	chmod +x /usr/bin/rabbitmqadmin

	# Set defaults for mcollective
	# TODO: generate the password
	MCO_VHOST=mcollective
	MCO_USERNAME=mcollective
	MCO_PASSWORD=marionette

	# Set defaults for astute
	# TODO: generate the password
	ASTUTE_RABBIT_USERNAME=naily
	ASTUTE_RABBIT_PASS=naily

	# Setup rabbitmq
	echo "===> Enabling rabbitmq"
	# The management is needed for rabbitmqadmin to work
	rabbitmq-plugins enable rabbitmq_management
	# stomp is what mcollective uses
	rabbitmq-plugins enable rabbitmq_stomp
	# Enabling plugins need a restart
	service rabbitmq-server restart

	# Add the mcollective and naily users
	rabbitmqctl add_user ${MCO_USERNAME} ${MCO_PASSWORD}
	rabbitmqctl add_user ${ASTUTE_RABBIT_USERNAME} ${ASTUTE_RABBIT_PASS}

	# Fuel on CentOS have these users as administrators, so let's do the same
	rabbitmqctl set_user_tags mcollective administrator
	rabbitmqctl set_user_tags ${ASTUTE_RABBIT_USERNAME} administrator

	# Fuel needs 2 queues, on the / vhost:
	rabbitmqadmin --username=guest --password=${RC_RABIT_PASS} --host=localhost declare queue name=nailgun durable=true
	rabbitmqadmin --username=guest --password=${RC_RABIT_PASS} --host=localhost declare queue name=naily durable=true
	# And also 2 exchanges:
	rabbitmqadmin --username=guest --password=${RC_RABIT_PASS} --host=localhost declare exchange name=nailgun type=topic durable=true
	rabbitmqadmin --username=guest --password=${RC_RABIT_PASS} --host=localhost declare exchange name=naily_service type=fanout durable=true

	# Declare the mcollective vhost and its exchanges
	rabbitmqctl add_vhost ${MCO_VHOST}
	rabbitmqadmin --username=guest --password=${RC_RABIT_PASS} --host=localhost declare permission vhost=${MCO_VHOST} user=${MCO_USERNAME} configure='.*' write='.*' read='.*'
	rabbitmqadmin --username=guest --password=${RC_RABIT_PASS} --host=localhost declare permission vhost=${MCO_VHOST} user=${ASTUTE_RABBIT_USERNAME} configure='.*' write='.*' read='.*'
	rabbitmqadmin --username=guest --password=${RC_RABIT_PASS} --host=localhost declare permission vhost=/ user=${ASTUTE_RABBIT_USERNAME} configure='.*' write='.*' read='.*'
	rabbitmqadmin --username=${MCO_USERNAME} --password=${MCO_PASSWORD} --host=localhost --vhost=${MCO_VHOST} declare exchange name=mcollective_directed type=direct durable=true
	rabbitmqadmin --username=${MCO_USERNAME} --password=${MCO_PASSWORD} --host=localhost --vhost=${MCO_VHOST} declare exchange name=mcollective_broadcast type=topic durable=true

	sed -i s/activemq/rabbitmq/ /etc/mcollective/server.cfg
	sed -i s/activemq/rabbitmq/ /etc/mcollective/client.cfg
	sed -i "s/^plugin.rabbitmq.pool.1.host\b.*$/plugin.rabbitmq.pool.1.host = 10.20.0.2/" /etc/mcollective/server.cfg
	sed -i "s/^plugin.rabbitmq.pool.1.host\b.*$/plugin.rabbitmq.pool.1.host = 10.20.0.2/" /etc/mcollective/client.cfg
	echo "plugin.rabbitmq.vhost = mcollective" >>/etc/mcollective/server.cfg
	echo "plugin.rabbitmq.vhost = mcollective" >>/etc/mcollective/client.cfg
	# Nailgun expects this to run the provisionning of the image (ie: fa_build_image)
	# through nailgun sending a message using mcollective.
	echo "identity = master" >>/etc/mcollective/server.cfg

	service mcollective restart

	# Fixup nailgun login and passwords for Keystone and Rabbit
	echo "===> Fixing up Nailgun settings.yaml and restarting"
	sed -i 's|admin_password: "XXXXXX"|admin_password: "'${RC_KEYSTONE_ADMINPASS}'"|' /etc/nailgun/settings.yaml
	# Fix the ssh pubkey in settings.yaml
	LINE_NUM=`grep -n AUTHORIZED_KEYS: /etc/nailgun/settings.yaml  | cut -d: -f1`
	LINE_NUM=$(($LINE_NUM +1))
	MASTER_NODE_SSH_PUB_KEY=`cat .ssh/id_rsa.pub`
	sed -i "$LINE_NUM s:.*:  - \"${MASTER_NODE_SSH_PUB_KEY}\":" /etc/nailgun/settings.yaml
	# Finally restart all nailgun daemons
	service nailgun-api restart
	service nailgun-reciever restart
	service nailgun-assassin restart

	# Fixup astute rabbitmq login and pass
	echo "Fixing up astute.conf and restarting"
	# TODO: This is a workaround so that Astute also works on
	# Jessie. In Sid, ruby-popen4 doesn't exist anymore.
	osinstall_install_if_not_installed ruby-popen4
	# Fixme: find out why this is needed:
	systemctl enable astute.service
	service astute stop || true
	service astute start

	# Fixup tftp-hpa boot IP. This is in fact not really needed,
	# but better for security.
	echo "===> Fixing up /etc/default/tftp-hpa tftp path and IP, and restarting"
	sed -i 's|TFTP_ADDRESS="0.0.0.0:69"|TFTP_ADDRESS="10.20.0.2:69"|' /etc/default/tftpd-hpa
	service tftpd-hpa restart
	# Fixup cobbler and dnsmasq

	# Download the Trusty kernel and initrd
	wget http://archive.ubuntu.com/ubuntu/dists/trusty-updates/main/installer-amd64/current/images/netboot/ubuntu-installer/amd64/linux -O /var/lib/nailgun/www/vmlinux-trusty-amd64
	wget http://archive.ubuntu.com/ubuntu/dists/trusty-updates/main/installer-amd64/current/images/netboot/ubuntu-installer/amd64/initrd.gz -O /var/lib/nailgun/www/initrd-trusty-amd64.gz

	# Normal Debian Jessie kernel
	cobbler distro add --name=jessie --owners=cobbler --kernel=/boot/vmlinuz-3.16.0-4-amd64 --initrd=/boot/initrd.img-3.16.0-4-amd64 --breed=debian --arch=x86_64 --os-version=jessie
	# Downloaded Trusty kernel
	cobbler distro add --name=trusty --owners=cobbler --kernel=/var/lib/nailgun/www/vmlinux-trusty-amd64 --initrd=/var/lib/nailgun/www/initrd-trusty-amd64.gz --breed=ubuntu --arch=x86_64 --os-version=trusty
	# Profile for using the fuel-bootstrap-iso generated discovery bootstrap image. Uses the debian distro
	cobbler profile add --name=ubuntu_bootstrap  --owners=cobbler --distro=jessie --kickstart=/var/lib/cobbler/kickstarts/ubuntu-server.preseed \
		--kopts='console=ttyS0,9600 console=tty0 panic=60 ethdevice-timeout=120 boot=live toram components fetch=http://10.20.0.2/fuel-bootstrap-iso/filesystem.squashfs biosdevname=0 url=http://10.20.0.2 mco_user=mcollective mco_pass=marionette'
	# This is to provision Debian Jessie
	cobbler profile add --name=debian_jessie_x86_64 --owners=cobbler --distro=jessie \
		--kopts='auto=true priority=critical locale=en_US net.ifnames=0 biosdevname=0 netcfg/choose_interface=eth0 netcfg/dhcp_timeout=120 netcfg/link_detection_timeout=20' \
		--kickstart=/var/lib/cobbler/kickstarts/debian-jessie.preseed
	# This is to provision Ubuntu Trusty
	cobbler profile add --name=ubuntu_1404_x86_64 --owners=cobbler --distro=trusty \
		--kopts='auto=true priority=critical locale=en_US net.ifnames=0 biosdevname=0 netcfg/choose_interface=eth0 netcfg/dhcp_timeout=120 netcfg/link_detection_timeout=20' \
		--kickstart=/var/lib/cobbler/kickstarts/ubuntu-server.preseed

	# Hack to remove the udev rules installed by fuel-agent. This is to be removed and
	# fixed properly.
	rm /lib/udev/rules.d/80-net-name-slot.rules

	# Some convenience for the root shell
	echo ". /root/openrc.sh" >>/root/.bashrc
	echo 'alias fuel="fuel --user $OS_USERNAME --password $OS_PASSWORD"' >>/root/.bashrc

	# Configure the rsync server to serve /etc/puppet
	echo "[puppet]
path		= /etc/puppet
read only	= true
uid		= 0
gid		= 0
use chroot	= 0
" >/etc/rsyncd.conf
	sed -i 's/^[ \t#]*RSYNC_ENABLE[ \t]*=.*/RSYNC_ENABLE=true/' /etc/default/rsync
	invoke-rc.d rsync start

	mkdir -p /etc/puppet/liberty-9.0
	ln -s /etc/puppet/modules /etc/puppet/liberty-9.0/modules
	ln -s /etc/puppet/manifests /etc/puppet/liberty-9.0/manifests

	echo "===> Do cobbler sync and restart dnsmasq"
	cobbler sync
	service dnsmasq restart
}

osinstall_write_openrc () {
	echo "export SERVICE_ENDPOINT=http://${RC_KEYSTONE_ENDPOINT_IP}:35357/v2.0/
export SERVICE_TOKEN=${RC_KEYSTONE_AUTHTOKEN}

export OS_PROJECT_DOMAIN_ID=default
export OS_USER_DOMAIN_ID=default
export OS_USERNAME=admin
export OS_PASSWORD=${RC_KEYSTONE_ADMINPASS}
export OS_TENANT_NAME=admin
export OS_PROJECT_NAME=admin
export OS_AUTH_URL=http://${RC_KEYSTONE_ENDPOINT_IP}:5000/v3
export OS_IDENTITY_API_VERSION=3
export OS_AUTH_VERSION=3
export OS_PROJECT_DOMAIN_ID=default
export OS_USER_DOMAIN_ID=default
export OS_NO_CACHE=1" >openrc.sh
}

if [ "${1}" = "--non-interactive" ] ; then
	OS_INTERACTIVE="no"
	shift
else
	OS_INTERACTIVE="yes"
fi

case "${1}" in
"controller")
	os_pressed_misc
	osinstall_mysql_set_localhost
	osinstall_install_services
	osinstall_rabbit_host_and_pass
	osinstall_keystone
	osinstall_preseed_all
	DEBIAN_FRONTEND=noninteractive ${APTGET} install openstack-proxy-node
	osinstall_write_openrc
;;
"compute")
	os_pressed_misc
	osinstall_mysql_set_host
	osinstall_install_if_not_installed ntp
	osinstall_rcvalue_keystone_endpoint
	DEBIAN_FRONTEND=noninteractive ${APTGET} install openstack-compute-node
	osinstall_write_openrc
;;
"fuel-master-node")
	os_pressed_misc
	osinstall_mysql_set_localhost
	osinstall_install_if_not_installed ntp
	osinstall_install_if_not_installed curl
	osinstall_preseed_master_node_values
	osinstall_build_fuel_bootstrap_iso
	osinstall_install_fuel_services
	osinstall_rabbit_host_and_pass
	osinstall_keystone
	osinstall_write_openrc
	osinstall_install_if_not_installed fuel-master-node
	osinstall_fix_master_node_setup
;;
"all-in-one")
	os_pressed_misc
	osinstall_mysql_set_localhost
	osinstall_install_if_not_installed ntp
	osinstall_install_services
	osinstall_rabbit_host_and_pass
	osinstall_keystone
	osinstall_preseed_all
	DEBIAN_FRONTEND=noninteractive ${APTGET} install openstack-toaster
	osinstall_write_openrc
	# Install all the rest of the services not tested by Tempest,
	# but which we still want to test installation with.
#	osinstall_preseed_more_services
#	DEBIAN_FRONTEND=noninteractive ${APTGET} install \
#		designate \
#		barbican-api barbican-worker \
#		manila-api manila-scheduler manila-share \
#		mistral-engine mistral-executor mistral-api \
#		trove-api trove-taskmanager trove-conductor \
#		congress-server \
#		sahara-api sahara-engine \
#		murano-api murano-engine
;;
*)
	usage
;;
esac
