#!/bin/bash
PID=$$
SCRIPTNAME="$(basename $0)"
WORKER_LIST="worker.list"

## Send error messages to stderr
function echo_err {
	echo "Error: $@" >&2
}


## Check file from parameters
if [ ! -f $WORKER_LIST ]; then
	echo_err "Worker list file ($WORKER_LIST) not exists."
	exit 1
fi

## Check the file contents
if [ ! -s $WORKER_LIST ]; then
	echo_err "Worker list file ($WORKER_LIST) is empty."
	exit 1
fi

## Create WORKER array from file
readarray WORKER < $WORKER_LIST

# Reset Master node
./withdraw/node_reset.sh
rm -rf ~/.kube

# Reset the workers0
for LINE in $(cat $WORKER_LIST | grep -vE "^#"); do
	WORKERNAME=`echo $LINE | awk -F"/" '{print $NF}'`

	echo "[worker:$WORKERNAME] Evicating..."
	ssh $WORKERNAME -o "StrictHostKeyChecking no" "bash -s" < ./withdraw/node_reset.sh

	echo "[worker:$WORKERNAME] Eviction is completed."
done