#!/bin/ksh
#
# pragma ident  "@(#)apache_svc_abort.shi 1.3     00/05/25 SMI"
#
# Copyright (c) 1996-1997 by Sun Microsystems, Inc.
# All rights reserved.
#

# Usage: apache_svc_abort
#

#
#pragma ident "@(#)ds_boiler	1.3	98/09/15 SMI"
#
# common boiler for HA data services
#
#


ARGV0=`basename $0`
LOGGER=logger
HA_SLOGFACILITY=`haget -f syslog_facility`
HA_SLOGTAG=hadf
prog_path=`dirname $0`

# source in ha-services common utilities
. hads_utilities

# add the ha-service directory to the path
PATH=${prog_path}:${PATH}

#
# for use by subsequent hactl command, get hostnames of local and remote hosts
#
LOCALHOST=`uname -n`

#!/bin/ksh
#
#	Copyright 06/24/99 Sun Microsystems, Inc.  All Rights Reserved.
#
# pragma ident  "@(#)do_service 1.19     00/06/19 SMI"
#
# do_service 
# Standard-name file that defines the standard-name routine bundle_do_svc(), 
# which implements the HA framework-defined methods for this data service.
# The _boiler file in this directory sources this file when the data
# service method scripts execute.


SYSLOG_PREFIX="SUNWcluster.ha.apache"

# Read in and interpret the data service environment configuration file
source_env_file /etc/opt/SUNWscapc/hadsconf || exit 1
STOP_PROBE_TIMEOUT=15

#
# bundle_do_svc <action>
#
# called for each instance
#
bundle_do_svc ()
{

	action=$1
	prefix="$SYSLOG_PREFIX.$action"

	LOGICAL_HOST=`get_config_param ${_INST_NAME} LOGICAL_HOST`
	BIN_DIR=`get_config_param ${_INST_NAME} PRIV_BIN_DIR`
	CONF_DIR=`get_config_param ${_INST_NAME} PRIV_CONF_DIR`
	APACHE_SERVER=$BIN_DIR/httpd
	CONF_FILE=$CONF_DIR/httpd.conf

	case $action in
	'start')
		# launch the daemon using the process monitor
		ha_svc_not_running ${_INST_NAME}
		if [ $? -eq 0 ]; then
			#make sure the httpd is present and executable
			if [ ! -x $APACHE_SERVER ]; then
				logerr "$prefix.4000" `gettext "$APACHE_SERVER is not executable for instance ${_INST_NAME}"`
				exit 1
			fi
			
			#check to see if the httpd.conf file is there
			if [ ! -f $CONF_FILE ]; then
				logerr "$prefix.4099" `gettext "$CONF_FILE could not be found for instance ${_INST_NAME}"`
				exit 1
			fi

			# check whether the conf file looks ok to apache
			# the webserver doesnt actually start here,
			# the -T option only tests the syntax of the config file
			# -f specifies the config file to be examined
			$APACHE_SERVER -f $CONF_FILE -T >/dev/null 2>&1
			if [ $? -ne 0 ]; then
				logerr "$prefix.4098" `gettext "Apache web server found errors in $CONF_FILE for instance ${_INST_NAME}"`
				exit 1
			fi

			# the -f option instructs the web server to use the
			# specified config file rather than the compiled
			# in default
			pmfadm -c ${_INST_NAME} $APACHE_SERVER -f $CONF_FILE
			if [ $? -ne 0 ]; then
				logerr "$prefix.4001" `gettext "Failed to start Apache web server instance ${_INST_NAME}"`
				exit 1
			else
				lognotice "$prefix.2000" `gettext "Started Apache web server instance ${_INST_NAME}"`
			fi
		fi
		;;

	'start_net')
		ha_svc_not_running ${_INST_NAME}
		if [ $? -eq 0 ]; then
			logerr "$prefix.4002" `gettext "start_net: instance ${_INST_NAME} is down"`
			exit 1
		fi
		;;

	'stop' )
		ha_svc_not_running ${_INST_NAME}
		if [ $? -ne 0 ]; then
			method_timeout=`hareg -q apache -T stop`
			if [ -z "$method_timeout" ]; then
				method_timeout=60
				lognotice "$prefix.2001" `gettext "Instance ${_INST_NAME} failed to obtain stop timeout value. Using a default of $method_timeout"`
			fi

			#calculate the time left for us to do a clean stop
			#save 5 seconds in case a KILL is needed
			#SECONDS tells us how much time has elapsed since
			#this script started (its a ksh variable)
			method_timeout=`expr $method_timeout - 5 - $SECONDS`

			if [ $method_timeout -gt 0 ] ; then
				#attempt an orderly shutdown
				pmfadm -s ${_INST_NAME} -w $method_timeout TERM
			fi

			# Now kill any process which still remains
			ha_svc_not_running ${_INST_NAME}
			if [ $? -ne 0 ]; then
				pmfadm -k ${_INST_NAME}
				if [ $? -ne 0 ]; then
					logerr "$prefix.4005" \
					`gettext "pmfadm failed to kill ${_INST_NAME}'s process"`
					exit 1
				fi
			else
				lognotice "$prefix.2002" \
				`gettext "Stopped APACHE instance ${_INST_NAME}"`
			fi
		fi
		;;

	'abort' | 'abort_net')
		#do nothing if the instance is not running
                ha_svc_not_running ${_INST_NAME} && exit 0

		# kill everything, and do it fast
		pmfadm -s ${_INST_NAME} KILL
		if [ $? -ne 0 ]; then
			logerr "$prefix.4006" `gettext "Failed to abort instance ${_INST_NAME}"`
			exit 1
		fi
		;;

	'fm_start')
		#do nothing if the probe is already running
		ha_svc_not_running ${_INST_NAME}.probe || exit 0
		
		maint=`haget -f is_maint -h $LOGICAL_HOST`
		if [ $? -ne 0 ]; then
			logerr "$prefix.4007" `gettext "haget(1M) failed for logical host $LOGICAL_HOST"`
			exit 1
		elif  [ $maint -eq 0 ]; then
			if is_member "$LOGICAL_HOST" "$MASTERED_LOGICAL_HOSTS" ; then
				local=yes
			else
				local=no
			fi
			
			# Launch a probe using the process monitor.
			pmfadm -c ${_INST_NAME}.probe /bin/sh -c "${_INST_PROBE_PROG_1} ${_INST_NAME} $local >/dev/null 2>&1"
			if [ $? -ne 0 ]; then
				logerr "$prefix.4008" `gettext "failed to start probe ${_INST_PROBE_PROG_1} for instance ${_INST_NAME}"`
				exit 1
			fi
		fi
		;;

	'fm_stop')
		# Kill the probe that is associated with this instance,
		# but only if it running
                ha_svc_not_running ${_INST_NAME}.probe && exit 0

                # PMF kills apache_probe
		pmfadm -s ${_INST_NAME}.probe -w ${STOP_PROBE_TIMEOUT} TERM || pmfadm -s ${_INST_NAME}.probe KILL
		if [ $? -ne 0 ]; then
                        logerr "$prefix.4009" \
    `gettext "pmfadm failed to stop ${_INST_NAME}.probe"`
                        exit 1
                else
                        lognotice "$prefix.2003" \
                        `gettext "Stopped probe for instance ${_INST_NAME}"`
                fi
		;;

	esac

	exit 0
}

#include_boiler

### Main program ###

# Pass args explicitly to preserve null ones
# the return code from generic_svc will be the method exit status
generic_svc abort "$1" "$2" "$3"

