forked from elastic/apm-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magefile.go
284 lines (236 loc) · 8 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
// 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 (
"context"
"fmt"
"os"
"path/filepath"
"time"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/pkg/errors"
"github.com/elastic/apm-server/beater/config"
"github.com/elastic/beats/dev-tools/mage"
)
func init() {
mage.SetBuildVariableSources(&mage.BuildVariableSources{
BeatVersion: "vendor/github.com/elastic/beats/libbeat/version/version.go",
GoVersion: ".go-version",
DocBranch: "docs/version.asciidoc",
})
mage.BeatDescription = "Elastic APM Server"
mage.BeatURL = "https://www.elastic.co/products/apm"
mage.BeatIndexPrefix = "apm"
mage.XPackDir = "x-pack"
mage.BeatUser = "apm-server"
}
// 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{
ShortParts: []string{
mage.OSSBeatDir("_meta/beat.yml"),
},
ExtraVars: map[string]interface{}{
"elasticsearch_hostport": "localhost:9200",
"listen_hostport": "localhost:" + config.DefaultPort,
},
}
}
func dockerConfigFileParams() mage.ConfigFileParams {
return mage.ConfigFileParams{
DockerParts: []string{
mage.OSSBeatDir("_meta/beat.yml"),
},
ExtraVars: map[string]interface{}{
"elasticsearch_hostport": "elasticsearch:9200",
"listen_hostport": "0.0.0.0:" + config.DefaultPort,
},
}
}
// Package packages the Beat for distribution.
// Use SNAPSHOT=true to build snapshots.
// Use PLATFORMS to control the target platforms.
func Package() {
start := time.Now()
defer func() { fmt.Println("package ran for", time.Since(start)) }()
mage.UseElasticBeatPackaging()
customizePackaging()
mg.Deps(Update, prepareIngestPackaging)
mg.Deps(CrossBuild, CrossBuildXPack, CrossBuildGoDaemon)
mg.SerialDeps(mage.Package, TestPackages)
}
// 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 (aka make update).
func Update() error {
return sh.Run("make", "update")
}
func Fields() error {
return mage.GenerateFieldsYAML("model")
}
// 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())
}
// -----------------------------------------------------------------------------
// 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
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]
pkgType := args.Types[0]
switch 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:
mage.Packages = append(mage.Packages[:idx], mage.Packages[idx+1:]...)
default:
panic(errors.Errorf("unhandled package type: %v", pkgType))
}
}
}
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()
}