#!/bin/bash
#
# Resource script for ntp daemon with namespace support
#
# Description:  Manages ntp daemon as an OCF resource in
#               an High Availability setup inside a namespace
#
# HAProxy OCF script's Author: Mirantis
# License: GNU General Public License (GPL)
#
#  usage: $0 {start|stop|restart|status|monitor|validate-all|meta-data}
#
#  The "start" arg starts ntp.
#
#  The "stop" arg stops it.
#
# OCF parameters:
# OCF_RESKEY_ns
# OCF_RESKEY_conffile
# OCF_RESKEY_pidfile
# OCF_RESKEY_binpath
# OCF_RESKEY_extraconf
#
# Note: This RA requires that the ntp config files has a "pidfile"
# entry so that it is able to act on the correct process
##########################################################################
# Initialization:

OCF_ROOT_default="/usr/lib/ocf"

OCF_RESKEY_ns_default="vrouter"
OCF_RESKEY_conffile_default="/etc/ntp.conf"
OCF_RESKEY_pidfile_default="/var/run/ntpd.pid"
OCF_RESKEY_binpath_default="/usr/sbin/ntpd"
OCF_RESKEY_extraconf_default=""

: ${OCF_ROOT=${OCF_ROOT_default}}
: ${HA_LOGTAG="ocf-ns_ntp"}
: ${HA_LOGFACILITY="daemon"}

: ${OCF_RESKEY_ns=${OCF_RESKEY_ns_default}}
: ${OCF_RESKEY_conffile=${OCF_RESKEY_conffile_default}}
: ${OCF_RESKEY_pidfile=${OCF_RESKEY_pidfile_default}}
: ${OCF_RESKEY_binpath=${OCF_RESKEY_binpath_default}}
: ${OCF_RESKEY_extraconf=${OCF_RESKEY_extraconf_default}}

: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/resource.d/heartbeat}
. ${OCF_FUNCTIONS_DIR}/.ocf-shellfuncs
: ${OCF_FUEL_FUNCTIONS_DIR=${OCF_ROOT}/resource.d/fuel}
. ${OCF_FUEL_FUNCTIONS_DIR}/ocf-fuel-funcs

USAGE="Usage: $0 {start|stop|restart|status|monitor|validate-all|meta-data}";

if [ -n "${OCF_RESKEY_ns}" ]; then
  RUN="ip netns exec ${OCF_RESKEY_ns} "
else
  RUN=''
fi

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

usage()
{
  echo $USAGE >&2
}

meta_data()
{
cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="ntp">
<version>1.0</version>
<longdesc lang="en">
This script manages ntp daemon with namespace support
</longdesc>
<shortdesc lang="en">Manages an ntp daemon inside a namespace</shortdesc>

<parameters>

<parameter name="ns">
<longdesc lang="en">
Name of network namespace.
Should be present.
</longdesc>
<shortdesc lang="en">Name of network namespace.</shortdesc>
<content type="string" default="${OCF_RESKEY_ns_default}"/>
</parameter>

<parameter name="conffile">
<longdesc lang="en">
The ntp daemon configuration file name with full path.
For example, "/etc/ntp/ntp.cfg"
</longdesc>
<shortdesc lang="en">Configuration file name with full path</shortdesc>
<content type="string" default="${OCF_RESKEY_conffile_default}" />
</parameter>

<parameter name="pidfile">
<longdesc lang="en">
The ntp pid file path.
For example, "/var/run/ntp.pid"
</longdesc>
<shortdesc lang="en">Full path to the ntp pid file</shortdesc>
<content type="string" default="${OCF_RESKEY_pidfile_default}"/>
</parameter>

<parameter name="binpath">
<longdesc lang="en">
The ntp binary path.
For example, "/usr/sbin/ntp"
</longdesc>
<shortdesc lang="en">Full path to the ntp binary</shortdesc>
<content type="string" default="${OCF_RESKEY_binpath_default}"/>
</parameter>

<parameter name="extraconf">
<longdesc lang="en">
Extra command line arguments to pass to ntp.
For example, "-f /etc/ntp/shared.cfg"
</longdesc>
<shortdesc lang="en">Extra command line arguments for ntp</shortdesc>
<content type="string" default="${OCF_RESKEY_extraconf_default}" />
</parameter>

</parameters>

<actions>
<action name="start" timeout="20s"/>
<action name="stop" timeout="20s"/>
<action name="monitor" depth="0" timeout="20s" interval="60s" />
<action name="validate-all" timeout="20s"/>
<action name="meta-data"  timeout="5s"/>
</actions>
</resource-agent>
END
exit $OCF_SUCCESS
}

get_variables() {
  CONF_FILE="${OCF_RESKEY_conffile}"
  COMMAND="${RUN} ${OCF_RESKEY_binpath}"
  PIDFILE="${OCF_RESKEY_pidfile}"
}

ntp_status() {
  get_variables
  if [ -n "${PIDFILE}" -a -f "${PIDFILE}" ]; then
    # ntp is probably running
    # get pid from pidfile
    PID="`cat ${PIDFILE}`"
    if [ -n "${PID}" ]; then
      # check if process exists
      if ps -p "${PID}" | grep -q ntp; then
        if [ -n "${OCF_RESKEY_ns}" ]; then
          NS=`ip netns identify "${PID}"`
          if [ "${NS}" != "${OCF_RESKEY_ns}" ]; then
            # ntp is running with the correct pid
            # but not in the right network namespace
            ocf_log info "ntp daemon is running in a wrong namespace."
            return "${OCF_ERR_GENERIC}"
          fi
        fi
        ocf_log info "ntp daemon running"
        return "${OCF_SUCCESS}"
      else
        ocf_log info "ntp daemon is not running but pid file exists"
        return "${OCF_NOT_RUNNING}"
      fi
    else
      ocf_log err "PID file empty!"
      return "${OCF_ERR_GENERIC}"
    fi
  fi
  # ntp is not running
  ocf_log info "ntp daemon is not running"
  return "${OCF_NOT_RUNNING}"
}

ntp_start()
{
  get_variables
  # if ntp is running return success
  ntp_status
  rc="${?}"
  if [ "${rc}" -eq "${OCF_SUCCESS}" ]; then
    return "${OCF_SUCCESS}"
  elif [ "${rc}" -ne "${OCF_NOT_RUNNING}" ]; then
    ocf_log err "Error. Unknown status."
    return "${OCF_ERR_GENERIC}"
  fi

  # run the ntp binary
  ocf_run_as_root ${COMMAND} ${OCF_RESKEY_extraconf} -u ntp:ntp -p "${PIDFILE}" -4 -g -c "${CONF_FILE}"
  if [ "${?}" -ne "0" ]; then
    ocf_log err "Error. ntp daemon returned error $?."
    return "${OCF_ERR_GENERIC}"
  fi

  ocf_log info "Started ntp daemon."
  return "${OCF_SUCCESS}"
}

ntp_stop()
{
    local rc
    local LH="${LL} haproxy_stop():"
    local shutdown_timeout=15
    if [ -n "$OCF_RESKEY_CRM_meta_timeout" ]; then
        shutdown_timeout=$(( ($OCF_RESKEY_CRM_meta_timeout/1000) ))
    fi
    get_variables
    ntp_status
    rc="${?}"
    if [ "${rc}" -eq "${OCF_NOT_RUNNING}" ]; then
        ocf_log info "${LH} ntp already stopped."
        return "${OCF_SUCCESS}"
    fi

    proc_stop "${PIDFILE}" "${OCF_RESKEY_binpath}" $shutdown_timeout
    return "${?}"
}

ntp_monitor()
{
  ntp_status
}

ntp_validate_all()
{
  get_variables
  if [ -n "${OCF_RESKEY_binpath}" -a ! -x "${OCF_RESKEY_binpath}" ]; then
    ocf_log err "Binary path $OCF_RESKEY_binpath does not exist."
    return "${OCF_ERR_ARGS}"
  fi
  if [ -n "${OCF_RESKEY_conffile}" -a ! -f "${OCF_RESKEY_conffile}" ]; then
    ocf_log err "Config file $OCF_RESKEY_conffile does not exist."
    return "${OCF_ERR_ARGS}"
  fi

  if  grep -v '^#' "${CONF_FILE}" | grep 'pidfile' > /dev/null ; then
    :
  else
    ocf_log err "Error. 'pidfile' entry required in the ntp config file by ntp OCF RA."
    return "${OCF_ERR_GENERIC}"
  fi

  return "${OCF_SUCCESS}"
}

ntp_restart()
{
  ntp_stop
  ntp_start
}

#
# Main
#

if [ $# -ne 1 ]; then
  usage
  exit "${OCF_ERR_ARGS}"
fi

export LL="${OCF_RESOURCE_INSTANCE}:"
case $1 in
  start) ntp_start
  ;;

  stop) ntp_stop
  ;;

  restart) ntp_restart
  ;;

  status)  ntp_status
  ;;

  monitor) ntp_monitor
  ;;

  validate-all) ntp_validate_all
  ;;

  meta-data) meta_data
  ;;

  usage) usage; exit "${OCF_SUCCESS}"
  ;;

  *) usage; exit "${OCF_ERR_UNIMPLEMENTED}"
  ;;
esac
