-
Notifications
You must be signed in to change notification settings - Fork 158
/
DockerExtension.groovy
177 lines (144 loc) · 5.17 KB
/
DockerExtension.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
171
172
173
174
175
176
177
/*
* (c) Copyright 2020 Palantir Technologies Inc. All rights reserved.
*
* 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 com.palantir.gradle.docker
import com.google.common.base.Preconditions
import com.google.common.base.Strings
import com.google.common.collect.ImmutableMap
import com.google.common.collect.ImmutableSet
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.file.CopySpec
import org.gradle.internal.logging.text.StyledTextOutput
import org.gradle.internal.logging.text.StyledTextOutputFactory
class DockerExtension {
Project project
private static final String DEFAULT_DOCKERFILE_PATH = 'Dockerfile'
private String name = null
private File dockerfile = null
private String dockerComposeTemplate = 'docker-compose.yml.template'
private String dockerComposeFile = 'docker-compose.yml'
private Set<Task> dependencies = ImmutableSet.of()
private Set<String> tags = ImmutableSet.of()
private Map<String, String> namedTags = new HashMap<>()
private Map<String, String> labels = ImmutableMap.of()
private Map<String, String> buildArgs = ImmutableMap.of()
private boolean pull = false
private boolean noCache = false
private String network = null
private File resolvedDockerfile = null
private File resolvedDockerComposeTemplate = null
private File resolvedDockerComposeFile = null
// The CopySpec defining the Docker Build Context files
private final CopySpec copySpec
public DockerExtension(Project project) {
this.project = project
this.copySpec = project.copySpec()
}
public void setName(String name) {
this.name = name
}
public String getName() {
Preconditions.checkArgument(!Strings.isNullOrEmpty(name), "name is a required docker configuration item.")
return name
}
public void setDockerfile(File dockerfile) {
this.dockerfile = dockerfile
}
public void setDockerComposeTemplate(String dockerComposeTemplate) {
this.dockerComposeTemplate = dockerComposeTemplate
Preconditions.checkArgument(project.file(dockerComposeTemplate).exists(),
"Could not find specified template file: %s", project.file(dockerComposeTemplate))
}
public void setDockerComposeFile(String dockerComposeFile) {
this.dockerComposeFile = dockerComposeFile
}
public void dependsOn(Task... args) {
this.dependencies = ImmutableSet.copyOf(args)
}
public Set<Task> getDependencies() {
return dependencies
}
public void files(Object... files) {
copySpec.from(files)
}
public Set<String> getTags() {
return tags
}
@Deprecated
public void tags(String... args) {
this.tags = ImmutableSet.copyOf(args)
}
public Map<String, String> getNamedTags() {
return ImmutableMap.copyOf(namedTags)
}
public void tag(String taskName, String tag) {
if (namedTags.putIfAbsent(taskName, tag) != null) {
StyledTextOutput o = project.services.get(StyledTextOutputFactory.class).create(DockerExtension)
o.withStyle(StyledTextOutput.Style.Error).println("WARNING: Task name '${taskName}' is existed.")
}
}
public Map<String, String> getLabels() {
return labels
}
public void labels(Map<String, String> labels) {
this.labels = ImmutableMap.copyOf(labels)
}
public File getResolvedDockerfile() {
return resolvedDockerfile
}
File getResolvedDockerComposeTemplate() {
return resolvedDockerComposeTemplate
}
File getResolvedDockerComposeFile() {
return resolvedDockerComposeFile
}
public CopySpec getCopySpec() {
return copySpec
}
public void resolvePathsAndValidate() {
if (dockerfile != null) {
resolvedDockerfile = dockerfile
} else {
resolvedDockerfile = project.file(DEFAULT_DOCKERFILE_PATH)
}
resolvedDockerComposeFile = project.file(dockerComposeFile)
resolvedDockerComposeTemplate = project.file(dockerComposeTemplate)
}
public Map<String, String> getBuildArgs() {
return buildArgs
}
public String getNetwork() {
return network
}
public void setNetwork(String network) {
this.network = network
}
public void buildArgs(Map<String, String> buildArgs) {
this.buildArgs = ImmutableMap.copyOf(buildArgs)
}
public boolean getPull() {
return pull
}
public void pull(boolean pull) {
this.pull = pull
}
public boolean getNoCache() {
return noCache
}
public void noCache(boolean noCache) {
this.noCache = noCache
}
}