From 20318e6c04a1e911fd9a11dae0b06f7f96987c81 Mon Sep 17 00:00:00 2001 From: Kai Inkinen Date: Wed, 5 May 2010 15:13:48 +0300 Subject: [PATCH 1/2] [Debian install] Make sure we run as root and verify that the http port is not already in use * Winstone server does not exit with error, even if the port is already reserved, at least when in daemon mode. Do this check beforehand and exit if port is in use --- debian/debian/hudson.init | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/debian/debian/hudson.init b/debian/debian/hudson.init index 1cd18d2fe8..218921b1f3 100644 --- a/debian/debian/hudson.init +++ b/debian/debian/hudson.init @@ -49,6 +49,34 @@ fi # Depend on lsb-base (>= 3.0-6) to ensure that this file is present. . /lib/lsb/init-functions +# Make sure we run as root, since setting the max open files through +# ulimit requires root access +if [ `id -u` -ne 0 ]; then + echo "The $NAME init script can only be run as root" + exit 1 +fi + + +check_tcp_port() { + local service=$1 + local assigned=$2 + local default=$3 + + if [ -n "$assigned" ]; then + port=$default + else + port=$assigned + fi + + count=`netstat --listen --numeric-ports | grep \:$port | grep -c . ` + + if [ $count -ne 0 ]; then + echo "The selected $service port ($port) seems to be in use by another program " + echo "Please select another port to use for $NAME" + return 1 + fi +} + # # Function that starts the daemon/service # @@ -63,11 +91,15 @@ do_start() # 2 if daemon could not be started $DAEMON $DAEMON_ARGS --running && return 1 + # Verify that the hudson port is not already in use, winstone does not exit + # even for BindException + check_tcp_port "http" "$HTTP_PORT" "8080" || return 1 + # If the var MAXOPENFILES is enabled in /etc/default/hudson then set the max open files to the # proper value if [ ! -z "$MAXOPENFILES" ]; then - echo Setting up max open files limit to $MAXOPENFILES - ulimit -n $MAXOPENFILES + [ "$VERBOSE" != no ] && echo Setting up max open files limit to $MAXOPENFILES + ulimit -n $MAXOPENFILES fi # --user in daemon doesn't prepare environment variables like HOME, USER, LOGNAME or USERNAME, From b52d73f139a5fe8cd57dec42d8664a72570c9406 Mon Sep 17 00:00:00 2001 From: Kai Inkinen Date: Wed, 5 May 2010 15:17:55 +0300 Subject: [PATCH 2/2] [Debian install] Make sure that all hudson processes are stopped after the init script stop * Using the hudson-cli.jar client causes hudson to spawn new processes (e.g. restart), which might lead to stop/restart failing in the init script --- debian/debian/hudson.init | 61 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 3 deletions(-) diff --git a/debian/debian/hudson.init b/debian/debian/hudson.init index 218921b1f3..d02b4d6909 100644 --- a/debian/debian/hudson.init +++ b/debian/debian/hudson.init @@ -107,6 +107,31 @@ do_start() $SU $HUDSON_USER --shell=/bin/bash -c "$DAEMON $DAEMON_ARGS -- $JAVA $JAVA_ARGS -jar $HUDSON_WAR $HUDSON_ARGS" || return 2 } + +# +# Verify that all hudson processes have been shutdown +# and if not, then do killall for them +# +get_running() +{ + return `ps -U $HUDSON_USER --no-headers -f | egrep -e '(java|daemon)' | grep -c . ` +} + +force_stop() +{ + get_running + if [ $? -ne 0 ]; then + killall -u $HUDSON_USER java daemon || return 3 + fi +} + +# Get the status of the daemon process +get_daemon_status() +{ + $DAEMON $DAEMON_ARGS --running || return 1 +} + + # # Function that stops the daemon/service # @@ -117,8 +142,16 @@ do_stop() # 1 if daemon was already stopped # 2 if daemon could not be stopped # other if a failure occurred - $DAEMON $DAEMON_ARGS --running || return 1 - $DAEMON $DAEMON_ARGS --stop || return 2 + get_daemon_status + case "$?" in + 0) + $DAEMON $DAEMON_ARGS --stop || return 2 + ;; + *) + force_stop || return 3 + ;; + esac + # Many daemons don't delete their pidfiles when they exit. rm -f $PIDFILE return 0 @@ -163,8 +196,30 @@ case "$1" in ;; esac ;; + status) + get_daemon_status + case "$?" in + 0) echo "Hudson is running with the pid `cat $PIDFILE`";; + *) + get_running + procs=$? + if [ $procs -eq 0 ]; then + echo -n "Hudson is not running" + if [ -f $PIDFILE ]; then + echo ", but the pidfile ($PIDFILE) still exists" + else + echo + fi + + else + echo "$procs instances of hudson are running at the moment" + echo "but the pidfile $PIDFILE is missing" + fi + ;; + esac + ;; *) - echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2 + echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2 exit 3 ;; esac