#!/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 ###
echo "deb http://stretch-queens.debian.net/debian stretch-queens-backports main
deb-src http://stretch-queens.debian.net/debian stretch-queens-backports main

deb http://stretch-queens.debian.net/debian stretch-queens-backports-nochange main
deb-src http://stretch-queens.debian.net/debian stretch-queens-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

### INSTALL openstack-cluster-installer ###
DEBIAN_FRONTEND=noninteractive chroot ${BODI_CHROOT_PATH} apt-get install -y 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

### 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} <<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
