forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.gradle
280 lines (242 loc) · 9 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
import org.opensearch.gradle.Architecture
import org.opensearch.gradle.DockerBase
import org.opensearch.gradle.LoggedExec
import org.opensearch.gradle.VersionProperties
import org.opensearch.gradle.docker.DockerBuildTask
import org.opensearch.gradle.info.BuildParams
import org.opensearch.gradle.testfixtures.TestFixturesPlugin
import org.gradle.internal.os.OperatingSystem
apply plugin: 'opensearch.standalone-rest-test'
apply plugin: 'opensearch.test.fixtures'
apply plugin: 'opensearch.internal-distribution-download'
apply plugin: 'opensearch.rest-resources'
testFixtures.useFixture()
configurations {
arm64DockerSource
s390xDockerSource
dockerSource
}
dependencies {
arm64DockerSource project(path: ":distribution:archives:linux-arm64-tar", configuration:"default")
s390xDockerSource project(path: ":distribution:archives:linux-s390x-tar", configuration:"default")
dockerSource project(path: ":distribution:archives:linux-tar", configuration:"default")
}
ext.expansions = { Architecture architecture, DockerBase base, boolean local ->
String classifier
if (local) {
if (architecture == Architecture.ARM64) {
classifier = "linux-arm64"
} else if (architecture == Architecture.X64) {
classifier = "linux-x64"
} else if (architecture == Architecture.S390X) {
classifier = "linux-s390x"
} else {
throw new IllegalArgumentException("Unsupported architecture [" + architecture + "]")
}
} else {
/* When sourcing the OpenSearch build remotely, the same Dockerfile needs
* to be able to fetch the artifact for any supported platform. We can't make
* the decision here. Bash will interpolate the `arch` command for us. */
classifier = "linux-\$(arch)"
}
final String opensearch = "opensearch-min-${VersionProperties.getOpenSearch()}-${classifier}.tar.gz"
/* Both the following Dockerfile commands put the resulting artifact at
* the same location, regardless of classifier, so that the commands that
* follow in the Dockerfile don't have to know about the runtime
* architecture. */
String sourceOpenSearch
if (local) {
sourceOpenSearch = "COPY $opensearch /opt/opensearch.tar.gz"
} else {
//TODO - replace the URL for OpenSearch when available
sourceOpenSearch = """
RUN curl --retry 8 -S -L \\
--output /opt/opensearch.tar.gz \\
<artifacts_link_placeholder>
"""
}
return [
'base_image' : base.getImage(),
'build_date' : BuildParams.buildDate,
'git_revision' : BuildParams.gitRevision,
'license' : 'Apache-2.0',
'package_manager' : 'yum',
'source_opensearch' : sourceOpenSearch,
'docker_base' : base.name().toLowerCase(),
'version' : VersionProperties.getOpenSearch()
]
}
private static String buildPath(Architecture architecture, DockerBase base) {
return 'build/' +
(architecture == Architecture.ARM64 ? 'arm64-' : '') +
(architecture == Architecture.S390X ? 's390x-' : '') +
'docker'
}
private static String taskName(String prefix, Architecture architecture, DockerBase base, String suffix) {
return prefix +
(architecture == Architecture.ARM64 ? 'Arm64' : '') +
(architecture == Architecture.S390X ? 'S390x' : '') +
suffix
}
project.ext {
dockerBuildContext = { Architecture architecture, DockerBase base, boolean local ->
copySpec {
into('bin') {
from project.projectDir.toPath().resolve("src/docker/bin")
}
into('config') {
from project.projectDir.toPath().resolve("src/docker/config")
}
from(project.projectDir.toPath().resolve("src/docker/Dockerfile")) {
expand(expansions(architecture, base, local))
}
}
}
}
void addCopyDockerContextTask(Architecture architecture, DockerBase base) {
if (base != DockerBase.CENTOS) {
throw new GradleException("The only allowed docker base image for builds is CENTOS")
}
tasks.register(taskName("copy", architecture, base, "DockerContext"), Sync) {
expansions(architecture, base, true).findAll { it.key != 'build_date' }.each { k, v ->
inputs.property(k, { v.toString() })
}
into buildPath(architecture, base)
with dockerBuildContext(architecture, base, true)
if (architecture == Architecture.ARM64) {
from configurations.arm64DockerSource
} else if (architecture == Architecture.S390X) {
from configurations.s390xDockerSource
} else {
from configurations.dockerSource
}
}
}
def createAndSetWritable(Object... locations) {
locations.each { location ->
File file = file(location)
file.mkdirs()
file.setWritable(true, false)
}
}
opensearch_distributions {
Architecture.values().each { eachArchitecture ->
"docker${ eachArchitecture == Architecture.ARM64 ? '_arm64' : '' }" {
architecture = eachArchitecture
type = 'docker'
version = VersionProperties.getOpenSearch()
failIfUnavailable = false // This ensures we don't attempt to build images if docker is unavailable
}
}
}
tasks.named("preProcessFixture").configure {
dependsOn opensearch_distributions.docker
// always run the task, otherwise the folders won't be created
outputs.upToDateWhen {
false
}
doLast {
// tests expect to have an empty repo
project.delete(
"${buildDir}/repo"
)
createAndSetWritable(
"${buildDir}/repo",
"${buildDir}/logs/1",
"${buildDir}/logs/2"
)
}
}
tasks.register("integTest", Test) {
outputs.doNotCacheIf('Build cache is disabled for Docker tests') { true }
maxParallelForks = '1'
include '**/*IT.class'
}
tasks.named("check").configure {
dependsOn "integTest"
}
void addBuildDockerImage(Architecture architecture, DockerBase base) {
if (base != DockerBase.CENTOS) {
throw new GradleException("The only allowed docker base image for builds is CENTOS")
}
final TaskProvider<DockerBuildTask> buildDockerImageTask =
tasks.register(taskName("build", architecture, base, "DockerImage"), DockerBuildTask) {
onlyIf { Architecture.current() == architecture && !OperatingSystem.current().isWindows() }
TaskProvider<Sync> copyContextTask = tasks.named(taskName("copy", architecture, base, "DockerContext"))
dependsOn(copyContextTask)
dockerContext.fileProvider(copyContextTask.map { it.destinationDir })
baseImages = [ base.getImage() ]
String version = VersionProperties.getOpenSearch()
tags = [
"docker.opensearch.org/opensearch:${version}",
"opensearch:test"
]
}
tasks.named("assemble").configure {
dependsOn(buildDockerImageTask)
}
}
for (final Architecture architecture : Architecture.values()) {
// We only create Docker images for the distribution on CentOS.
for (final DockerBase base : DockerBase.values()) {
if (base == DockerBase.CENTOS) {
addCopyDockerContextTask(architecture, base)
addBuildDockerImage(architecture, base)
}
}
}
// We build the images used in compose locally, but the pull command insists on using a repository
// thus we must disable it to prevent it from doing so.
// Everything will still be pulled since we will build the local images on a pull
if (tasks.findByName("composePull")) {
tasks.composePull.enabled = false
}
/*
* The export subprojects write out the generated Docker images to disk, so
* that they can be easily reloaded, for example into a VM for distribution testing
*/
subprojects { Project subProject ->
if (subProject.name.endsWith('-export')) {
apply plugin: 'distribution'
final Architecture architecture = subProject.name.contains('arm64-') ? Architecture.ARM64 : Architecture.X64
final DockerBase base = DockerBase.CENTOS
final String arch = architecture == Architecture.ARM64 ? '-arm64' : ''
final String extension = 'docker.tar'
final String artifactName = "opensearch${arch}_test"
final String exportTaskName = taskName("export", architecture, base, "DockerImage")
final String buildTaskName = taskName("build", architecture, base, "DockerImage")
final String tarFile = "${parent.projectDir}/build/${artifactName}_${VersionProperties.getOpenSearch()}.${extension}"
tasks.register(exportTaskName, LoggedExec) {
inputs.file("${parent.projectDir}/build/markers/${buildTaskName}.marker")
executable 'docker'
outputs.file(tarFile)
args "save",
"-o",
tarFile,
"opensearch:test"
dependsOn(parent.path + ":" + buildTaskName)
onlyIf { Architecture.current() == architecture && !OperatingSystem.current().isWindows()}
}
artifacts.add('default', file(tarFile)) {
type 'tar'
name artifactName
builtBy exportTaskName
}
tasks.named("assemble").configure {
dependsOn(exportTaskName)
}
}
}
tasks.named("composeUp").configure {
dependsOn preProcessFixture
}