-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
magefile.go
214 lines (183 loc) · 6.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
// 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.
//go:build mage
package main
import (
"context"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
devtools "github.com/elastic/beats/v7/dev-tools/mage"
"github.com/elastic/beats/v7/dev-tools/mage/target/build"
packetbeat "github.com/elastic/beats/v7/packetbeat/scripts/mage"
//mage:import
"github.com/elastic/beats/v7/dev-tools/mage/target/common"
//mage:import
_ "github.com/elastic/beats/v7/dev-tools/mage/target/unittest"
//mage:import
"github.com/elastic/beats/v7/dev-tools/mage/target/test"
)
// NpcapVersion specifies the version of the OEM Npcap installer to bundle with
// the packetbeat executable. It is used to specify which npcap builder crossbuild
// image to use and the installer to obtain from the cloud store for testing.
const (
NpcapVersion = "1.80"
installer = "npcap-" + NpcapVersion + "-oem.exe"
)
func init() {
common.RegisterCheckDeps(Update)
test.RegisterDeps(SystemTest)
devtools.BeatDescription = "Packetbeat analyzes network traffic and sends the data to Elasticsearch."
devtools.BeatLicense = "Elastic License"
packetbeat.SelectLogic = devtools.XPackProject
}
// Update updates the generated files.
func Update() {
mg.SerialDeps(packetbeat.FieldsYML, Dashboards, Config)
}
// Config generates the config files.
func Config() error {
return devtools.Config(devtools.AllConfigTypes, packetbeat.ConfigFileParams(), ".")
}
// Dashboards packages kibana dashboards
func Dashboards() error {
return devtools.KibanaDashboards(devtools.OSSBeatDir("protos"))
}
// Build builds the Beat binary.
func Build() error {
return devtools.Build(devtools.DefaultBuildArgs())
}
// GolangCrossBuild build the Beat binary inside of the golang-builder.
// Do not use directly, use crossBuild instead.
func GolangCrossBuild() error {
if devtools.Platform.GOOS == "windows" && (devtools.Platform.GOARCH == "amd64" || devtools.Platform.GOARCH == "386") {
err := sh.Copy("./npcap/installer/"+installer, "/installer/"+installer)
if err != nil {
return fmt.Errorf("failed to copy Npcap installer into source tree: %w", err)
}
}
return packetbeat.GolangCrossBuild()
}
// CrossBuild cross-builds the beat for all target platforms.
//
// On Windows platforms, if CrossBuild is invoked with the environment variables
// CI or NPCAP_LOCAL set to "true", a private cross-build image is selected that
// provides the OEM Npcap installer for the build. This behaviour requires access
// to the private image.
func CrossBuild() error {
return devtools.CrossBuild(
// Run all builds serially to try to address failures that might be caused
// by concurrent builds. See https://github.com/elastic/beats/issues/24304.
devtools.Serially(),
devtools.ImageSelector(func(platform string) (string, error) {
image, err := devtools.CrossBuildImage(platform)
if err != nil {
return "", err
}
if os.Getenv("CI") != "true" && os.Getenv("NPCAP_LOCAL") != "true" {
return image, nil
}
if platform == "windows/amd64" {
image = strings.ReplaceAll(image, "beats-dev", "observability-ci") // Temporarily work around naming of npcap image.
image = strings.ReplaceAll(image, "main", "npcap-"+NpcapVersion+"-debian9")
}
return image, nil
}),
)
}
// BuildGoDaemon builds the go-daemon binary (use crossBuildGoDaemon).
func BuildGoDaemon() error {
return devtools.BuildGoDaemon()
}
// CrossBuildGoDaemon cross-builds the go-daemon binary using Docker.
func CrossBuildGoDaemon() error {
return devtools.CrossBuildGoDaemon()
}
// 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.
func AssembleDarwinUniversal() error {
return build.AssembleDarwinUniversal()
}
// 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)) }()
if v, found := os.LookupEnv("AGENT_PACKAGING"); found && v != "" {
devtools.UseElasticBeatXPackReducedPackaging()
} else {
devtools.UseElasticBeatXPackPackaging()
}
devtools.PackageKibanaDashboardsFromBuildDir()
packetbeat.CustomizePackaging()
mg.Deps(Update)
mg.Deps(CrossBuild, CrossBuildGoDaemon)
mg.SerialDeps(devtools.Package, TestPackages)
}
// Package packages the Beat for IronBank distribution.
//
// Use SNAPSHOT=true to build snapshots.
func Ironbank() error {
start := time.Now()
defer func() { fmt.Println("ironbank ran for", time.Since(start)) }()
return devtools.Ironbank()
}
// TestPackages tests the generated packages (i.e. file modes, owners, groups).
func TestPackages() error {
return devtools.TestPackages()
}
func SystemTest(ctx context.Context) error {
// Buildkite (CI) images have preinstalled npcap
if os.Getenv("CI") == "true" {
mg.SerialDeps(devtools.BuildSystemTestBinary)
} else {
mg.SerialDeps(getNpcapInstaller, devtools.BuildSystemTestBinary)
}
args := devtools.DefaultGoTestIntegrationArgs()
args.Packages = []string{"./tests/system/..."}
return devtools.GoTest(ctx, args)
}
func getBucketName() string {
return "ingest-buildkite-ci"
}
// getNpcapInstaller gets the installer from the Google Cloud Storage service.
//
// On Windows platforms, if getNpcapInstaller is invoked with the environment variables
// CI or NPCAP_LOCAL set to "true" and the OEM Npcap installer is not available it is
// obtained from the cloud storage. This behaviour requires access to the private store.
// If NPCAP_LOCAL is set to "true" and the file is in the npcap/installer directory, no
// fetch will be made.
func getNpcapInstaller() error {
// TODO: Consider whether to expose this as a target.
if runtime.GOOS != "windows" {
return nil
}
if os.Getenv("CI") != "true" && os.Getenv("NPCAP_LOCAL") != "true" {
return errors.New("only available if running in the CI or with NPCAP_LOCAL=true")
}
dstPath := filepath.Join("./npcap/installer", installer)
if os.Getenv("NPCAP_LOCAL") == "true" {
fi, err := os.Stat(dstPath)
if err == nil && !fi.IsDir() {
fmt.Println("using local Npcap installer with NPCAP_LOCAL=true")
return nil
}
if !errors.Is(err, fs.ErrNotExist) {
return err
}
}
ciBucketName := getBucketName()
fmt.Printf("getting %s from private cache\n", installer)
return sh.RunV("gsutil", "cp", "gs://"+ciBucketName+"/private/"+installer, dstPath)
}