#!/bin/bash

# manually run:
#  make sandbox SANDBOXFLAGS="--nbdb-mode=clustered" 

set -x

SANDBOX_DIR=`pwd`/sandbox
nb1_pid=$(cat sandbox/nb1.pid)
nb1_cmd=$(cat /proc/${nb1_pid}/cmdline|tr '\0' ' ')

echo "Killing nb1 without leaving the cluster"
kill $nb1_pid
sleep 2

for server in nb3 nb2; do
    echo ${server} status before leaving the cluster
    ovn-appctl -t ${SANDBOX_DIR}/${server} cluster/status OVN_Northbound
    echo ${server} leaving the cluster
    ovn-appctl -t ${SANDBOX_DIR}/${server} cluster/leave OVN_Northbound
    sleep 2
    echo ${server} status after leaving the cluster
    ovn-appctl -t ${SANDBOX_DIR}/${server} cluster/status OVN_Northbound
done;

pushd sandbox
echo Restarting nb1
$nb1_cmd
popd
sleep 5

echo "Cluster status from each server"
for server in nb1 nb2 nb3;do
    ovn-appctl -t ${SANDBOX_DIR}/${server} cluster/status OVN_Northbound
done;
