#!/bin/sh
#
# External STONITH module for Hetzner.
#
# Copyright (c) 2011 MMUL S.a.S. - Raoul Scarazzini <rasca@mmul.it>
#
# 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.
#

# Read parameters from config file, format is based upon the hetzner OCF resource agent
# developed by Kumina: http://blog.kumina.nl/2011/02/hetzner-failover-ip-ocf-script/
conf_file="/etc/hetzner.cfg"

case $1 in
	get*) ;; # don't print errors if conf_file not present
	*)
		user=`sed -n 's/^user.*=\ *//p' $conf_file`
		pass=`sed -n 's/^pass.*=\ *//p' $conf_file`
	;;
esac

hetzner_server="https://robot-ws.your-server.de"

check_http_response() {
     # If the response is 200 then return 0
     if [ $1 = 200 ]
      then
       return 0
      else
       # If the response is not 200 then display a description of the problem and return 1
       case $1 in
        400) ha_log.sh err  "INVALID_INPUT - Invalid input parameters"
               ;;
        404) ha_log.sh err  "SERVER_NOT_FOUND - Server with ip $remote_ip not found"
               ;;
        409) ha_log.sh err  "RESET_MANUAL_ACTIVE - There is already a running manual reset"
               ;;
        500) ha_log.sh err "RESET_FAILED - Resetting failed due to an internal error"
               ;;
       esac
       return 1
     fi
}

case $1 in
gethosts)
        echo $hostname
	exit 0
	;;
on)
	# Can't really be implemented because Hetzner's webservice cannot power on a system
	ha_log.sh err "Power on is not available since Hetzner's webservice can't do this operation."
	exit 1
	;;
off)
	# Can't really be implemented because Hetzner's webservice cannot power on a system
	ha_log.sh err "Power off is not available since Hetzner's webservice can't do this operation."
	exit 1
	;;
reset)
        # Launching the reset action via webservice
        check_http_response $(curl --silent -o /dev/null -w '%{http_code}' -u $user:$pass $hetzner_server/reset/$remote_ip -d type=hw)
        exit $?
	;;
status)
        # Check if we can contact the webservice
        check_http_response "$(curl --silent -o /dev/null -w '%{http_code}' -u $user:$pass $hetzner_server/server/$remote_ip)"
        exit $?
	;;
getconfignames)
	echo "hostname"
	echo "remote_ip"
	exit 0
	;;
getinfo-devid)
	echo "Hetzner STONITH device"
	exit 0
	;;
getinfo-devname)
	echo "Hetzner STONITH external device"
	exit 0
	;;
getinfo-devdescr)
	echo "Hetzner host reset"
	echo "Manages the remote webservice for reset a remote server."
	exit 0
	;;
getinfo-devurl)
	echo "http://wiki.hetzner.de/index.php/Robot_Webservice_en"
	exit 0
	;;
getinfo-xml)
	cat << HETZNERXML
<parameters>
<parameter name="hostname" unique="1" required="1">
<content type="string" />
<shortdesc lang="en">
Hostname
</shortdesc>
<longdesc lang="en">
The name of the host to be managed by this STONITH device.
</longdesc>
</parameter>

<parameter name="remote_ip" unique="1" required="1">
<content type="string" />
<shortdesc lang="en">
Remote IP
</shortdesc>
<longdesc lang="en">
The address of the remote IP that manages this server.
</longdesc>
</parameter>
</parameters>
HETZNERXML
	exit 0
	;;
*)
	ha_log.sh err "Don't know what to do for '$remote_ip'"
	exit 1
	;;
esac
