#!/bin/sh
#
# $Id: ICP.in,v 1.1 2004/12/20 16:19:37 sunjd Exp $
# 
# ICP
#
# Description:	Manages an ICP Vortex clustered host drive as an HA resource
#
#
# Author:	Lars Marowsky-Bree <lmb@suse.de>
# Support:	linux-ha-dev@lists.tummy.com
# License:	GNU General Public License (GPL)
# Copyright:	(C) 2002 SuSE Linux AG
#
#
# An example usage in /etc/ha.d/haresources: 
#       node1  10.0.0.170 LinuxSCSI::0:0 ICP::c0h1::/dev/sdb1 LVM::myvolname
#
# Notice that you will need to get the utility "icpclucon" from the ICP
# support to use this.
#
# See usage() function below for more details...
#
#	  OCF parameters are as below:
#		OCF_RESKEY_driveid
#		OCF_RESKEY_device

#######################################################################
# Initialization:

. /usr/lib/heartbeat/ocf-shellfuncs

#######################################################################

prefix=/usr
exec_prefix=/usr

#
ICPCLUCON=/usr/sbin/icpclucon
#
BLOCKDEV=/sbin/blockdev
#

usage() {
  methods=`ICP_methods | grep -v methods`
  methods=`echo $methods | tr ' ' '|'`
  cat <<-! >&1
	usage: $0 ($methods)

	$0 manages an ICP Vortex clustered host drive.

	The 'start' operation reserves the given host drive.
	The 'stop' operation releses the given host drive.
	The 'status' operation reports whether the host drive is reserved.
	The 'monitor' operation reports whether the host drive is reserved.
	The 'methods' operation reports on the methods $0 supports

	$Id: ICP.in,v 1.1 2004/12/20 16:19:37 sunjd Exp $
	!
}

meta_data() {
	cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="ICP" version="0.9">
<version>1.0</version>

<longdesc lang="en">
Resource script for ICP. It Manages an ICP Vortex clustered host drive as an 
HA resource. 
</longdesc>
<shortdesc lang="en">ICP resource agent</shortdesc>

<parameters>
<parameter name="driveid" unique="0">
<longdesc lang="en">
The ICP cluster drive ID.
</longdesc>
<shortdesc lang="en">ICP cluster drive ID</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="device" unique="0">
<longdesc lang="en">
The device name.
</longdesc>
<shortdesc lang="en">device</shortdesc>
<content type="string" default="" />
</parameter>
</parameters>

<actions>
<action name="start" timeout="10" />
<action name="stop" timeout="10" />
<action name="status" depth="0" timeout="10" interval="10" start-delay="10" />
<action name="monitor" depth="0" timeout="10" interval="10" start-delay="10" />
<action name="meta-data" timeout="5" />
</actions>
</resource-agent>
END
}

#
#	run:  run a script, and log its output.
#
run() {
  output=`"$@" 2>&1`
  rc=$?
  output=`echo $output`
  if
    [ $rc -eq 0 ]
  then 
    if
      [ ! -z "$output" ]
    then
      ocf_log "info" "$output"
    fi
    return 0
  else
    if
      [ ! -z "$output" ]
    then
      ocf_log "err" "$output"
    else
      ocf_log "err" "command failed: $*"
    fi
    return $rc
  fi
}



#
# methods: What methods/operations do we support?
#
ICP_methods() {
  cat <<-!
	start
	stop
	status
	monitor
	methods
	meta-data
	usage
	!
}

ICP_status() {
        local icp_out
        
        icp_out=$($ICPCLUCON -v -status $1)
        if [ $? -ne 0 ]; then
                ocf_log "err" "Hostdrive not reserved by us."
                return 1
        fi

        if expr match "$icp_out" \
                '.*Drive is reserved by this host.*' >/dev/null 2>&1 ; then
                ocf_log "info" "Volume $1 is reserved by us."
                return 0
        elif expr match "$icp_out" \
                '.*Drive is not reserved by any host.*' >/dev/null 2>&1 ; then
                ocf_log "err" "Volume $1 not reserved by any host."
                return 1
        else
                ocf_log "err" "Unknown output from icpclucon. Assuming we do not have a reservation:"
		ocf_log "err" "$icp_out"
                return 1
        fi
}

ICP_report_status() {
  if ICP_status $1 ; then
	echo "$1: running"
	return 0
  else
	echo "$1: not running"
	return 1
  fi
}


#
#	Monitor the host drive - does it really seem to be working?
#
#
ICP_monitor() {

  if
    ICP_status $1
  then
    : OK
  else
    ocf_log "err" "ICP host drive $1 is offline"
    return 1
  fi

}

Clear_bufs() {
    $BLOCKDEV --flushbufs $2
}

#
#	Enable ICP host drive
#
ICP_start() {

  ocf_log "info" "Activating host drive $1"
  run $ICPCLUCON -v -reserve $1
  if [ $? -ne 0 ]; then
  	ocf_log "info" "Forcing reservation of $1"
	run $ICPCLUCON -v -force $1 || return 1
  fi

  if
    ICP_status $1
  then
    : OK
    # A reservation isn't as prompt as it should be
    sleep 3
    return 0
  else
    ocf_log "err" "ICP: $1 was not reserved correctly"
    return 1
  fi
}

#
#	Release the ICP host drive
#
ICP_stop() {

  ocf_log "info" "Releasing ICP host drive $1"
  run $ICPCLUCON -v -release $1 || return 1

  ocf_log "info" "Verifying reservation"
  if ICP_status $1 ; then
    ocf_log "err" "ICP: $1 was not released correctly"
    return 1
  fi
  return 0
}


#
#	'main' starts here...
#

if
  ( [ $# -eq 0 ] || [ $# -gt 1 ] )
then
  usage
  exit 1
fi

if 
  ( [ -z "$OCF_RESKEY_driveid" ] ||  [ -z "$OCF_RESKEY_device" ] )
then
  usage
  exit 1
fi

driveid=$OCF_RESKEY_driveid
device=$OCF_RESKEY_device

# What kind of method was invoked?
case "$1" in

  meta-data)	meta_data
		exit $OCF_SUCCESS;;

  start)	ICP_start $driveid
		Clear_bufs $device
		exit $?;;

  stop)		ICP_stop $driveid
		Clear_bufs $device
		exit $?;;

  status)	ICP_report_status $driveid
		exit $?;;

  monitor)	ICP_monitor $driveid
		exit $?;;

  methods)	ICP_methods
		exit $OCF_SUCCESS;;

  *)		usage
		exit $OCF_ERR_UNIMPLEMENTED;;
esac

exit 1
