Skip to content

Instantly share code, notes, and snippets.

@rhwood
Last active December 10, 2015 15:20
Show Gist options
  • Save rhwood/4136130 to your computer and use it in GitHub Desktop.
Save rhwood/4136130 to your computer and use it in GitHub Desktop.
OS X LaunchDaemon for a Jenkins Java Web Start slave. This is now maintained at https://github.com/rhwood/jenkins-slave-osx Please refer to that version instead.
#!/bin/sh
# create jenkins group
NEXT_GID=$((`dscl /Local/Default list /Groups gid | awk '{ print $2 }' | sort -n | grep -v ^[5-9] | tail -n1` + 1))
sudo dscl /Local/Default create /Groups/jenkins
sudo dscl /Local/Default create /Groups/jenkins PrimaryGroupID $NEXT_GID
sudo dscl /Local/Default create /Groups/jenkins Password \*
sudo dscl /Local/Default create /Groups/jenkins RealName 'Jenkins Node Service'
# create jenkins user
NEXT_UID=$((`dscl /Local/Default list /Users uid | awk '{ print $2 }' | sort -n | grep -v ^[5-9] | tail -n1` + 1))
sudo dscl /Local/Default create /Users/jenkins
sudo dscl /Local/Default create /Users/jenkins UniqueID $NEXT_UID
sudo dscl /Local/Default create /Users/jenkins PrimaryGroupID $NEXT_GID
sudo dscl /Local/Default create /Users/jenkins UserShell /usr/bin/false
sudo dscl /Local/Default create /Users/jenkins NFSHomeDirectory /var/lib/jenkins
sudo dscl /Local/Default create /Users/jenkins Password \*
sudo dseditgroup -o edit -a jenkins -t user jenkins
# download the LaunchDaemon
sudo curl --url https://raw.github.com/gist/4136130/d3c9d7275ce78e050d8594037a2d509652a766e5/org.jenkins-ci.slave.jnlp.plist -o /Library/LaunchDaemons/org.jenkins-ci.slave.jnlp.plist
# create the jenkins home dir
sudo mkdir /var/lib/jenkins
# download the jenkins JNLP slave script
sudo curl --url https://raw.github.com/gist/4136130/2553c4cec9bc7ed5557359a22c6c1b61028afa40/slave.jnlp.sh -o /var/lib/jenkins/slave.jnlp.sh
sudo chown -R jenkins:wheel /var/lib/jenkins
sudo chmod 755 /var/lib/jenkins/slave.jnlp.sh
# create a logging space
sudo mkdir /var/log/jenkins
sudo chown jenkins:wheel /var/log/jenkins
# remember to edit the LaunchDaemon
echo "IMPORTANT:"
echo "edit /Library/LaunchDaemons/org.jenkins-ci.slave.jnlp.plist to use the correct master"
<?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>
<key>Label</key>
<string>org.jenkins-ci.slave.jnlp</string>
<key>ProgramArguments</key>
<array>
<string>/var/lib/jenkins/slave.jnlp.sh</string>
<string>--master=http://jenkins.example.com</string>
</array>
<key>KeepAlive</key>
<true/>
<key>RunAtLoad</key>
<true/>
<key>UserName</key>
<string>jenkins</string>
<key>WorkingDirectory</key>
<string>/var/lib/jenkins</string>
<key>StandardInPath</key>
<string>/dev/null</string>
<key>StandardErrorPath</key>
<string>/var/log/jenkins/jenkins.log</string>
<key>StandardOutPath</key>
<string>/var/log/jenkins/jenkins.log</string>
</dict>
</plist>
#!/bin/bash
JENKINS_SLAVE=`hostname -s | tr '[:upper:]' '[:lower:]'`
JENKINS_MASTER=http://jenkins
JENKINS_PORT=''
if [ -f /etc/jenkins.node.conf ]; then
source /etc/jenkins.node.conf
fi
while [ $# -gt 0 ]; do
case $1 in
--node=*)
JENKINS_SLAVE=${1#*=}
;;
--master=*)
JENKINS_MASTER=${1#*=}
;;
--jnlp-port=*)
JENKINS_PORT=":${1#*=}"
;;
esac
shift
done
echo "Getting slave.jar from ${JENKINS_MASTER}"
RESULT=-1
while [ $RESULT -ne 0 ]; do
curl --url ${JENKINS_MASTER}/jnlpJars/slave.jar -o /var/lib/jenkins/slave.jar
RESULT=$?
if [ $RESULT -ne 0 ]; then
sleep 10
fi
done
JENKINS_JNLP_URL=${JENKINS_MASTER}${JENKINS_PORT}/computer/${JENKINS_SLAVE}/slave-agent.jnlp
echo "Launching slave process at ${JENKINS_JNLP_URL}"
java -jar /var/lib/jenkins/slave.jar -jnlpUrl ${JENKINS_JNLP_URL}
@rhwood
Copy link
Author

rhwood commented Nov 23, 2012

There are two variables that can be passed as strings in the ProgramArguments array:

  • --node=MyNodeName This variable should be set if the node name for the slave as configured in Jenkins is not the short hostname for the slave.
  • --master=http://jenkins.example.com This variable should be set if the Jenkins master can not be pinged by the slave with just the short name jenkins (i.e. if ping jenkins does not work).

Alternately, these two variables can be set in /etc/jenkins.node.conf as JENKINS_SLAVE and JENKINS_MASTER respectively.

@rhwood
Copy link
Author

rhwood commented Feb 7, 2013

This script works for Jenkins versions prior to version 1.498 (see JENKINS-16273).

@rhwood
Copy link
Author

rhwood commented Jun 19, 2013

I am now maintaining a more complete version of this work at https://github.com/rhwood/jenkins-slave-osx Please use that version instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment