forked from elastic/apm-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagefile.go
266 lines (230 loc) · 8.23 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
// 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.
//go:build mage
// +build mage
package main
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/magefile/mage/mg"
"github.com/pkg/errors"
"github.com/elastic/beats/v7/dev-tools/mage"
"github.com/elastic/beats/v7/dev-tools/mage/target/build"
"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
mage.VirtualenvReqs = []string{filepath.Join(repo.RootDir, "script", "requirements.txt")}
}
// 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())
}
// AssembleDarwinUniversal merges the darwin/amd64 and darwin/arm64 into a single
// universal binary using `lipo`. It assumes the darwin/amd64 and darwin/arm64
// were built and only performs the merge.
//
// This is used by crossbuild.
func AssembleDarwinUniversal() error {
return build.AssembleDarwinUniversal()
}
// Clean cleans all generated files and build artifacts.
func Clean() error {
return mage.Clean()
}
// Config generates apm-server.yml and apm-server.docker.yml.
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,
},
}
}
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,
},
}
}
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 builds and packages apm-server 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() error {
mg.Deps(Update)
mg.Deps(func() error {
// TODO(axw) when we disable cgo, build all binaries using
// Go's native cross-compiling (GOOS=..., GOARCH=...)
return mage.CrossBuildXPack()
})
return PackageOnly()
}
// PackageOnly packages apm-server for distribution, relying on
// the binaries having already been built. See Package for more.
func PackageOnly() error {
mage.MustUsePackaging("elastic_beat_xpack_separate_binaries", "dev-tools/packaging/packages.yml")
customizePackaging()
if packageTypes := os.Getenv("TYPES"); packageTypes != "" {
filterPackages(packageTypes)
}
return mage.Package()
}
// Version prints out the qualified stack version.
func Version() error {
v, err := mage.BeatQualifiedVersion()
if err != nil {
return err
}
fmt.Print(v)
return nil
}
// Update updates the generated files.
func Update() error {
mg.Deps(Config)
return nil
}
// Use RACE_DETECTOR=true to enable the race detector.
func GoTestUnit(ctx context.Context) error {
return mage.GoTest(ctx, mage.DefaultGoTestUnitArgs())
}
// -----------------------------------------------------------------------------
func customizePackaging() {
const emptyDir = "build/empty"
if err := os.MkdirAll(emptyDir, 0750); err != nil {
panic(errors.Wrapf(err, "failed to create dir %v", emptyDir))
}
for idx := len(mage.Packages) - 1; idx >= 0; idx-- {
args := &mage.Packages[idx]
// Replace the generic Beats README.md with an APM specific one, and remove files unused by apm-server.
for filename, filespec := range args.Spec.Files {
switch filespec.Source {
case "{{ elastic_beats_dir }}/dev-tools/packaging/templates/common/README.md.tmpl":
args.Spec.Files[filename] = mage.PackageFile{Mode: 0644, Template: "packaging/files/README.md.tmpl"}
case "_meta/kibana.generated", "fields.yml", "{{.BeatName}}.reference.yml":
delete(args.Spec.Files, filename)
}
}
switch pkgType := args.Types[0]; pkgType {
case mage.Zip, mage.TarGz:
args.Spec.Files["java-attacher.jar"] = mage.PackageFile{Mode: 0750, Source: "build/java-attacher.jar", Owner: mage.BeatUser}
case mage.Docker:
args.Spec.ExtraVars["expose_ports"] = config.DefaultPort
args.Spec.ExtraVars["repository"] = "docker.elastic.co/apm"
args.Spec.Files["java-attacher.jar"] = mage.PackageFile{Mode: 0750, Source: "build/java-attacher.jar", Owner: mage.BeatUser}
case mage.Deb, mage.RPM:
// 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/log/{{.BeatName}}"] = mage.PackageFile{Mode: 0750, Source: emptyDir, Owner: mage.BeatUser}
args.Spec.Files["/usr/share/{{.BeatName}}/bin/java-attacher.jar"] = mage.PackageFile{Mode: 0750, Source: "build/java-attacher.jar", Owner: mage.BeatUser}
// Customise the pre-install and post-install scripts.
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"
}
// All our supported Linux distros have systemd, so don't package any SystemV init scripts or go-daemon.
delete(args.Spec.Files, "/usr/share/{{.BeatName}}/bin/{{.BeatName}}-god")
delete(args.Spec.Files, "/etc/init.d/{{.BeatServiceName}}")
default:
panic(errors.Errorf("unhandled package type: %v", pkgType))
}
}
}
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
}