Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherry-pick #17105 to 7.x: Copy missing go version to the generated Beats #17118

Merged
merged 1 commit into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,14 @@ jobs:
stage: test

# Generators
# https://github.com/elastic/beats/issues/16951
#- os: linux
# env: TARGETS="-C generator/_templates/metricbeat test test-package"
# go: $TRAVIS_GO_VERSION
# stage: test
#- os: linux
# env: TARGETS="-C generator/_templates/beat test test-package"
# go: $TRAVIS_GO_VERSION
# stage: test
- os: linux
env: TARGETS="-C generator/_templates/metricbeat test test-package"
go: $TRAVIS_GO_VERSION
stage: test
- os: linux
env: TARGETS="-C generator/_templates/beat test test-package"
go: $TRAVIS_GO_VERSION
stage: test

- os: osx
env: TARGETS="-C generator/_templates/metricbeat test"
Expand Down
78 changes: 44 additions & 34 deletions dev-tools/mage/gomod.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,35 @@ import (
"os"
"path/filepath"

"github.com/pkg/errors"

"github.com/elastic/beats/v7/dev-tools/mage/gotool"
)

// copyModule contains a module name and the list of files or directories
// CopyModule contains a module name and the list of files or directories
// to copy recursively.
type copyModule struct {
name string
filesToCopy []string
type CopyModule struct {
Name string
FilesToCopy []string
}

var (
copyAll = []copyModule{
copyModule{
name: "github.com/godror/godror",
filesToCopy: []string{
"odpi",
copyAll = []CopyModule{
CopyModule{
Name: "github.com/tsg/go-daemon",
FilesToCopy: []string{
"src",
},
},
copyModule{
name: "github.com/tsg/go-daemon",
filesToCopy: []string{
"src",
CopyModule{
Name: "github.com/godror/godror",
FilesToCopy: []string{
"odpi",
},
},
}
filesToRemove = []string{
filepath.Join("vendor", "github.com", "yuin", "gopher-lua", "parse", "Makefile"),
filepath.Join("github.com", "yuin", "gopher-lua", "parse", "Makefile"),
}
)

Expand All @@ -57,49 +59,57 @@ func Vendor() error {

err := mod.Tidy()
if err != nil {
return err
return errors.Wrap(err, "error while running go mod tidy")
}

err = mod.Vendor()
if err != nil {
return err
return errors.Wrap(err, "error while running go mod vendor")
}

err = mod.Verify()
if err != nil {
return err
return errors.Wrap(err, "error while running go mod verify")
}

repo, err := GetProjectRepoInfo()
if err != nil {
return err
return errors.Wrap(err, "error while getting repository information")
}

vendorFolder := filepath.Join(repo.RootDir, "vendor")
err = CopyFilesToVendor(vendorFolder, copyAll)
if err != nil {
return errors.Wrap(err, "error copying required files")
}

for _, p := range filesToRemove {
p = filepath.Join(vendorFolder, p)
err = os.RemoveAll(p)
if err != nil {
return errors.Wrapf(err, "error while removing file: %s", p)
}
}
return nil
}

// copy packages which require the whole tree
for _, p := range copyAll {
path, err := gotool.ListModuleCacheDir(p.name)
// CopyFilesToVendor copies packages which require the whole tree
func CopyFilesToVendor(vendorFolder string, modulesToCopy []CopyModule) error {
for _, p := range modulesToCopy {
path, err := gotool.ListModuleCacheDir(p.Name)
if err != nil {
return err
return errors.Wrapf(err, "error while looking up cached dir of module: %s", p.Name)
}

for _, f := range p.filesToCopy {
for _, f := range p.FilesToCopy {
from := filepath.Join(path, f)
to := filepath.Join(vendorFolder, p.name, f)
copyTask := &CopyTask{Source: from, Dest: to, DirMode: os.ModeDir | 0750}
to := filepath.Join(vendorFolder, p.Name, f)
copyTask := &CopyTask{Source: from, Dest: to, Mode: 0600, DirMode: os.ModeDir | 0750}
err = copyTask.Execute()
if err != nil {
return err
return errors.Wrapf(err, "error while copying file from %s to %s", from, to)
}
}
}

for _, p := range filesToRemove {
p = filepath.Join(repo.RootDir, p)
err = os.RemoveAll(p)
if err != nil {
return err
}
}
return nil
}
1 change: 1 addition & 0 deletions generator/_templates/beat/{beat}/tools/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package tools

import (
_ "github.com/pierrre/gotestcover"
_ "github.com/tsg/go-daemon"
_ "golang.org/x/tools/cmd/goimports"

_ "github.com/mitchellh/gox"
Expand Down
1 change: 1 addition & 0 deletions generator/_templates/metricbeat/{beat}/tools/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package tools

import (
_ "github.com/pierrre/gotestcover"
_ "github.com/tsg/go-daemon"
_ "golang.org/x/tools/cmd/goimports"

_ "github.com/mitchellh/gox"
Expand Down
47 changes: 24 additions & 23 deletions generator/common/beatgen/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ import (
"github.com/elastic/beats/v7/dev-tools/mage/gotool"
)

var (
makefileDeps = []string{
"dev-tools",
"libbeat",
"licenses",
"metricbeat",
"script",
}
)

func InitModule() error {
err := gotool.Mod.Init()
if err != nil {
Expand Down Expand Up @@ -109,24 +99,35 @@ func copyReplacedModules() error {
func CopyVendor() error {
err := gotool.Mod.Vendor()
if err != nil {
return err
return errors.Wrapf(err, "error while running go mod vendor")
}

path, err := gotool.ListModuleCacheDir("github.com/elastic/beats/v7")
err = devtools.CopyFilesToVendor(
"./vendor",
[]devtools.CopyModule{
devtools.CopyModule{
Name: "github.com/elastic/beats/v7",
FilesToCopy: []string{
"dev-tools",
"libbeat",
"licenses",
"metricbeat",
"script",
".go-version",
},
},
devtools.CopyModule{
Name: "github.com/tsg/go-daemon",
FilesToCopy: []string{
"src",
},
},
},
)
if err != nil {
return err
return errors.Wrapf(err, "error while copying required files to vendor")
}

vendorPath := "./vendor/github.com/elastic/beats/v7"
for _, d := range makefileDeps {
from := filepath.Join(path, d)
to := filepath.Join(vendorPath, d)
copyTask := &devtools.CopyTask{Source: from, Dest: to, Mode: 0640, DirMode: os.ModeDir | 0750}
err = copyTask.Execute()
if err != nil {
return err
}
}
return nil
}

Expand Down