I would like to launch Jenkins slaves with the option “Launch agent via execution of command on master”.
I have a small script that would take in which container to launch Jenkins via jnlp.
#!/bin/bash
JENKINS_NAME=$1
if [ -z "${JENKINS_URL:-}" ] || [ -z "${JENKINS_NAME:-}" ]; then
echo "Missing Jenkins configuration" >&2
exit 1
fi
JENKINS_KEY=$(curl -L -s -u username:password -X GET "${JENKINS_URL}/computer/${JENKINS_NAME}/slave-agent.jnlp" | sed "s/.*<application-desc main-class=\"hudson.remoting.jnlp.Main\"><argument>\([a-z0-9]*\).*/\1/")
lxc file push agent.jar ${JENKINS_NAME}/root/
lxc exec ${JENKINS_NAME} -- sudo apt-get install -y openjdk-8-jdk
lxc exec ${JENKINS_NAME} -- java -jar ~/agent.jar -jnlpUrl "${JENKINS_URL}/computer/${JENKINS_NAME}/slave-agent.jnlp" -secret "${JENKINS_KEY}" -workDir "/root/Jenkins"
When I launch this script via jenkins i get the error
Error: Get http://unix.socket/1.0: dial unix /var/snap/lxd/common/lxd/unix.socket: connect: permission denied
jenkins user was created before installing jenkins, with usermod appended. Manually using jenkins user account can access lxc.
$ sudo su jenkins
$ lxc exec c1 -- pwd
/root
Is there a better way to launch slaves using the master?