Skip to content

Instantly share code, notes, and snippets.

@jbrisbin
Created August 25, 2016 18:07
Show Gist options
  • Save jbrisbin/29d26c8413035249bfc3bbaba3d92fee to your computer and use it in GitHub Desktop.
Save jbrisbin/29d26c8413035249bfc3bbaba3d92fee to your computer and use it in GitHub Desktop.
Automatic boostrapping of Mesos slaves in Jenkins
#!/bin/bash
curl -sSL -XPOST -H "Content-Type: application/json" -d @- "http://marathon.mesos:8080/v2/apps" <<END
{
"id": "/jenkins/build/$3",
"cpus": 1,
"mem": 8192,
"instances": 1,
"cmd": "java -jar slave.jar -jnlpUrl http://$1/jenkins/computer/$3/slave-agent.jnlp -secret $4",
"constraints": [["hostname", "CLUSTER", "$2"]],
"uris": [
"http://$1/jenkins/jnlpJars/slave.jar"
]
}
END
// Put in Jenkins' init.groovy.d directory
import groovy.json.*
import jenkins.model.*
import hudson.slaves.*
def env = System.getenv()
String host = env['HOST']
int port = env['PORT0']?.toInteger() ?: 8080
def jenkins = Jenkins.getInstance()
def nodeMgr = jenkins.getNodesObject()
def launcher = new JNLPLauncher()
Thread.start {
def slavesJson = "http://leader.mesos:5050/slaves".toURL().getText()
def nodes = []
new JsonSlurper().parseText(slavesJson)["slaves"].each {
def parts = it.id.split('-')
name = "${parts[0]}-${parts[-1]}".toLowerCase()
def slave = new DumbSlave(name, "/tmp", launcher)
nodeMgr.addNode(slave)
nodes << [name: name, hostname: it.hostname, slave: slave]
}
nodeMgr.save()
nodes.each {
println "connect-slave.sh $host:$port ${it.hostname} ${it.name} ${it.slave.getComputer().getJnlpMac()}".execute().text
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment