diff --git a/auditbeat/magefile.go b/auditbeat/magefile.go index 962713383eaf..efd4b007beb9 100644 --- a/auditbeat/magefile.go +++ b/auditbeat/magefile.go @@ -25,6 +25,7 @@ import ( "time" "github.com/magefile/mage/mg" + "go.uber.org/multierr" auditbeat "github.com/elastic/beats/v7/auditbeat/scripts/mage" devtools "github.com/elastic/beats/v7/dev-tools/mage" @@ -59,7 +60,10 @@ func Build() error { // GolangCrossBuild build the Beat binary inside of the golang-builder. // Do not use directly, use crossBuild instead. func GolangCrossBuild() error { - return devtools.GolangCrossBuild(devtools.DefaultGolangCrossBuildArgs()) + return multierr.Combine( + devtools.GolangCrossBuild(devtools.DefaultGolangCrossBuildArgs()), + devtools.TestLinuxForCentosGLIBC(), + ) } // BuildGoDaemon builds the go-daemon binary (use crossBuildGoDaemon). diff --git a/dev-tools/mage/pkg.go b/dev-tools/mage/pkg.go index ba6809831440..c9448e99ab2b 100644 --- a/dev-tools/mage/pkg.go +++ b/dev-tools/mage/pkg.go @@ -18,6 +18,7 @@ package mage import ( + "errors" "fmt" "log" "os" @@ -28,7 +29,6 @@ import ( "github.com/magefile/mage/mg" "github.com/magefile/mage/sh" - "github.com/pkg/errors" ) // Package packages the Beat for distribution. It generates packages based on @@ -201,14 +201,14 @@ func saveIronbank() error { ironbank := getIronbankContextName() buildDir := filepath.Join("build", ironbank) if _, err := os.Stat(buildDir); os.IsNotExist(err) { - return fmt.Errorf("cannot find the folder with the ironbank context: %+v", err) + return fmt.Errorf("cannot find the folder with the ironbank context: %w", err) } distributionsDir := "build/distributions" if _, err := os.Stat(distributionsDir); os.IsNotExist(err) { err := os.MkdirAll(distributionsDir, 0750) if err != nil { - return fmt.Errorf("cannot create folder for docker artifacts: %+v", err) + return fmt.Errorf("cannot create folder for docker artifacts: %w", err) } } tarGzFile := filepath.Join(distributionsDir, ironbank+".tar.gz") @@ -216,24 +216,13 @@ func saveIronbank() error { // Save the build context as tar.gz artifact err := TarWithOptions(buildDir, tarGzFile, true) if err != nil { - return fmt.Errorf("cannot compress the tar.gz file: %+v", err) + return fmt.Errorf("cannot compress the tar.gz file: %w", err) } - return errors.Wrap(CreateSHA512File(tarGzFile), "failed to create .sha512 file") -} - -// updateWithDarwinUniversal checks if darwin/amd64 and darwin/arm64, are listed -// if so, the universal binary was built, then we need to package it as well. -func updateWithDarwinUniversal(platforms BuildPlatformList) BuildPlatformList { - if IsDarwinUniversal() { - platforms = append(platforms, - BuildPlatform{ - Name: "darwin/universal", - Flags: CGOSupported | CrossBuildSupported | Default, - }) + if err = CreateSHA512File(tarGzFile); err != nil { + return fmt.Errorf("failed to create .sha512 file: %w", err) } - - return platforms + return nil } // isPackageTypeSelected returns true if SelectedPackageTypes is empty or if @@ -260,8 +249,11 @@ type packageBuilder struct { func (b packageBuilder) Build() error { fmt.Printf(">> package: Building %v type=%v for platform=%v\n", b.Spec.Name, b.Type, b.Platform.Name) log.Printf("Package spec: %+v", b.Spec) - return errors.Wrapf(b.Type.Build(b.Spec), "failed building %v type=%v for platform=%v", - b.Spec.Name, b.Type, b.Platform.Name) + if err := b.Type.Build(b.Spec); err != nil { + return fmt.Errorf("failed building %v type=%v for platform=%v: %w", + b.Spec.Name, b.Type, b.Platform.Name, err) + } + return nil } type testPackagesParams struct { @@ -366,12 +358,12 @@ func TestPackages(options ...TestPackagesOption) error { } // TestLinuxForCentosGLIBC checks the GLIBC requirements of linux/amd64 and -// linux/386 binaries to ensure they meet the requirements for RHEL 6 which has -// glibc 2.12. +// linux/386 binaries to ensure they meet the requirements for RHEL 7 which has +// glibc 2.17. func TestLinuxForCentosGLIBC() error { switch Platform.Name { case "linux/amd64", "linux/386": - return TestBinaryGLIBCVersion(filepath.Join("build/golang-crossbuild", BeatName+"-linux-"+Platform.GOARCH), "2.12") + return TestBinaryGLIBCVersion(filepath.Join("build/golang-crossbuild", BeatName+"-linux-"+Platform.GOARCH), "2.17") default: return nil } diff --git a/packetbeat/scripts/mage/build.go b/packetbeat/scripts/mage/build.go index 199834b41ed1..ad889396a2a2 100644 --- a/packetbeat/scripts/mage/build.go +++ b/packetbeat/scripts/mage/build.go @@ -17,11 +17,30 @@ package mage -import devtools "github.com/elastic/beats/v7/dev-tools/mage" +import ( + "strings" + + devtools "github.com/elastic/beats/v7/dev-tools/mage" +) // CrossBuild cross-builds the beat for all target platforms. func CrossBuild() error { - // Run all builds serially to try to address failures that might be caused - // by concurrent builds. See https://github.com/elastic/beats/issues/24304. - return devtools.CrossBuild(devtools.Serially()) + 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 platform == "linux/386" { + // Use Debian 9 because the linux/386 build needs an older glibc + // to remain compatible with CentOS 7 (glibc 2.17). + image = strings.ReplaceAll(image, "main-debian10", "main-debian9") + } + return image, nil + }), + ) } diff --git a/packetbeat/scripts/mage/pcap.go b/packetbeat/scripts/mage/pcap.go index 81ee493a8710..f1ed34177d8f 100644 --- a/packetbeat/scripts/mage/pcap.go +++ b/packetbeat/scripts/mage/pcap.go @@ -18,6 +18,8 @@ package mage import ( + "go.uber.org/multierr" + devtools "github.com/elastic/beats/v7/dev-tools/mage" ) @@ -34,7 +36,10 @@ func GolangCrossBuild() error { params.Env["CGO_CFLAGS"] = flags } - return devtools.GolangCrossBuild(params) + return multierr.Combine( + devtools.GolangCrossBuild(params), + devtools.TestLinuxForCentosGLIBC(), + ) } // ----------------------------------------------------------------------------- diff --git a/x-pack/auditbeat/magefile.go b/x-pack/auditbeat/magefile.go index 6112b601a57b..b7750301d540 100644 --- a/x-pack/auditbeat/magefile.go +++ b/x-pack/auditbeat/magefile.go @@ -12,6 +12,7 @@ import ( "time" "github.com/magefile/mage/mg" + "go.uber.org/multierr" auditbeat "github.com/elastic/beats/v7/auditbeat/scripts/mage" devtools "github.com/elastic/beats/v7/dev-tools/mage" @@ -46,7 +47,10 @@ func Build() error { // GolangCrossBuild build the Beat binary inside of the golang-builder. // Do not use directly, use crossBuild instead. func GolangCrossBuild() error { - return devtools.GolangCrossBuild(devtools.DefaultGolangCrossBuildArgs()) + return multierr.Combine( + devtools.GolangCrossBuild(devtools.DefaultGolangCrossBuildArgs()), + devtools.TestLinuxForCentosGLIBC(), + ) } // CrossBuild cross-builds the beat for all target platforms. diff --git a/x-pack/metricbeat/magefile.go b/x-pack/metricbeat/magefile.go index 9a23e78103cd..012b1703eb96 100644 --- a/x-pack/metricbeat/magefile.go +++ b/x-pack/metricbeat/magefile.go @@ -16,6 +16,8 @@ import ( "strings" "time" + "go.uber.org/multierr" + "github.com/magefile/mage/mg" "github.com/magefile/mage/sh" @@ -59,7 +61,10 @@ func GolangCrossBuild() error { if isWindows32bitRunner() { args.LDFlags = append(args.LDFlags, "-w") } - return devtools.GolangCrossBuild(args) + return multierr.Combine( + devtools.GolangCrossBuild(args), + devtools.TestLinuxForCentosGLIBC(), + ) } // CrossBuild cross-builds the beat for all target platforms. diff --git a/x-pack/packetbeat/magefile.go b/x-pack/packetbeat/magefile.go index bf56527bda00..3136ee3a4814 100644 --- a/x-pack/packetbeat/magefile.go +++ b/x-pack/packetbeat/magefile.go @@ -99,6 +99,11 @@ func CrossBuild() error { if err != nil { return "", err } + if platform == "linux/386" { + // Use Debian 9 because the linux/386 build needs an older glibc + // to remain compatible with CentOS 7 (glibc 2.17). + image = strings.ReplaceAll(image, "main-debian10", "main-debian9") + } if os.Getenv("CI") != "true" && os.Getenv("NPCAP_LOCAL") != "true" { return image, nil }