Skip to content

Instantly share code, notes, and snippets.

@willis7
Last active July 10, 2018 15:56
Show Gist options
  • Save willis7/9caa5bd5ec1e5fd09a14b8c488a149dc to your computer and use it in GitHub Desktop.
Save willis7/9caa5bd5ec1e5fd09a14b8c488a149dc to your computer and use it in GitHub Desktop.
Programatically add templates and clouds to Jenkins via Docker plugin
import com.nirima.jenkins.plugins.docker.DockerCloud
import com.nirima.jenkins.plugins.docker.DockerTemplate
import com.nirima.jenkins.plugins.docker.DockerTemplateBase
import com.nirima.jenkins.plugins.docker.launcher.AttachedDockerComputerLauncher
import io.jenkins.docker.connector.DockerComputerAttachConnector
//
// TEMPLATES
def tmplConf = '''
params {
pullCredentialsId = ''
dnsString = ''
network = ''
dockerCommand = ''
volumesString = ''
volumesFromString = ''
environmentsString =''
hostname = ''
memoryLimit = null
memorySwap = null
cpuShares = null
bindPorts = ''
bindAllPorts = false
privileged = false
tty = true
macAddress = ''
extraHostsString = ''
instanceCapStr = '4'
remoteFs = ''
}
images {
'jenkins-slave-base' {}
'foo' {}
}
environments {
'jenkins-slave-base' {
params {
labelString = 'jenkins.slave'
image = 'ONSDigital/jenkins-slave-base:latest'
tty= false
}
}
'foo' {
params {
labelString = 'jenkins.foo'
image = 'ONSDigital/jenkins-foo-base:latest'
tty= false
}
}
}
'''
//
// CLOUDS
def cloudConf = '''
params{
containerCapStr = '10'
connectTimeout = 5
credentialsId = ''
dockerHostname = ''
readTimeout = 15
version = ''
}
clouds {
download {}
build {}
test {}
}
environments {
download{
params{
name = 'docker-download'
serverUrl = 'tcp://127.0.0.1:4243'
templates = ['jenkins-slave-base', 'foo']
}
}
build{
params{
name = 'docker-build'
serverUrl = 'tcp://127.0.0.1:4243'
templates = ['jenkins-slave-base']
}
}
test{
params{
name = 'docker-test'
serverUrl = 'tcp://127.0.0.1:4243'
templates = ['jenkins-slave-base']
}
}
}
'''
def newDockerTemplateBase(Map templateParams) {
println templateParams
new DockerTemplateBase(
templateParams.image,
templateParams.pullCredentialsId,
templateParams.dnsString,
templateParams.network,
templateParams.dockerCommand,
templateParams.volumesString,
templateParams.volumesFromString,
templateParams.environmentsString,
templateParams.hostname,
templateParams.memoryLimit,
templateParams.memorySwap,
templateParams.cpuShares,
templateParams.bindPorts,
templateParams.bindAllPorts,
templateParams.privileged,
templateParams.tty,
templateParams.macAddress,
templateParams.extraHostsString
)
}
def newDockerTemplate(DockerTemplateBase dockerTemplateBase, Map templateParams) {
new DockerTemplate(
dockerTemplateBase,
new DockerComputerAttachConnector(),
templateParams.labelString,
templateParams.remoteFs,
templateParams.instanceCapStr
)
}
def newDockerCloud(def cloudParams, def templates) {
new DockerCloud(
cloudParams.name,
templates,
cloudParams.serverUrl,
cloudParams.containerCapStr,
cloudParams.connectTimeout,
cloudParams.readTimeout,
cloudParams.credentialsId,
cloudParams.version,
cloudParams.dockerHostname
)
}
// buildTemplates returns a Map whose key is the image name (from tmpl config) as a String
// and the value is a DockerTemplate
def buildTemplates(def templateConfig) {
def config = new ConfigSlurper().parse(templateConfig)
def templates = [:]
//println config
// for each template image
config.images.each { name, unused ->
img = new ConfigSlurper(name).parse(templateConfig)
// construct a DockerTemplateBase obj
dockerTemplateBase = newDockerTemplateBase(img.params)
// pass that new object to newDockerTemplate and
// construct a DockerTemplate
dockerTemplate = newDockerTemplate(dockerTemplateBase, img.params)
templates.put(name, dockerTemplate)
}
return templates
}
// buildClouds returns a List of DockerCloud objects which have been constructed
// with the Templates linked
def buildClouds(def cloudConfig, Map templates) {
def config = new ConfigSlurper().parse(cloudConfig)
def clouds = []
config.clouds.each { name, unused ->
cloudParams = new ConfigSlurper(name).parse(cloudConfig)
// create a list of DockerTemplate(s) that need to be attached to
// the cloud. The names in the cloud config match the keys of
// the templates Map, so we generate the list from that
def attachTmpl = []
cloudParams.params.templates.each {
attachTmpl << templates.get(it)
}
clouds << newDockerCloud(cloudParams.params, attachTmpl)
}
return clouds
}
def BuildAll(def templateConfig, def cloudConfig) {
templates = buildTemplates(templateConfig)
clouds = buildClouds(cloudConfig, templates)
return clouds
}
// get Jenkins instance
Jenkins jenkins = Jenkins.getInstance()
// remove existing clouds
jenkins.clouds.removeAll(DockerCloud)
// install new clouds
c = BuildAll(tmplConf, cloudConf)
jenkins.clouds.addAll(c)
// save current Jenkins state to disk
jenkins.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment