Skip to content

Commit

Permalink
Update imports to elastic-agent-libs/file where needed (elastic#31419)
Browse files Browse the repository at this point in the history
  • Loading branch information
kvch authored and kush-elastic committed May 2, 2022
1 parent 0a5a58c commit 2254bb7
Show file tree
Hide file tree
Showing 26 changed files with 153 additions and 1,796 deletions.
14 changes: 7 additions & 7 deletions NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6039,11 +6039,11 @@ SOFTWARE

--------------------------------------------------------------------------------
Dependency : github.com/elastic/elastic-agent-libs
Version: v0.1.1
Version: v0.2.1
Licence type (autodetected): Apache-2.0
--------------------------------------------------------------------------------

Contents of probable licence file $GOMODCACHE/github.com/elastic/elastic-agent-libs@v0.1.1/LICENSE:
Contents of probable licence file $GOMODCACHE/github.com/elastic/elastic-agent-libs@v0.2.1/LICENSE:

Apache License
Version 2.0, January 2004
Expand Down Expand Up @@ -16439,11 +16439,11 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

--------------------------------------------------------------------------------
Dependency : go.uber.org/atomic
Version: v1.8.0
Version: v1.9.0
Licence type (autodetected): MIT
--------------------------------------------------------------------------------

Contents of probable licence file $GOMODCACHE/go.uber.org/atomic@v1.8.0/LICENSE.txt:
Contents of probable licence file $GOMODCACHE/go.uber.org/atomic@v1.9.0/LICENSE.txt:

Copyright (c) 2016 Uber Technologies, Inc.

Expand All @@ -16468,13 +16468,13 @@ THE SOFTWARE.

--------------------------------------------------------------------------------
Dependency : go.uber.org/multierr
Version: v1.6.0
Version: v1.8.0
Licence type (autodetected): MIT
--------------------------------------------------------------------------------

Contents of probable licence file $GOMODCACHE/go.uber.org/multierr@v1.6.0/LICENSE.txt:
Contents of probable licence file $GOMODCACHE/go.uber.org/multierr@v1.8.0/LICENSE.txt:

Copyright (c) 2017 Uber Technologies, Inc.
Copyright (c) 2017-2021 Uber Technologies, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
59 changes: 10 additions & 49 deletions dev-tools/mage/crossbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package mage

import (
"errors"
"fmt"
"go/build"
"log"
Expand All @@ -30,10 +31,9 @@ import (

"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/pkg/errors"

"github.com/elastic/beats/v7/dev-tools/mage/gotool"
"github.com/elastic/beats/v7/libbeat/common/file"
"github.com/elastic/elastic-agent-libs/file"
)

const defaultCrossBuildTarget = "golangCrossBuild"
Expand Down Expand Up @@ -158,7 +158,7 @@ func CrossBuild(options ...CrossBuildOption) error {
}
}
// If we're here, something isn't set.
return errors.New("Cannot crossbuild on AIX. Either run `mage build` or set PLATFORMS='aix/ppc64'")
return errors.New("cannot crossbuild on AIX, either run `mage build` or set PLATFORMS='aix/ppc64'")
}

// Docker is required for this target.
Expand All @@ -184,8 +184,8 @@ func CrossBuild(options ...CrossBuildOption) error {
builder := GolangCrossBuilder{buildPlatform.Name, params.Target, params.InDir, params.ImageSelector}
if params.Serial {
if err := builder.Build(); err != nil {
return errors.Wrapf(err, "failed cross-building target=%s for platform=%s",
params.Target, buildPlatform.Name)
return fmt.Errorf("failed cross-building target=%s for platform=%s: %w",
params.Target, buildPlatform.Name, err)
}
} else {
deps = append(deps, builder.Build)
Expand All @@ -195,48 +195,9 @@ func CrossBuild(options ...CrossBuildOption) error {
// Each build runs in parallel.
Parallel(deps...)

// // It needs to run after all the builds, as it needs the darwin binaries.
// if err := assembleDarwinUniversal(params); err != nil {
// return err
// }

return nil
}

// assembleDarwinUniversal checks if darwin/amd64 and darwin/arm64 were build,
// if so, it generates a darwin/universal binary that is the merge fo them two.
func assembleDarwinUniversal(params crossBuildParams) error {
if !IsDarwinUniversal() {
return nil // nothing to do
}

fmt.Println("-----------------------------------------")
fmt.Println(">> assembleDarwinUniversal DEBUG")
out, err := sh.Output("pwd")
fmt.Println(">> assembleDarwinUniversal on:", out, err)
fmt.Println("-----------------------------------------")
out, err = sh.Output("ls", "build")
fmt.Println(">> assembleDarwinUniversal:", "ls", "build:", out, err)
fmt.Println("-----------------------------------------")
out, err = sh.Output("ls", "build/golang-crossbuild")
fmt.Println(">> assembleDarwinUniversal debug:", out, err)
fmt.Println("-----------------------------------------")
fmt.Println(">> assembleDarwinUniversal DEBUG END")
fmt.Println("-----------------------------------------")

builder := GolangCrossBuilder{
// the docker image for darwin/arm64 is the one capable of merging the binaries.
Platform: "darwin/arm64",
Target: "assembleDarwinUniversal",
InDir: params.InDir,
ImageSelector: params.ImageSelector}
return errors.Wrapf(builder.Build(),
"failed merging darwin/amd64 and darwin/arm64 into darwin/universal target=%v for platform=%v",
builder.Target,
builder.Platform)

}

// CrossBuildXPack executes the 'golangCrossBuild' target in the Beat's
// associated x-pack directory to produce a version of the Beat that contains
// Elastic licensed content.
Expand Down Expand Up @@ -312,7 +273,7 @@ func (b GolangCrossBuilder) Build() error {

repoInfo, err := GetProjectRepoInfo()
if err != nil {
return errors.Wrap(err, "failed to determine repo root and package sub dir")
return fmt.Errorf("failed to determine repo root and package sub dir: %w", err)
}

mountPoint := filepath.ToSlash(filepath.Join("/go", "src", repoInfo.CanonicalRootImportPath))
Expand All @@ -326,13 +287,13 @@ func (b GolangCrossBuilder) Build() error {
builderArch := runtime.GOARCH
buildCmd, err := filepath.Rel(workDir, filepath.Join(mountPoint, repoInfo.SubDir, "build/mage-linux-"+builderArch))
if err != nil {
return errors.Wrap(err, "failed to determine mage-linux-"+builderArch+" relative path")
return fmt.Errorf("failed to determine mage-linux-"+builderArch+" relative path: %w", err)
}

dockerRun := sh.RunCmd("docker", "run")
image, err := b.ImageSelector(b.Platform)
if err != nil {
return errors.Wrap(err, "failed to determine golang-crossbuild image tag")
return fmt.Errorf("failed to determine golang-crossbuild image tag: %w", err)
}
verbose := ""
if mg.Verbose() {
Expand Down Expand Up @@ -393,7 +354,7 @@ func chownPaths(uid, gid int, path string) error {
start := time.Now()
numFixed := 0
defer func() {
log.Printf("chown took: %v, changed %d files", time.Now().Sub(start), numFixed)
log.Printf("chown took: %v, changed %d files", time.Since(start), numFixed)
}()

return filepath.Walk(path, func(name string, info os.FileInfo, err error) error {
Expand All @@ -414,7 +375,7 @@ func chownPaths(uid, gid int, path string) error {
}

if err := os.Chown(name, uid, gid); err != nil {
return errors.Wrapf(err, "failed to chown path=%v", name)
return fmt.Errorf("failed to chown path=%v: %w", name, err)
}
numFixed++
return nil
Expand Down
4 changes: 2 additions & 2 deletions filebeat/inputsource/unix/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ import (
"github.com/stretchr/testify/require"

"github.com/elastic/beats/v7/filebeat/inputsource"
"github.com/elastic/beats/v7/libbeat/common/file"
"github.com/elastic/beats/v7/libbeat/logp"
conf "github.com/elastic/elastic-agent-libs/config"
"github.com/elastic/elastic-agent-libs/file"
)

func defaultConfig() Config {
Expand Down Expand Up @@ -397,7 +397,7 @@ func randomString(l int) string {
charsets := []byte("abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWZYZ0123456789")
message := make([]byte, l)
for i := range message {
message[i] = charsets[rand.Intn(len(charsets))]
message[i] = charsets[rand.Intn(len(charsets))] //nolint:gosec // it generates test string, allowed to be weak
}
return string(message)
}
Expand Down
30 changes: 14 additions & 16 deletions filebeat/registrar/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,21 @@ import (
"os"
"path/filepath"

"github.com/pkg/errors"

"github.com/elastic/beats/v7/filebeat/config"
"github.com/elastic/beats/v7/filebeat/input/file"
helper "github.com/elastic/beats/v7/libbeat/common/file"
"github.com/elastic/beats/v7/libbeat/logp"
"github.com/elastic/beats/v7/libbeat/paths"
"github.com/elastic/beats/v7/libbeat/statestore/backend/memlog"
helper "github.com/elastic/elastic-agent-libs/file"
)

type registryVersion string

const (
noRegistry registryVersion = ""
legacyVersion = "<legacy>"
version0 = "0"
version1 = "1"
legacyVersion registryVersion = "<legacy>"
version0 registryVersion = "0"
version1 registryVersion = "1"
)

const currentVersion = version1
Expand Down Expand Up @@ -162,7 +160,7 @@ func initVersion0Registry(regHome string, perm os.FileMode) error {
if !isDir(regHome) {
logp.Info("No registry home found. Create: %v", regHome)
if err := os.MkdirAll(regHome, 0o750); err != nil {
return errors.Wrapf(err, "failed to create registry dir '%v'", regHome)
return fmt.Errorf("failed to create registry dir '%v': %w", regHome, err)
}
}

Expand All @@ -171,7 +169,7 @@ func initVersion0Registry(regHome string, perm os.FileMode) error {
logp.Info("Initialize registry meta file")
err := safeWriteFile(metaFile, []byte(`{"version": "0"}`), perm)
if err != nil {
return errors.Wrap(err, "failed writing registry meta.json")
return fmt.Errorf("failed writing registry meta.json: %w", err)
}
}

Expand All @@ -193,14 +191,14 @@ func (m *Migrator) updateToVersion1(regHome string) error {
states, err := func() ([]file.State, error) {
origIn, err := os.Open(origDataFile)
if err != nil {
return nil, errors.Wrap(err, "failed to open original data file")
return nil, fmt.Errorf("failed to open original data file: %w", err)
}
defer origIn.Close()

var states []file.State
decoder := json.NewDecoder(origIn)
if err := decoder.Decode(&states); err != nil {
return nil, errors.Wrapf(err, "Error decoding original data file '%v'", origDataFile)
return nil, fmt.Errorf("error decoding original data file '%v': %w", origDataFile, err)
}
return states, nil
}()
Expand All @@ -217,18 +215,18 @@ func (m *Migrator) updateToVersion1(regHome string) error {
IgnoreVersionCheck: true,
})
if err != nil {
return errors.Wrap(err, "failed to create new registry backend")
return fmt.Errorf("failed to create new registry backend: %w", err)
}
defer registryBackend.Close()

store, err := registryBackend.Access("filebeat")
if err != nil {
return errors.Wrap(err, "failed to open filebeat registry store")
return fmt.Errorf("failed to open filebeat registry store: %w", err)
}
defer store.Close()

if err := writeStates(store, states); err != nil {
return errors.Wrap(err, "failed to migrate registry states")
return fmt.Errorf("failed to migrate registry states: %w", err)
}

if checkpointer, ok := store.(interface{ Checkpoint() error }); ok {
Expand All @@ -239,7 +237,7 @@ func (m *Migrator) updateToVersion1(regHome string) error {
}

if err := os.Remove(origDataFile); err != nil {
return errors.Wrapf(err, "migration complete but failed to remove original data file: %v", origDataFile)
return fmt.Errorf("migration complete but failed to remove original data file: %v: %w", origDataFile, err)
}

if err := ioutil.WriteFile(filepath.Join(regHome, "meta.json"), []byte(`{"version": "1"}`), m.permissions); err != nil {
Expand All @@ -265,12 +263,12 @@ func readVersion(regHome, migrateFile string) (registryVersion, error) {

tmp, err := ioutil.ReadFile(metaFile)
if err != nil {
return noRegistry, errors.Wrap(err, "failed to open meta file")
return noRegistry, fmt.Errorf("failed to open meta file: %w", err)
}

meta := struct{ Version string }{}
if err := json.Unmarshal(tmp, &meta); err != nil {
return noRegistry, errors.Wrap(err, "failed reading meta file")
return noRegistry, fmt.Errorf("failed reading meta file: %w", err)
}

return registryVersion(meta.Version), nil
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ require (
github.com/josephspurrier/goversioninfo v0.0.0-20190209210621-63e6d1acd3dd
github.com/lib/pq v1.10.3
github.com/magefile/mage v1.13.0
github.com/mailru/easyjson v0.7.6 // indirect
github.com/mattn/go-colorable v0.1.12
github.com/miekg/dns v1.1.42
github.com/mitchellh/gox v1.0.1
Expand Down Expand Up @@ -128,8 +129,8 @@ require (
go.elastic.co/ecszap v1.0.0
go.elastic.co/go-licence-detector v0.5.0
go.etcd.io/bbolt v1.3.6
go.uber.org/atomic v1.8.0
go.uber.org/multierr v1.6.0
go.uber.org/atomic v1.9.0
go.uber.org/multierr v1.8.0
go.uber.org/zap v1.21.0
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616
Expand Down Expand Up @@ -159,7 +160,7 @@ require (
)

require (
github.com/elastic/elastic-agent-libs v0.1.1
github.com/elastic/elastic-agent-libs v0.2.1
github.com/shirou/gopsutil/v3 v3.21.12
go.elastic.co/apm/module/apmelasticsearch/v2 v2.0.0
go.elastic.co/apm/module/apmhttp/v2 v2.0.0
Expand Down Expand Up @@ -243,7 +244,6 @@ require (
github.com/karrick/godirwalk v1.15.8 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/markbates/pkger v0.17.0 // indirect
github.com/mattn/go-ieproxy v0.0.0-20191113090002-7c0f6868bffe // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
Expand Down
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -569,8 +569,8 @@ github.com/elastic/dhcp v0.0.0-20200227161230-57ec251c7eb3 h1:lnDkqiRFKm0rxdljqr
github.com/elastic/dhcp v0.0.0-20200227161230-57ec251c7eb3/go.mod h1:aPqzac6AYkipvp4hufTyMj5PDIphF3+At8zr7r51xjY=
github.com/elastic/elastic-agent-client/v7 v7.0.0-20210727140539-f0905d9377f6 h1:nFvXHBjYK3e9+xF0WKDeAKK4aOO51uC28s+L9rBmilo=
github.com/elastic/elastic-agent-client/v7 v7.0.0-20210727140539-f0905d9377f6/go.mod h1:uh/Gj9a0XEbYoM4NYz4LvaBVARz3QXLmlNjsrKY9fTc=
github.com/elastic/elastic-agent-libs v0.1.1 h1:13Y268vfgkZK99Ql1X2WdT8jjpd01stln2wLvlewkbo=
github.com/elastic/elastic-agent-libs v0.1.1/go.mod h1:h8K/f7RcdxM2f19VahcS1jeco170ItqV9N7HyYsn9Ss=
github.com/elastic/elastic-agent-libs v0.2.1 h1:v6dxLeyC7jsyUcRXLPRqRHxqREx2wYWp9epxDu1VasM=
github.com/elastic/elastic-agent-libs v0.2.1/go.mod h1:1xDLBhIqBIjhJ7lr2s+xRFFkQHpitSp8q2zzv1Dqg+s=
github.com/elastic/fsevents v0.0.0-20181029231046-e1d381a4d270 h1:cWPqxlPtir4RoQVCpGSRXmLqjEHpJKbR60rxh1nQZY4=
github.com/elastic/fsevents v0.0.0-20181029231046-e1d381a4d270/go.mod h1:Msl1pdboCbArMF/nSCDUXgQuWTeoMmE/z8607X+k7ng=
github.com/elastic/glog v1.0.1-0.20210831205241-7d8b5c89dfc4 h1:ViJxdtOsHeO+SWVekzM82fYHH1xnvZ8CvGPXZj+G4YI=
Expand Down Expand Up @@ -1824,8 +1824,9 @@ go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/atomic v1.5.1/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.8.0 h1:CUhrE4N1rqSE6FM9ecihEjRkLQu8cDfgDyoOs83mEY4=
go.uber.org/atomic v1.8.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/goleak v1.0.0/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A=
go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A=
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
Expand All @@ -1835,8 +1836,9 @@ go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/
go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4=
go.uber.org/multierr v1.4.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4=
go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU=
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/multierr v1.8.0 h1:dg6GjLku4EH+249NNmoIciG9N/jURbDG+pFlTkhzIC8=
go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA=
go.uber.org/zap v1.9.1/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
Expand Down
Loading

0 comments on commit 2254bb7

Please sign in to comment.