forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle
637 lines (571 loc) · 22.7 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/*
* 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.
*/
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you 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.
*/
import org.apache.tools.ant.filters.FixCrLfFilter
import org.opensearch.gradle.ConcatFilesTask
import org.opensearch.gradle.DependenciesInfoTask
import org.opensearch.gradle.MavenFilteringHack
import org.opensearch.gradle.NoticeTask
import org.opensearch.gradle.VersionProperties
import org.opensearch.gradle.info.BuildParams
import java.nio.file.Files
import java.nio.file.Path
plugins {
id 'base'
}
/*****************************************************************************
* Third party dependencies report *
*****************************************************************************/
// Concatenates the dependencies CSV files into a single file
tasks.register("generateDependenciesReport", ConcatFilesTask) {
dependsOn rootProject.allprojects.collect { it.tasks.withType(DependenciesInfoTask) }
files = fileTree(dir: project.rootDir, include: '**/dependencies.csv')
headerLine = "name,version,url,license,sourceURL"
target = new File(System.getProperty('csv') ?: "${project.buildDir}/reports/dependencies/opensearch-dependencies.csv")
// explicitly add our dependency on the JDK
String jdkVersion = VersionProperties.versions.get('bundled_jdk')
String jdkMajorVersion = jdkVersion.split('[+.]')[0]
String sourceUrl = "https://hg.openjdk.java.net/jdk-updates/jdk${jdkMajorVersion}u/archive/jdk-${jdkVersion}.tar.gz"
additionalLines << "OpenJDK,${jdkVersion},https://openjdk.java.net/,GPL-2.0-with-classpath-exception,${sourceUrl}".toString()
}
/*****************************************************************************
* Notice file *
*****************************************************************************/
// integ test zip only uses server, so a different notice file is needed there
def buildServerNoticeTaskProvider = tasks.register("buildServerNotice", NoticeTask)
def buildNoticeTaskProvider = tasks.register("buildNotice", NoticeTask) {
licensesDir new File(project(':distribution').projectDir, 'licenses')
}
def buildNoJdkNoticeTaskProvider = tasks.register("buildNoJdkNotice", NoticeTask)
// The :server and :libs projects belong to all distributions
tasks.withType(NoticeTask).configureEach {
licensesDir project(':server').file('licenses')
source project(':server').file('src/main/java')
project(':libs').subprojects.each { Project lib ->
licensesDir lib.file('licenses')
source lib.file('src/main/java')
}
}
/*****************************************************************************
* Modules *
*****************************************************************************/
String distOutputs = 'build/outputs/dist'
String systemdOutputs = 'build/outputs/systemd'
String transportOutputs = 'build/outputs/transport-only'
String externalTestOutputs = 'build/outputs/external-test'
def processOutputsTaskProvider = tasks.register("processOutputs", Sync) {
into distOutputs
}
def processSystemdOutputsTaskProvider = tasks.register("processSystemdOutputs", Sync) {
into systemdOutputs
}
def processExternalTestOutputsTaskProvider = tasks.register("processExternalTestOutputs", Sync) {
into externalTestOutputs
}
// Integ tests work over the rest http layer, so we need a transport included with the integ test zip.
// All transport modules are included so that they may be randomized for testing
def processTransportOutputsTaskProvider = tasks.register("processTransportOutputs", Sync) {
into transportOutputs
}
// these are dummy tasks that can be used to depend on the relevant sub output dir
def buildModulesTaskProvider = tasks.register("buildModules") {
dependsOn processOutputsTaskProvider
outputs.dir "${distOutputs}/modules"
}
tasks.register("buildBin") {
dependsOn "processOutputs"
outputs.dir "${distOutputs}/bin"
}
tasks.register("buildConfig") {
dependsOn "processOutputs"
outputs.dir "${distOutputs}/config"
}
def buildSystemdModuleTaskProvider = tasks.register("buildSystemdModule") {
dependsOn "processSystemdOutputs"
outputs.dir "${systemdOutputs}/modules"
}
def buildTransportModulesTaskProvider = tasks.register("buildTransportModules") {
dependsOn processTransportOutputsTaskProvider
outputs.dir "${transportOutputs}/modules"
}
def buildExternalTestModulesTaskProvider = tasks.register("buildExternalTestModules") {
dependsOn "processExternalTestOutputs"
outputs.dir "${externalTestOutputs}/modules"
}
Configuration moduleZip(Project module) {
Dependency dep = project.dependencies.project(path: module.path, configuration: 'zip')
Configuration config = project.configurations.detachedConfiguration(dep)
return config
}
void copyModule(TaskProvider<Sync> copyTask, Project module) {
copyTask.configure {
Configuration moduleConfig = moduleZip(module)
dependsOn moduleConfig
from({ zipTree(moduleConfig.singleFile) }) {
includeEmptyDirs false
// these are handled separately in the log4j config tasks below
exclude '*/config/log4j2.properties'
exclude 'config/log4j2.properties'
eachFile { details ->
String name = module.plugins.hasPlugin('opensearch.opensearchplugin') ? module.opensearchplugin.name : module.opensearch_meta_plugin.name
// Copy all non config/bin files
// Note these might be unde a subdirectory in the case of a meta plugin
if ((details.relativePath.pathString ==~ /([^\/]+\/)?(config|bin)\/.*/) == false) {
details.relativePath = details.relativePath.prepend('modules', name)
} else if ((details.relativePath.pathString ==~ /([^\/]+\/)(config|bin)\/.*/)) {
// this is the meta plugin case, in which we need to remove the intermediate dir
String[] segments = details.relativePath.segments
details.relativePath = new RelativePath(true, segments.takeRight(segments.length - 1))
}
}
}
}
}
// log4j config could be contained in modules, so we must join it together using these tasks
def buildLog4jConfigTaskProvider = tasks.register("buildLog4jConfig") {
dependsOn "processOutputs"
ext.contents = []
ext.log4jFile = file("${distOutputs}/log4j2.properties")
outputs.file log4jFile
}
Closure writeLog4jProperties = {
String mainLog4jProperties = file('src/config/log4j2.properties').getText('UTF-8')
it.log4jFile.setText(mainLog4jProperties, 'UTF-8')
for (String moduleLog4jProperties : it.contents.reverse()) {
it.log4jFile.append(moduleLog4jProperties, 'UTF-8')
}
}
buildLog4jConfigTaskProvider.configure {
doLast(writeLog4jProperties)
}
// copy log4j2.properties from modules that have it
void copyLog4jProperties(TaskProvider buildTask, Project module) {
buildTask.configure {
Configuration moduleConfig = moduleZip(module)
dependsOn moduleConfig
doFirst {
FileTree tree = zipTree(moduleConfig.singleFile)
FileTree filtered = tree.matching {
include 'config/log4j2.properties'
include '*/config/log4j2.properties' // could be in a bundled plugin
}
if (filtered.isEmpty() == false) {
contents.add('\n\n' + filtered.singleFile.getText('UTF-8'))
}
}
}
}
ext.restTestExpansions = [
'expected.modules.count': 0,
]
// we create the buildModules task above but fill it here so we can do a single
// loop over modules to also setup cross task dependencies and increment our modules counter
project.rootProject.subprojects.findAll { it.parent.path == ':modules' }.each { Project module ->
if (module.name == 'systemd') {
// the systemd module is only included in the package distributions or in linux and freebsd archives
return
}
File licenses = new File(module.projectDir, 'licenses')
if (licenses.exists()) {
buildNotice.configure {
licensesDir licenses
source module.file('src/main/java')
}
}
copyModule(processOutputsTaskProvider, module)
if (module.name.startsWith('transport-')) {
copyModule(processTransportOutputsTaskProvider, module)
}
copyLog4jProperties(buildLog4jConfigTaskProvider, module)
restTestExpansions['expected.modules.count'] += 1
}
// copy all sandbox modules if the distribution is a snapshot
if (VersionProperties.isOpenSearchSnapshot()) {
Properties sysProps = System.getProperties();
// setting this property to false will exclude the sandbox modules from the distribution
final String enableSandbox = sysProps.getProperty("sandbox.enabled", "true");
if(sysProps != null && enableSandbox == "true") {
tasks.withType(NoticeTask).configureEach {
project(':sandbox:libs').subprojects.each { Project lib ->
licensesDir lib.file('licenses')
source lib.file('src/main/java')
}
}
project.rootProject.subprojects.findAll { it.parent.path == ':sandbox:modules' }.each { Project module ->
File licenses = new File(module.projectDir, 'licenses')
if (licenses.exists()) {
buildNotice.configure {
licensesDir licenses
source module.file('src/main/java')
}
}
copyModule(processOutputsTaskProvider, module)
copyLog4jProperties(buildLog4jConfigTaskProvider, module)
restTestExpansions['expected.modules.count'] += 1
}
}
}
copyModule(processSystemdOutputsTaskProvider, project(':modules:systemd'))
project(':test:external-modules').subprojects.each { Project testModule ->
copyModule(processExternalTestOutputsTaskProvider, testModule)
}
configure(subprojects.findAll { ['archives', 'packages'].contains(it.name) }) {
apply plugin: 'opensearch.jdk-download'
apply plugin: 'opensearch.repositories'
// Setup all required JDKs
project.jdks {
['darwin', 'linux', 'windows'].each { platform ->
(platform == 'linux' || platform == 'darwin' ? ['x64', 'aarch64', 's390x'] : ['x64']).each { architecture ->
"bundled_${platform}_${architecture}" {
it.platform = platform
it.version = VersionProperties.getBundledJdk(platform)
it.vendor = VersionProperties.bundledJdkVendor
it.architecture = architecture
}
}
}
}
// TODO: the map needs to be an input of the tasks, so that when it changes, the task will re-run...
/*****************************************************************************
* Properties to expand when copying packaging files *
*****************************************************************************/
configurations {
['libs', 'libsPluginCli', 'libsKeystoreCli', 'libsUpgradeCli'].each {
create(it) {
canBeConsumed = false
canBeResolved = true
attributes {
attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category, Category.LIBRARY))
attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage, Usage.JAVA_RUNTIME))
attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling, Bundling.EXTERNAL))
}
}
}
}
dependencies {
libs project(':server')
libs project(':libs:opensearch-plugin-classloader')
libs project(':distribution:tools:java-version-checker')
libs project(':distribution:tools:launchers')
libsPluginCli project(':distribution:tools:plugin-cli')
libsKeystoreCli project(path: ':distribution:tools:keystore-cli')
libsUpgradeCli project(path: ':distribution:tools:upgrade-cli')
}
project.ext {
/*****************************************************************************
* Common files in all distributions *
*****************************************************************************/
libFiles = {
copySpec {
// delay by using closures, since they have not yet been configured, so no jar task exists yet
from(configurations.libs)
into('tools/plugin-cli') {
from(configurations.libsPluginCli)
}
into('tools/keystore-cli') {
from(configurations.libsKeystoreCli)
}
into('tools/upgrade-cli') {
from(configurations.libsUpgradeCli)
}
}
}
modulesFiles = { platform ->
copySpec {
eachFile {
if (it.relativePath.segments[-2] == 'bin' || (platform == 'darwin-x64' && it.relativePath.segments[-2] == 'MacOS')) {
// bin files, wherever they are within modules (eg platform specific) should be executable
// and MacOS is an alternative to bin on macOS
it.mode = 0755
} else {
it.mode = 0644
}
}
def buildModules = buildModulesTaskProvider
List excludePlatforms = ['darwin-x64', 'freebsd-x64', 'linux-x64', 'linux-arm64', 'linux-s390x', 'windows-x64', 'darwin-arm64']
if (platform != null) {
excludePlatforms.remove(excludePlatforms.indexOf(platform))
} else {
excludePlatforms = []
}
from(buildModules) {
for (String excludePlatform : excludePlatforms) {
exclude "**/platform/${excludePlatform}/**"
}
}
if (BuildParams.isSnapshotBuild()) {
from(buildExternalTestModulesTaskProvider)
}
if (project.path.startsWith(':distribution:packages') || ['freebsd-x64','linux-x64', 'linux-arm64'].contains(platform)) {
from(buildSystemdModuleTaskProvider)
}
}
}
transportModulesFiles = copySpec {
from buildTransportModulesTaskProvider
}
configFiles = { distributionType, jdk ->
copySpec {
with copySpec {
// main config files, processed with distribution specific substitutions
from '../src/config'
exclude 'log4j2.properties' // this is handled separately below
MavenFilteringHack.filter(it, expansionsForDistribution(distributionType, jdk))
}
from project(':distribution').buildLog4jConfig
from project(':distribution').buildConfig
}
}
binFiles = { distributionType, jdk ->
copySpec {
// non-windows files, for all distributions
with copySpec {
from '../src/bin'
exclude '*.exe'
exclude '*.bat'
eachFile { it.setMode(0755) }
MavenFilteringHack.filter(it, expansionsForDistribution(distributionType, jdk))
}
// windows files, only for zip
if (distributionType == 'zip') {
with copySpec {
from '../src/bin'
include '*.bat'
filter(FixCrLfFilter, eol: FixCrLfFilter.CrLf.newInstance('crlf'))
MavenFilteringHack.filter(it, expansionsForDistribution(distributionType, jdk))
}
with copySpec {
from '../src/bin'
include '*.exe'
}
}
// module provided bin files
with copySpec {
eachFile { it.setMode(0755) }
from project(':distribution').buildBin
if (distributionType != 'zip') {
exclude '*.bat'
}
}
}
}
noticeFile = { jdk ->
copySpec {
if (project.name == 'integ-test-zip') {
from buildServerNoticeTaskProvider
} else {
if (jdk) {
from buildNoticeTaskProvider
} else {
from buildNoJdkNoticeTaskProvider
}
}
}
}
jdkFiles = { Project project, String platform, String architecture ->
return copySpec {
/*
* Jdk uses aarch64 from ARM. Translating from arm64 to aarch64 which Jdk understands.
*/
if ("arm64".equals(architecture)) {
architecture = "aarch64"
}
from project.jdks."bundled_${platform}_${architecture}"
exclude "demo/**"
/*
* The Contents/MacOS directory interferes with notarization, and is unused by our distribution, so we exclude
* it from the build.
*/
if ("darwin".equals(platform)) {
exclude "Contents/MacOS"
}
eachFile { FileCopyDetails details ->
if (details.relativePath.segments[-2] == 'bin' || details.relativePath.segments[-1] == 'jspawnhelper') {
details.mode = 0755
}
if (details.name == 'src.zip') {
details.exclude()
}
}
}
}
}
}
/**
* Build some variables that are replaced in the packages. This includes both
* scripts like bin/opensearch and bin/opensearch-plugin that a user might run and also
* scripts like postinst which are run as part of the installation.
*
* <dl>
* <dt>package.name</dt>
* <dd>The name of the project. Its sprinkled throughout the scripts.</dd>
* <dt>package.version</dt>
* <dd>The version of the project. Its mostly used to find the exact jar name.
* </dt>
* <dt>path.conf</dt>
* <dd>The default directory from which to load configuration. This is used in
* the packaging scripts, but in that context it is always
* /etc/opensearch. Its also used in bin/opensearch-plugin, where it is
* /etc/opensearch for the os packages but $OPENSEARCH_HOME/config otherwise.</dd>
* <dt>path.env</dt>
* <dd>The env file sourced before bin/opensearch to set environment
* variables. Think /etc/defaults/opensearch.</dd>
* <dt>heap.min and heap.max</dt>
* <dd>Default min and max heap</dd>
* <dt>scripts.footer</dt>
* <dd>Footer appended to control scripts embedded in the distribution that is
* (almost) entirely there for cosmetic reasons.</dd>
* <dt>stopping.timeout</dt>
* <dd>RPM's init script needs to wait for opensearch to stop before
* returning from stop and it needs a maximum time to wait. This is it. One
* day. DEB retries forever.</dd>
* </dl>
*/
subprojects {
ext.expansionsForDistribution = { distributionType, jdk ->
final String defaultHeapSize = "1g"
final String packagingPathData = "path.data: /var/lib/opensearch"
final String pathLogs = "/var/log/opensearch"
final String packagingPathLogs = "path.logs: ${pathLogs}"
final String packagingLoggc = "${pathLogs}/gc.log"
String licenseText = rootProject.file('licenses/APACHE-LICENSE-2.0.txt').getText('UTF-8')
// license text needs to be indented with a single space
licenseText = ' ' + licenseText.replace('\n', '\n ')
String footer = "# Built for ${project.name}-${project.version} " +
"(${distributionType})"
Map<String, Object> expansions = [
'project.name': project.name,
'project.version': version,
'path.conf': [
'deb': '/etc/opensearch',
'rpm': '/etc/opensearch',
'def': '"$OPENSEARCH_HOME"/config'
],
'path.data': [
'deb': packagingPathData,
'rpm': packagingPathData,
'def': '#path.data: /path/to/data'
],
'path.env': [
'deb': '/etc/default/opensearch',
'rpm': '/etc/sysconfig/opensearch',
/* There isn't one of these files for tar or zip but its important to
make an empty string here so the script can properly skip it. */
'def': 'if [ -z "$OPENSEARCH_PATH_CONF" ]; then OPENSEARCH_PATH_CONF="$OPENSEARCH_HOME"/config; done',
],
'source.path.env': [
'deb': 'source /etc/default/opensearch',
'rpm': 'source /etc/sysconfig/opensearch',
'def': 'if [ -z "$OPENSEARCH_PATH_CONF" ]; then OPENSEARCH_PATH_CONF="$OPENSEARCH_HOME"/config; fi',
],
'path.logs': [
'deb': packagingPathLogs,
'rpm': packagingPathLogs,
'def': '#path.logs: /path/to/logs'
],
'loggc': [
'deb': packagingLoggc,
'rpm': packagingLoggc,
'def': 'logs/gc.log'
],
'heap.min': defaultHeapSize,
'heap.max': defaultHeapSize,
'heap.dump.path': [
'deb': "-XX:HeapDumpPath=/var/lib/opensearch",
'rpm': "-XX:HeapDumpPath=/var/lib/opensearch",
'def': "-XX:HeapDumpPath=data"
],
'error.file': [
'deb': "-XX:ErrorFile=/var/log/opensearch/hs_err_pid%p.log",
'rpm': "-XX:ErrorFile=/var/log/opensearch/hs_err_pid%p.log",
'def': "-XX:ErrorFile=logs/hs_err_pid%p.log"
],
'stopping.timeout': [
'rpm': 86400,
],
'scripts.footer': [
/* Debian needs exit 0 on these scripts so we add it here and preserve
the pretty footer. */
'deb': "exit 0\n${footer}",
'def': footer
],
'opensearch.distribution.type': [
'deb': 'deb',
'rpm': 'rpm',
'tar': 'tar',
'zip': 'zip'
],
'opensearch.bundled_jdk': [
'def': jdk ? 'true' : 'false'
],
'license.name': [
'deb': 'ASL-2.0'
],
'license.text': [
'deb': licenseText,
],
]
Map<String, String> result = [:]
expansions = expansions.each { key, value ->
if (value instanceof Map) {
// 'def' is for default but its three characters like 'rpm' and 'deb'
value = value[distributionType] ?: value['def']
if (value == null) {
return
}
}
result[key] = value
}
return result
}
ext.assertLinesInFile = { Path path, List<String> expectedLines ->
final List<String> actualLines = Files.readAllLines(path)
int line = 0
for (final String expectedLine : expectedLines) {
final String actualLine = actualLines.get(line)
if (expectedLine != actualLine) {
throw new GradleException("expected line [${line + 1}] in [${path}] to be [${expectedLine}] but was [${actualLine}]")
}
line++
}
}
}
['archives:darwin-tar',
'archives:integ-test-zip',
'archives:linux-arm64-tar',
'archives:linux-tar',
'archives:windows-zip',
'packages:arm64-rpm', 'packages:arm64-deb',
'packages:rpm', 'packages:deb'
].forEach { subName ->
Project subproject = project("${project.path}:${subName}")
Configuration configuration = configurations.create(subproject.name)
dependencies {
"${configuration.name}" project(path: subproject.path, configuration:'default')
}
}