-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathmagefile.go
261 lines (213 loc) · 7.24 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
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
// +build mage
package main
import (
"context"
"fmt"
"time"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/pkg/errors"
auditbeat "github.com/elastic/beats/auditbeat/scripts/mage"
"github.com/elastic/beats/dev-tools/mage"
)
func init() {
mage.BeatDescription = "Audit the activities of users and processes on your system."
mage.BeatLicense = "Elastic License"
mage.Platforms = mage.Platforms.Filter("!linux/ppc64 !linux/mips64")
}
// Aliases provides compatibility with CI while we transition all Beats
// to having common testing targets.
var Aliases = map[string]interface{}{
"goTestUnit": GoUnitTest, // dev-tools/jenkins_ci.ps1 uses this.
}
// 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 {
if d, ok := deps[mage.Platform.Name]; ok {
mg.Deps(d)
}
return mage.GolangCrossBuild(mage.DefaultGolangCrossBuildArgs())
}
// CrossBuild cross-builds the beat for all target platforms.
func CrossBuild() error {
return mage.CrossBuild()
}
// BuildGoDaemon builds the go-daemon binary (use crossBuildGoDaemon).
func BuildGoDaemon() error {
return mage.BuildGoDaemon()
}
// 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()
}
// Package packages the Beat for distribution.
// Use SNAPSHOT=true to build snapshots.
// Use PLATFORMS to control the target platforms.
// Use VERSION_QUALIFIER to control the version qualifier.
func Package() {
start := time.Now()
defer func() { fmt.Println("package ran for", time.Since(start)) }()
mage.UseElasticBeatXPackPackaging()
mage.PackageKibanaDashboardsFromBuildDir()
auditbeat.CustomizePackaging(auditbeat.XPackPackaging)
mg.SerialDeps(Fields, Dashboards, Config, mage.GenerateModuleIncludeListGo)
mg.Deps(CrossBuild, CrossBuildGoDaemon)
mg.SerialDeps(mage.Package, TestPackages)
}
// TestPackages tests the generated packages (i.e. file modes, owners, groups).
func TestPackages() error {
return mage.TestPackages()
}
// Update is an alias for running fields, dashboards, config.
func Update() {
mg.SerialDeps(Fields, Dashboards, Config, mage.GenerateModuleIncludeListGo)
}
// Config generates both the short and reference configs.
func Config() error {
return mage.Config(mage.AllConfigTypes, auditbeat.XPackConfigFileParams(), ".")
}
// Fields generates a fields.yml and include/fields.go.
func Fields() {
mg.SerialDeps(fieldsYML, moduleFieldsGo)
}
func moduleFieldsGo() error {
return mage.GenerateModuleFieldsGo("module")
}
// fieldsYML generates the fields.yml file containing all fields.
func fieldsYML() error {
return mage.GenerateFieldsYAML(mage.OSSBeatDir("module"), "module")
}
// ExportDashboard exports a dashboard and writes it into the correct directory.
//
// Required environment variables:
// - MODULE: Name of the module
// - ID: Dashboard id
func ExportDashboard() error {
return mage.ExportDashboard()
}
// Dashboards collects all the dashboards and generates index patterns.
func Dashboards() error {
return mage.KibanaDashboards(mage.OSSBeatDir("module"), "module")
}
// Fmt formats source code and adds file headers.
func Fmt() {
mg.Deps(mage.Format)
}
// Check runs fmt and update then returns an error if any modifications are found.
func Check() {
mg.SerialDeps(mage.Format, Update, mage.Check)
}
// IntegTest executes integration tests (it uses Docker to run the tests).
func IntegTest() {
mage.AddIntegTestUsage()
defer mage.StopIntegTestEnv()
mg.SerialDeps(GoIntegTest, PythonIntegTest)
}
// UnitTest executes the unit tests.
func UnitTest() {
mg.SerialDeps(GoUnitTest, PythonUnitTest)
}
// GoUnitTest executes the Go unit tests.
// Use TEST_COVERAGE=true to enable code coverage profiling.
// Use RACE_DETECTOR=true to enable the race detector.
func GoUnitTest(ctx context.Context) error {
return mage.GoTest(ctx, mage.DefaultGoTestUnitArgs())
}
// GoIntegTest executes the Go integration tests.
// Use TEST_COVERAGE=true to enable code coverage profiling.
// Use RACE_DETECTOR=true to enable the race detector.
func GoIntegTest(ctx context.Context) error {
return mage.RunIntegTest("goIntegTest", func() error {
return mage.GoTest(ctx, mage.DefaultGoTestIntegrationArgs())
})
}
// PythonUnitTest executes the python system tests.
func PythonUnitTest() error {
mg.SerialDeps(Fields, mage.BuildSystemTestBinary)
return mage.PythonNoseTest(mage.DefaultPythonTestUnitArgs())
}
// PythonIntegTest executes the python system tests in the integration environment (Docker).
func PythonIntegTest(ctx context.Context) error {
if !mage.IsInIntegTestEnv() {
mg.Deps(Fields)
}
return mage.RunIntegTest("pythonIntegTest", func() error {
mg.Deps(mage.BuildSystemTestBinary)
return mage.PythonNoseTest(mage.DefaultPythonTestIntegrationArgs())
})
}
// -----------------------------------------------------------------------------
// - Install the librpm-dev package
var (
deps = map[string]func() error{
"linux/386": installLinux386,
"linux/amd64": installLinuxAMD64,
"linux/arm64": installLinuxARM64,
"linux/armv5": installLinuxARMLE,
"linux/armv6": installLinuxARMLE,
"linux/armv7": installLinuxARMHF,
"linux/mips": installLinuxMIPS,
"linux/mipsle": installLinuxMIPSLE,
"linux/mips64le": installLinuxMIPS64LE,
"linux/ppc64le": installLinuxPPC64LE,
"linux/s390x": installLinuxS390X,
//"linux/ppc64": installLinuxPpc64,
//"linux/mips64": installLinuxMips64,
}
)
const (
librpmDevPkgName = "librpm-dev"
)
func installLinuxAMD64() error {
return installDependencies(librpmDevPkgName, "")
}
func installLinuxARM64() error {
return installDependencies(librpmDevPkgName+":arm64", "arm64")
}
func installLinuxARMHF() error {
return installDependencies(librpmDevPkgName+":armhf", "armhf")
}
func installLinuxARMLE() error {
return installDependencies(librpmDevPkgName+":armel", "armel")
}
func installLinux386() error {
return installDependencies(librpmDevPkgName+":i386", "i386")
}
func installLinuxMIPS() error {
return installDependencies(librpmDevPkgName+":mips", "mips")
}
func installLinuxMIPS64LE() error {
return installDependencies(librpmDevPkgName+":mips64el", "mips64el")
}
func installLinuxMIPSLE() error {
return installDependencies(librpmDevPkgName+":mipsel", "mipsel")
}
func installLinuxPPC64LE() error {
return installDependencies(librpmDevPkgName+":ppc64el", "ppc64el")
}
func installLinuxS390X() error {
return installDependencies(librpmDevPkgName+":s390x", "s390x")
}
func installDependencies(pkg, arch string) error {
if arch != "" {
err := sh.Run("dpkg", "--add-architecture", arch)
if err != nil {
return errors.Wrap(err, "error while adding architecture")
}
}
if err := sh.Run("apt-get", "update"); err != nil {
return err
}
return sh.Run("apt-get", "install", "-y", "--no-install-recommends", pkg)
}