BDII heartbeat script

From PDP/Grid Wiki
Jump to navigationJump to search


#!/bin/bash
#
#
#       Dummy OCF RA. Does nothing but wait a few seconds, can be
#       configured to fail occassionally.
#
# Copyright (c) 2004 SUSE LINUX AG, Lars Marowsky-Br<E9>e
#                    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_FUNCTIONS_DIR=${OCF_ROOT}/resource.d/heartbeat}
. ${OCF_FUNCTIONS_DIR}/.ocf-shellfuncs

prog=bdii
lockfile=/var/lock/subsys/$prog
if [ -r /etc/sysconfig/bdii ] ; then
    . /etc/sysconfig/bdii
fi

BDII_CONF=${BDII_CONF:-/etc/bdii/bdii.conf}

if [ -f "${BDII_CONF}" ]; then
    . "${BDII_CONF}"
fi

UPDATE_LOCK_FILE=${UPDATE_LOCK_FILE:-/var/lock/subsys/bdii-update}
SLAPD_LOCK_FILE=${SLAPD_LOCK_FILE:-/var/lock/subsys/bdii-slapd}
UPDATE_PID_FILE=${PID_FILE:-/var/run/bdii-update.pid}
BDII_USER=${BDII_USER:-ldap}
BDII_VAR_DIR=${BDII_VAR_DIR:-/var/run/bdii}
BDII_UPDATE=${BDII_UPDATE:-/usr/sbin/bdii-update}
BDII_PROXY=${BDII_PROXY:-/usr/sbin/bdii-proxy}
SLAPD=${SLAPD:-/usr/sbin/slapd}
SLAPD_CONF=${SLAPD_CONF:-/etc/bdii/bdii-slapd.conf}
SLAPD_PORT=${SLAPD_PORT:-2170}
SLAPD_DB_DIR=${SLAPD_DB_DIR:-$BDII_VAR_DIR/db}
SLAPD_PID_FILE=${SLAPD_PID_FILE:-$SLAPD_DB_DIR/slapd.pid}
DB_CONFIG=${DB_CONFIG:-/etc/bdii/DB_CONFIG}

if [ -x /sbin/runuser ] ; then
    RUNUSER=runuser
else
    RUNUSER=su
fi


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

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

<longdesc lang="en">
This is the BDII heartbeat resource agent.

</longdesc>
<shortdesc lang="en">BDII heartbeat resource agent</shortdesc>

<parameters>
<parameter name="state" unique="1">
<longdesc lang="en">
Location to store the resource state in.
</longdesc>
<shortdesc lang="en">State file</shortdesc>
<content type="string" default="${HA_RSCTMP}/bdii-{OCF_RESOURCE_INSTANCE}.state" />
</parameter>

</parameters>

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

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

dummy_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
}

dummy_start() {
    dummy_monitor
    if [ $? =  $OCF_SUCCESS ]; then
        return $OCF_SUCCESS
    fi
    touch ${OCF_RESKEY_state}

    # kill lingering processes

    if [ -f "${SLAPD_PID_FILE}" ]; then
        pid=`cat "${SLAPD_PID_FILE}"`
        kill -9 $pid >/dev/null 2>&1
        rm -f "${SLAPD_LOCK_FILE}" "${SLAPD_PID_FILE}"
    fi
    if [ -f "${UPDATE_PID_FILE}" ]; then
        pid=`cat "${UPDATE_PID_FILE}"`
        kill -9 $pid >/dev/null 2>&1
        rm -f "${UPDATE_LOCK_FILE}" "${UPDATE_PID_FILE}"
    fi


    # Create proxy
    ${BDII_PROXY} ${BDII_CONF}

#    # Create RAM Disk
#    if [ "${BDII_RAM_DISK}" = "yes" ]; then
#       mount -t tmpfs -o size=1G,mode=0744 tmpfs ${SLAPD_DB_DIR}
#    fi

    #Initialize the database directory.
    mkdir -p ${SLAPD_DB_DIR}/stats
    mkdir -p ${SLAPD_DB_DIR}/glue2
    mkdir -p ${BDII_VAR_DIR}/archive
    mkdir -p /var/lock/subsys
    chown -R ${BDII_USER}:${BDII_USER} ${BDII_VAR_DIR}
    [ -x /sbin/restorecon ] && /sbin/restorecon -R ${BDII_VAR_DIR}
    chown -R ${BDII_USER}:${BDII_USER} ${SLAPD_DB_DIR}
    $RUNUSER -s /bin/sh ${BDII_USER} -c "rm -f ${SLAPD_DB_DIR}/stats/* 2>/dev/null"
    $RUNUSER -s /bin/sh ${BDII_USER} -c "rm -f ${SLAPD_DB_DIR}/glue2/* 2>/dev/null"
    $RUNUSER -s /bin/sh ${BDII_USER} -c "rm -f ${SLAPD_DB_DIR}/* 2>/dev/null"
    $RUNUSER -s /bin/sh ${BDII_USER} -c "rm -f ${BDII_VAR_DIR}/old.ldif 2>/dev/null"
    $RUNUSER -s /bin/sh ${BDII_USER} -c "cp ${DB_CONFIG} ${SLAPD_DB_DIR}"
    $RUNUSER -s /bin/sh ${BDII_USER} -c "cp ${DB_CONFIG} ${SLAPD_DB_DIR}/stats/"
    $RUNUSER -s /bin/sh ${BDII_USER} -c "cp ${DB_CONFIG} ${SLAPD_DB_DIR}/glue2/"

#    echo -n "Starting BDII slapd: "
    COMMAND="${SLAPD} -f ${SLAPD_CONF} -h ldap://${BDII_IP:-$(hostname -f)}:${SLAPD_PORT} -u ${BDII_USER}"
    ${COMMAND}
    touch ${SLAPD_LOCK_FILE}
    if [ ! -f "${SLAPD_PID_FILE}" ]; then
        sleep 2
    fi

    if [ -f "${SLAPD_PID_FILE}" ]; then
        ps $(cat ${SLAPD_PID_FILE}) >/dev/null 2>&1
        RETVAL=$?
    else
        RETVAL=1
    fi

    if [ ${RETVAL} -gt 0 ]; then
        echo -n "BDII slapd failed to start" 1>&2

        rm -f ${SLAPD_LOCK_FILE}
#        eval log_failure_msg
#        echo "${COMMAND} -d 256"
#        ${COMMAND} -d 256
        return 1
    else
        :
#        eval log_success_msg
    fi

    export SLAPD_CONF=${SLAPD_CONF}
    $RUNUSER -s /bin/sh ${BDII_USER} -c "${BDII_UPDATE} -c ${BDII_CONF} -d"
    touch ${UPDATE_LOCK_FILE}

    if [ ! -f ${BDII_VAR_DIR}/bdii-update.pid ]; then
        sleep 2
    fi
    if [ -f ${BDII_VAR_DIR}/bdii-update.pid ]; then
        touch ${UPDATE_PID_FILE}
        chown ${BDII_USER} ${UPDATE_PID_FILE}
        mv ${BDII_VAR_DIR}/bdii-update.pid ${UPDATE_PID_FILE}
        ps $(cat ${UPDATE_PID_FILE}) >/dev/null 2>&1
        RETVAL=$?
    else
        RETVAL=1
    fi

#    echo -n "Starting BDII update process: "
    if [ ${RETVAL} -gt 0 ]; then
        echo -n "BDII update process failed to start" 1>&2
        rm -f ${UPDATE_LOCK_FILE}
#        eval log_failure_msg
        return 1
    else
#        eval log_success_msg
        touch $lockfile
        return 0
    fi

}

dummy_stop() {
    rm ${OCF_RESKEY_state}

    RETVAL=0

#    echo -n "Stopping BDII update process: "

    if [ -f "${UPDATE_PID_FILE}" ]; then
        UPDATE_PID=$(cat ${UPDATE_PID_FILE})
    fi

    $RUNUSER -s /bin/sh ${BDII_USER} -c "kill -15 ${UPDATE_PID} 2>/dev/null"
    if [ -n "${UPDATE_PID}" ]; then
        ps ${UPDATE_PID} >/dev/null 2>&1
        if [ $? = 0 ]; then
            sleep 2
            ps ${UPDATE_PID} >/dev/null 2>&1
            if [ $? = 0 ]; then
                $RUNUSER -s /bin/sh ${BDII_USER} -c "kill -9 ${UPDATE_PID} 2>/dev/null"
                sleep 2
                ps ${UPDATE_PID} >/dev/null 2>&1
                if [ $? = 0 ]; then
                    echo -n "Could not kill BDII update process ${UPDATE_PID}" 1>&2
                    RETVAL=1
                fi
            fi
        fi
    fi

    if [ ${RETVAL} = 0 ];  then
        rm -f ${UPDATE_PID_FILE}
        rm -f ${UPDATE_LOCK_FILE}
#        eval log_success_msg
    else
        :
#        eval log_failure_msg
    fi

#    echo -n "Stopping BDII slapd: "

    if [ -f "${SLAPD_PID_FILE}" ]; then
        SLAPD_PID=$(cat ${SLAPD_PID_FILE})
    fi

    $RUNUSER -s /bin/sh ${BDII_USER} -c "kill -15 ${SLAPD_PID} 2>/dev/null"
    if [ -n "${SLAPD_PID}" ]; then
        ps ${SLAPD_PID} >/dev/null 2>&1
        if [ $? = 0 ]; then
            sleep 2
            ps ${SLAPD_PID} >/dev/null 2>&1
            if [ $? = 0 ]; then
                $RUNUSER -s /bin/sh ${BDII_USER} -c "kill -9 ${SLAPD_PID} 2>/dev/null"
                sleep 2
                ps ${SLAPD_PID} >/dev/null 2>&1
                if [ $? = 0 ]; then
                    echo -n "Could not kill BDII slapd process ${SLAPD_PID}" 1>&2
                    RETVAL=2
                else
                    rm -f {SLAPD_PID_FILE}
                fi
            fi
        fi
    fi


    if [ ${RETVAL} = 2 ];  then
        :
        #eval log_failure_msg
    else
        rm -f ${SLAPD_LOCK_FILE}
        #eval log_success_msg
    fi

    if [ ! ${RETVAL} = 0 ];  then
        return 1
    else
#       mountpoint -q ${SLAPD_DB_DIR} && umount ${SLAPD_DB_DIR}
        rm -f $lockfile
        return 0
    fi
}

dummy_monitor() {
        # Monitor _MUST!_ differentiate correctly between running
        # (SUCCESS), failed (ERROR) or _cleanly_ stopped (NOT RUNNING).
        # That is THREE states, not just yes/no.

####
#       if [ -f ${OCF_RESKEY_state} ]; then
#           return $OCF_SUCCESS
#       fi
#       if false ; then
#               return $OCF_ERR_GENERIC
#       fi
#       return $OCF_NOT_RUNNING
####

    if [ ! -f "${SLAPD_LOCK_FILE}" ] && [ ! -f "${UPDATE_LOCK_FILE}" ]; then
        # no lock files found
        return $OCF_NOT_RUNNING
    fi

    if [ -f ${SLAPD_PID_FILE} ]; then
        ps $(cat ${SLAPD_PID_FILE}) >/dev/null 2>&1
        if [ ! $? = 0 ]; then
            # slapd lockfile found but no process
            return $OCF_ERR_GENERIC
        fi
    else
        # no slapd lockfile found
        return $OCF_ERR_GENERIC
    fi

    if [ -f ${UPDATE_PID_FILE} ]; then
        ps $(cat ${UPDATE_PID_FILE}) >/dev/null 2>&1
        if [ ! $? = 0 ]; then
            # no bdii_update process
            return $OCF_ERR_GENERIC
        fi
    else
        # no bdii_update lockfile
        return $OCF_ERR_GENERIC
    fi

    # all's well that ends well
    return $OCF_SUCCESS
}

dummy_validate() {
    # Is the state directory writable?
    state_dir=`dirname $SLAPD_LOCK_FILE"`
    touch "$state_dir/$$"
    if [ $? != 0 ]; then
        return $OCF_ERR_ARGS
    fi
    rm "$state_dir/$$"

    return $OCF_SUCCESS
}


: ${OCF_RESKEY_state=${HA_RSCTMP}/bdii-${OCF_RESOURCE_INSTANCE}.state}

case $__OCF_ACTION in
meta-data)      meta_data
                exit $OCF_SUCCESS
                ;;
start)          dummy_start;;
stop)           dummy_stop;;
monitor)        dummy_monitor;;
migrate_to)     ocf_log info "Migrating ${OCF_RESOURCE_INSTANCE} to ${OCF_RESKEY_CRM_meta_migrate_to}."
                dummy_stop
                ;;
migrate_from)   ocf_log info "Migrating ${OCF_RESOURCE_INSTANCE} to ${OCF_RESKEY_CRM_meta_migrated_from}."
                dummy_start
                ;;
reload)         ocf_log err "Reloading..."
                dummy_start
                ;;
validate-all)   dummy_validate;;
usage|help)     dummy_usage
                exit $OCF_SUCCESS
                ;;
*)              dummy_usage
                exit $OCF_ERR_UNIMPLEMENTED
                ;;
esac
rc=$?
ocf_log debug "${OCF_RESOURCE_INSTANCE} $__OCF_ACTION : $rc"
exit $rc