#!/bin/bash

#arguments will come from calling program
#runalltests [-v][-m][-d][-a IPADDRESS][-s SVNBRANCH][-i IMAGE][-c EC2CONFFILE][-k KEYNAME][-f KEYFILE]

# subroutine for generating instances.

# euca create instance
# verify instance is running
# return ip address of instance.




DATE=`date -u +'%Y%m%d'`
DATETIME=`date -u +'%Y%m%d-%H.%M.%S'`
#export VMUSER=ec2-user
VMUSER=ubuntu
#export VMSYSTEM=sl6-mpi
VMSYSTEM=

NEWINSTANCE=1


while getopts s:i:a:dvhumc:k:f:n:t:e: arguments
do
	case $arguments in
	h)
		echo "runalltests [-v][-m][-u][-d][-a IPADDRESS][-s SVNBRANCH][-i IMAGE][-t FLAVOR][[-n NUMBER_INSTANCES]-c EC2CONFFILE][-k KEYNAME][-f KEYFILE][-e SERVER_DEBUG_PARAMS]"
		echo ""
		echo "Examples: "
		echo ""
		echo "runalltests -vmd -s branches/stable -i cloud-ubuntu-12.04 -t c1.small -n 2 -c ~/ec2rc.sh -k JamesB -f ~/jamesb.pem"
		echo ""
		echo "This runs all tests against branches/stable, including vfs and mpi, on an new instance of Ubuntu 12.04LTS and deletes the instance upon completion."
		echo ""
		echo "runalltests -vmd -s trunk -i cloud-rhel6 -t c1.small -n 2 -c ~/ec2rc.sh -k JamesB -f ~/jamesb.pem"
		echo ""
		echo "This runs just the sysint and build tests against trunk on a running Redhat Enterprise Linux 6 instance at 10.20.102.30"
		echo ""
		;;
	s)	
		SVNBRANCH=$OPTARG
		;;
	a)	
		VMIPADDR=$OPTARG
		NEWINSTANCE=0
		;;
	i)
		VMSYSTEM=$OPTARG
		;;
	v)
		#echo "Running VFS tests"
		RUN_VFS_TEST=1
		;;
	m)
		#echo "Running MPI tests"
		RUN_MPI_TEST=1
		;;
	u)
		#echo "Running userlib tests"
		RUN_USERLIB_TEST=1
		;;
	c)
		EC2CONFFILE=$OPTARG
		;;
	k)	
		KEYNAME=$OPTARG
		;;
	f)	
		KEYFILE=$OPTARG
		;;
	d)
		DELETE_INSTANCE=1
		;;
	n)
		NUMBER_INSTANCES=$OPTARG
		;;
	t)
		VMTYPE=$OPTARG
		;;
	e)
		SERVER_DEBUG_PARAMS=$OPTARG
		;;
	esac
done
	
#the parameters will change, but the basic functionality will not
echo "DATE is ${DATE}"
echo "SVNBRANCH is ${SVNBRANCH}"
echo "NEWINSTANCE is ${NEWINSTANCE}"
echo "EC2CONFFILE is $EC2CONFFILE"
echo "KEYNAME is $KEYNAME"
echo "VMSYSTEM is $VMSYSTEM"
echo "VMTYPE  is $VMTYPE"
echo "NUMBER_INSTANCES is $NUMBER_INSTANCES"

echo "RUN_MPI_TEST is $RUN_MPI_TEST"
echo "RUN_VFS_TEST is $RUN_VFS_TEST"
echo "RUN_USERLIB_TEST is $RUN_USERLIB_TEST"
echo "VMIPADDR is $VMIPADDR"
echo "DELETE_INSTANCE is $DELETE_INSTANCE"

echo "SERVER_DEBUG_PARAMS is $SERVER_DEBUG_PARAMS"


export SVNBRANCH
export NEWINSTANCE
export VMSYSTEM

export RUN_VFS_TEST
export RUN_MPI_TEST
export RUN_USERLIB_TEST
export KEYFILE
export KEYNAME
export EC2CONFFILE
export DELETE_INSTANCE
export SERVER_DEBUG_PARAMS

#Determine VM User from system
case $VMSYSTEM in 

	cloud-*buntu*|cloud-*mint*)
		VMUSER=ubuntu
		;;
	cloud-rhel*|cloud-centos*|cloud-sl6*|cloud-fedora*|cloud-*suse)
		VMUSER=ec2-user
		;;
	*)
		echo "System $VMSYSTEM not supported."
		exit 1
esac

echo "VMUSER is $VMUSER"
export VMUSER


SVNBRANCH_SHORT=`echo $SVNBRANCH | awk -F"/" '{print $NF}'`

#Look for configuration file
if [ ! -f ${EC2CONFFILE} ] ; then 
	echo "EC2 Configuration file $EC2CONFFILE not found."
	exit 1
fi

#Look for Keyfile
if [ ! -f ${KEYFILE} ] ; then 
	echo "KEYFILE $KEYFILE not found."
	exit 1
fi

#test keypairs
euca-describe-keypairs --config=${EC2CONFFILE} --filter key-name=$KEYNAME

if [ $? != 0 ]
then
	echo "Keyname $KEYNAME not found."
	exit 1
fi

#here are the parameters. Hard coded for now, but will change

#
. ./instancefunctions.sh

#create the VM
#exit 0
if [ ${NEWINSTANCE} != 0 ]
then

	generate_instances $NUMBER_INSTANCES $VMTYPE
	
fi
	
#Now grab the IP Address of the new instance
VMIPADDRARR=( $(for i in ${VMINSTANCEARR[@]}; do euca-describe-instances instance-id=$i --config ${EC2CONFFILE}; done | grep INSTANCE | awk '{ print $13 }') )
VMINSTANCENAMEARR=( $(for i in ${VMINSTANCEARR[@]}; do euca-describe-instances instance-id=$i --config ${EC2CONFFILE}; done | grep INSTANCE | awk '{ print $4 }') ) 

VMIPADDR=${VMIPADDRARR[0]}
	
echo "VM IP ADDRESS is ${VMIPADDR}"

VFS_HOSTS=`echo ${VMINSTANCENAMEARR[@]}`

echo "VFS_HOSTS are $VFS_HOSTS"

for i in ${VMIPADDRARR[@]}
do
	echo "Removing obsolete ssh host records at ~/.ssh/known_hosts for $i."
	ssh-keygen -f ~/.ssh/known_hosts -R $i
	
	# prepare each instance on a separate process.
	prepare_instance $i &

done
wait

for i in ${VMIPADDRARR[@]}
do

	#Verify all instances are running before continuing.
	check_instance $i

done


echo ""
echo "Running tests..."

# run scripted via bash -s to work around remote issues
(echo "script run-test.txt" && cat ./run-test.sh) > run-test.scr

echo ssh -i ${KEYFILE} ${VMUSER}@${VMIPADDR} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "VFS_HOSTS='${VFS_HOSTS}' RUN_USERLIB_TEST=${RUN_USERLIB_TEST} RUN_VFS_TEST=${RUN_VFS_TEST} RUN_MPI_TEST=${RUN_MPI_TEST} SVNBRANCH=${SVNBRANCH} SERVER_DEBUG_PARAMS=${SERVER_DEBUG_PARAMS} bash -s " < ./run-test.scr 

ssh -i ${KEYFILE} ${VMUSER}@${VMIPADDR} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "VFS_HOSTS='${VFS_HOSTS}' RUN_VFS_TEST=${RUN_VFS_TEST} RUN_USERLIB_TEST=${RUN_USERLIB_TEST} RUN_MPI_TEST=${RUN_MPI_TEST} SVNBRANCH=${SVNBRANCH} SERVER_DEBUG_PARAMS=${SERVER_DEBUG_PARAMS} bash -s " < ./run-test.scr

echo "Grabbing logs and scripts from server"
rsync -a -e "ssh -i ${KEYFILE} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" ${VMUSER}@${VMIPADDR}:/tmp/${VMUSER}/pvfs2-nightly/${DATE}/${SVNBRANCH}/*.log logs-$DATETIME/
rsync -a -e "ssh -i ${KEYFILE} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" ${VMUSER}@${VMIPADDR}:/home/${VMUSER}/*.txt logs-$DATETIME/
rsync -a -e "ssh -i ${KEYFILE} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" ${VMUSER}@${VMIPADDR}:/home/${VMUSER}/*.out logs-$DATETIME/

# create link to latest logs so that buildbot can find them.
rm `pwd`/logs-current
ln -s `pwd`/logs-$DATETIME `pwd`/logs-current

echo "Processing the logs" 
./checklogs.pl `pwd`/logs-$DATETIME/alltests-${SVNBRANCH_SHORT}.log



if [ $? -ne 0 ]
then
	#if the log check failed, bailout before deleteing instance
	echo "Some tests failed. Please check `pwd`/logs-$DATETIME/alltests-$SVNBRANCH_SHORT for more details."
	#echo "Instance $VMINSTANCEID at $VMIPADDR NOT deleted."
	#exit 1
fi

#if successful, delete the VM
if [ ${DELETE_INSTANCE} ]
then
		for my_instance in ${VMINSTANCEARR[@]}
		do
			echo "Deleting instance $my_instance."
			euca-terminate-instances --config=$EC2CONFFILE $my_instance
		done

fi

