#!/bin/sh

set -e
#set -x

# Just in case, make sure lsb_release is installed
if ! [ -x /usr/bin/lsb_release ] ; then
	apt-get install lsb-release
fi
SUITE_NAME=$(lsb_release -c -s)

if [ "${SUITE_NAME}" = "stretch" ] || [ "${SUITE_NAME}" = "buster" ] ; then
	systemctl restart virtlogd
	sleep 2
	systemctl restart libvirtd
	sleep 2
fi

if [ -e /etc/ceph/ceph.client.openstack.keyring ] ; then
	LIBVIRT_SECRET=$(cat /etc/ceph/ceph.client.openstack.keyring | grep key | awk '{print $3}')
	LIBVIRT_UUID=$(xpath -e "//secret/uuid/text()" /etc/nova/secret.xml 2> /dev/null)

	TMP_FILE=$(mktemp)
	virsh secret-list >${TMP_FILE}
	if ! grep -q ${LIBVIRT_UUID} ${TMP_FILE} ; then
		virsh secret-define --file /etc/nova/secret.xml
	fi
	if ! virsh secret-get-value --secret ${LIBVIRT_UUID} >/dev/null 2>&1; then
		virsh secret-set-value --secret ${LIBVIRT_UUID} ${LIBVIRT_SECRET}
	fi
fi

if [ "${SUITE_NAME}" = "stretch" ] || [ "${SUITE_NAME}" = "buster" ] ; then
	systemctl restart nova-compute
#else
	# With Bullseye and beyond, TCP and TLS must be
	# activated by enabling the libvirtd-tcp.socket
	# and libvirtd-tls.socket thingy.
#	systemctl enable libvirtd-tcp.socket
#	systemctl enable libvirtd-tls.socket
#	systemctl restart libvirtd.service
fi
