forked from datastax/docker-images
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle
executable file
·263 lines (220 loc) · 8.14 KB
/
build.gradle
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
/*
* Copyright DataStax, Inc.
*
* Please see the included license file for details.
*/
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.freemarker:freemarker:2.3.33'
classpath 'commons-io:commons-io:2.6'
classpath 'com.google.guava:guava:28.0-jre'
}
}
plugins {
id 'base'
id 'com.bmuschko.docker-remote-api' version '4.10.0'
id "nebula.override" version "3.0.2"
id "de.undercouch.download" version "5.6.0"
}
import com.bmuschko.gradle.docker.tasks.AbstractDockerRemoteApiTask
import com.google.common.base.CaseFormat
import de.undercouch.gradle.tasks.download.Download
import freemarker.template.Configuration
import freemarker.template.TemplateExceptionHandler
import org.apache.commons.io.input.ProxyReader
defaultPropertyValue 'agentVersion', '6.7.4'
defaultPropertyValue 'downloadUrlPrefix', 'https://downloads.datastax.com'
defaultPropertyValue 'downloadServerUrlTemplate', "${downloadUrlPrefix}/enterprise/dse-\${VERSION}-bin.tar.gz"
defaultPropertyValue 'downloadAgentUrlTemplate', "${downloadUrlPrefix}/enterprise/datastax-agent-\${VERSION}.tar.gz"
defaultPropertyValue 'downloadOpscenterUrlTemplate', "${downloadUrlPrefix}/enterprise/opscenter-\${VERSION}.tar.gz"
defaultPropertyValue 'downloadStudioUrlTemplate', "${downloadUrlPrefix}/datastax-studio/datastax-studio-\${VERSION}.tar.gz"
defaultPropertyValue 'downloadDdacUrlTemplate', "${downloadUrlPrefix}/ddac/ddac-\${VERSION}-bin.tar.gz"
defaultPropertyValue 'imagePrefix', (project.hasProperty('dockerRegistry') ? ("$dockerRegistry/" - 'http://' - 'https://') : "") + 'datastax'
docker {
registryCredentials {
if (project.hasProperty('dockerRegistry')) {
url = project.dockerRegistry
}
if (project.hasProperty('registryUsername')) {
username = project.registryUsername
}
if (project.hasProperty('registryPassword')) {
password = project.registryPassword
}
}
}
def renderDockerContextTask(templateDir, version) {
tasks.create("renderDocker${templateDir.name.capitalize()}ContextForVersion$version", RenderDir) {
vars.version = version
vars.agentVersion = agentVersion
sourceDir templateDir
into "$buildDir/${templateDir.name}/$version"
preserve {
include "**/*.tar.gz"
include "**/*.zip"
}
}
}
tasks.addRule("Pattern: (build|push)<Image><Version>Image: build/push Docker image out of templates in <Image> folder and for given <Version>") { taskName ->
def m = taskName =~ /^build([A-Z]\p{Alpha}*)(\d+)\.(\d+)\.(\d+)((-\S+)?)Image$/
def templateName
def templateDir
def version
if (m) {
templateName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, m[0][1])
templateDir = file(CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, m[0][1]))
version = new Version(m[0][2], m[0][3], m[0][4], m[0][5])
} else {
m = taskName =~ /^build([A-Z]\p{Alpha}*)Image$/
if (m) {
templateName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, m[0][1])
templateDir = file(CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, m[0][1]))
}
}
if (templateDir) {
assert templateDir.exists(): "Template dir $templateDir does not exist"
if (!version) {
def versionProperty = "${templateName}Version"
assert project.hasProperty(versionProperty): "Property $versionProperty must be specified"
version = new Version(project.getProperty(versionProperty))
}
def url = renderTemplate(project.ext.get("download${templateName.capitalize()}UrlTemplate"), [VERSION: version])
def renderTask = renderDockerContextTask(templateDir, version)
def imageName = templateDir.name
if (imageName in ['server', 'studio', 'opscenter']) {
imageName = "dse-" + imageName
}
def downloadTask = tasks.create("download${templateName.capitalize()}$version", Download) {
src url
onlyIfModified true
dest renderTask.destinationDir
}
if (new File(url).exists()) {
dependsOn(tasks.create("copy${templateName.capitalize()}ToBuildCtx", Copy) {
from url
into renderTask.destinationDir
})
}
tasks.create(taskName) {
dependsOn renderTask, downloadTask
}
}
}
def renderTemplate(tpl, vars = [:]) {
def engine = new groovy.text.SimpleTemplateEngine()
engine.createTemplate(tpl).make(vars).toString()
}
task enableLocalDownloads
tasks.withType(AbstractDockerRemoteApiTask) {
outputs.upToDateWhen { false }
}
tasks.matching { it.name.startsWith('buildServer') }.all {
def url = renderTemplate(project.ext.get("downloadAgentUrlTemplate"), [VERSION: agentVersion])
}
tasks.matching { it.name.startsWith('downloadServer') }.all {
def url = renderTemplate(project.ext.get("downloadAgentUrlTemplate"), [VERSION: agentVersion])
src([src, url])
}
tasks.withType(Download) {
enabled = false
}
gradle.taskGraph.whenReady { g ->
if (g.hasTask(enableLocalDownloads)) {
tasks.withType(Download) { enabled = true }
}
}
if (hasProperty('downloadUsername') && hasProperty('downloadPassword')) {
tasks.withType(Download) {
username downloadUsername
password downloadPassword
}
}
void defaultPropertyValue(name, value) {
if (!project.ext.has(name)) {
project.ext.set(name, value)
}
}
class RenderDir extends Sync {
private File sourceDir
@Internal
def freeMarkerCfg
@Input
def vars
RenderDir() {
freeMarkerCfg = new Configuration(Configuration.VERSION_2_3_28)
freeMarkerCfg.defaultEncoding = "UTF-8"
freeMarkerCfg.tagSyntax = Configuration.ANGLE_BRACKET_TAG_SYNTAX
freeMarkerCfg.interpolationSyntax = Configuration.SQUARE_BRACKET_INTERPOLATION_SYNTAX
freeMarkerCfg.templateExceptionHandler = TemplateExceptionHandler.RETHROW_HANDLER
freeMarkerCfg.logTemplateExceptions = false
freeMarkerCfg.wrapUncheckedExceptions = true
vars = [:]
eachFile { fd ->
if (sourceName.endsWith('.ftl')) {
name = sourceName - '.ftl'
filter(["freeMarkerCfg": freeMarkerCfg,
"fileName" : fd.relativeSourcePath.pathString,
"vars" : vars], FreeMarkerReader)
}
}
}
void sourceDir(File dir) {
from dir
sourceDir = dir
freeMarkerCfg.directoryForTemplateLoading = dir
}
}
class FreeMarkerReader extends ProxyReader {
// def in
def freeMarkerCfg
def fileName
def rendered
def vars
FreeMarkerReader(Reader r) {
super(r)
}
@Override
void beforeRead(int i) {
if (!rendered) {
def template = freeMarkerCfg.getTemplate(fileName)
def w = new StringWriter()
template.process(vars, w)
this.in = new StringReader(w.toString())
rendered = true
}
}
}
class Version implements Serializable {
int major, minor, bugfix
def customSuffix
Version(major, minor, bugfix, customSuffix) {
this.major = Integer.parseInt(major)
this.minor = Integer.parseInt(minor)
this.bugfix = Integer.parseInt(bugfix)
this.customSuffix = customSuffix
}
Version(String v) {
this(parseVersion(v))
}
Version(Map m) {
this(m.major, m.minor, m.bugfix, m.customSuffix)
}
static def parseVersion(String v) {
def m = v =~ /^(\d+)\.(\d+)\.(\d+)((-\S+)?)$/
assert m: "$v is not parseable version"
[major: m[0][1], minor: m[0][2], bugfix: m[0][3], customSuffix: m[0][4]]
}
String toString() { "$major.$minor.$bugfix$customSuffix" }
int getNumericValue() {
major * 100000 + minor * 100 + bugfix
}
boolean lowerThan(String givenVersionStr) {
return numericValue < new Version(givenVersionStr).numericValue
}
boolean greaterEqualThan(String givenVersionStr) {
return numericValue >= new Version(givenVersionStr).numericValue
}
}