forked from elastic/apm-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magefile.go
495 lines (428 loc) · 13.7 KB
/
magefile.go
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
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. 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.
// +build mage
package main
import (
"bufio"
"bytes"
"context"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"time"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/pkg/errors"
"github.com/elastic/beats/v7/dev-tools/mage"
"github.com/elastic/beats/v7/libbeat/asset"
"github.com/elastic/beats/v7/libbeat/generator/fields"
"github.com/elastic/beats/v7/licenses"
"github.com/elastic/apm-server/beater/config"
)
func init() {
repo, err := mage.GetProjectRepoInfo()
if err != nil {
panic(err)
}
mage.SetBuildVariableSources(&mage.BuildVariableSources{
BeatVersion: filepath.Join(repo.RootDir, "cmd", "version.go"),
GoVersion: filepath.Join(repo.RootDir, ".go-version"),
DocBranch: filepath.Join(repo.RootDir, "docs/version.asciidoc"),
})
mage.BeatDescription = "Elastic APM Server"
mage.BeatURL = "https://www.elastic.co/apm"
mage.BeatIndexPrefix = "apm"
mage.XPackDir = "x-pack"
mage.BeatUser = "apm-server"
mage.CrossBuildMountModcache = true
}
// Build builds the Beat binary.
func Build() error {
return mage.Build(mage.DefaultBuildArgs())
}
// GolangCrossBuild build the Beat binary inside of the golang-builder.
// Do not use directly, use crossBuild instead.
func GolangCrossBuild() error {
return mage.GolangCrossBuild(mage.DefaultGolangCrossBuildArgs())
}
// BuildGoDaemon builds the go-daemon binary (use crossBuildGoDaemon).
func BuildGoDaemon() error {
return mage.BuildGoDaemon()
}
// CrossBuild cross-builds the beat for all target platforms.
func CrossBuild() error {
return mage.CrossBuild()
}
func CrossBuildXPack() error {
return mage.CrossBuildXPack()
}
// CrossBuildGoDaemon cross-builds the go-daemon binary using Docker.
func CrossBuildGoDaemon() error {
return mage.CrossBuildGoDaemon()
}
// Clean cleans all generated files and build artifacts.
func Clean() error {
return mage.Clean()
}
func Config() error {
if err := mage.Config(mage.ShortConfigType, shortConfigFileParams(), "."); err != nil {
return err
}
return mage.Config(mage.DockerConfigType, dockerConfigFileParams(), ".")
}
func shortConfigFileParams() mage.ConfigFileParams {
return mage.ConfigFileParams{
Short: mage.ConfigParams{Template: mage.OSSBeatDir("_meta/beat.yml")},
ExtraVars: map[string]interface{}{
"elasticsearch_hostport": "localhost:9200",
"listen_hostport": "localhost:" + config.DefaultPort,
"jaeger_grpc_hostport": "localhost:14250",
"jaeger_http_hostport": "localhost:14268",
},
}
}
func dockerConfigFileParams() mage.ConfigFileParams {
return mage.ConfigFileParams{
Docker: mage.ConfigParams{Template: mage.OSSBeatDir("_meta/beat.yml")},
ExtraVars: map[string]interface{}{
"elasticsearch_hostport": "elasticsearch:9200",
"listen_hostport": "0.0.0.0:" + config.DefaultPort,
"jaeger_grpc_hostport": "0.0.0.0:14250",
"jaeger_http_hostport": "0.0.0.0:14268",
},
}
}
func keepPackages(types []string) map[mage.PackageType]struct{} {
keep := make(map[mage.PackageType]struct{})
for _, t := range types {
var pt mage.PackageType
if err := pt.UnmarshalText([]byte(t)); err != nil {
log.Printf("skipped filtering package type %s", t)
continue
}
keep[pt] = struct{}{}
}
return keep
}
func filterPackages(types string) {
var packages []mage.OSPackageArgs
keep := keepPackages(strings.Split(types, " "))
for _, p := range mage.Packages {
for _, t := range p.Types {
if _, ok := keep[t]; !ok {
continue
}
packages = append(packages, p)
break
}
}
mage.Packages = packages
}
// Package packages the Beat for distribution.
// Use SNAPSHOT=true to build snapshots.
// Use PLATFORMS to control the target platforms. eg linux/amd64
// Use TYPES to control the target types. eg docker
func Package() {
start := time.Now()
defer func() { fmt.Println("package ran for", time.Since(start)) }()
mage.UseElasticBeatPackaging()
customizePackaging()
if packageTypes := os.Getenv("TYPES"); packageTypes != "" {
filterPackages(packageTypes)
}
if os.Getenv("SKIP_BUILD") != "true" {
mg.Deps(Update, prepareIngestPackaging)
mg.Deps(CrossBuild, CrossBuildXPack, CrossBuildGoDaemon)
}
mg.SerialDeps(mage.Package, TestPackages)
}
func Version() error {
v, err := mage.BeatQualifiedVersion()
if err != nil {
return err
}
fmt.Print(v)
return nil
}
// TestPackages tests the generated packages (i.e. file modes, owners, groups).
func TestPackages() error {
return mage.TestPackages()
}
// TestPackagesInstall integration tests the generated packages
func TestPackagesInstall() error {
// make the test script available to containers first
copy := &mage.CopyTask{
Source: "tests/packaging/test.sh",
Dest: mage.MustExpand("{{.PWD}}/build/distributions/test.sh"),
Mode: 0755,
}
if err := copy.Execute(); err != nil {
return err
}
defer sh.Rm(copy.Dest)
goTest := sh.OutCmd("go", "test")
var args []string
if mg.Verbose() {
args = append(args, "-v")
}
args = append(args, mage.MustExpand("tests/packaging/package_test.go"))
args = append(args, "-timeout", "20m")
args = append(args, "-files", mage.MustExpand("{{.PWD}}/build/distributions/*"))
args = append(args, "-tags=package")
if out, err := goTest(args...); err != nil {
if mg.Verbose() {
fmt.Println(out)
}
return err
}
return nil
}
// Update updates the generated files.
func Update() error {
mg.Deps(Fields, Config)
return nil
}
func Fields() error {
fieldsInclude := "include/fields.go"
xpackFieldsInclude := mage.XPackBeatDir(fieldsInclude)
ossFieldsModules := []string{"model"}
xpackFieldsModules := []string{mage.XPackBeatDir()}
allFieldsModules := append(ossFieldsModules[:], xpackFieldsModules...)
// Create include/fields.go from the OSS-only fields.
if err := generateFieldsYAML(mage.FieldsYML, ossFieldsModules...); err != nil {
return err
}
if err := mage.GenerateFieldsGo(mage.FieldsYML, fieldsInclude); err != nil {
return err
}
// Create docs/fields.asciidoc from all fields from all license types.
if err := generateFieldsYAML(mage.FieldsAllYML, allFieldsModules...); err != nil {
return err
}
if err := mage.Docs.FieldDocs(mage.FieldsAllYML); err != nil {
return err
}
// Create x-pack/apm-server/include/fields.go from the X-Pack fields.
// These supplement the OSS fields, they don't replace them.
xpackBeatDir := mage.XPackBeatDir()
xpackBeatDirRel, err := filepath.Rel(mage.OSSBeatDir(), xpackBeatDir)
if err != nil {
return err
}
xpackFieldsYMLFiles, err := fields.CollectModuleFiles(xpackBeatDir)
if err != nil {
return err
}
xpackFieldsData, err := fields.GenerateFieldsYml(xpackFieldsYMLFiles)
if err != nil {
return err
}
assetData, err := asset.CreateAsset(
licenses.Elastic, mage.BeatName,
"XPackFields", // asset name
"include", // package name
xpackFieldsData,
"asset.ModuleFieldsPri",
xpackBeatDirRel,
)
if err != nil {
panic(err)
}
return ioutil.WriteFile(xpackFieldsInclude, assetData, 0644)
}
func generateFieldsYAML(output string, modules ...string) error {
if err := mage.GenerateFieldsYAMLTo(output, modules...); err != nil {
return err
}
contents, err := ioutil.ReadFile(output)
if err != nil {
return err
}
// We don't use autodiscover at all, so we can remove those modules from our fields.
//
// TODO(axw) modify libbeat to make the "common" modules configurable.
beatsdir, err := mage.ElasticBeatsDir()
if err != nil {
return err
}
files, err := fields.CollectModuleFiles(filepath.Join(beatsdir, "libbeat", "autodiscover", "providers"))
if err != nil {
return err
}
var buf bytes.Buffer
for _, ymlfile := range files {
file, err := os.Open(ymlfile.Path)
if err != nil {
return err
}
defer file.Close()
buf.Reset()
prefix := strings.Repeat(" ", ymlfile.Indent)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
buf.WriteString(prefix)
buf.WriteString(scanner.Text())
buf.WriteRune('\n')
}
if err := scanner.Err(); err != nil {
return err
}
// Remove the contents from the combined file.
if i := bytes.Index(contents, buf.Bytes()); i == -1 {
return fmt.Errorf("could not find contents of %s in fields.yml", ymlfile.Path)
} else {
contents = append(contents[:i], contents[i+buf.Len():]...)
}
}
return ioutil.WriteFile(output, contents, 0644)
}
// Use RACE_DETECTOR=true to enable the race detector.
func GoTestUnit(ctx context.Context) error {
return mage.GoTest(ctx, mage.DefaultGoTestUnitArgs())
}
// GoTestIntegration executes the Go integration tests.
// Use TEST_COVERAGE=true to enable code coverage profiling.
// Use RACE_DETECTOR=true to enable the race detector.
func GoTestIntegration(ctx context.Context) error {
return mage.GoTest(ctx, mage.DefaultGoTestIntegrationArgs())
}
// PythonUnitTest executes the python system tests.
func PythonUnitTest() error {
return mage.PythonTest(mage.DefaultPythonTestUnitArgs())
}
// -----------------------------------------------------------------------------
// Customizations specific to apm-server.
// - readme.md.tmpl used in packages is customized.
// - apm-server.reference.yml is not included in packages.
// - ingest .json files are included in packaging
// - fields.yml is sourced from the build directory
var emptyDir = filepath.Clean("build/empty")
var ingestDirGenerated = filepath.Clean("build/packaging/ingest")
func customizePackaging() {
if err := os.MkdirAll(emptyDir, 0750); err != nil {
panic(errors.Wrapf(err, "failed to create dir %v", emptyDir))
}
var (
readmeTemplate = mage.PackageFile{
Mode: 0644,
Template: "packaging/files/README.md.tmpl",
}
ingestTarget = "ingest"
ingest = mage.PackageFile{
Mode: 0644,
Source: ingestDirGenerated,
}
)
for idx := len(mage.Packages) - 1; idx >= 0; idx-- {
args := &mage.Packages[idx]
switch pkgType := args.Types[0]; pkgType {
case mage.Zip, mage.TarGz:
// Remove the reference config file from packages.
delete(args.Spec.Files, "{{.BeatName}}.reference.yml")
// Replace the README.md with an APM specific file.
args.Spec.ReplaceFile("README.md", readmeTemplate)
args.Spec.Files[ingestTarget] = ingest
case mage.Docker:
delete(args.Spec.Files, "{{.BeatName}}.reference.yml")
args.Spec.ReplaceFile("README.md", readmeTemplate)
args.Spec.Files[ingestTarget] = ingest
args.Spec.ExtraVars["expose_ports"] = config.DefaultPort
args.Spec.ExtraVars["repository"] = "docker.elastic.co/apm"
case mage.Deb, mage.RPM:
delete(args.Spec.Files, "/etc/{{.BeatName}}/{{.BeatName}}.reference.yml")
args.Spec.ReplaceFile("/usr/share/{{.BeatName}}/README.md", readmeTemplate)
args.Spec.Files["/usr/share/{{.BeatName}}/"+ingestTarget] = ingest
// update config file Owner
pf := args.Spec.Files["/etc/{{.BeatName}}/{{.BeatName}}.yml"]
pf.Owner = mage.BeatUser
args.Spec.Files["/etc/{{.BeatName}}/{{.BeatName}}.yml"] = pf
args.Spec.Files["/var/lib/{{.BeatName}}"] = mage.PackageFile{Mode: 0750, Source: emptyDir, Owner: mage.BeatUser}
args.Spec.Files["/var/log/{{.BeatName}}"] = mage.PackageFile{Mode: 0750, Source: emptyDir, Owner: mage.BeatUser}
args.Spec.PreInstallScript = "packaging/files/linux/pre-install.sh.tmpl"
if pkgType == mage.Deb {
args.Spec.PostInstallScript = "packaging/files/linux/deb-post-install.sh.tmpl"
}
case mage.DMG:
// We do not build macOS packages.
mage.Packages = append(mage.Packages[:idx], mage.Packages[idx+1:]...)
continue
default:
panic(errors.Errorf("unhandled package type: %v", pkgType))
}
for filename, filespec := range args.Spec.Files {
switch {
case strings.HasPrefix(filespec.Source, "_meta/kibana"):
// Remove Kibana dashboard files.
delete(args.Spec.Files, filename)
case filespec.Source == "fields.yml":
// Source fields.yml from the build directory.
if args.Spec.License == "Elastic License" {
filespec.Source = mage.FieldsAllYML
} else {
filespec.Source = mage.FieldsYML
}
args.Spec.Files[filename] = filespec
}
}
}
}
func prepareIngestPackaging() error {
if err := sh.Rm(ingestDirGenerated); err != nil {
return err
}
copy := &mage.CopyTask{
Source: "ingest",
Dest: ingestDirGenerated,
Mode: 0644,
DirMode: 0755,
Exclude: []string{".*.go"},
}
return copy.Execute()
}
// DumpVariables writes the template variables and values to stdout.
func DumpVariables() error {
return mage.DumpVariables()
}
func Check() error {
fmt.Println(">> check: Checking source code for common problems")
mg.Deps(mage.GoVet, mage.CheckPythonTestNotExecutable, mage.CheckYAMLNotExecutable)
changes, err := mage.GitDiffIndex()
if err != nil {
return errors.Wrap(err, "failed to diff the git index")
}
if len(changes) > 0 {
if mg.Verbose() {
mage.GitDiff()
}
return errors.Errorf("some files are not up-to-date. "+
"Run 'make fmt update' then review and commit the changes. "+
"Modified: %v", changes)
}
return nil
}
// PythonEnv ensures the Python venv is up-to-date with the beats requrements.txt.
func PythonEnv() error {
_, err := mage.PythonVirtualenv()
return err
}
// PythonAutopep8 executes autopep8 on all .py files.
func PythonAutopep8() error {
return mage.PythonAutopep8()
}