forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 22
/
build.gradle
569 lines (511 loc) · 20 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
/*
* 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
*/
import org.opensearch.gradle.LoggedExec
import org.opensearch.gradle.MavenFilteringHack
import org.opensearch.gradle.OS
import org.opensearch.gradle.info.BuildParams
import org.redline_rpm.header.Flags
import java.nio.file.Files
import java.nio.file.Path
import java.util.regex.Matcher
import java.util.regex.Pattern
/*****************************************************************************
* Deb and rpm configuration *
*****************************************************************************
*
* The general strategy here is to build a directory on disk that contains
* stuff that needs to be copied into the distributions. This is
* important for two reasons:
* 1. ospackage wants to copy the directory permissions that it sees off of the
* filesystem. If you ask it to create a directory that doesn't already
* exist on disk it petulantly creates it with 0755 permissions, no matter
* how hard you try to convince it otherwise.
* 2. Convincing ospackage to pick up an empty directory as part of a set of
* directories on disk is reasonably easy. Convincing it to just create an
* empty directory requires more wits than I have.
* 3. ospackage really wants to suck up some of the debian control scripts
* directly from the filesystem. It doesn't want to process them through
* MavenFilteringHack or any other copy-style action.
*
* The following commands are useful when it comes to check the user/group
* and files permissions set within the RPM and DEB packages:
*
* rpm -qlp --dump path/to/wazuh-indexer.rpm
* dpkg -c path/to/wazuh-indexer.deb
*/
plugins {
id "com.netflix.nebula.ospackage-base" version "11.8.1"
}
void addProcessFilesTask(String type, boolean jdk) {
String packagingFiles = "build/packaging/${jdk ? '' : 'no-jdk-'}${type}"
String taskName = "process'${jdk ? '' : 'NoJdk'}${type.capitalize()}Files"
tasks.register(taskName, Copy) {
into packagingFiles
with copySpec {
from 'src/common'
from "src/${type}"
MavenFilteringHack.filter(it, expansionsForDistribution(type, jdk))
}
into('etc/wazuh-indexer') {
with configFiles(type, jdk)
}
MavenFilteringHack.filter(it, expansionsForDistribution(type, jdk))
doLast {
// create empty dirs, we set the permissions when configuring the packages
mkdir "${packagingFiles}/var/log/wazuh-indexer"
mkdir "${packagingFiles}/var/lib/wazuh-indexer"
mkdir "${packagingFiles}/usr/share/wazuh-indexer/plugins"
// bare empty dir for /etc/wazuh-indexer and /etc/wazuh-indexer/jvm.options.d
mkdir "${packagingFiles}/wazuh-indexer"
mkdir "${packagingFiles}/wazuh-indexer/jvm.options.d"
}
}
}
addProcessFilesTask('deb', true)
addProcessFilesTask('deb', false)
addProcessFilesTask('rpm', true)
addProcessFilesTask('rpm', false)
// Common configuration that is package dependent. This can't go in ospackage
// since we have different templated files that need to be consumed, but the structure
// is the same
Closure commonPackageConfig(String type, boolean jdk, String architecture) {
project.version = rootProject.file('VERSION').getText()
return {
onlyIf {
OS.current().equals(OS.WINDOWS) == false
}
dependsOn "process'${jdk ? '' : 'NoJdk'}${type.capitalize()}Files"
packageName "wazuh-indexer"
if (type == 'deb') {
if (architecture == 'x64') {
arch('amd64')
} else {
assert architecture == 'arm64' : architecture
arch('arm64')
}
} else {
assert type == 'rpm' : type
if (architecture == 'x64') {
arch('x86_64')
} else {
assert architecture == 'arm64' : architecture
arch('aarch64')
}
}
// Follow opensearch's file naming convention
String jdkString = jdk ? "" : "-no-jdk"
String prefix = "${architecture == 'arm64' ? 'arm64-' : ''}${jdk ? '' : 'no-jdk-'}${type}"
destinationDirectory = file("${prefix}/build/distributions")
// SystemPackagingTask overrides default archive task convention mappings, but doesn't provide a setter so we have to override the convention mapping itself
// Deb convention uses a '_' for final separator before architecture, rpm uses a '.'
if (type == 'deb') {
archiveFileName.value(project.provider({ "${destinationDirectory.get()}/${packageName}-min_${project.version}${jdkString}_${archString}.${type}" }))
} else {
archiveFileName.value(project.provider({ "${destinationDirectory.get()}/${packageName}-min-${project.version}${jdkString}.${archString}.${type}" }))
}
String packagingFiles = "build/packaging/${jdk ? '' : 'no-jdk-'}${type}"
String scripts = "${packagingFiles}/scripts"
preInstall file("${scripts}/preinst")
postInstall file("${scripts}/postinst")
preUninstall file("${scripts}/prerm")
postUninstall file("${scripts}/postrm")
if (type == 'rpm') {
postTrans file("${scripts}/posttrans")
}
// top level "into" directive is not inherited from ospackage for some reason, so we must
// specify it again explicitly for copying common files
into('/usr/share/wazuh-indexer') {
into('bin') {
with binFiles(type, jdk)
}
from(rootProject.projectDir) {
include 'README.md'
fileMode 0644
}
into('lib') {
with libFiles()
}
into('modules') {
with modulesFiles('linux-' + ((architecture == 'x64') ? 'x64' : architecture))
}
if (jdk) {
into('jdk') {
with jdkFiles(project, 'linux', architecture)
}
}
into ('') {
with versionFile()
}
// we need to specify every intermediate directory in these paths so the package managers know they are explicitly
// intended to manage them; otherwise they may be left behind on uninstallation. duplicate calls of the same
// directory are fine
eachFile { FileCopyDetails fcp ->
String[] segments = fcp.relativePath.segments
for (int i = segments.length - 2; i > 2; --i) {
if (type == 'rpm') {
directory('/' + segments[0..i].join('/'), 0755)
}
if (segments[-2] == 'bin' || segments[-1] == 'jspawnhelper') {
fcp.mode = 0755
} else {
fcp.mode = 0644
}
}
}
}
// license files
if (type == 'deb') {
into("/usr/share/doc/${packageName}") {
from "${packagingFiles}/copyright"
fileMode 0644
}
} else {
assert type == 'rpm'
into('/usr/share/wazuh-indexer') {
from(rootProject.file('licenses')) {
include 'APACHE-LICENSE-2.0.txt'
rename { 'LICENSE.txt' }
}
fileMode 0644
}
}
// ========= config files =========
configurationFile '/etc/wazuh-indexer/opensearch.yml'
configurationFile '/etc/wazuh-indexer/jvm.options'
configurationFile '/etc/wazuh-indexer/log4j2.properties'
from("${packagingFiles}") {
dirMode 0750
into('/etc')
permissionGroup 'wazuh-indexer'
includeEmptyDirs true
createDirectoryEntry true
include("wazuh-indexer") // empty dir, just to add directory entry
include("wazuh-indexer/jvm.options.d") // empty dir, just to add directory entry
}
from("${packagingFiles}/etc/wazuh-indexer") {
into('/etc/wazuh-indexer')
dirMode 0750
fileMode 0660
permissionGroup 'wazuh-indexer'
includeEmptyDirs true
createDirectoryEntry true
fileType CONFIG | NOREPLACE
}
String envFile = expansionsForDistribution(type, jdk)['path.env']
configurationFile envFile
into(new File(envFile).getParent()) {
fileType CONFIG | NOREPLACE
permissionGroup 'wazuh-indexer'
fileMode 0660
from "${packagingFiles}/env/wazuh-indexer"
}
// ========= systemd =========
into('/usr/lib/tmpfiles.d') {
from "${packagingFiles}/systemd/wazuh-indexer.conf"
fileMode 0644
}
into('/usr/lib/systemd/system') {
fileType CONFIG | NOREPLACE
from "${packagingFiles}/systemd/wazuh-indexer.service"
fileMode 0644
}
into('/usr/lib/sysctl.d') {
fileType CONFIG | NOREPLACE
from "${packagingFiles}/systemd/sysctl/wazuh-indexer.conf"
fileMode 0644
}
into('/usr/share/wazuh-indexer/bin') {
from "${packagingFiles}/systemd/systemd-entrypoint"
fileMode 0755
}
// ========= sysV init =========
configurationFile '/etc/init.d/wazuh-indexer'
into('/etc/init.d') {
fileMode 0750
fileType CONFIG | NOREPLACE
from "${packagingFiles}/init.d/wazuh-indexer"
}
// ========= empty dirs =========
// NOTE: these are created under packagingFiles as empty, but the permissions are set here
Closure copyEmptyDir = { path, u, g, mode ->
File file = new File(path)
into(file.parent) {
from "${packagingFiles}/${file.parent}"
include file.name
includeEmptyDirs true
createDirectoryEntry true
user u
permissionGroup g
dirMode mode
}
}
copyEmptyDir('/var/log/wazuh-indexer', 'wazuh-indexer', 'wazuh-indexer', 0750)
copyEmptyDir('/var/lib/wazuh-indexer', 'wazuh-indexer', 'wazuh-indexer', 0750)
copyEmptyDir('/usr/share/wazuh-indexer/plugins', 'root', 'root', 0755)
into '/usr/share/wazuh-indexer'
with noticeFile(jdk)
}
}
apply plugin: 'com.netflix.nebula.ospackage-base'
// this is package indepdendent configuration
ospackage {
maintainer 'Wazuh, Inc <info@wazuh.com>'
summary 'Distributed RESTful search engine built for the cloud'
packageDescription '''
Reference documentation can be found at
https://documentation.wazuh.com/current/getting-started/components/wazuh-indexer.html
'''.stripIndent().trim()
url 'https://documentation.wazuh.com/current/getting-started/components/wazuh-indexer.html'
// signing setup
if (project.hasProperty('signing.password') && BuildParams.isSnapshotBuild() == false) {
signingKeyId = project.hasProperty('signing.keyId') ? project.property('signing.keyId') : 'D88E42B4'
signingKeyPassphrase = project.property('signing.password')
signingKeyRingFile = project.hasProperty('signing.secretKeyRingFile') ?
project.file(project.property('signing.secretKeyRingFile')) :
new File(new File(System.getProperty('user.home'), '.gnupg'), 'secring.gpg')
}
// version found on oldest supported distro, centos-6
requires('coreutils', '8.4', GREATER | EQUAL)
fileMode 0644
dirMode 0755
user 'root'
permissionGroup 'root'
into '/usr/share/wazuh-indexer'
}
Closure commonDebConfig(boolean jdk, String architecture) {
return {
configure(commonPackageConfig('deb', jdk, architecture))
// jdeb does not provide a way to set the License control attribute, and ospackage
// silently ignores setting it. Instead, we set the license as "custom field"
customFields['License'] = 'ASL-2.0'
archiveVersion = project.version.replace('-', '~')
packageGroup 'web'
// versions found on oldest supported distro, centos-6
requires('bash', '4.1', GREATER | EQUAL)
requires('lsb-base', '4', GREATER | EQUAL)
requires 'libc6'
requires 'adduser'
}
}
tasks.register('buildArm64Deb', Deb) {
configure(commonDebConfig(true, 'arm64'))
}
tasks.register('buildNoJdkArm64Deb', Deb) {
configure(commonDebConfig(false, 'arm64'))
}
tasks.register('buildDeb', Deb) {
configure(commonDebConfig(true, 'x64'))
}
tasks.register('buildNoJdkDeb', Deb) {
configure(commonDebConfig(true, 'x64'))
}
Closure commonRpmConfig(boolean jdk, String architecture) {
return {
configure(commonPackageConfig('rpm', jdk, architecture))
license 'ASL-2.0'
packageGroup 'Application/Internet'
requires '/bin/bash'
obsoletes packageName, '7.0.0', Flags.LESS
prefix '/usr'
packager 'OpenSearch'
archiveVersion = project.version.replace('-', '_')
release = '1'
os 'LINUX'
distribution 'OpenSearch'
vendor 'OpenSearch'
// TODO ospackage doesn't support icon but we used to have one
// without this the rpm will have parent dirs of any files we copy in, eg /etc/wazuh-indexer
addParentDirs false
}
}
tasks.register('buildArm64Rpm', Rpm) {
configure(commonRpmConfig(true, 'arm64'))
}
tasks.register('buildNoJdkArm64Rpm', Rpm) {
configure(commonRpmConfig(false, 'arm64'))
}
tasks.register('buildRpm', Rpm) {
configure(commonRpmConfig(true, 'x64'))
}
tasks.register('buildNoJdkRpm', Rpm) {
configure(commonRpmConfig(false, 'x64'))
}
Closure dpkgExists = { it -> new File('/bin/dpkg-deb').exists() || new File('/usr/bin/dpkg-deb').exists() || new File('/usr/local/bin/dpkg-deb').exists() }
Closure rpmExists = { it -> new File('/bin/rpm').exists() || new File('/usr/bin/rpm').exists() || new File('/usr/local/bin/rpm').exists() }
Closure debFilter = { f -> f.name.endsWith('.deb') }
// This configures the default artifact for the distribution specific
// subprojects. We have subprojects because Gradle project substitutions
// can only bind to the default configuration of a project
subprojects {
apply plugin: 'distribution'
String buildTask = "build${it.name.replaceAll(/-[a-z]/) { it.substring(1).toUpperCase() }.capitalize()}"
ext.buildDist = parent.tasks.named(buildTask)
artifacts {
'default' buildDist
}
if (dpkgExists() || rpmExists()) {
// sanity checks if packages can be extracted
final File extractionDir = new File(buildDir, 'extracted')
File packageExtractionDir
if (project.name.contains('deb')) {
packageExtractionDir = new File(extractionDir, 'deb-extracted')
} else {
assert project.name.contains('rpm')
packageExtractionDir = new File(extractionDir, 'rpm-extracted')
}
tasks.register('checkExtraction', LoggedExec) {
dependsOn buildDist
doFirst {
delete(extractionDir)
extractionDir.mkdirs()
}
}
tasks.named("check").configure { dependsOn "checkExtraction" }
if (project.name.contains('deb')) {
tasks.named("checkExtraction").configure {
onlyIf dpkgExists
commandLine 'dpkg-deb', '-x', "${-> buildDist.get().outputs.files.filter(debFilter).singleFile}", packageExtractionDir
}
} else {
assert project.name.contains('rpm')
tasks.named("checkExtraction").configure {
onlyIf rpmExists
final File rpmDatabase = new File(extractionDir, 'rpm-database')
commandLine 'rpm',
'--badreloc',
'--ignorearch',
'--ignoreos',
'--nodeps',
'--noscripts',
'--notriggers',
'--dbpath',
rpmDatabase,
'--relocate',
"/=${packageExtractionDir}",
'-i',
"${-> buildDist.get().outputs.files.singleFile}"
}
}
tasks.register("checkLicense") {
dependsOn buildDist, "checkExtraction"
}
check.dependsOn "checkLicense"
if (project.name.contains('deb')) {
tasks.named("checkLicense").configure {
onlyIf dpkgExists
doLast {
Path copyrightPath
String expectedLicense
String licenseFilename
copyrightPath = packageExtractionDir.toPath().resolve("usr/share/doc/wazuh-indexer/copyright")
expectedLicense = "ASL-2.0"
licenseFilename = "APACHE-LICENSE-2.0.txt"
final List<String> header = Arrays.asList("Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/",
"Copyright: Elasticsearch B.V. <info@elastic.co>",
"Copyright: OpenSearch Contributors",
"License: " + expectedLicense)
final List<String> licenseLines = Files.readAllLines(rootDir.toPath().resolve("licenses/" + licenseFilename))
final List<String> expectedLines = header + licenseLines.collect { " " + it }
assertLinesInFile(copyrightPath, expectedLines)
}
}
} else {
assert project.name.contains('rpm')
tasks.named("checkLicense").configure {
onlyIf rpmExists
doLast {
String licenseFilename = "APACHE-LICENSE-2.0.txt"
final List<String> licenseLines = Files.readAllLines(rootDir.toPath().resolve("licenses/" + licenseFilename))
final Path licensePath = packageExtractionDir.toPath().resolve("usr/share/wazuh-indexer/LICENSE.txt")
assertLinesInFile(licensePath, licenseLines)
}
}
}
tasks.register("checkNotice") {
dependsOn buildDist, "checkExtraction"
onlyIf {
(project.name.contains('deb') && dpkgExists.call(it)) || (project.name.contains('rpm') && rpmExists.call(it))
}
doLast {
final List<String> noticeLines = Arrays.asList("OpenSearch (https://opensearch.org/)", "Copyright OpenSearch Contributors")
final Path noticePath = packageExtractionDir.toPath().resolve("usr/share/wazuh-indexer/NOTICE.txt")
assertLinesInFile(noticePath, noticeLines)
}
}
tasks.named("check").configure { dependsOn "checkNotice" }
tasks.register('checkLicenseMetadata', LoggedExec) {
dependsOn buildDist, "checkExtraction"
}
check.dependsOn checkLicenseMetadata
if (project.name.contains('deb')) {
checkLicenseMetadata { LoggedExec exec ->
onlyIf dpkgExists
final ByteArrayOutputStream output = new ByteArrayOutputStream()
exec.commandLine 'dpkg-deb', '--info', "${-> buildDist.get().outputs.files.filter(debFilter).singleFile}"
exec.standardOutput = output
doLast {
String expectedLicense = "ASL-2.0"
final Pattern pattern = Pattern.compile("\\s*License: (.+)")
final String info = output.toString('UTF-8')
final String[] actualLines = info.split("\n")
int count = 0
for (final String actualLine : actualLines) {
final Matcher matcher = pattern.matcher(actualLine)
if (matcher.matches()) {
count++
final String actualLicense = matcher.group(1)
if (expectedLicense != actualLicense) {
throw new GradleException("expected license [${expectedLicense} for package info but found [${actualLicense}]")
}
}
}
if (count == 0) {
throw new GradleException("expected license [${expectedLicense}] for package info but found none in:\n${info}")
}
if (count > 1) {
throw new GradleException("expected a single license for package info but found [${count}] in:\n${info}")
}
}
}
} else {
assert project.name.contains('rpm')
checkLicenseMetadata { LoggedExec exec ->
onlyIf rpmExists
final ByteArrayOutputStream output = new ByteArrayOutputStream()
exec.commandLine 'rpm', '-qp', '--queryformat', '%{License}', "${-> buildDist.get().outputs.files.singleFile}"
exec.standardOutput = output
doLast {
String license = output.toString('UTF-8')
String expectedLicense = "ASL-2.0"
if (license != expectedLicense) {
throw new GradleException("expected license [${expectedLicense}] for [${-> buildDist.get().outputs.files.singleFile}] but was [${license}]")
}
}
}
}
}
}