svc-pkg-depot revision 2852
849N/A#!/usr/bin/ksh -p
661N/A#
661N/A# CDDL HEADER START
661N/A#
661N/A# The contents of this file are subject to the terms of the
661N/A# Common Development and Distribution License (the "License").
661N/A# You may not use this file except in compliance with the License.
661N/A#
661N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
661N/A# or http://www.opensolaris.org/os/licensing.
661N/A# See the License for the specific language governing permissions
661N/A# and limitations under the License.
661N/A#
661N/A# When distributing Covered Code, include this CDDL HEADER in each
661N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
661N/A# If applicable, add the following below this CDDL HEADER, with the
661N/A# fields enclosed by brackets "[]" replaced with your own identifying
661N/A# information: Portions Copyright [yyyy] [name of copyright owner]
661N/A#
661N/A# CDDL HEADER END
661N/A#
2852N/A#
2852N/A# Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
1902N/A#
661N/A
661N/A. /lib/svc/share/smf_include.sh
661N/A
2852N/AAPACHE_HOME=/usr/apache2/2.2
2852N/AAPACHE_ETC_ROOT=/etc/pkg/depot
2852N/AAPACHE_BIN=${APACHE_HOME}/bin/64
661N/A
2852N/A#
2852N/A# Calling $APACHE_BIN/apachectl would source $APACHE_BIN/envvars, which
2852N/A# in turn would source /etc/apache2/2.2/envvars, if it exists.
2852N/A#
2852N/A# We want to keep the depot service completely separate from
2852N/A# the site system configuration used by svc:/network/http:apache22
2852N/A# avoiding /etc/apache2, which means we must avoid apachectl.
2852N/A#
2852N/A# In this method script, we call httpd directly instead, but as a
2852N/A# result, we also need to include the relevant contents of
2852N/A# $APACHE_BIN/envvars - setting $LD_LIBRARY_PATH as it does.
2852N/A#
2852N/AHTTPD=${APACHE_BIN}/httpd.worker
2852N/ALD_LIBRARY_PATH=${APACHE_HOME}/lib/64:${LD_LIBRARY_PATH}
2852N/Aexport LD_LIBRARY_PATH
2852N/A
2852N/A
2852N/Afunction check_prop {
2852N/A if [ -z "$1" ]; then
2852N/A echo "ERROR: SMF property for $2 was empty"
2852N/A exit $SMF_EXIT_ERR_FATAL
2852N/A fi
2852N/A}
2852N/A
2852N/Afunction get_pair {
2852N/A NAME=$1
2852N/A shift 2
2852N/A echo "${NAME}=\"$@\""
2852N/A echo "export ${NAME}"
2852N/A}
661N/A
2852N/A# A function to pull in the config properties from the FMRI given
2852N/A# as the first argument. This allows us to make a single call
2852N/A# to svcprop, rather than calling svcprop for each value we
2852N/A# want to retrieve.
2852N/Afunction get_smf_props {
2852N/A
2852N/A IFS="
2852N/A"
2852N/A SMF_PROPS="$(svcprop -t -p config $1 |\
2852N/A sed -e 's#config/#depot_#g' -e 's/$/,/g')"
2852N/A IFS=,
2852N/A for line in $SMF_PROPS ; do
2852N/A IFS='
2852N/A'
2852N/A eval $(get_pair $line)
2852N/A done
2852N/A}
2852N/A
1518N/A
2852N/Afunction check_failure {
2852N/A RESULT=$1
2852N/A MESSAGE=$2
2852N/A NON_FATAL=$3
2852N/A if [ $RESULT -ne 0 ]; then
2852N/A echo $MESSAGE
2852N/A if [ -n "$NON_FATAL" ]; then
2852N/A return
2852N/A fi
2852N/A exit $SMF_EXIT_ERR_FATAL
1518N/A fi
2852N/A}
1518N/A
2852N/Afunction check_apache_failure {
2852N/A RESULT=$1
2852N/A MESSAGE=$2
2852N/A if [ $RESULT -ne 0 ]; then
2852N/A echo $MESSAGE
2852N/A kill_apache
1527N/A exit $SMF_EXIT_ERR_FATAL
1527N/A fi
2852N/A}
1518N/A
2852N/Afunction abspath {
2852N/A # print a normalized version of a path, needed because
2852N/A # many utilities will not dereference non-existent directories
2852N/A # in path names, e.g. cat /etc/no-such-dir/../motd will fail.
2852N/A echo $1 | /usr/bin/python -c \
2852N/A 'import os.path ; import sys ; print os.path.normpath(sys.stdin.read())'
2852N/A}
2852N/A
2852N/Afunction run_depot {
1976N/A
2852N/A /usr/lib/pkg.depot-config \
2852N/A -S \
2852N/A -c ${depot_cache_dir} \
2852N/A -h ${depot_host} \
2852N/A -l ${depot_log_dir} \
2852N/A -p ${depot_port} \
2852N/A -r ${depot_runtime_dir} \
2852N/A -s ${depot_cache_max} \
2852N/A -T ${depot_template_dir} \
2852N/A -t apache2 \
2852N/A ${depot_allow_refresh}
2852N/A failure=$?
2852N/A if [ $? -ne 0 ] ; then
2852N/A # make sure we leave nothing behind
2852N/A kill_apache
2852N/A kill_htcacheclean
2852N/A check_failure $failure \
2852N/A "pkg.depot-config: failed to create Apache config"
2852N/A fi
2852N/A}
719N/A
2852N/Afunction run_htcacheclean {
2852N/A # if we dropped to maintenance and are in the process of
2852N/A # clearing that state, we may have htcacheclean processes
2852N/A # hanging around.
2852N/A kill_htcacheclean
2852N/A if [ "${depot_cache_max}" != "0" ] ; then
2852N/A # Start a cache cleaning daemon, scanning every 2 weeks,
2852N/A # being intelligent about only running if the cache has
2852N/A # changed, limiting the cache to ${depot_cache_max}
2852N/A # megabytes, being nice about scheduling and removing
2852N/A # empty directories if necessary.
2852N/A interval=$((60 * 24 * 14))
2852N/A /usr/bin/su pkg5srv \
2852N/A -c "/usr/apache2/2.2/bin/htcacheclean \
2852N/A -d${interval} -i -l ${depot_cache_max}M -n \
2852N/A -p ${depot_cache_dir} \
2852N/A -P ${depot_cache_dir}/../depot_htcacheclean.pid \
2852N/A -t"
2852N/A check_failure $? "htcacheclean failed to run cleanly"
2852N/A fi
2852N/A}
1976N/A
2852N/Afunction kill_htcacheclean {
2852N/A pid_file=$(abspath ${depot_cache_dir}/../depot_htcacheclean.pid)
2852N/A if [ -f $pid_file ]; then
2852N/A PID=$(< $pid_file)
2852N/A /usr/bin/kill -TERM $PID
2852N/A check_failure $? "failed to kill htcacheclean process\
2852N/A $PID" "not_fatal"
2852N/A fi
2852N/A}
775N/A
2852N/Afunction kill_apache {
2852N/A # We go to lengths to kill remaining httpd processes: if we kill
2852N/A # just the pid, then child httpd.worker processes become
2852N/A # zombies, hanging onto the server port, which causes problems
2852N/A # when trying to start a service that is transitioning from
2852N/A # maintenance.
2852N/A # This function should only be called when the service is
2852N/A # transitioning to maintenance: normal Apache shutdown is
2852N/A # preferable.
2852N/A pid_file=$(abspath ${depot_runtime_dir}/../depot_httpd.pid)
2852N/A if [ -f $pid_file ]; then
2852N/A PID=$(< $pid_file)
2852N/A /usr/bin/ptree $PID | /usr/bin/awk '{print $1}' | \
2852N/A /usr/bin/xargs /usr/bin/kill -TERM
2852N/A check_failure $? "failed to kill apache process $PID" \
2852N/A "not_fatal"
2852N/A /usr/bin/rm $pid_file
775N/A fi
2852N/A}
2852N/A
2852N/Afunction server_ping {
2852N/A # Ping the service, ensuring the index gets built if does
2852N/A # not exist.
2852N/A # Since curl --retry uses an exponential backoff algorithm, this
2852N/A # can result in us waiting 40 seconds, which ought to be long
2852N/A # enough for Apache to come online. (index refreshes are run in
2852N/A # the background on the server after it has returned a response)
2852N/A url="http://${depot_host}:${depot_port}"
2852N/A ipv6=$(echo ${depot_host} | /usr/bin/grep :)
2852N/A if [ -n "$ipv6" ] ; then
2852N/A url="http://\[${depot_host}\]:${depot_port}"
2852N/A fi
2852N/A /usr/bin/curl -s --max-time 5 --retry 4 -o /dev/null ${url} \
2852N/A2> /dev/null
2852N/A check_failure $? "Unable to access the server at ${url}. Check\
2852N/A the SMF service log or the error log at ${depot_log_dir}/error_log for\
2852N/A more information, if any."
2852N/A}
775N/A
2852N/Aget_smf_props $SMF_FMRI
2852N/Acheck_prop ${depot_host} config/host
2852N/Acheck_prop ${depot_port} config/port
2852N/Acheck_prop ${depot_log_dir} config/log_dir
2852N/Acheck_prop ${deport_template_dir} config/template_dir
2852N/Acheck_prop ${depot_runtime_dir} config/runtime_dir
2852N/Acheck_prop ${depot_cache_dir} config/cache_dir
2852N/Acheck_prop ${depot_cache_max} config/cache_max
2852N/Acheck_prop ${depot_allow_refresh} config/allow_refresh
2852N/Aif [ "${depot_allow_refresh}" == "true" ] ; then
2852N/A depot_allow_refresh="-A"
2852N/Aelse
2852N/A depot_allow_refresh=""
2852N/Afi
2852N/A
2852N/AFAILED_TO_RUN="Server failed to %s. Check the SMF service log or the\
2852N/A error log at ${depot_log_dir}/error_log for more information, if any."
2852N/A
2852N/Acase "$1" in
2852N/A"start")
2852N/A cmd="start"
2852N/A run_depot
2852N/A run_htcacheclean
2852N/A emsg=$(/usr/bin/printf ${FAILED_TO_RUN} start)
2852N/A ${HTTPD} -f ${depot_runtime_dir}/depot_httpd.conf \
2852N/A ${STARTUP_OPTIONS} -k ${cmd} 2>&1
2852N/A check_apache_failure $? $emsg
2852N/A server_ping
775N/A ;;
2852N/A"refresh")
2852N/A cmd="graceful"
2852N/A run_depot
2852N/A kill_htcacheclean
2852N/A run_htcacheclean
2852N/A emsg=$(/usr/bin/printf ${FAILED_TO_RUN} refresh)
2852N/A ${HTTPD} -f ${depot_runtime_dir}/depot_httpd.conf \
2852N/A ${STARTUP_OPTIONS} -k ${cmd} 2>&1
2852N/A check_apache_failure $? $emsg
2852N/A server_ping
2852N/A ;;
2852N/A"stop")
2852N/A cmd="stop"
2852N/A kill_htcacheclean
2852N/A emsg=$(/usr/bin/printf ${FAILED_TO_RUN} stop)
2852N/A ${HTTPD} -f ${depot_runtime_dir}/depot_httpd.conf \
2852N/A ${STARTUP_OPTIONS} -k ${cmd} 2>&1
2852N/A check_apache_failure $? $emsg
2852N/A ;;
661N/A*)
2852N/A echo "Usage: $0 {start|stop|refresh}"
661N/A exit $SMF_EXIT_ERR_CONFIG
661N/A ;;
2852N/Aesac
661N/A
661N/Aexit $SMF_EXIT_OK