#!/usr/bin/env bash

# default to docker if not specified
engine="${1:-docker}"
shift 1

if ! [[ "$engine" =~ ^(docker|podman)$ ]]; then
    echo "Invalid container engine: ${engine}"
    exit 1
fi

echo "Using container engine: $engine"

containers_running=$(sudo $engine ps --filter "label=kolla_version" --format "{{.Names}}")

QEMU_PIDS=$(pgrep -l qemu | awk '!/qemu-ga/  && !/qemu-img/ {print $1}')
if [[ "${containers_running}" =~ "nova_libvirt" ]] && [[ $QEMU_PIDS ]] && [[ $(ps --no-headers wwwup $QEMU_PIDS | grep --invert-match '\-xen\-domid 0') ]]; then
    echo "Some qemu processes were detected."
    echo "Container engine ($engine) will not be able to stop the nova_libvirt container with those running."
    echo "Please clean them up before rerunning this script."
    exit 1
fi 

if [ -n "$1" ]; then
    containers_to_kill=$(sudo $engine ps --filter "label=kolla_version" --format "{{.Names}}" -a | grep -E "$1" | awk '{print $1}')
    volumes_to_remove=$(sudo $engine inspect -f '{{range .Mounts}} {{printf "%s\n" .Name }}{{end}}' ${containers_to_kill} | \
        egrep -v '(^\s*$)' | sort | uniq)
else
    containers_to_kill=$(sudo $engine ps --filter "label=kolla_version" --format "{{.Names}}" -a)

    volumes_to_remove=$(sudo $engine inspect -f '{{range .Mounts}} {{printf "%s\n" .Name }}{{end}}' ${containers_to_kill} | \
        egrep -v '(^\s*$)' | sort | uniq)
fi

if [[ "${containers_to_kill}" =~ "openvswitch_vswitchd" ]] && [[ "${containers_running}" =~ "neutron_openvswitch_agent" ]]; then
echo "Removing ovs bridge..."
(sudo $engine exec -u root neutron_openvswitch_agent neutron-ovs-cleanup \
    --config-file /etc/neutron/neutron.conf --config-file /etc/neutron/plugins/ml2/openvswitch_agent.ini \
    --ovs_all_ports) > /dev/null
(sudo $engine exec -it openvswitch_vswitchd bash -c 'for br in `ovs-vsctl list-br`;do ovs-vsctl --if-exists del-br $br;done') > /dev/null
fi

echo "Stopping containers..."
for container in ${containers_to_kill}; do
sudo systemctl stop kolla-${container}-container.service
done

echo "Removing containers..."
(sudo $engine rm -v -f ${containers_to_kill} 2>&1) > /dev/null

echo "Disconnecting containers from $engine host network"
for container in ${containers_to_kill}; do
(sudo $engine network disconnect -f host $container 2>&1) > /dev/null
done

echo "Removing volumes..."
(sudo $engine volume rm ${volumes_to_remove} 2>&1) > /dev/null

echo "Removing link of kolla_log volume..."
(sudo rm -f /var/log/kolla 2>&1) > /dev/null

echo "Removing unit files..."
sudo rm -f /etc/systemd/system/kolla-*-container.service
sudo systemctl daemon-reload

echo "All cleaned up!"
