#!/bin/sh
#
#	Openldap OCF Anonymous Multistate Clone RA.
#
# Author: Vitaly Ostanin <vitaly.ostanin@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.
#

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

. ${OCF_ROOT}/resource.d/heartbeat/.ocf-shellfuncs
OPENLDAP_TOOLS=/usr/lib/alterator-heartbeat/
LOCK_FILE=/var/run/alterator-heartbeat/Openldap.lock

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

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

<longdesc lang="en">
Master/Slave Resource Agent for Openldap.
</longdesc>
<shortdesc lang="en">Openldap master/slave resource agent</shortdesc>

<parameters>
<parameter name="openldap_base" unique="1" required="1">
<longdesc lang="en">
The name of the openldap base.
</longdesc>
<shortdesc lang="en">Openldap base</shortdesc>
<content type="string" default="db01" />
</parameter>

<parameter name="clone_max" required="1">
<longdesc lang="en">
Number of clones of this drbd resource. Do not fiddle with the default.
</longdesc>
<shortdesc lang="en">Number of clones</shortdesc>
<content type="integer" default="2"/>
</parameter>

<parameter name="clone_node_max" required="1">
<longdesc lang="en">
Clones per node. Do not fiddle with the default.
</longdesc>
<shortdesc lang="en">Number of nodes</shortdesc>
<content type="integer" default="1"/>
</parameter>

<parameter name="master_max" required="1">
<longdesc lang="en">
Maximum number of active primaries. Do not fiddle with the default.
</longdesc>
<shortdesc lang="en">Number of primaries</shortdesc>
<content type="integer" default="1"/>
</parameter>

<parameter name="master_node_max" required="1">
<longdesc lang="en">
Maximum number of primaries per node. Do not fiddle with the default.
</longdesc>
<shortdesc lang="en">Number of primaries per node</shortdesc>
<content type="integer" default="1"/>
</parameter>
</parameters>

<actions>
<action name="start"   timeout="10" />
<action name="stop"    timeout="10" />
<action name="promote" timeout="60" />
<action name="demote"  timeout="60" />
<action name="notify"  timeout="60" />
<action name="monitor" depth="0"  timeout="30" interval="10" role="Slave" />
<action name="monitor" depth="0"  timeout="30" interval="11" role="Master" />
<action name="monitor" depth="0"  timeout="30" interval="12" role="Started" />
<action name="validate-all" timeout="5" />
<action name="meta-data"    timeout="5" />
</actions>
</resource-agent>
END
    exit $OCF_SUCCESS
}

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

openldap_usage() {
	cat <<END
usage: $0 {start|stop|monitor|notify|promote|demote|validate-all|meta-data}

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

monitor() {
    [ -f "$LOCK_FILE" ] || return $OCF_NOT_RUNNING
    local base_mode=$("$OPENLDAP_TOOLS"/openldap-get-base-mode.sh "$OCF_RESKEY_openldap_base")
    case "$base_mode" in
	slave|normal)
	    return $OCF_SUCCESS
	    ;;
	master)
	    return $OCF_RUNNING_MASTER
	    ;;
	*)
	    ocf_log err "Cannot get mode for openldap base $OCF_RESKEY_openldap_base."
	    return $OCF_ERR_GENERIC
	    ;;
    esac
}

start() {
    if touch "$LOCK_FILE"  ; then
	if "$OPENLDAP_TOOLS"/openldap-change-base-mode.sh "$OCF_RESKEY_openldap_base" slave ; then
		service slapd condrestart
		crm_master -v 50 -l reboot
		ocf_log info "$OCF_RESKEY_openldap_base: start succeeded"
		return $OCF_SUCCESS
	else
	    ocf_log err "$OCF_RESKEY_openldap_base: start (to slave) failed with exit code $?."
	    return $OCF_ERR_GENERIC
	fi
    else
	ocf_log err "start failed, cannot create lock file."
	return $OCF_ERR_GENERIC
    fi
}

stop() {
    crm_master -D
    if rm -f "$LOCK_FILE"  ; then
	ocf_log info "Openldap: stop succeeded"
	return $OCF_SUCCESS
    else
	ocf_log err "Openldap: stop failed with exit code $?."
	return $OCF_ERR_GENERIC
    fi
}

notify() {
#    [ "$OCF_RESKEY_CRM_meta_notify_type" = "post" ] && \
#	crm_master -v 50 -l reboot
    return $OCF_SUCCESS
}

promote() {
    if "$OPENLDAP_TOOLS"/openldap-change-base-mode.sh "$OCF_RESKEY_openldap_base" master ; then
	service slapd condrestart
	ocf_log info "$OCF_RESKEY_openldap_base promote: master succeeded"
	return $OCF_SUCCESS
    else
	ocf_log err "$OCF_RESKEY_openldap_base promote: Failed with exit code $?."
	return $OCF_ERR_GENERIC
    fi
}

demote() {
    if "$OPENLDAP_TOOLS"/openldap-change-base-mode.sh "$OCF_RESKEY_openldap_base" slave ; then
	service slapd condrestart
	ocf_log info "$OCF_RESKEY_openldap_base demote: slave succeeded"
    else
	ocf_log info "$OCF_RESKEY_openldap_base demote: failed changing base mode to slave."
    fi
    return $OCF_SUCCESS
}

check() {
    return $OCF_SUCCESS
}

case $__OCF_ACTION in
    meta-data)
	meta_data
	exit $OCF_SUCCESS
	;;
    start)
	start
	;;
    stop)
	stop
	;;
    validate-all)
	check
	;;
    monitor)
	monitor
	;;
    notify)
	notify
	;;
    promote)
	promote
	;;
    demote)
	demote
	;;
    usage|help)
	openldap_usage
	exit $OCF_SUCCESS
	;;
    *)
	openldap_usage
	exit $OCF_ERR_UNIMPLEMENTED
	;;
esac
rc=$?
ocf_log debug "${OCF_RESOURCE_INSTANCE} $__OCF_ACTION : $rc"
exit $rc
