#!/bin/sh

set -e
set -x

if ! [ -r /etc/oci-poc/oci-poc.conf ] ; then
	echo "Could not read /etc/oci-poc/oci-poc.conf"
	exit 1
fi
. /etc/oci-poc/oci-poc.conf

### SETUP REPOSITORIES ###
if [ "${use_debian_dot_net_backport}" = "yes" ] ; then
	echo "deb ${DEBIAN_DOT_NET_REPO} ${debian_release}-${openstack_release}-backports main
deb-src ${DEBIAN_DOT_NET_REPO} ${debian_release}-${openstack_release}-backports main

deb ${DEBIAN_DOT_NET_REPO} ${debian_release}-${openstack_release}-backports-nochange main
deb-src ${DEBIAN_DOT_NET_REPO} ${debian_release}-${openstack_release}-backports-nochange main
" >${BODI_CHROOT_PATH}/etc/apt/sources.list.d/openstack.list

	chroot ${BODI_CHROOT_PATH} apt-get update
	chroot ${BODI_CHROOT_PATH} apt-get install --allow-unauthenticated -y openstack-backports-archive-keyring
	chroot ${BODI_CHROOT_PATH} apt-get update
	chroot ${BODI_CHROOT_PATH} apt-get -y dist-upgrade -o Dpkg::Options::="--force-confnew"
fi

if [ "${install_buildd_incoming}" = "yes" ] ; then
	echo "deb ${DEBIAN_INCOMING_BUILDD} buildd-sid main
deb-src ${DEBIAN_INCOMING_BUILDD} buildd-sid main
" >${BODI_CHROOT_PATH}/etc/apt/sources.list.d/buildd-incoming.list
	chroot ${BODI_CHROOT_PATH} apt-get update
	chroot ${BODI_CHROOT_PATH} apt-get -y dist-upgrade -o Dpkg::Options::="--force-confnew"
fi

### INSTALL openstack-cluster-installer ###
DEBIAN_FRONTEND=noninteractive chroot ${BODI_CHROOT_PATH} apt-get install -y -o Dpkg::Options::="--force-confnew" openstack-cluster-installer

# configure it with the settings from oci-poc
sed -i "s#^OPENSTACK_CLUSTER_NETWORK=.*#OPENSTACK_CLUSTER_NETWORK=${HOST_BRIDGE_NET}#" ${BODI_CHROOT_PATH}/etc/openstack-cluster-installer/openstack-cluster-installer.conf
sed -i "s#^debian_mirror=.*#debian_mirror=${DEBIAN_MIRROR}#" ${BODI_CHROOT_PATH}/etc/openstack-cluster-installer/openstack-cluster-installer.conf
sed -i "s#^debian_security_mirror=.*#debian_security_mirror=${DEBIAN_SECURITY_MIRROR}#" ${BODI_CHROOT_PATH}/etc/openstack-cluster-installer/openstack-cluster-installer.conf
sed -i "s#^OCI_IP=.*#OCI_IP=${OCI_VM_IP}#" ${BODI_CHROOT_PATH}/etc/openstack-cluster-installer/openstack-cluster-installer.conf
# Forward settings of PoC inside OCI
sed -i "s#^openstack_release=.*#openstack_release=${openstack_release}#" ${BODI_CHROOT_PATH}/etc/openstack-cluster-installer/openstack-cluster-installer.conf
sed -i "s#^use_debian_dot_net_backport=.*#use_debian_dot_net_backport=${use_debian_dot_net_backport}#" ${BODI_CHROOT_PATH}/etc/openstack-cluster-installer/openstack-cluster-installer.conf
sed -i "s#^debian_release=.*#debian_release=${debian_release}#" ${BODI_CHROOT_PATH}/etc/openstack-cluster-installer/openstack-cluster-installer.conf
sed -i "s#^install_buildd_incoming=.*#install_buildd_incoming=${install_buildd_incoming}#" ${BODI_CHROOT_PATH}/etc/openstack-cluster-installer/openstack-cluster-installer.conf

echo "
  ___        __                             _       _
 |_ _|_ __  / _| ___  _ __ ___   __ _ _ __ (_) __ _| | __
  | || '_ \| |_ / _ \| '_ \` _ \ / _\` | '_ \| |/ _\` | |/ /
  | || | | |  _| (_) | | | | | | (_| | | | | | (_| |   <
 |___|_| |_|_|  \___/|_| |_| |_|\__,_|_| |_|_|\__,_|_|\_\
" >${BODI_CHROOT_PATH}/etc/openstack-cluster-installer/motd


### CONFIGURE isc-dhcpd ###
sed -i 's/INTERFACESv4=.*/INTERFACESv4="eth0"/' ${BODI_CHROOT_PATH}/etc/default/isc-dhcp-server

echo "allow booting;
allow bootp;
default-lease-time 600;
max-lease-time 7200;
ddns-update-style none;
authoritative;
ignore-client-uids On;

subnet ${HOST_NETWORK_PREFIX}.0 netmask 255.255.255.0 {
	range ${GUEST_FIRST_SLAVE_IP} ${GUEST_LAST_SLAVE_IP};
	option domain-name \"${GUESTS_DOMAIN_NAME}\";
	option domain-name-servers ${PRIMARY_DNS}, ${SECONDA_DNS};
	option routers ${HOST_BRIDGE_GW};
	option subnet-mask 255.255.255.0;
	option broadcast-address ${HOST_NETWORK_PREFIX}.255;
	next-server ${OCI_VM_IP};
	if exists user-class and option user-class = \"iPXE\" {
		filename \"http://${OCI_VM_IP}/oci/ipxe.php\";
	} else {
		filename \"pxelinux.0\";
	}
}
" >${BODI_CHROOT_PATH}/etc/dhcp/dhcpd.conf

### CONFIGURE tftpd ###
sed -i 's#TFTP_DIRECTORY=.*#TFTP_DIRECTORY="/var/lib/openstack-cluster-installer/tftp"#' ${BODI_CHROOT_PATH}/etc/default/tftpd-hpa

# Copy our authorized_keys in the image BEFORE we build the live image
# so that it will be there
if [ -e /root/.ssh/authorized_keys ] ; then
	mkdir -p ${BODI_CHROOT_PATH}/root/.ssh
	chmod 700 ${BODI_CHROOT_PATH}/root/.ssh
	cat /root/.ssh/authorized_keys >>${BODI_CHROOT_PATH}/root/.ssh/authorized_keys
fi

### Build OCI's live image ###
mkdir -p ${BODI_CHROOT_PATH}/root/live-image
chroot ${BODI_CHROOT_PATH} /bin/bash <<EOF
cd /root/live-image
openstack-cluster-installer-build-live-image --pxe-server-ip ${OCI_VM_IP} --debian-mirror-addr ${DEBIAN_MIRROR} --debian-security-mirror-addr ${DEBIAN_SECURITY_MIRROR}
EOF
# We remove the workdir, because it's too heavy to copy
rm -rf ${BODI_CHROOT_PATH}/root/live-image

# Configure puppet's ENC
. /usr/share/openstack-pkg-tools/pkgos_func

pkgos_add_directive ${BODI_CHROOT_PATH}/etc/puppet/puppet.conf master "external_nodes = /usr/bin/oci-puppet-external-node-classifier" "# Path to enc"
pkgos_inifile set ${BODI_CHROOT_PATH}/etc/puppet/puppet.conf master external_nodes /usr/bin/oci-puppet-external-node-classifier
pkgos_add_directive ${BODI_CHROOT_PATH}/etc/puppet/puppet.conf master "node_terminus = exec" "# Tell what type of ENC"
pkgos_inifile set ${BODI_CHROOT_PATH}/etc/puppet/puppet.conf master node_terminus exec
