#!/bin/bash
# vim: ai noexpandtab
#
# Wrapper to run something executable for conserver with passwords, special initialization etc.
#
# Usage: "$0 clienthost type"
# E.g.: conserver-exec myhost001 ipmi
#
# Details for the connection are in /etc/conserver-exec.cf
#
# At the moment, only IPMI is supported :) and that requires ipmitool
#
# Andras.Horvath@cern.ch, 2008

CONFIGFILE=/etc/conserver-exec.cf
IPMITOOL=ipmitool

# run an ipmitool session
# $1: client hostname
function do_ipmi() {
	THISLINE=$( grep "^ipmi\W${1}\W" $CONFIGFILE )
	if [ $? -ne 0 ]; then
		echo "$0: Client not found in configfile $CONFIGFILE"
		exit 4
	fi
	_type="${THISLINE%%	*}";THISLINE="${THISLINE#*	}"
	_hostname="${THISLINE%%	*}";THISLINE="${THISLINE#*	}"
	_ipmihost="${THISLINE%%	*}";THISLINE="${THISLINE#*	}"
	_username="${THISLINE%%	*}";THISLINE="${THISLINE#*	}"
	_password="${THISLINE%%	*}";THISLINE="${THISLINE#*	}"
	_options="${THISLINE}"

	# is the network there?
	# prevent the LONG tcp timeout of ipmitool
	ping -q -c1 -W2 ${_ipmihost} >/dev/null 2>&1 
	r=$?
	if [ $r -ne 0 ]; then
		echo "*** host ${_ipmihost} unreachable ***"
		exit $r
	fi

	# more robustness: deactivate other sessions, if any
	# the only connection should be through us
	IPMI_PASSWORD="$_password" $IPMITOOL -e'*' -H $_ipmihost -U $_username $_options -E -I lanplus sol deactivate 2>&1

	#[ $? -eq 0 ] && sleep 3
	IPMI_PASSWORD="$_password" $IPMITOOL -e'*' -H $_ipmihost -U $_username $_options -E -I lanplus sol activate 2>&1
	r=$?

	# on some BMCs, the 'sol payload enable' thing just dies off after a while all by itself,
	# and has to be re-enabled
	# This might be the cause of an error above...
	if [ $r -ne 0 ]; then
		echo "*** Trying to re-enable payload ***"
		# first we need the numeric user ID of the user that's used to connect..
		_userid=$( IPMI_PASSWORD="$_password" $IPMITOOL -H $_ipmihost -U $_username $_options -E -I lanplus user list 1  2>&1 | grep -F "$_username" )
		_userid=${_userid%% *}

		# We *hope* that the LAN channel is channel 1. Which it usually is.
		# This can not easily be queried unfortunately.
		IPMI_PASSWORD="$_password" $IPMITOOL -H $_ipmihost -U $_username $_options -E -I lanplus sol payload enable 1 $_userid 2>&1

		# retry connection
		IPMI_PASSWORD="$_password" $IPMITOOL -e'*' -H $_ipmihost -U $_username $_options -E -I lanplus sol activate 2>&1
		r=$?
	fi

	# Still no cigar? 
	# Well, we bring out the shotgun then
	if [ $r -ne 0 ]; then
		echo "*** Trying to cold-reset BMC ***"
		IPMI_PASSWORD="$_password" $IPMITOOL -e'*' -H $_ipmihost -U $_username $_options -E mc reset cold
		echo "*** Waiting for BMC to reboot (2 min) ***"
		sleep 120
		echo "*** Retrying connection ***"
		IPMI_PASSWORD="$_password" $IPMITOOL -e'*' -H $_ipmihost -U $_username $_options -E -I lanplus sol activate 2>&1
		r=$?
	fi

	exit $r
}

function do_telnet() {
	THISLINE=$( grep "^telnet\W${1}\W" $CONFIGFILE )
	if [ $? -ne 0 ]; then
		echo "$0: Client not found in configfile $CONFIGFILE"
		exit 4
	fi
	#ugly, but works
	_type="${THISLINE%%	*}";THISLINE="${THISLINE#*	}"
	_hostname="${THISLINE%%	*}";THISLINE="${THISLINE#*	}"
	_hostname="${THISLINE%%	*}";THISLINE="${THISLINE#*	}"
	_port="${THISLINE%%	*}"
	telnet $_hostname $_port 2>&1
	exit $?
}


## ---------- main ----------- ##

if [ $# -ne 2 ]; then
	echo "$0: Wrong number of parameters (2 required != $# provided). Usage: $0 hostname sessiontype"
	exit 1
fi

if [ ! -r "$CONFIGFILE" ]; then
	echo "$0: Config file $CONFIGFILE unreadable?"
	exit 2
fi

case "$2" in
	ipmi)
		do_ipmi $1
		;;
	telnet)
		do_telnet $1
		;;
	*)
		echo "$0: session type $2 is not supported. Usage: $0 hostname sessiontype"
		exit 3
		;;
esac


# EOT
