#! /bin/ksh 
#
#	Copyright 11/20/96 Sun Microsystems, Inc.  All Rights Reserved.
#
#
#pragma ident "@(#)nshttp_svc_start.shi	1.1 97/06/12 SMI"
#
#ident "@(#)nshttp_svc_start.shi		1.5	96/11/20 SMI"
#
# nshttp_svc_start
#	Called by cltrans_start_svcs.
#
#	Start WEB server (httpd)
#
#	Exits non-zero if the service fails to start up or is
#	configured improperly.
#

# Include HTTP utilities library
#
#	Copyright 11/18/96 Sun Microsystems, Inc.  All Rights Reserved.
#
#
#pragma ident "@(#)ds_boiler	1.1 97/06/12 SMI"
#
#ident "@(#)ds_boiler		1.7	96/11/18 SMI"
#
# common boiler for HA Internet Pro 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
. ds_utilities

# add the ha-service specific clust_progs
expr "$prog_path" : '.*/clust_progs' >/dev/null 2>&1
if [ $? -eq 0 ]; then
	PATH=${prog_path}:${PATH}
else
	PATH=${prog_path}:${prog_path}/../clust_progs:${PATH}
fi

# add the ha-service specific fault_progs
expr "$prog_path" : '.*/fault_progs' >/dev/null 2>&1
if [ $? -eq 0 ]; then
    PATH=${prog_path}:${PATH}
else
    PATH=${prog_path}:${prog_path}/../fault_progs:${PATH}
fi

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

if [ $? -ne 0 ]; then
	logerr `gettext "Cannot obtain name of local host"`
	exit 1
fi
# compute hostnames of remote nodes
PHYS_HOSTS="`haget -f all_physical_hosts`"
for i in $PHYS_HOSTS; do
	if [ "$i" != "$LOCALHOST" ]; then
		REMOTEHOSTS="$REMOTEHOSTS $i"
	fi
done
#! /bin/sh 
#
#	Copyright 12/20/96 Sun Microsystems, Inc.  All Rights Reserved.
#
#
#pragma ident "@(#)do_service	1.8 01/06/08 SMI"
#
#ident "@(#)do_service		1.13	96/12/20 SMI"
#
#

ARG_MASTERED=$1
ARG_NOT_MASTERED=$2
SYSLOG_PREFIX="SUNWcluster.ha.nshttp"

# Replace comma with space to form an sh word list
MASTERED="`echo $ARG_MASTERED | tr ',' ' '`"
NOT_MASTERED="`echo $ARG_NOT_MASTERED | tr ',' ' '`"

source_env NSHTTP

if [ $? -ne 0 ]; then
	# source_env logs error message if it fails.
	# No need to log another; just exit.
	exit 1
fi

#
# Timeout to waiting for SIGTERM to stop a process
# This should be in the config file
#
STOP_TIMEOUT=15
#
# bundle_do_svc <action>
#
# is called for each instance
#
bundle_do_svc ()
{
	typeset method_timeout
	typeset wait_time

	action=$1

	HTTP_START=${_INST_BASE_DIR}/start
	HTTP_STOP=${_INST_BASE_DIR}/stop

	prefix="$SYSLOG_PREFIX.$action"

	case $action in

	'start')

		# First do some error checking.

		if [ ! -x $HTTP_START ]; then
			logerr "$prefix.4000" \
				`gettext "<$HTTP_START> is not executable."`
			exit 1
		fi

		# Note that we're using pmf to start/stop, but not to probe.

		grep "Security on" ${_INST_BASE_DIR}/config/magnus.conf
		if [ $? -eq 0 ] ;
		then
			if [ ! -f  ${_INST_BASE_DIR}/keyPass ] ; then
				logerr "$prefix.4011" \
					`gettext "Password File not found"`
				exit 1
			fi
			HTTP_START_PARAM=0
		else
			HTTP_START_PARAM=1
		fi


		if [ ${_INST_RETRY} = "n" ]; then

			if [ $HTTP_START_PARAM -eq 1 ]; then
				pmfadm -c ${_INST_NAME} $HTTP_START
			else
				pmfadm -c ${_INST_NAME} /bin/sh -c \
                                        "$HTTP_START < ${_INST_BASE_DIR}/keyPass "
			fi
				
		else
			if [ $HTTP_START_PARAM  -eq 1 ]; then
				pmfadm -c ${_INST_NAME}          \
				       -n ${_INST_RETRY_TIMES}    \
				       -t ${_INST_RETRY_INTERVAL} \
				       -a ${_INST_PROBE_CALLBACK_1} \
				       $HTTP_START
			else
				pmfadm -c ${_INST_NAME}          \
				       -n ${_INST_RETRY_TIMES}    \
				       -t ${_INST_RETRY_INTERVAL} \
				       -a ${_INST_PROBE_CALLBACK_1} \
					 /bin/sh -c \
					"$HTTP_START < ${_INST_BASE_DIR}/keyPass "
			fi
		fi

		if [ $? -ne 0 ]; then
			logerr "$prefix.4001" \
				`gettext "pmfadm failed to start HTTP instance ${_INST_NAME}"`
			exit 1
		else
			lognotice "$prefix.2000" \
				`gettext "Started HTTP instance ${_INST_NAME}"`

		fi
	;;

	'stop' | 'abort')

		# delete from queue, but don't kill
		pmfadm -s ${_INST_NAME}
		if [ $? -ne 0 ]; then
			logerr "$prefix.4002" \
				`gettext "pmfadm failed to delete ${_INST_NAME} from queue"`
			exit 1
		fi

		# use Netscape's stop script to stop nshttp instance
		if [ ! -x $HTTP_STOP ]; then
			logerr "$prefix.4003" `gettext "<$HTTP_STOP> is not executable"`
			exit 1
		fi
		
		method_timeout=`hareg -q nshttp -T stop`
		wait_time=`expr $method_timeout - 5`

		hatimerun -t $wait_time $HTTP_STOP

		ha_svc_not_running ${_INST_NAME}

		if [[ $? -ne 0 ]]; then
        	# Now kill any processes left out
        	pmfadm -s ${_INST_NAME} KILL
        	if [ $? -ne 0 ]; then
            	logerr "$prefix.4009" \
                	`gettext "pmfadm failed to kill ${_INST_NAME}'s process and its sub-processes (if any) : pmfadm returned $?"`
            	exit 1
        	fi
		fi

	;;

	'fm_start')

		# XXX
		need_to_run_probe ${_INST_LOGICAL_HOST} ${LOCALHOST}

		if [ $? -ne 0 ]; then
			exit 0
		fi

		# pmf starts nshttp_probe
		# nshttp_probe runs until nshttp_fm_stop kills it.
		# Don't start probe if diskset is in maintenance mode.

		# If this HTTP instance's diskset is in maint mode, exit now.
		MAINT=`haget -f is_maint -h ${_INST_LOGICAL_HOST}`
		if [ "$MAINT" = "1" ]; then
			exit 0
		fi

		pmfadm -c ${_INST_NAME}.probe ${_INST_PROBE_PROG_1} \
		    ${_INST_NAME}

		if [ $? -ne 0 ]; then
			logerr "$prefix.4005" \
	`gettext "pmfadm failed to start HTTP probe for instance ${_INST_NAME}"`
			exit 1
		else
			lognotice "$prefix.2002" \
		`gettext "Started HTTP probe instance ${_INST_NAME}.probe"`
		fi
	;;

	'fm_stop')

		# If probe not running, do nothing
		ha_svc_not_running ${_INST_NAME}.probe && exit 0

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

	'fm_check_this_host_ok')

		# If the HA-HTTP logical host for this instance
		# is not currently mastered by this machine, exit now.

		is_member "${_INST_LOGICAL_HOST}" "$MASTERED"
		if [ $? -ne 0 ]; then
			exit 0
		fi

		# Otherwise, probe Web service now.
		# If dead, request will time out in
		# ${_INST_PROBE_TIMEOUT_1} secs.

		HTTPPROBEFILE=/var/opt/SUNWcluster/run/${_INST_NAME}.probe.$$

		hatimerun -t ${_INST_PROBE_TIMEOUT_1} \
		    /usr/bin/telnet ${_INST_LOGICAL_HOST} \
		    ${_INST_PORT} <<EOF > $HTTPPROBEFILE 2>&1

EOF
		if [ $? -eq 99 ]; then
			# timeout
logerr "$prefix.4007" \
	`gettext "This server is supposed to be providing HTTP service for instance <${_INST_NAME}>, but request timed out"`
			exit 1
		fi
		grep refused $HTTPPROBEFILE > /dev/null 2>&1
		if [ $? -eq 0 ]; then
logerr "$prefix.4008" \
	`gettext "This server is supposed to be providing HTTP service for instance <${_INST_NAME}>, but isn't"`
			exit 1
		fi
	;;

	esac

	exit 0
}
#include_boiler

generic_svc start "$1" "$2" "$3"
