Skip to content

Instantly share code, notes, and snippets.

@ngyuki
Last active August 29, 2015 14:09
Show Gist options
  • Save ngyuki/b2a75a06b178fdf20288 to your computer and use it in GitHub Desktop.
Save ngyuki/b2a75a06b178fdf20288 to your computer and use it in GitHub Desktop.
daemontools ocf script for pacemaker and heartbeat
#!/bin/sh
#######################################################################
# TEST
#
# export OCF_ROOT=/usr/lib/ocf
# export OCF_RESKEY_service_dir=/service/dummy
# export OCF_RESKEY_svc_bin=/usr/bin/svc
# export OCF_RESKEY_svstat_bin=/usr/bin/svstat
#
# /usr/lib/ocf/resource.d/ngyuki/daemontools meta-data ; echo $?
# /usr/lib/ocf/resource.d/ngyuki/daemontools validate-all ; echo $?
#######################################################################
# Initialization
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/resource.d/heartbeat}
. ${OCF_FUNCTIONS_DIR}/.ocf-shellfuncs
#######################################################################
# Default
OCF_RESKEY_svc_bin_default=`PATH=/usr/local/bin:$PATH which svc 2> /dev/null`
OCF_RESKEY_svstat_bin_default=`PATH=/usr/local/bin:$PATH which svstat 2> /dev/null`
: ${OCF_RESKEY_svc_bin=${OCF_RESKEY_svc_bin_default}}
: ${OCF_RESKEY_svstat_bin=${OCF_RESKEY_svstat_bin_default}}
#######################################################################
daemontools_meta_data()
{
cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="daemontools">
<version>1.0</version>
<longdesc lang="en">daemontools resource agent</longdesc>
<shortdesc lang="en">daemontools resource agent</shortdesc>
<parameters>
<parameter name="service_dir" unique="1" required="1">
<longdesc lang="en">service directory</longdesc>
<shortdesc lang="en">service directory</shortdesc>
<content type="string" default="/service/xxx" />
</parameter>
<parameter name="svc_bin" unique="1" required="0">
<longdesc lang="en">svc binary path</longdesc>
<shortdesc lang="en">svc binary path</shortdesc>
<content type="string" default="${OCF_RESKEY_svc_bin_default}" />
</parameter>
<parameter name="svstat_bin" unique="1" required="0">
<longdesc lang="en">svstat binary path</longdesc>
<shortdesc lang="en">svstat binary path</shortdesc>
<content type="string" default="${OCF_RESKEY_svstat_bin}" />
</parameter>
</parameters>
<actions>
<action name="start" timeout="5" />
<action name="stop" timeout="30" />
<action name="monitor" timeout="20" interval="20" depth="0" />
<action name="meta-data" timeout="5" />
<action name="validate-all" timeout="20" />
</actions>
</resource-agent>
END
return $OCF_SUCCESS
}
daemontools_usage()
{
cat <<END
usage: $0 {start|stop|monitor|validate-all|meta-data}
END
return $OCF_SUCCESS
}
daemontools_validate()
{
if [ -z "$OCF_RESKEY_service_dir" ]; then
exit $OCF_ERR_GENERIC
fi
if [ ! -d "$OCF_RESKEY_service_dir" ]; then
ocf_log notice "$OCF_RESKEY_service_dir not directory"
exit $OCF_ERR_ARGS
fi
if [ ! -x "$OCF_RESKEY_service_dir/run" ]; then
ocf_log notice "$OCF_RESKEY_service_dir/run not executable"
exit $OCF_ERR_ARGS
fi
if [ ! -e "$OCF_RESKEY_service_dir/down" ]; then
ocf_log notice "$OCF_RESKEY_service_dir/down notfound"
exit $OCF_ERR_ARGS
fi
if [ ! -x "$OCF_RESKEY_svc_bin_default" ]; then
ocf_log notice "$OCF_RESKEY_svc_bin_default not executable"
exit $OCF_ERR_INSTALLED
fi
if [ ! -x "$OCF_RESKEY_svstat_bin" ]; then
ocf_log notice "$OCF_RESKEY_svstat_bin not executable"
exit $OCF_ERR_INSTALLED
fi
return $OCF_SUCCESS
}
daemontools_start()
{
daemontools_validate
$OCF_RESKEY_svc_bin_default -u $OCF_RESKEY_service_dir
ocf_log info "$OCF_RESKEY_service_dir started"
return $OCF_SUCCESS
}
daemontools_stop()
{
daemontools_validate
$OCF_RESKEY_svc_bin_default -d $OCF_RESKEY_service_dir
ocf_log info "$OCF_RESKEY_service_dir stoped"
return $OCF_SUCCESS
}
daemontools_monitor()
{
daemontools_validate
$OCF_RESKEY_svstat_bin $OCF_RESKEY_service_dir | /bin/grep 'up (pid' >/dev/null 2>&1
case "$?" in
0)
ocf_log debug "$OCF_RESKEY_service_dir monitor running"
exit $OCF_SUCCESS
;;
1)
ocf_log info "$OCF_RESKEY_service_dir monitor stoped"
exit $OCF_NOT_RUNNING
;;
*)
ocf_log notice "$OCF_RESKEY_service_dir monitor unknown"
exit $OCF_ERR_GENERIC
;;
esac
}
case $__OCF_ACTION in
meta-data)
daemontools_meta_data
;;
usage|help)
daemontools_usage
;;
start)
daemontools_start
;;
stop)
daemontools_stop
;;
monitor)
daemontools_monitor
;;
validate-all)
daemontools_validate
;;
*)
daemontools_usage
exit $OCF_ERR_UNIMPLEMENTED
;;
esac
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment