forked from datastax/docker-images
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle
199 lines (166 loc) · 6.4 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
/*
* Copyright DataStax, Inc.
*
* Please see the included license file for details.
*/
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'de.undercouch:gradle-download-task:3.3.0'
}
}
plugins {
id "com.chrisgahlert.gradle-dcompose-plugin" version "0.9.2"
}
apply plugin: 'de.undercouch.download'
import de.undercouch.gradle.tasks.download.Download
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
mavenCentral()
}
dependencies {
dcompose 'com.github.docker-java:docker-java:3.0.13'
}
ext {
downloadsDir = file("$buildDir/downloads")
}
defaultPropertyValue 'imagePrefix', (project.hasProperty('dockerRegistry') ? "$dockerRegistry/" : "") + 'datastax'
def components = ['server', 'opscenter', 'studio']
components.each {
defaultPropertyValue "${it}Version", "**"
defaultPropertyValue "${it}LatestVersion", "false"
}
defaultPropertyValue 'downloadUrlPrefix', 'https://downloads.datastax.com/enterprise'
defaultPropertyValue 'downloadServerUrlTemplate', "${downloadUrlPrefix}/dse-\${VERSION}-bin.tar.gz"
defaultPropertyValue 'downloadAgentUrlTemplate', "${downloadUrlPrefix}/datastax-agent-\${VERSION}.tar.gz"
defaultPropertyValue 'downloadOpscenterUrlTemplate', "${downloadUrlPrefix}/opscenter-\${VERSION}.tar.gz"
defaultPropertyValue 'downloadStudioUrlTemplate', "${downloadUrlPrefix}/datastax-studio-\${VERSION}.tar.gz"
// configure dcompose plugin logging to show build image progress
System.setProperty("org.slf4j.simpleLogger.log.com.github.dockerjava.core.command.BuildImageResultCallback", "debug")
System.setProperty("org.slf4j.simpleLogger.showThreadName", "false")
System.setProperty("org.slf4j.simpleLogger.showLogName", "false")
System.setProperty("org.slf4j.simpleLogger.logFile", "System.out")
dcompose {
if (project.hasProperty('dockerRegistry'))
{
registry(dockerRegistry) {
if (project.hasProperty('registryUsername'))
{
withUsername project.registryUsername
}
if (project.hasProperty('registryPassword'))
{
withPassword project.registryPassword
}
}
} else {
dockerClientConfig = {
if (project.hasProperty('registryUsername'))
{
withRegistryUsername project.registryUsername
}
if (project.hasProperty('registryPassword'))
{
withRegistryPassword project.registryPassword
}
}
}
downloadServer {
image = 'svenbrnn/alpine-lighttpd2:latest'
publishAllPorts = true
binds = ["$downloadsDir:/var/www"]
deploy = false
}
dseBase {
baseDir = file('base')
repository = 'dse-base'
deploy = false
forcePull = true
}
mkdir downloadsDir
components.each { name ->
def dockerFile = fileTree(dir: name, include: "${project.ext.get("${name}Version")}/Dockerfile").singleFile
def version = dockerFile.readLines().find { it.contains('ARG VERSION=')}.split('=')[1].trim()
services.maybeCreate(name)
services.getByName(name) {
baseDir = dockerFile.parentFile
repository = "$imagePrefix/dse-$name:$version"
tasks.getByName(buildImageTaskName).dependsOn(services.getByName('dseBase').buildImageTaskName)
}
if (project.ext.get("${name}LatestVersion") == "true") {
services.maybeCreate("${name}Latest")
services.getByName("${name}Latest") {
image = "$imagePrefix/dse-$name:$version"
repository = "$imagePrefix/dse-$name:latest"
tasks.getByName(pullImageTaskName).dependsOn(services.getByName(name).buildImageTaskName)
}
}
def t = tasks.create("download${name.capitalize()}Archives", Download) {
dest downloadsDir
onlyIfModified true
def engine = new groovy.text.SimpleTemplateEngine()
def urlsTemplates = [ project.ext.get("download${name.capitalize()}UrlTemplate")]
def urls = urlsTemplates.collect { engine.createTemplate(it).make('VERSION': version).toString() }
if (name == 'server') {
def agentVersion = dockerFile.readLines().find { it.contains('ARG DSE_AGENT_VERSION=')}.split('=')[1].trim()
urls << engine.createTemplate(project.ext.get("downloadAgentUrlTemplate")).make('VERSION': agentVersion).toString()
}
src(urls)
}
configure(tasks."build${name.capitalize()}Image") {
dependsOn t, startDownloadServerContainer
finalizedBy stopDownloadServerContainer
}
stopDownloadServerContainer.mustRunAfter tasks."build${name.capitalize()}Image"
}
}
task skipDownloads
startDownloadServerContainer {
mustRunAfter tasks.withType(Download)
doLast {
def host = service.dockerHost
def port = service.findHostPort(80)
if (host in ['localhost', '127.0.0.1'])
{
host = runInDockerClasspath {
def response = client.execCreateCmd(containerId)
.withCmd('sh', '-c', 'ip route | awk \'\$1 == \"default\" {print \$3}\'')
.withAttachStdout(true)
.exec()
def out = new ByteArrayOutputStream()
def callback = loadClass('com.github.dockerjava.core.command.ExecStartResultCallback').newInstance(out, System.err)
client.execStartCmd(response.id).exec(callback).awaitCompletion()
out.toString().trim()
}
}
defaultPropertyValue("dockerHost", host)
ext.url = "http://${dockerHost}:${port}"
logger.lifecycle "Serving files on ${url}"
components.each { name ->
dcompose.services.getByName(name) {
buildArgs = [ 'URL_PREFIX': url ]
}
}
}
}
[pushDownloadServerImage, pushDseBaseImage]*.enabled = false
gradle.taskGraph.whenReady { g ->
if (g.hasTask(skipDownloads)) {
tasks.withType(Download) { enabled = false }
}
}
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)
}
}