#!/bin/sh
# External STONITH module plugin for HMC
#
# Copyright (c) 2007 Xinwei Hu <hxinwei@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it would be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# Further, this software is distributed without any warranty that it is
# free of the rightful claim of any third person regarding infringement
# or the like.  Any license provided herein, whether implied or
# otherwise, applies only to this software file.  Patent licenses, if
# any, provided herein do not apply to combinations of this program with
# other software, or any other product whatsoever.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
#

. /etc/ha.d/shellfuncs

hostlist=`echo $hostlist | tr ',' ' '`

COOKIEFILE=`maketempfile`
trap "cat $COOKIEFILE >&2; rm -f $COOKIEFILE" EXIT

: ${CURLBIN="/usr/bin/curl"}
: ${user=admin}
: ${password=admin}

check_parameter() {
  if [ ! -x $CURLBIN ]
  then
    echo "Curl can't be found in normal place. Setting CURLBIN to override the default value"
    exit 1
  fi

  if [ -z $hmc_ipaddr ]
  then
    echo "The address of HMC webconsole is not specified"
    exit 1
  fi
}

HMCUSERNAME=$user
HMCPASSWORD=$password

HMC_LOGIN_COMMAND="$CURLBIN -c $COOKIEFILE -d user=$HMCUSERNAME -d password=$HMCPASSWORD -d lang=0 -d submit=Log+in "
HMC_LOGOUT_COMMAND="$CURLBIN -b $COOKIEFILE -d submit=Log+out "
HMC_POWERON_COMMAND="$CURLBIN -b $COOKIEFILE -d form=60 -d sp=255 -d is=0 -d om=4 -d id=1 -d ip=2 -d plt=1 -d pm=0 -d on=Save+settings+and+power+on "
HMC_POWEROFF_COMMAND="$CURLBIN -b $COOKIEFILE -d form=30 -d submit=Continue "
HMC_REBOOT_COMMAND="$CURLBIN -b $COOKIEFILE -d form=74 -d shbmit=Continue "

hmc_reboot() {
  check_parameter
  $HMC_LOGIN_COMMAND http://$1/cgi-bin/cgi
  $HMC_REBOOT_COMMAND http://$1/cgi-bin/cgi
  $HMC_LOGOUT_COMMAND http://$1/cgi-bin/cgi
}
hmc_poweron() {
  check_parameter
  $HMC_LOGIN_COMMAND http://$1/cgi-bin/cgi
  $HMC_POWERON_COMMAND http://$1/cgi-bin/cgi
  $HMC_LOGOUT_COMMAND http://$1/cgi-bin/cgi
}
hmc_poweroff() {
  check_parameter
  $HMC_LOGIN_COMMAND http://$1/cgi-bin/cgi
  $HMC_POWEROFF_COMMAND http://$1/cgi-bin/cgi
  $HMC_LOGOUT_COMMAND http://$1/cgi-bin/cgi
}

case $1 in
gethosts)
	for h in $hostlist; do
		echo $h
	done
	exit 0
	;;
on)
	hmc_poweron $hmc_ipaddr
	exit 0
	;;
off)
	hmc_poweroff $hmc_ipaddr
	exit 0
	;;
reset)
	hmc_reboot $hmc_ipaddr
	exit 0
	;;
status)
	exit 0
	;;
getconfignames)
	for f in hostlist hmc_ipaddr user password; do
		echo $f
	done
	exit 0
	;;
getinfo-devid)
	echo "HMC webconsole STONITH"
	exit 0
	;;
getinfo-devname)
	echo "HMC STONITH webconsole"
	exit 0
	;;
getinfo-devdescr)
	echo "HMC webconsole based host power control"
	exit 0
	;;
getinfo-devurl)
	echo "http://www.ibm.org"
	exit 0
	;;
getinfo-xml)
	cat << HMCXML
<parameters>

<parameter name="hostlist" unique="1" required="1">
<content type="string"/>
<shortdesc lang="en">Hostlist</shortdesc>
<longdesc lang="en">
The list of hosts that the STONITH device controls
</longdesc>
</parameter>

<parameter name="hmc_ipaddr" unique="1" required="1">
<content type="string"/>
<shortdesc lang="en">HMC IPAddr</shortdesc>
<longdesc lang="en">
The IP address of the HMC web console.
</longdesc>
</parameter>

<parameter name="user" unique="1" required="1">
<content type="string"/>
<shortdesc lang="en">User</shortdesc>
<longdesc lang="en">
User name to log into HMC webconsole.
</longdesc>
</parameter>

<parameter name="password" unique="1" required="1">
<content type="string"/>
<shortdesc lang="en">Password</shortdesc>
<longdesc lang="en">
The password of HMC admin.
</longdesc>
</parameter>

</parameters>
HMCXML
	exit 0
	;;
*)
	exit 1
	;;
esac
