#!/bin/sh

set -e

# If we've installed the openstack-tempest-ci, then
# parameters are taken from config file
if [ -r /etc/default/openstack-tempest-ci ] ; then
	. /etc/default/openstack-tempest-ci
fi

useme () {
	echo "                    --virt-type <kvm|xen|ipmi|openstack|live-booter|none>" > /dev/stderr
	echo "                     --ssh-user [root]" > /dev/stderr
	echo "                     --ssh-port [22]" > /dev/stderr
	echo "                      --ssh-key <private-key>" > /dev/stderr
	echo "                  --debian-repo [http://deb.debian.org/debian]" > /dev/stderr
	echo "        --test-gateway-iface-ip [192.168.100.2]" > /dev/stderr
	echo "      --management-network-cidr [172.17.1.0/24]" > /dev/stderr
	echo "        --management-network-ip [172.17.1.1]" > /dev/stderr
	echo "--management-network-iface-name [ens4]" > /dev/stderr
	echo "              --public-ip-start [192.168.100.103]" > /dev/stderr
	echo "                --public-ip-end [192.168.100.119]" > /dev/stderr
	echo "         --do-lastminute-fixups [no]" > /dev/stderr
	echo "                  --run-tempest [no]" > /dev/stderr
	echo "Example: ./openstack-tempest-ci --virt-type live-booter --management-network-iface-name enp0s8 --do-lastminute-fixups yes" > /dev/stderr
}

DO_EXIT=no
############################
### PARAMETER VALIDATION ###
############################
for i in $@ ; do
	case "${1}" in
	"--help")
		useme
		exit 0
		;;
	"--virt-type")
		if [ -z "${2}" ] ; then echo "Parameter for option --virt-type is missing" > /dev/stderr ; DO_EXIT="yes" ; fi
		if [ "${2}" = "kvm" ] ; then
			OTCI_VIRT_MODE=kvm
		elif [ "${2}" = "xen" ] ; then
			OTCI_VIRT_MODE=xen
		elif [ "${2}" = "ipmi" ] ; then
			OTCI_VIRT_MODE=ipmi
		elif [ "${2}" = "openstack" ] ; then
			OTCI_VIRT_MODE=openstack
		elif [ "${2}" = "live-booter" ] ; then
			OTCI_VIRT_MODE=live-booter
		elif [ "${2}" = "none" ] ; then
			OTCI_VIRT_MODE=none
		else
			echo "Parameter for option --virt-type is wrong: ${2}" > /dev/stderr ; DO_EXIT="yes"
		fi
		shift
		shift
		;;
	"--otci-use-swift")
		OTCI_USE_SWIFT=yes
		shift
		;;
	"--ssh-user")
		if [ -z "${2}" ] ; then echo "Parameter for option --ssh-user is missing" > /dev/stderr ; DO_EXIT="yes" ; fi
		OTCI_SSH_USER=${2}
		shift
		shift
		;;
	"--ssh-port")
		if [ -z "${2}" ] ; then echo "Parameter for option --ssh-port is missing" > /dev/stderr ; DO_EXIT="yes" ; fi
		OTCI_SSH_PORT=${2}
		shift
		shift
		;;
	"--ssh-key")
		if [ -z "${2}" ] ; then echo "Parameter for option --ssh-port is missing" > /dev/stderr ; DO_EXIT="yes" ; fi
		OTCI_SSH_KEY_PARAM="-i ${2}"
		shift
		shift
		;;
	"--debian-repo")
		if [ -z "${2}" ] ; then echo "Parameter for option --debian-repo is missing" > /dev/stderr ; DO_EXIT="yes" ; fi
		OTCI_DEBIAN_REPO_URL=${2}
		shift
		shift
		;;
	"--test-gateway-iface-ip")
		if [ -z "${2}" ] ; then echo "Parameter for option --test-gateway-iface-ip" > /dev/stderr ; DO_EXIT="yes" ; fi
		OTCI_FLOATING_IP_ADDR=${2}
		shift
		shift
		;;
	"--management-network-cidr")
		if [ -z "${2}" ] ; then echo "Parameter for option --management-network-cidr is missing" > /dev/stderr ; DO_EXIT="yes" ; fi
		OTCI_ETH1_CIDR=${2}
		shift
		shift
		;;
	"--management-network-iface-name")
		if [ -z "${2}" ] ; then echo "Parameter for option --management-network-iface-name is missing" > /dev/stderr ; DO_EXIT="yes" ; fi
		OTCI_MANAGEMENT_IFACE_NAME=${2}
		shift
		shift
		;;
	"--public-ip-start")
		if [ -z "${2}" ] ; then echo "Parameter for option --public-ip-start is missing" > /dev/stderr ; DO_EXIT="yes" ; fi
		OTCI_PUBIP_START=${2}
		shift
		shift
		;;
	"--public-ip-end")
		if [ -z "${2}" ] ; then echo "Parameter for option --public-ip-end is missing" > /dev/stderr ; DO_EXIT="yes" ; fi
		OTCI_PUBIP_END=${2}
		shift
		shift
		;;
	"--management-network-ip")
		if [ -z "${2}" ] ; then echo "Parameter for option --management-network-ip is missing" > /dev/stderr ; DO_EXIT="yes" ; fi
		OTCI_LOCAL_IP=${2}
		shift
		shift
		;;
	"--kvm-pid-file")
		if [ -z "${2}" ] ; then echo "Parameter for option --kvm-pid-file is missing" > /dev/stderr ; DO_EXIT="yes" ; fi
		KVM_PID_FILE=${2}
		shift
		shift
		;;
	"--kvm-start-script")
		if [ -z "${2}" ] ; then echo "Parameter for option --kvm-start-script is missing" > /dev/stderr ; DO_EXIT="yes" ; fi
		KVM_START_SCRIPT=${2}
		shift
		shift
		;;
	"--kvm-debian-image-file")
		if [ -z "${2}" ] ; then echo "Parameter for option --test-image-file is missing" > /dev/stderr ; DO_EXIT="yes" ; fi
		KVM_SOURCE_IMAGE=${2}
		shift
		shift
		;;
	"--do-lastminute-fixups")
		OTCI_LASTMINUTE_FIXUPS=yes
		shift
		;;
	"--run-tempest")
		OTCI_RUN_TEMPEST=yes
		shift
		;;
	*)
		;;
	esac
done

if [ -z "${OTCI_DEBIAN_REPO_URL}" ] ; then
	OTCI_DEBIAN_REPO_URL=http://deb.debian.org/debian
fi

if [ -z "${OTCI_SSH_USER}" ] ; then
	if [ "${OTCI_VIRT_MODE}" = "xen" ] || [ "${OTCI_VIRT_MODE}" = "ipmi" ] || [ "${OTCI_VIRT_MODE}" = "kvm" ] || [ "${OTCI_VIRT_MODE}" = "live-booter" ] || [ "${OTCI_VIRT_MODE}" = "none" ] ; then
		OTCI_SSH_USER=root
	else
		OTCI_SSH_USER=debian
	fi
fi
if ! [ "${OTCI_SSH_USER}" = "root" ] ; then
	SUDO="sudo "
else
	SUDO=""
fi

if [ -z "${OTCI_FLOATING_IP_ADDR}" ] ; then
	# Just a convenient default, not really helpful in fact...
	OTCI_FLOATING_IP_ADDR=192.168.100.2
fi

if [ -z ${OTCI_SSH_PORT} ] ; then
	OTCI_SSH_PORT=22
fi

if [ -z ${OTCI_ETH1_CIDR} ] ; then
	OTCI_ETH1_CIDR=172.17.1.0/24
fi

if [ -z "${OTCI_PUBIP_START}" ] ; then
	OTCI_PUBIP_START=192.168.100.103
fi

if [ -z "${OTCI_PUBIP_END}" ] ; then
	OTCI_PUBIP_END=192.168.100.119
fi

if [ -z "${OTCI_LOCAL_IP}" ] ; then
	OTCI_LOCAL_IP=172.17.1.1
fi

if [ -z "${OTCI_VIRT_MODE}" ] ; then
	echo "Parameter --virt-type is missing" > /dev/stderr ; DO_EXIT="yes"
fi

if [ -z "${KVM_PID_FILE}" ] ; then
	KVM_PID_FILE=`pwd`/vm.pid
fi
if [ -z "${KVM_START_SCRIPT}" ] ; then
	KVM_START_SCRIPT=`pwd`/kvm-start
fi
if [ -z "${KVM_SOURCE_IMAGE}" ] ; then
	KVM_SOURCE_IMAGE=`pwd`/openstack-pristine.qcow2
fi
if [ -z "${OTCI_MANAGEMENT_IFACE_NAME}" ] ; then
	OTCI_MANAGEMENT_IFACE_NAME=ens4
fi

if [ "${DO_EXIT}" = "yes" ] ; then
	echo "Parameters not validated: will exit now!" > /dev/stderr
	exit 1
fi


################################
### END PARAMETER VALIDATION ###
################################
CURDIR=`pwd`
OTCI_SSH_PORT_PARAM="-p ${OTCI_SSH_PORT}"
OTCI_SCP_PORT_PARAM="-P ${OTCI_SSH_PORT}"

APT_INSTALL="${SUDO}DEBIAN_FRONTEND=noninteractive apt-get -o Dpkg::Options::=--force-confnew --allow-downgrades --allow-change-held-packages install -y"

get_id () {
	"$@" | awk '/ id / { print $4 }'
}

otci_delete_vm_if_already_running () {
	case "${OTCI_VIRT_MODE}" in
	"kvm")
		if [ -e ${KVM_PID_FILE} ] ; then
			VM_PID=`cat ${KVM_PID_FILE}`
			echo "===> Found PID running: ${VM_PID}. Killing process..."
			kill -9 ${VM_PID}
			rm -f ${KVM_PID_FILE}
		else
			echo "===> Previous instance not running: not killing it"
		fi
		;;
	"ipmi")
		echo "===> Nothing to do to delete VM in IPMI mode"
		;;
	"xen")
		echo "===> Connecting to dom0 root@${OTCI_XEN_DOM0_IP} to destroy ${OTCI_XEN_VM_NAME}"
		ssh -o "StrictHostKeyChecking no" root@${OTCI_XEN_DOM0_IP} "xl destroy ${OTCI_XEN_VM_NAME} || true"
		sleep 2
		;;
	"openstack")
		echo "===> Searching for already running instance (to eventually delete)..."
		# Kill the VM if it is online already
		RUNNING_ID=`nova list | grep ${OTCI_VM_HOSTNAME} | awk '{print $2}'`
		if [ -n "${RUNNING_ID}" ] ; then
			echo "===> Deleting instance ${RUNNING_ID}..."
			nova delete ${RUNNING_ID}
			echo "-> Killed"
		else
			echo "-> Instance not running"
		fi
		;;
	"live-booter")
		openstack-tempest-ci-stop
		sleep 2
		;;
	"none")
		;;
	*)
		echo "Virtualization type ${OTCI_VIRT_MODE} not implemented" > /dev/stderr
		exit 1
	esac
}

otci_wait_until_openstack_vm_active () {
	COUNT=50
	CYCLES=0
	OTCI_STATUS=BUILD
	echo "===> Wait until nova list status is active"
	echo "Waiting: "
	while [ "${OTCI_STATUS}" != "ACTIVE" ] && [ ${COUNT} != 0 ] ; do
		echo ${CYCLES}
		OTCI_STATUS=`nova list | grep ${OTCI_VM_HOSTNAME} | awk '{print $6}'`
		COUNT=$(( ${COUNT} - 1 ))
		CYCLES=$(( ${CYCLES} + 1 ))
	done
	if [ "${OTCI_STATUS}" != "ACTIVE"  ] ; then
		echo "-> Timed out spawning the OTCI VM: exiting"
		exit 1
	else
		echo " -> VM is spawned \o/"
	fi
}

otci_launch_vm () {
	case "${OTCI_VIRT_MODE}" in
	"kvm")
		# Under KVM, we expect a pristine image to be available.
		# We then copy that image, and start the kvm-start script
		# which will bring the machine up.
		echo "===> Copying VM image"
		cp ${KVM_SOURCE_IMAGE} ${CURDIR}/openstack.qcow2
		${KVM_START_SCRIPT}
		;;
	"ipmi")
		# In the case of ipmi provisionning, we expect the machine to be setup to
		# just run Debian live with the Jenkins /var/lib/jenkins/.ssh/id_rsa.pub key
		# setup in /root/.ssh/authorized_keys2. So just a reboot of the machine
		# brings it fresh and ready. Therefore, we just turn the machine off, then
		# on again, using IPMI.
		echo "===> Powering the tempest machine down"
		IPMI_PASSWORD=${OTCI_IPMI_PASS} ipmitool -H ${OTCI_IPMI_IP_ADDR} -U ${OTCI_IPMI_LOGIN} -I lan -E chassis power off
		echo "===> Waiting 10 seconds before starting it again"
		for i in `seq 0 10` ; do
			echo $i
			sleep 1
		done
		echo "===> Powering the machine on again"
		IPMI_PASSWORD=${OTCI_IPMI_PASS} ipmitool -H ${OTCI_IPMI_IP_ADDR} -U ${OTCI_IPMI_LOGIN} -I lan -E chassis power on
		echo "===> Waiting 90 seconds for the machine to be up again"
		for i in `seq 0 90` ; do
			echo $i
			sleep 1
		done
		;;
	"xen")
		# Under Xen, we expect /root/p to be the provision script on the dom0
		# this script should create a new VM, and start it, then wait enough
		# time for the machine to be reachable (at least ping).
		ssh -o "StrictHostKeyChecking no" root@${OTCI_XEN_DOM0_IP} /root/p
		;;
	"openstack")
		# Under OpenStack, we simply boot an instance, and associate
		# the floating IP address to it.
		echo "===> Starting instance"
		OTCI_VM_ID=`get_id nova boot --flavor ${OTCI_INSTANCE_FLAVOR} --nic net-id=${OTCI_ETH0_NET_ID} --nic net-id=${OTCI_ETH1_NET_ID} --image ${OTCI_DEBIAN_IMAGE_ID} --key-name ${OTCI_KEYPAIR_NAME} ${OTCI_VM_HOSTNAME}`
		echo "-> Got ID: ${OTCI_VM_ID}"
		otci_wait_until_openstack_vm_active
		echo "===> Associating floating IP"
		nova floating-ip-associate ${OTCI_VM_ID} ${OTCI_FLOATING_IP_ADDR}
		;;
	"live-booter")
		openstack-tempest-ci-start
		;;
	"none")
		;;
	*)
		echo "Virtualization type ${OTCI_VIRT_MODE} not implemented" > /dev/stderr
	esac
}

otci_wait_for_ssh () {
	if [ -e /var/lib/jenkins/.ssh/known_hosts ] ; then
		rm /var/lib/jenkins/.ssh/known_hosts
	fi
	COUNT=120
	CYCLES=0
	OTCI_CAN_SSH=no
	echo "===> Wait until we can ssh"
	echo "Waiting: "
	while [ "${OTCI_CAN_SSH}" != "yes" ] && [ ${COUNT} != 0 ] ; do
		echo ${CYCLES}
		if ssh ${OTCI_SSH_PORT_PARAM} -o "StrictHostKeyChecking no" -o "ConnectTimeout 2" ${OTCI_SSH_USER}@${OTCI_FLOATING_IP_ADDR} 'echo -n ""' ; then
			OTCI_CAN_SSH=yes
		else
			COUNT=$(( ${COUNT} - 1 ))
			CYCLES=$(( ${CYCLES} + 1 ))
			sleep 1
		fi
	done
	ssh-keygen -f ~/.ssh/known_hosts -R ${OTCI_FLOATING_IP_ADDR} || true
}

otci_remote () {
	ssh ${OTCI_SSH_PORT_PARAM} -o "StrictHostKeyChecking no" ${OTCI_SSH_USER}@${OTCI_FLOATING_IP_ADDR} $@
}

otci_scp () {
	scp ${OTCI_SCP_PORT_PARAM} -o "StrictHostKeyChecking no" ${1} ${OTCI_SSH_USER}@${OTCI_FLOATING_IP_ADDR}:${2}
}

otci_fix_live_networking () {
	# Debian Live configures all interfaces to use DHCP. That's not
	# what we want, and this piece of script fixes that.
	GATEWAY=$(ipcalc ${OTCI_FLOATING_IP_ADDR}/24 | grep HostMin: | awk '{print $2}')
	NETWORK=$(ipcalc ${OTCI_FLOATING_IP_ADDR}/24 | grep Network: | awk '{print $2}' | cut -d/ -f1)
	echo "auto lo
iface lo inet loopback

allow-hotplug ens3
iface ens3 inet static
	address ${OTCI_FLOATING_IP_ADDR}
	network ${NETWORK}
	netmask 255.255.255.0
	gateway ${GATEWAY}

auto ens4
iface ens4 inet static
	address ${OTCI_LOCAL_IP}
	netmask 255.255.255.0
" >interfaces
	otci_scp interfaces ""
	otci_remote "${SUDO}cp interfaces /etc/network"
	otci_remote "${SUDO}nohup /etc/init.d/networking restart &"
	sleep 5
	otci_remote "rm interfaces"
	rm interfaces
	otci_remote "${SUDO}ip addr del ${OTCI_FLOATING_IP_ADDR}/24 dev ens4" || true
}

otci_aptget_update () {
	echo "===> Adding strech-queens apt key"
	otci_remote "wget http://stretch-queens.infomaniak.ch/debian/dists/pubkey.gpg"
	otci_remote "apt-key add pubkey.gpg"
	echo "===> Fixing sources.list and updating"
	# Do not use pdiffs, they are bad...
	echo "Acquire::PDiffs \"false\";" >temp_file
	otci_scp temp_file ""
	otci_remote "${SUDO}cp temp_file /etc/apt/apt.conf.d/98nopdiff"
	otci_remote "rm temp_file"
	rm temp_file
	# TODO: Make it so we select it depending on target (support unstable?)
	IN_TARGET_LSB_RELEASE=`lsb_release -c | awk '{print $2}'`
	# If we're having a sources.list locally, let's use it
	if ! [ -e sources.list ] ; then
		if [ "${IN_TARGET_LSB_RELEASE}" = "wheezy" ] || [ "${IN_TARGET_LSB_RELEASE}" = "jessie" ] || [ "${IN_TARGET_LSB_RELEASE}" = "stretch" ] || [ "${IN_TARGET_LSB_RELEASE}" = "buster" ] ; then
			SECURITY="/updates"
		else
			SECURITY="-security"
		fi
		# Use the closest mirror, as defined in pkgos.conf
		echo "deb ${OTCI_DEBIAN_REPO_URL}/debian ${IN_TARGET_LSB_RELEASE} main
deb-src ${OTCI_DEBIAN_REPO_URL}/debian ${IN_TARGET_LSB_RELEASE} main

deb http://security.debian.org/ ${IN_TARGET_LSB_RELEASE}${SECURITY} main contrib non-free
deb-src http://security.debian.org/ ${IN_TARGET_LSB_RELEASE}${SECURITY} main contrib non-free
" >sources.list
	fi
	# scp to dest and install as new sources.list
	otci_scp sources.list ""
	otci_remote "${SUDO}cp sources.list /etc/apt"

	# Add the current repos if it exists
	if [ -e /etc/openstack-tempest-ci/openstack-ci.list ] ; then
		echo "-> scp source list"
		otci_scp /etc/openstack-tempest-ci/openstack-ci.list ""
		echo "-> Install new source list"
		otci_remote "${SUDO}mv openstack-ci.list /etc/apt/sources.list.d/openstack.list"
		if [ -r /etc/pkgos/pkgos.conf ] ; then
			. /etc/pkgos/pkgos.conf
		fi
		if [ -z "${TARGET_OPENSTACK_REL}" ] ; then
			TARGET_OPENSTACK_REL=mitaka
		fi
		echo "-> wget Jenkins key"
		otci_remote "wget http://${TARGET_OPENSTACK_REL}-${IN_TARGET_LSB_RELEASE}.pkgs.mirantis.com/debian/dists/pubkey.gpg"
		echo "-> Install Jenkins key"
		otci_remote "${SUDO}apt-key add pubkey.gpg"
	fi

	# Finally update
	otci_remote "${SUDO}apt-get update"

	# dist-upgrade
	otci_remote "${SUDO}DEBIAN_FRONTEND=noninteractive apt-get dist-upgrade -y"
}

# Params: $1: local script file name
#         $2: destination folder
otci_scp_script_to_node () {
	local SCRIPT_NAME DEST_FOLDER
	SCRIPT_NAME=${1}
	DEST_FOLDER=${2}
	if [ -r "${SCRIPT_NAME}" ] ; then
		echo "=> Copying local ${SCRIPT_NAME} to remote in ${DEST_FOLDER}"
		otci_scp ${SCRIPT_NAME} ""
		otci_remote "${SUDO}cp ${SCRIPT_NAME} ${DEST_FOLDER}"
		otci_remote "rm ${SCRIPT_NAME}"
	fi
}

otci_install_openstack_deploy () {
	# We do this because in most virtualalization env,
	# there's no good source of entropy
	echo "===> Installing haveged"
	otci_remote "${APT_INSTALL} haveged"
	echo "===> Installing openstack-deploy"
	otci_remote "${APT_INSTALL} openstack-deploy"
	otci_scp_script_to_node openstack-deploy /usr/sbin
	otci_scp_script_to_node preseed-lib /usr/share/openstack-deploy
	echo "===> Deploying OpenStack"
	otci_remote "${SUDO}openstack-deploy --non-interactive all-in-one"
#	HARDWARE_ACCEL=$(otci_remote "egrep -c '(vmx|svm)' /proc/cpuinfo")
#	if [ "${HARDWARE_ACCEL}" = "0" ] ; then
	if ! [ "${OTCI_VIRT_MODE}" = "ipmi" ] ; then
		echo "===> Switching to qemu"
		otci_remote "${APT_INSTALL} nova-compute-qemu"
		otci_remote "/etc/init.d/nova-compute stop || true"
		sleep 2
		otci_remote "/etc/init.d/nova-compute start"
	else
		otci_remote "${APT_INSTALL} qemu-utils"
	fi
	# Make sure we do have a br-int
	echo "===> Setting-up br-int"
	otci_remote "ovs-vsctl add-br br-int" || true

	# Copy eventually hacked local init scripts
	for i in openstack-proxy-node-network openstack-proxy-node-nat ; do
		if [ -x $i ] ; then
			COPY_ME=$i
		elif [ -x init.d/$i ] ; then
			COPY_ME=init.d/$i
		else
			COPY_ME=""
		fi
		if [ -n "${COPY_ME}" ] ; then
			echo "-> Copying and installing $COPY_ME"
			otci_scp $COPY_ME ""
			otci_remote "${SUDO}cp $i /usr/share/openstack-deploy"
			otci_remote "rm $i"
		fi
	done

	if [ "${OTCI_VIRT_MODE}" = "live-booter" ] ; then
		otci_remote "${SUDO}ifconfig ens4 ${OTCI_LOCAL_IP} netmask 255.255.255.0 up"
	fi

	echo "===> Running openstack-deploy-proxy-network"
	otci_scp_script_to_node openstack-deploy-proxy-network /usr/sbin
	otci_remote "${SUDO}openstack-deploy-proxy-network --mgmt-ip-cidr ${OTCI_ETH1_CIDR} --mgmt-if ${OTCI_MANAGEMENT_IFACE_NAME} --ext-host-min ${OTCI_PUBIP_START} --ext-host-max ${OTCI_PUBIP_END} --local-ip ${OTCI_LOCAL_IP}"
	echo "===> Deploying tempest"
	if [ "${OTCI_VIRT_MODE}" = "ipmi" ] ; then
		LVMTYPE="resetup --otci-lvm-device sda"
	elif [ "${OTCI_VIRT_MODE}" = "live-booter" ] ; then
		LVMTYPE="resetup --otci-lvm-device vda"
	else
		LVMTYPE=loopback
	fi
	if [ "${OTCI_USE_SWIFT}" = yes ] ; then
		USE_SWIFT="--otci-use-swift"
	fi

	echo "-> Running: otci_remote sudo openstack-deploy-tempest --virt-type ${OTCI_VIRT_MODE} --otci-lvmtype ${LVMTYPE} ${USE_SWIFT} --otci-openstack-debian-images-deb-repo ${OTCI_DEBIAN_REPO_URL}"
	otci_scp_script_to_node openstack-deploy-tempest /usr/sbin
	otci_remote "${SUDO}openstack-deploy-tempest --virt-type ${OTCI_VIRT_MODE} --otci-lvmtype ${LVMTYPE} ${USE_SWIFT} --otci-openstack-debian-images-deb-repo ${OTCI_DEBIAN_REPO_URL}"

	if [ "${OTCI_VIRT_MODE}" = "live-booter" ] ; then
		otci_remote "${SUDO}ifconfig ens4 ${OTCI_LOCAL_IP} netmask 255.255.255.0 up"
		otci_remote "${SUDO}service neutron-openvswitch-agent start" || true
	fi

	otci_scp_script_to_node openstack-deploy-fix-nova /usr/sbin
	otci_remote "${SUDO}openstack-deploy-fix-nova"

	otci_scp_script_to_node openstack-deploy-fix-heat /usr/sbin
	otci_remote "${SUDO}openstack-deploy-fix-heat"
}

otci_run_tempest () {
	echo "===> Running tempest"
	otci_remote "${SUDO}tempest_debian_shell_wrapper"
	echo "
All tests passed:                                                                               
______  _______ ______  _____ _______ __   _       ______ _     _        _______ ______   /
|     \ |______ |_____]   |   |_____| | \  |      |_____/ |     | |      |______  ____/  / 
|_____/ |______ |_____] __|__ |     | |  \_|      |    \_ |_____| |_____ |______ /_____ .  
"
	echo "===> Merging repos"
	openstack-tempest-merge-repo

	echo "
 █████╗ ██╗     ██╗         ██████╗  █████╗ ███████╗███████╗███████╗██████╗ ██╗
██╔══██╗██║     ██║         ██╔══██╗██╔══██╗██╔════╝██╔════╝██╔════╝██╔══██╗██║
███████║██║     ██║         ██████╔╝███████║███████╗███████╗█████╗  ██║  ██║██║
██╔══██║██║     ██║         ██╔═══╝ ██╔══██║╚════██║╚════██║██╔══╝  ██║  ██║╚═╝
██║  ██║███████╗███████╗    ██║     ██║  ██║███████║███████║███████╗██████╔╝██╗
╚═╝  ╚═╝╚══════╝╚══════╝    ╚═╝     ╚═╝  ╚═╝╚══════╝╚══════╝╚══════╝╚═════╝ ╚═╝"

}

otci_last_minute_fixup () {
	# No idea why the wrong IP ends up there...
	otci_remote "ip addr del ${OTCI_ETH1_CIDR} dev ${OTCI_MANAGEMENT_IFACE_NAME}" || true
	otci_remote "ip addr del ${OTCI_FLOATING_IP_ADDR}/24 dev ${OTCI_MANAGEMENT_IFACE_NAME}" || true
	otci_remote "ip addr add ${OTCI_LOCAL_IP}/24 dev ${OTCI_MANAGEMENT_IFACE_NAME}" || true
	otci_remote "/etc/init.d/neutron-openvswitch-agent stop"
	otci_remote "/etc/init.d/neutron-metadata-agent stop"
	sleep 2
	otci_remote "/etc/init.d/neutron-openvswitch-agent start"
	otci_remote "/etc/init.d/neutron-metadata-agent start"
	sleep 2
}

otci_delete_vm_if_already_running
otci_launch_vm
otci_wait_for_ssh
echo "===> Waiting 5 seconds to let network settle up..."
sleep 5
if [ "${OTCI_VIRT_MODE}" = "live-booter" ] ; then
	otci_fix_live_networking
fi
otci_aptget_update
otci_install_openstack_deploy
if [ "${OTCI_LASTMINUTE_FIXUPS}" = "yes" ] ; then
	otci_last_minute_fixup
fi
if [ "${OTCI_RUN_TEMPEST}" = "yes" ] ; then
	otci_run_tempest
fi
