-
Notifications
You must be signed in to change notification settings - Fork 319
/
docker-plugin-configuration-script.groovy
170 lines (157 loc) · 6.73 KB
/
docker-plugin-configuration-script.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import com.nirima.jenkins.plugins.docker.DockerCloud
import com.nirima.jenkins.plugins.docker.DockerDisabled
import com.nirima.jenkins.plugins.docker.DockerImagePullStrategy
import com.nirima.jenkins.plugins.docker.DockerTemplate
import com.nirima.jenkins.plugins.docker.DockerTemplateBase
import com.nirima.jenkins.plugins.docker.launcher.AttachedDockerComputerLauncher
import hudson.slaves.Cloud
import io.jenkins.docker.client.DockerAPI
import io.jenkins.docker.connector.DockerComputerAttachConnector
import io.jenkins.docker.connector.DockerComputerConnector
import jenkins.model.Jenkins
import org.jenkinsci.plugins.docker.commons.credentials.DockerServerEndpoint
/////////////////////////////////////////////////////////////////////////////
// Parameters in this script are placed in groovy maps.
// Most parameters are strings in 'single quotes'.
// Some are boolean, so either true or false.
// Some are numbers, either Integer or Long.
// Most parameters are optional.
// Default values are usually an empty string, false, or no number set.
/////////////////////////////////////////////////////////////////////////////
// Parameters listed here are used to create a
// https://github.com/jenkinsci/docker-plugin/blob/master/src/main/java/com/nirima/jenkins/plugins/docker/DockerTemplateBase.java
def templateBaseParameters = [
// image is mandatory param: use 'jenkins/agent:latest' if unsure.
image: 'jenkins/agent:latest',
// all other parameters are optional
// Uncomment them if you want to set them.
// bindAllPorts: false,
// bindPorts: '',
// capabilitiesToAddString: '',
// capabilitiesToDropString: '',
// cgroupParent: '',
// cpus: '',
// cpuPeriod: (Long)null,
// cpuQuota: (Long)null,
// cpuShares: (Integer)null,
// devicesString: '',
// dnsString: '',
// dnsSearchString: '',
// dockerCommand: '',
// environmentsString: '',
// extraDockerLabelsString: '',
// extraGroupsString: '',
// extraHostsString: '',
// hostname: '',
// macAddress: '',
// memoryLimit: (Integer)null,
// memorySwap: (Integer)null,
// network: '',
// privileged: false,
// pullCredentialsId: '',
// securityOptsString: '',
// shmSize: (Integer)null,
tty: true,
// user: '',
// volumesFromString: '',
// mountsString: '',
]
// Parameters listed here are used to create a
// https://github.com/jenkinsci/docker-plugin/blob/master/src/main/java/com/nirima/jenkins/plugins/docker/DockerTemplate.java
def templateParameters = [
// all parameters except name, remoteFs and labelString are optional
// disabled: false,
// instanceCapStr: '4',
labelString: 'docker linux',
// mode: hudson.model.Node.Mode.NORMAL,
name: 'docker-local',
// pullStrategy: DockerImagePullStrategy.PULL_LATEST,
pullTimeout: 300,
remoteFs: '/home/jenkins/agent',
removeVolumes: true,
// stopTimeout: 10,
]
// Parameters listed here are used to create a
// https://github.com/jenkinsci/docker-plugin/blob/master/src/main/java/io/jenkins/docker/client/DockerAPI.java
// and
// https://github.com/jenkinsci/docker-plugin/blob/master/src/main/java/com/nirima/jenkins/plugins/docker/DockerCloud.java
def cloudParameters = [
// serverUrl and name are required.
// everything else is optional
serverUrl: 'unix:///var/run/docker.sock',
name: 'local-docker-cloud',
containerCap: 4, // 0 means no cap
// credentialsId: '',
connectTimeout: 5,
readTimeout: 60,
// version: '',
// dockerHostname: '',
// exposeDockerHost: false,
// disabled: false,
// errorDuration: (Integer)null,
]
/////////////////////////////////////////////////////////////////////////////
// The code above defines our data.
// Now to turn that raw data into objects used by the
// docker-plugin code...
/////////////////////////////////////////////////////////////////////////////
DockerTemplateBase templateBase = new DockerTemplateBase(templateBaseParameters.image)
templateBaseParameters.findAll{ it.key != "image" }.each { k, v ->
templateBase."$k" = v
}
DockerComputerConnector computerConnector = new DockerComputerAttachConnector()
Set<String> templateParametersHandledSpecially = [ 'labelString', 'instanceCapStr' ]
DockerTemplate template = new DockerTemplate(
templateBase,
computerConnector,
templateParameters.labelString,
templateParameters.instanceCapStr
)
templateParameters.findAll{ !templateParametersHandledSpecially.contains(it.key) }.each { k, v ->
if ( k=="disabled" ) {
DockerDisabled dd = new DockerDisabled()
dd.disabledByChoice = v
template."$k" = dd
} else {
template."$k" = v
}
}
Set<String> cloudParametersHandledSpecially = [ 'serverUrl', 'credentialsId' ,'serverUrl' ,'credentialsId' ,'connectTimeout' ,'readTimeout' ,'version' ,'connectTimeout' ,'dockerHostname' ,'name' ]
DockerAPI api = new DockerAPI(new DockerServerEndpoint(cloudParameters.serverUrl, cloudParameters.credentialsId))
api.with {
connectTimeout = cloudParameters.connectTimeout
readTimeout = cloudParameters.readTimeout
apiVersion = cloudParameters.version
hostname = cloudParameters.dockerHostname
}
DockerCloud newCloud = new DockerCloud(
cloudParameters.name,
api,
[template]
)
cloudParameters.findAll{!cloudParametersHandledSpecially.contains(it.key)}.each { k, v ->
if ( k=="disabled" ) {
DockerDisabled dd = new DockerDisabled()
dd.disabledByChoice = v
newCloud."$k" = dd
} else {
newCloud."$k" = v
}
}
/////////////////////////////////////////////////////////////////////////////
// Now to push our data into Jenkins,
// replacing (overwriting) any cloud of the same name with this config.
/////////////////////////////////////////////////////////////////////////////
// get Jenkins instance
Jenkins jenkins = Jenkins.get()
// add/replace cloud configuration to Jenkins
Cloud oldCloudOrNull = jenkins.clouds.getByName(cloudParameters.name)
if ( oldCloudOrNull ) {
jenkins.clouds.remove(oldCloudOrNull)
}
jenkins.clouds.add(newCloud)
// save current Jenkins state to disk
jenkins.save()
/////////////////////////////////////////////////////////////////////////////
// ...and we're done.
/////////////////////////////////////////////////////////////////////////////