-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathDockerTask.groovy
More file actions
294 lines (255 loc) · 8.74 KB
/
Copy pathDockerTask.groovy
File metadata and controls
294 lines (255 loc) · 8.74 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/**
* Copyright 2014 Transmode AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package se.transmode.gradle.plugins.docker
import com.google.common.annotations.VisibleForTesting;
import com.google.common.io.Files
import org.gradle.api.DefaultTask
import org.gradle.api.logging.Logger
import org.gradle.api.logging.Logging
import org.gradle.api.tasks.TaskAction
import se.transmode.gradle.plugins.docker.client.DockerClient
import se.transmode.gradle.plugins.docker.client.JavaDockerClient
import se.transmode.gradle.plugins.docker.client.NativeDockerClient
import se.transmode.gradle.plugins.docker.image.Dockerfile
class DockerTask extends DefaultTask {
private static Logger logger = Logging.getLogger(DockerTask)
public static final String DEFAULT_IMAGE = 'ubuntu'
// full path to the docker executable
String dockerBinary
// Name and Email of the image maintainer
String maintainer
// Name of the application being wrapped into a docker image (default: project.name)
String applicationName
// What to tag the created docker image with (default: group/applicationName)
String tag
// Which version to use along with the tag (default: latest)
String tagVersion
// Whether or not to execute docker to build the image (default: false)
Boolean dryRun
// Whether or not to push the image into the registry (default: false)
Boolean push
// Hostname, port of the docker image registry unless Docker Registry Hub is used
String registry
/**
* Path to external Dockerfile
*/
File dockerfile
public void setDockerfile(String path) {
setDockerfile(project.file(path))
}
public void setDockerfile(File dockerfile) {
this.dockerfile = dockerfile
}
/**
* Name of the base docker image
*/
String baseImage
public String getBaseImage() {
return determineBaseImage()
}
/**
* Determine the name of the base docker image.
*
* If the base image is set in the task, return it. Otherwise return the base image
* defined in the 'docker' extension. If the extension base image is not set determine
* base image based on the 'targetCompatibility' property from the java plugin.
*
* @return Name of base docker image
*/
private String determineBaseImage() {
def defaultImage = project.hasProperty('targetCompatibility') ? JavaBaseImage.imageFor(project.targetCompatibility).imageName : DEFAULT_IMAGE
return baseImage ?: (project[DockerPlugin.EXTENSION_NAME].baseImage ?: defaultImage)
}
// Dockerfile instructions (ADD, RUN, etc.)
def instructions
// Dockerfile staging area i.e. context dir
File stageDir
// Tasks necessary to setup the stage before building an image
def stageBacklog
// Should we use Docker's remote API instead of the docker executable
Boolean useApi
// URL of the remote Docker host (default: localhost)
String hostUrl
// Docker remote API credentials
String apiUsername
String apiPassword
String apiEmail
DockerTask() {
instructions = []
stageBacklog = []
applicationName = project.name
stageDir = new File(project.buildDir, "docker")
}
void addFile(String source, String destination='/') {
addFile(project.file(source), destination)
}
void addFile(File source, String destination='/') {
def target = stageDir
if (source.isDirectory()) {
target = new File(stageDir, source.name)
}
stageBacklog.add { ->
project.copy {
from source
into target
}
}
instructions.add("ADD ${source.name} ${destination}")
}
void addFile(Closure copySpec) {
final tarFile = new File(stageDir, "add_${instructions.size()+1}.tar")
stageBacklog.add { ->
createTarArchive(tarFile, copySpec)
}
instructions.add("ADD ${tarFile.name} ${'/'}")
}
void createTarArchive(File tarFile, Closure copySpec) {
final tmpDir = Files.createTempDir()
logger.info("Creating tar archive {} from {}", tarFile, tmpDir)
/* copy all files to temporary directory */
project.copy {
with {
into('/') {
with copySpec
}
}
into tmpDir
}
/* create tar archive */
new AntBuilder().tar(
destfile: tarFile,
basedir: tmpDir
)
}
void workingDir(String wd) {
instructions.add("WORKDIR ${wd}")
}
void addInstruction(String cmd, String value) {
instructions.add("${cmd} ${value}")
}
void runCommand(String command) {
instructions.add("RUN ${command}")
}
void exposePort(Integer port) {
instructions.add("EXPOSE ${port}")
}
void setEnvironment(String key, String value) {
instructions.add("ENV ${key} ${value}")
}
void setTagVersion(String version) {
tagVersion = version;
}
void setTagVersionToLatest() {
tagVersion = 'latest';
}
void volume(String... paths) {
instructions.add('VOLUME ["' + paths.join('", "') + '"]')
}
void setEntryPoint(List entryPoint) {
instructions.add('ENTRYPOINT ["' + entryPoint.join('", "') + '"]')
}
void entryPoint(List entryPoint) {
this.setEntryPoint(entryPoint)
}
void setDefaultCommand(List cmd) {
instructions.add('CMD ["' + cmd.join('", "') + '"]')
}
void defaultCommand(List cmd) {
this.setDefaultCommand(cmd)
}
void contextDir(String contextDir) {
stageDir = new File(stageDir, contextDir)
}
private File createDirIfNotExists(File dir) {
if (!dir.exists())
dir.mkdirs()
return dir
}
@VisibleForTesting
protected void setupStageDir() {
logger.info('Setting up staging directory.')
createDirIfNotExists(stageDir)
stageBacklog.each() { it() }
}
@VisibleForTesting
protected Dockerfile buildDockerfile() {
def baseDockerfile
if (getDockerfile()) {
logger.info('Creating Dockerfile from file {}.', dockerfile)
baseDockerfile = Dockerfile.fromExternalFile(dockerfile)
} else {
def baseImage = determineBaseImage()
logger.info('Creating Dockerfile from base {}.', baseImage)
baseDockerfile = Dockerfile.fromBaseImage(baseImage)
}
if (getMaintainer()) {
baseDockerfile.append("MAINTAINER ${getMaintainer()}")
}
return baseDockerfile.appendAll(instructions)
}
@TaskAction
void build() {
setupStageDir()
buildDockerfile().writeToFile(new File(stageDir, 'Dockerfile'))
tag = getImageTag()
logger.info('Determining image tag: {}', tag)
if (!dryRun) {
DockerClient client = getClient()
client.buildImage(stageDir, tag)
if (push) {
client.pushImage(tag)
}
}
}
private String getImageTag() {
String tag
tag = this.tag ?: getDefaultImageTag()
return appendImageTagVersion(tag)
}
private String getDefaultImageTag() {
String tag
if (registry) {
tag = "${-> registry}/${-> applicationName}"
} else if (project.group) {
tag = "${-> project.group}/${-> applicationName}"
} else {
tag = "${-> applicationName}"
}
return tag
}
private String appendImageTagVersion(String tag) {
def version = tagVersion ?: project.version
if(version == 'unspecified') {
version = 'latest'
}
return "${tag}:${version}"
}
private DockerClient getClient() {
DockerClient client
if(getUseApi()) {
logger.info("Using the Docker remote API.")
client = JavaDockerClient.create(
getHostUrl(),
getApiUsername(),
getApiPassword(),
getApiEmail())
} else {
logger.info("Using the native docker binary.")
client = new NativeDockerClient(getDockerBinary())
}
return client
}
}