#!/bin/sh
#
#
#	Zentyal OCF RA. It manages any Zentyal module
#
# Copyright (c) 2014 Zentyal
#                    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=${OCF_ROOT}/resource.d/heartbeat/.ocf-shellfuncs}
. ${OCF_FUNCTIONS}
: ${__OCF_ACTION=$1}

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

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

<longdesc lang="en">
Resource Agent which manages any Zentyal module.

It starts/stops/monitor the upstart job depending on the configured parameter name.
Only modules which implement Service interface will work.
</longdesc>
<shortdesc lang="en">Zentyal module OCF RA</shortdesc>

<parameters>

<parameter name="module_name" unique="1" required="1">
<longdesc lang="en">
Zentyal module name. Only valid service modules are accepted
</longdesc>
<shortdesc lang="en">Module name</shortdesc>
<content type="string" default=""/>
</parameter>

</parameters>

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

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

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

zentyal_validate() {
    # Check the module is a valid one
    output=$(service zentyal $OCF_RESKEY_module_name enabled)
    if [ $? = 0 ]; then
        return $OCF_SUCCESS
    else
        return $OCF_ERR_GENERIC
    fi
}

zentyal_monitor () {
    # Parse output as the returned value is not enough
    output=$(service zentyal $OCF_RESKEY_module_name status)
    if echo $output | grep -q '\[ RUNNING \]'; then
        return $OCF_SUCCESS
    elif echo $output | grep -qP '(STOPPED|RUNNING UNMANAGED|DISABLED)'; then
        return $OCF_NOT_RUNNING
    else
        return $OCF_ERR_GENERIC
    fi  
}

zentyal_start () {
    zentyal_monitor
    if [ $? = $OCF_SUCCESS ]; then
        ocf_log info "$OCF_RESKEY_module_name is already running"
        return $OCF_SUCCESS
    fi

    service zentyal $OCF_RESKEY_module_name start
    rc=$?
    if [ $rc != 0 ]; then
        ocf_log err "$OCF_RESKEY_module_name start command failed: $rc"
        return $rc
    fi

    zentyal_monitor
    rc=$?
    if [ $rc = $OCF_SUCCESS ]; then
        ocf_log info "$OCF_RESKEY_module_name started"
    elif [ $rc = $OCF_NOT_RUNNING ]; then
        ocf_log err "$OCF_RESKEY_module_name start command failed: $rc"
    fi
    return $rc
}

zentyal_stop () {
    zentyal_monitor
    if [ $? =  $OCF_NOT_RUNNING ]; then
        ocf_log info "$OCF_RESKEY_module_name is already stopped"
        return $OCF_SUCCESS
    fi

    service zentyal $OCF_RESKEY_module_name stop
    rc=$?

    if [ $rc != 0 ]; then
        ocf_log err "$OCF_RESKEY_module_name stop command failed: $rc"
        return $rc
    fi

    # upstart waits until daemon is running
    zentyal_monitor
    rc=$?
    if [ $rc = $OCF_NOT_RUNNING ]; then
        ocf_log info "$OCF_RESKEY_module_name stopped"
        return $OCF_SUCCESS
    else
        ocf_log err "$OCF_RESKEY_module_name cannot be stopped: $rc"
        return $rc
    fi
}

case $__OCF_ACTION in
meta-data)      meta_data
                exit $OCF_SUCCESS
		;;
start)          zentyal_start;;
stop)           zentyal_stop;;
monitor)        zentyal_monitor;;
reload)         zentyal_start;;
validate-all)   zentyal_validate;;
usage|help)     zentyal_usage
	        exit $OCF_SUCCESS
		;;
*)              zentyal_usage
		exit $OCF_ERR_UNIMPLEMENTED
		;;
esac
exit $?
