Wednesday, September 17, 2008

Hudson on Winstone + Apache

I decided to make a quick post about hudson. I've been using it for work for a few weeks now and after the initial setup it's required almost no maintenance. I also like how easier it is to extend and setup.

Anyway, just thought I'd include some screenshots of installing plugins for hudson. It's as simple as clicking through the web interface and then restarting the server.



And here's downloading a plugin:


War Games?

Hudson has the neat ability to run only using the war. It makes use of a servlet container called "winstone". This was my first look into winstone, but one of the nice features (that tomcat also has) is AJP support. Let's look at how we can install hudson as a service (I'm using CentOS 5.2 for this).

First, create a hudson user and group. Next create a /var/hudson directory. Make sure hudson owns this directory. Place the hudson.war into it.

Here's an init script I modified from the comments on Hudson's install instructions:


# chkconfig: 2345 84 16
# description: Hudson CI server

# Source function library.
. /etc/rc.d/init.d/functions
[ -z "$JAVA_HOME" -a -x /etc/profile.d/java.sh ] && . /etc/profile.d/java.sh

USER=hudson
HUDSON_HOME=/var/hudson
WAR="$HUDSON_HOME/hudson.war"
LOG="/var/log/hudson.log"
LOCK="/var/lock/subsys/hudson"
export HUDSON_HOME

RETVAL=0

pid_of_hudson() {
ps auxwww | grep java | grep hudson | grep -v grep | awk '{print $2}'
}

start() {
[ -e "$LOG" ] && cnt=`wc -l "$LOG" | awk '{ print $1 }'` || cnt=1

echo -n $"Starting hudson: "

echo "" > "$LOG"
chown $USER $LOG
cd "$HUDSON_HOME"

CMD="nohup ${JAVA_HOME}/bin/java -DHUDSON_HOME=${HUDSON_HOME} -jar ${WAR} --httpPort=8080 --ajp13Port=8010 --prefix=/hudson >> ${LOG} 2>&1 &"
su -m --command="${CMD}" hudson
while { pid_of_hudson > /dev/null ; } &&
! { cat "$LOG" | grep -q 'Winstone Servlet Engine .* running' ; } ; do
sleep 1
done

pid_of_hudson > /dev/null
RETVAL=$?
[ $RETVAL = 0 ] && success $"$STRING" || failure $"$STRING"
echo

[ $RETVAL = 0 ] && touch "$LOCK"
}

stop() {
echo -n "Stopping hudson: "

pid=`pid_of_hudson`
[ -n "$pid" ] && kill $pid
RETVAL=$?
cnt=10 while [ $RETVAL = 0 -a $cnt -gt 0 ] &&
{ pid_of_hudson > /dev/null ; } ; do
sleep 1
((cnt--))
done

[ $RETVAL = 0 ] && rm -f "$LOCK"
[ $RETVAL = 0 ] && success $"$STRING" || failure $"$STRING"
echo
}

status() {
pid=`pid_of_hudson`
if [ -n "$pid" ]; then
echo "hudson (pid $pid) is running..."
return 0
fi
if [ -f "$LOCK" ]; then
echo $"${base} dead but subsys locked"
return 2
fi
echo "hudson is stopped"
return 3
}

# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|restart|status}"
exit 1
esac

exit $RETVAL



After creating the file, make sure you install it with
chkconfig --add hudson


Finally, if we only want to open port 80, it's easy to configure apache to forward to winstone using mod_ajp. Here's my apache configuration:
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
ProxyPass /hudson/ ajp://localhost:8010/hudson/


Now just head to http://localhost/hudson and you should see hudson.

No comments: