Skip to content

Commit

Permalink
Copy missing go version to the generated Beats (#17105)
Browse files Browse the repository at this point in the history
## What does this PR do?

This PR makes beat generator copy `.go-version` to the vendored beats folder as it is not copied automatically.

## Why is it important?

If the file `.go-version` is missing, the beat cannot be generated and package properly.
  • Loading branch information
kvch committed Mar 19, 2020
1 parent 1663236 commit 6cf8764
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 68 deletions.
21 changes: 10 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,16 @@ jobs:
stage: test

# Generators
# https://github.com/elastic/beats/issues/16951
#- os: linux
# before_install: .ci/scripts/travis_has_changes.sh generator metricbeat libbeat || travis_terminate 0
# env: TARGETS="-C generator/_templates/metricbeat test test-package"
# go: $TRAVIS_GO_VERSION
# stage: test
#- os: linux
# before_install: .ci/scripts/travis_has_changes.sh generator libbeat || travis_terminate 0
# env: TARGETS="-C generator/_templates/beat test test-package"
# go: $TRAVIS_GO_VERSION
# stage: test
- os: linux
before_install: .ci/scripts/travis_has_changes.sh generator metricbeat libbeat || travis_terminate 0
env: TARGETS="-C generator/_templates/metricbeat test test-package"
go: $TRAVIS_GO_VERSION
stage: test
- os: linux
before_install: .ci/scripts/travis_has_changes.sh generator libbeat || travis_terminate 0
env: TARGETS="-C generator/_templates/beat test test-package"
go: $TRAVIS_GO_VERSION
stage: test

- os: osx
before_install: .ci/scripts/travis_has_changes.sh generator metricbeat libbeat || travis_terminate 0
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

0 comments on commit 6cf8764

Please sign in to comment.