#! /bin/sh
# $Header: /auto/homes/pb22/sh/RCS/cl-condor-start,v 1.2 2009/06/25 07:26:43 pb22 Exp $

basemem=100000000	# how much memory does a null system use?
cpus=1			# guess if none given

# Start a condor pool machine

case "$1" in
pb0[0-4][0-9])	 : OK;;
'')	echo "$0: specfify condor pool machine to start" 2>&1; exit 1;;
-h|--help)	cat << EOF
$0: start a condor machine,
optionally giving number of CPUs and memory [in bytes] (per CPU). e.g.
	$0 pb020
	$0 pb020 2 8000000000
	$0 pb020 2 @4G

See 'cl-condor-list' for details of condor machines.
EOF
	exit;;
esac

vmid=$(cl-onserver --xe vm-list name-label=$1 --minimal)
case "$vmid" in
'')	echo "$0: failed to find vm $1" 1>&2; exit 1;;
esac
state=$(cl-onserver --xe vm-list uuid=$vmid params=power-state --minimal)
case "$state" in
halted)	: OK;;
*)	echo "$0: vm $1 is '$state' rather than 'halted'" 1>&2; exit 1;;
esac
case "$2" in '') : OK;;
[1-8]) cpus=$2; $echox cl-onserver --xe vm-param-set uuid=$vmid VCPUs-at-startup=$2;;
*) echo "$0: unexpected number of CPUs - expectting 1-8" 1>&2; exit 1;;
esac
case "$3" in '') : OK;;
*[kK])	mem=`echo $3 | sed 's/.$/000/'`;;
*[mM])	mem=`echo $3 | sed 's/.$/000000/'`;;
*[gG])	mem=`echo $3 | sed 's/.$/000000000/'`;;
*?)	mem=$3;;
esac
case "$mem" in
@*)	mem=`echo $mem | sed 's/.//'`;
	mem=$(($mem * $cpus + $basemem));
	;;
esac
case "$mem" in
'')	: OK;;
[1-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]*)
   $echox cl-onserver --xe vm-param-set uuid=$vmid memory-dynamic-max=$mem memory-dynamic-min=$mem memory-static-max=$mem;;
*) echo "$0: unexpected amount of memory of CPUs - expecting at least 100000000" 1>&2; exit 1;;
esac
cl-onserver --xe vm-list      uuid=$vmid params=VCPUs-max,VCPUs-at-startup,memory-dynamic-max,memory-dynamic-min,memory-static-min,memory-static-max
$echox cl-onserver --xe vm-start vm=$1
