From 771b49a128100a986ee6508c998f296162f7c197 Mon Sep 17 00:00:00 2001 From: Oyvind Albrigtsen Date: Wed, 29 Nov 2017 15:09:06 +0100 Subject: [PATCH] azure-lb: new resource agent --- doc/man/Makefile.am | 1 + heartbeat/Makefile.am | 1 + heartbeat/azure-lb | 213 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 215 insertions(+) create mode 100755 heartbeat/azure-lb diff --git a/doc/man/Makefile.am b/doc/man/Makefile.am index 03cdc8867..e3abdb5b7 100644 --- a/doc/man/Makefile.am +++ b/doc/man/Makefile.am @@ -100,6 +100,7 @@ man_MANS = ocf_heartbeat_AoEtarget.7 \ ocf_heartbeat_aws-vpc-move-ip.7 \ ocf_heartbeat_awseip.7 \ ocf_heartbeat_awsvip.7 \ + ocf_heartbeat_azure-lb.7 \ ocf_heartbeat_clvm.7 \ ocf_heartbeat_conntrackd.7 \ ocf_heartbeat_db2.7 \ diff --git a/heartbeat/Makefile.am b/heartbeat/Makefile.am index 1fde5e905..1e441b9c1 100644 --- a/heartbeat/Makefile.am +++ b/heartbeat/Makefile.am @@ -97,6 +97,7 @@ ocf_SCRIPTS = ClusterMon \ aws-vpc-move-ip \ awseip \ awsvip \ + azure-lb \ nginx \ AudibleAlarm \ clvm \ diff --git a/heartbeat/azure-lb b/heartbeat/azure-lb new file mode 100755 index 000000000..f41e83c6d --- /dev/null +++ b/heartbeat/azure-lb @@ -0,0 +1,213 @@ +#!/bin/sh +# + +# License: GNU General Public License (GPL) +# (c) 2017 O. Albrigtsen +# and Linux-HA contributors +# +# ----------------------------------------------------------------------------- +# O C F R E S O U R C E S C R I P T S P E C I F I C A T I O N +# ----------------------------------------------------------------------------- +# +# NAME +# azure-lb : OCF resource agent script for Azure Load Balancer +# +# Initialization: +: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat} +. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs + +# Defaults +OCF_RESKEY_nc_default="/usr/bin/nc" +OCF_RESKEY_port_default="61000" + +: ${OCF_RESKEY_nc=${OCF_RESKEY_nc_default}} +: ${OCF_RESKEY_port=${OCF_RESKEY_port_default}} + +process="$OCF_RESOURCE_INSTANCE" +pidfile="/var/run/$OCF_RESOURCE_INSTANCE.pid" + + +lb_usage() { + cat < + + +1.0 + +Resource agent to answer Azure Load Balancer health probe requests + +Answers Azure Load Balancer health probe requests + + + + + +The full name of the nc binary. + +Full path name of the nc binary + + + + + +Port to listen to. + +Listen to port + + + + + + + + + + + + + +END +exit 0 +} + +getpid() { + grep -o '[0-9]*' $1 +} + +lb_monitor() { + if test -f "$pidfile"; then + if pid=`getpid $pidfile` && [ "$pid" ] && kill -s 0 $pid; then + return $OCF_SUCCESS + else + # pidfile w/o process means the process died + return $OCF_ERR_GENERIC + fi + else + return $OCF_NOT_RUNNING + fi +} + +lb_start() { + cmd="$OCF_RESKEY_nc -l -k $OCF_RESKEY_port" + if ! lb_monitor; then + ocf_log debug "Starting $process: $cmd" + # Execute the command as created above + eval "$cmd & echo \$!" > $pidfile + if lb_monitor; then + ocf_log debug "$process: $cmd started successfully, calling monitor" + lb_monitor + return $? + else + ocf_log err "$process: $cmd could not be started" + return $OCF_ERR_GENERIC + fi + else + # If already running, consider start successful + ocf_log debug "$process: $cmd is already running" + return $OCF_SUCCESS + fi +} + +lb_stop() { + local rc=$OCF_SUCCESS + + if [ -n "$OCF_RESKEY_CRM_meta_timeout" ]; then + # Allow 2/3 of the action timeout for the orderly shutdown + # (The origin unit is ms, hence the conversion) + stop_timeout=$((OCF_RESKEY_CRM_meta_timeout/1500)) + else + stop_timeout=10 + fi + + if lb_monitor; then + pid=`getpid $pidfile` + kill $pid + + i=0 + while [ $i -lt $stop_timeout ]; do + if ! lb_monitor; then + rm -f $pidfile + return $OCF_SUCCESS + fi + sleep 1 + i=$((i+1)) + done + + ocf_log warn "Stop with SIGTERM failed/timed out, now sending SIGKILL." + kill -s 9 $pid + while :; do + if ! lb_monitor; then + ocf_log warn "SIGKILL did the job." + rc=$OCF_SUCCESS + break + fi + ocf_log info "The job still hasn't stopped yet. Waiting..." + sleep 1 + done + fi + rm -f $pidfile + return $rc +} + +lb_validate() { + check_binary "$OCF_RESKEY_nc" + + if ! ocf_is_decimal "$OCF_RESKEY_port"; then + ocf_exit_reason "$OCF_RESKEY_port is not a valid port" + exit $OCF_ERR_CONFIGURED + fi + + return $OCF_SUCCESS +} + +############################################################################### +# +# MAIN +# +############################################################################### + +case $__OCF_ACTION in + meta-data) + lb_metadata + exit $OCF_SUCCESS + ;; + usage|help) + lb_usage + exit $OCF_SUCCESS + ;; +esac + +if ! ocf_is_root; then + ocf_log err "You must be root for $__OCF_ACTION operation." + exit $OCF_ERR_PERM +fi + +case $__OCF_ACTION in + start) + lb_validate + lb_start;; + stop) + lb_stop;; + monitor) + lb_monitor;; + validate-all) + lb_validate;; + *) + echo $USAGE + exit $OCF_ERR_UNIMPLEMENTED + ;; +esac + +exit $?