#!/bin/sh
#
#
#	SyncHosts OCF RA. This resource is used to synchronize
#	directory and files easily between cluster nodes
#
# Copyright (c) 2008, Sebastian Reitenbach, l00 bugdead prods.
#                    All Rights Reserved.
#
# 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.
#

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

. ${OCF_ROOT}/resource.d/heartbeat/.ocf-shellfuncs

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

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

<longdesc lang="en">
This is a resource script to keep directories and files
between cluster nodes in sync, where it is not possible
to use a shared disk storage.
</longdesc>
<shortdesc lang="en">Sync directories and files between cluster nodes</shortdesc>

<parameters>
<parameter name="exclude_hosts" unique="1">
<longdesc lang="en">
A space separated list of hosts that should be excluded from
the cluster nodes that should be synched.
</longdesc>
<shortdesc lang="en">host exclude list</shortdesc>
<content type="string" default="/var/run/${OCF_RESOURCE_INSTANCE}" />
</parameter>

<parameter name="files" unique="1">
<longdesc lang="en">
A space separated list of files that should be synched.
The files must have full qualified file names
</longdesc>
<shortdesc lang="en">file list</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="directories" unique="1">
<longdesc lang="en">
A space separated list of directories that should be synched.
The files must have full qualified file names
</longdesc>
<shortdesc lang="en">directory list</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="pid" unique="1">
<longdesc lang="en">
The file to be used to store the PID of the 
actual rsync process.
</longdesc>
<shortdesc lang="en">the PID file</shortdesc>
<content type="string" default="" />
</parameter>

</parameters>

<actions>
<action name="start"        timeout="90" />
<action name="stop"         timeout="100" />
<action name="monitor"      timeout="20" interval="10" depth="0" start-delay="0" />
<action name="reload"       timeout="90" />
<action name="migrate_to"   timeout="100" />
<action name="migrate_from" timeout="90" />
<action name="meta-data"    timeout="5" />
<action name="validate-all" timeout="30" />
</actions>
</resource-agent>
END
}

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

# don't exit on TERM, to test that lrmd makes sure that we do exit
trap sigterm_handler TERM
sigterm_handler() {
	ocf_log info "They use TERM to bring us down. No such luck."
	return
}

SyncHosts_NodeList() {
	ALL_NODES=`cl_status listnodes` 
	for NODE in ${ALL_NODES};do
		EXCLUDE=0
		for EX in ${OCF_RESKEY_exclude_hosts} `hostname`;do
			if [ ${NODE} == ${EX} ];then
				EXCLUDE=1
				break
			fi
		done
		if [ ${EXCLUDE} -eq 0 ];then
			echo "${NODE}"
		fi
	done
}

SyncHosts_usage() {
	cat <<END
usage: $0 {start|stop|monitor|migrate_to|migrate_from|validate-all|meta-data}

Expects to have a fully populated OCF RA-compliant environment set.
END
}


SyncHosts_start() {
	# I am a dummy start, everything is triggered via Monitor
	# action 
	return $OCF_SUCCESS
}

SyncHosts_stop() {
	# I maybe have to kill rsync jobs that are started by the
	# monitor action
	NODE_LIST=`SyncHosts_NodeList`
	for NODE in ${NODE_LIST};do
		kill -9 `cat ${OCF_RESKEY_pid}.${NODE}` 2>/dev/null
	done
	# asuming that kill -9 will work as expected!
    	return $OCF_SUCCESS
}

SyncHosts_monitor() {
	# Monitor _MUST!_ differentiate correctly between running
	# (SUCCESS), failed (ERROR) or _cleanly_ stopped (NOT RUNNING).
	# That is THREE states, not just yes/no.
        NODE_LIST=`SyncHosts_NodeList`
        for NODE in ${NODE_LIST};do
                rsync -a -v -R ${OCF_RESKEY_files} ${OCF_RESKEY_directories} ${NODE}:/ &
                echo $! > ${OCF_RESKEY_pid}.${NODE}
        done
	return ${OCF_NOT_RUNNING}
}

SyncHosts_validate() {
    
    # Is the pid directory writable? 
    pid_dir=`dirname "$OCF_RESKEY_pid"`
    touch "$pid_dir/$$"
    if [ $? != 0 ]; then
	return $OCF_ERR_ARGS
    fi
    rm "$pid_dir/$$"

    return $OCF_SUCCESS
}

: ${OCF_RESKEY_pid="/var/run/${OCF_RESOURCE_INSTANCE}"}

case $__OCF_ACTION in
meta-data)	meta_data
		exit $OCF_SUCCESS
		;;
start)		SyncHosts_start
		;;
stop)		SyncHosts_stop;;
monitor)	SyncHosts_monitor
		;;
migrate_to)	ocf_log info "Migrating ${OCF_RESOURCE_INSTANCE} to ${OCF_RESKEY_CRM_meta_migrate_to}."
	        SyncHosts_stop
		;;
migrate_from)	ocf_log info "Migrating ${OCF_RESOURCE_INSTANCE} to ${OCF_RESKEY_CRM_meta_migrated_from}."
	        SyncHosts_start
		;;
reload)		ocf_log err "Restarting..."
	        SyncHosts_stop
	        SyncHosts_start
		;;
validate-all)	SyncHosts_validate;;
usage|help)	SyncHosts_usage
		exit $OCF_SUCCESS
		;;
*)		SyncHosts_usage
		exit $OCF_ERR_UNIMPLEMENTED
		;;
esac
rc=$?
ocf_log debug "${OCF_RESOURCE_INSTANCE} $__OCF_ACTION : $rc"
exit $rc

