donderdag 24 januari 2013

Starting Hudson automatically on Mac OSX.

With yesterdays release of Hudson 3.0.0 by the Eclipse Foundation a step forward was taken again in creating an open source toolchain under proper governance. Hudson is an extensible continuous integration platform allowing you to build, inspect and test code whenever you commit changes to your repository. The new release brings a reduction in footprint of 50% so you can also set it up to run on your local machine. After downloading and installing Hudson following the instructions on www.eclipse.org/hudson you that it is easy to start from the terminal using
> java -jar hudson.war

First try: launchd script

But this quickly becomes tiresome, so there must be a better way. On Mac OSX this is launchd, the launch service. Another important thing is that I don't want all the builds to clutter up my home directory so I want to have the HUDSON_HOME direct to somewhere else. Create a directory /usr/local/hudson and move hudson.war into it.
> sudo mkdir /usr/local/hudson
> mv hudson-3.0.0.war /usr/local/hudson/
> cd /usr/local/hudson/
> sudo chgrp admin hudson-3.0.0.war
> ln -s hudson-3.0.0.war hudson.war
I then created a file in /Library/LaunchDaemons named org.hudson-ci.agent.plist with the following contents (skip this step if you're in a hurry):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<!-- Move HUDSON_HOME to /Volumes/yourdisk/hudson_home/ -->
	<key>EnvironmentVariables</key>
	<dict>
		<key>HUDSON_HOME</key>
		<string>/Volumes/yourdisk/hudson_home/</string>
	</dict>
	<key>Label</key>
	<string>org.hudson-ci.agent</string>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
	<key>ProgramArguments</key>
	<array>
	  <string>java</string>
	  <string>-jar</string>
	  <string>/usr/local/hudson/hudson.war</string>
<!-- prevent Hudson from becoming visible in the Finder & Dock -->
	  <string>-Djava.awt.headless=true</string>
	</array>
	<key>StandardErrorPath</key>
	<string>/Library/Logs/hudson-err.log</string>
	<key>StandardOutPath</key>
	<string>/Library/Logs/hudson-out.log</string>
</dict>
</plist>

Trouble!

When I had used this for a couple of days I noticed something strange. All Jobs would suddenly disappear. The reason turned out to be that sometimes during startup the Volume where HUDSON_HOME was now located wasn't available yet. The result is that another directory is created inside /Volumes/ where a new path to HUDSON_HOME is created. Launchd allows for some very clever extra checks like PathState but in the end I settled for something simpler.

Solution

Just delay startup of Hudson for a few seconds.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<!-- Move HUDSON_HOME to /Volumes/yourdisk/hudson_home/ -->
	<key>EnvironmentVariables</key>
	<dict>
		<key>HUDSON_HOME</key>
		<string>/Volumes/yourdisk/hudson_home/</string>
	</dict>
	<key>Label</key>
	<string>org.hudson-ci.agent</string>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
	<dict>
		<key>PathState</key>
		<dict>
			<key>/Volumes/yourdisk/hudson_home/</key>
			<true/>
		</dict>
	</dict>
	<key>ProgramArguments</key>
	<array>
	  <string>/Library/Java/Hudson/start_hudson.sh</string>
	</array>
	<key>StandardErrorPath</key>
	<string>/Library/Logs/hudson-err.log</string>
	<key>StandardOutPath</key>
	<string>/Library/Logs/hudson-out.log</string>
</dict>
</plist>
and the following in the start_hudson.sh script:
#!/bin/tcsh
set hudsonVolume = "/Volumes/yourdisk"
# introduce delay of 120 secs
sleep 120

if (! -e $hudsonVolume ) then
  exit 0
endif
growlnotify -n Hudson -m "Hudson starting..." 
# start it
/usr/bin/java \
  -jar /usr/local/hudson/hudson.war \
  -Djava.awt.headless=true \
  --httpPort=9090
This has been running now for a couple of weeks and now my builds and inspections run automatically.