From c003285cf59bd6995517bd9ed4e2c64e68c04a90 Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Fri, 15 May 2020 15:17:11 +0200 Subject: [PATCH] [Elastic Agent] Pick up version from libbeat (#18350) (#18559) [Elastic Agent] Pick up version from libbeat (#18350) --- x-pack/elastic-agent/CHANGELOG.asciidoc | 1 + x-pack/elastic-agent/magefile.go | 67 ++++++++----------- .../elastic-agent/pkg/release/release_dev.go | 16 ----- x-pack/elastic-agent/pkg/release/version.go | 27 ++------ .../elastic-agent/pkg/release/version_test.go | 42 ------------ 5 files changed, 35 insertions(+), 118 deletions(-) delete mode 100644 x-pack/elastic-agent/pkg/release/release_dev.go delete mode 100644 x-pack/elastic-agent/pkg/release/version_test.go diff --git a/x-pack/elastic-agent/CHANGELOG.asciidoc b/x-pack/elastic-agent/CHANGELOG.asciidoc index a38beab5118..d884b7e8ea2 100644 --- a/x-pack/elastic-agent/CHANGELOG.asciidoc +++ b/x-pack/elastic-agent/CHANGELOG.asciidoc @@ -56,3 +56,4 @@ - Do not require unnecessary configuration {pull}18003[18003] - Use nested objects so fleet can handle metadata correctly {pull}18234[18234] - Enable debug log level for Metricbeat and Filebeat when run under the Elastic Agent. {pull}17935[17935] +- Pick up version from libbeat {pull}18350[18350] diff --git a/x-pack/elastic-agent/magefile.go b/x-pack/elastic-agent/magefile.go index 8733b0a3d10..2215d5a3d5a 100644 --- a/x-pack/elastic-agent/magefile.go +++ b/x-pack/elastic-agent/magefile.go @@ -108,7 +108,7 @@ func (Build) GenerateConfig() error { // Do not use directly, use crossBuild instead. func GolangCrossBuildOSS() error { params := devtools.DefaultGolangCrossBuildArgs() - params.LDFlags = flagsSet() + injectBuildVars(params.Vars) return devtools.GolangCrossBuild(params) } @@ -117,7 +117,8 @@ func GolangCrossBuildOSS() error { func GolangCrossBuild() error { params := devtools.DefaultGolangCrossBuildArgs() params.OutputDir = "build/golang-crossbuild" - params.LDFlags = flagsSet() + injectBuildVars(params.Vars) + if err := devtools.GolangCrossBuild(params); err != nil { return err } @@ -136,32 +137,23 @@ func BuildGoDaemon() error { // BinaryOSS build the fleet artifact. func (Build) BinaryOSS() error { mg.Deps(Prepare.Env) - return RunGo( - "build", - "-o", filepath.Join(buildDir, "elastic-agent-oss"), - "-ldflags", flags(), - ) + buildArgs := devtools.DefaultBuildArgs() + buildArgs.Name = "elastic-agent-oss" + buildArgs.OutputDir = buildDir + injectBuildVars(buildArgs.Vars) + + return devtools.Build(buildArgs) } // Binary build the fleet artifact. func (Build) Binary() error { mg.Deps(Prepare.Env) - return RunGo( - "build", - "-o", filepath.Join(buildDir, "elastic-agent"), - "-ldflags", flags(), - ) -} -// Dev make a special build with the Dev tags. -func (Build) Dev() error { - mg.Deps(Prepare.Env) - return RunGo( - "build", - "-tags", "dev", - "-o", filepath.Join(buildDir, "elastic-agent"), - "-ldflags", flags(), - ) + buildArgs := devtools.DefaultBuildArgs() + buildArgs.OutputDir = buildDir + injectBuildVars(buildArgs.Vars) + + return devtools.Build(buildArgs) } // Clean up dev environment. @@ -325,22 +317,6 @@ func commitID() string { return commitID } -func flags() string { - return strings.Join(flagsSet(), " ") -} - -func flagsSet() []string { - ts := time.Now().Format(time.RFC3339) - commitID := commitID() - isSnapshot, _ := os.LookupEnv(snapshotEnv) - - return []string{ - fmt.Sprintf(`-X "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/release.buildTime=%s"`, ts), - fmt.Sprintf(`-X "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/release.commit=%s"`, commitID), - fmt.Sprintf(` -X "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/release.snapshot=%s"`, isSnapshot), - } -} - // Update is an alias for executing fields, dashboards, config, includes. func Update() { mg.SerialDeps(Config, BuildSpec, BuildFleetCfg) @@ -540,3 +516,18 @@ func dockerTag() string { return tagBase } + +func buildVars() map[string]string { + vars := make(map[string]string) + + isSnapshot, _ := os.LookupEnv(snapshotEnv) + vars["github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/release.snapshot"] = isSnapshot + + return vars +} + +func injectBuildVars(m map[string]string) { + for k, v := range buildVars() { + m[k] = v + } +} diff --git a/x-pack/elastic-agent/pkg/release/release_dev.go b/x-pack/elastic-agent/pkg/release/release_dev.go deleted file mode 100644 index a8f9db58db1..00000000000 --- a/x-pack/elastic-agent/pkg/release/release_dev.go +++ /dev/null @@ -1,16 +0,0 @@ -// 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. - -// +build dev - -package release - -import "os" - -func init() { - envVersion, ok := os.LookupEnv("BEATS_VERSION") - if ok { - version = envVersion - } -} diff --git a/x-pack/elastic-agent/pkg/release/version.go b/x-pack/elastic-agent/pkg/release/version.go index 3561ddc5047..7c139d943a9 100644 --- a/x-pack/elastic-agent/pkg/release/version.go +++ b/x-pack/elastic-agent/pkg/release/version.go @@ -7,43 +7,26 @@ package release import ( "strconv" "time" -) - -// version is the current version of the elastic-agent. -var version = "7.8.0" - -// buildHash is the hash of the current build. -var commit = "" -// buildTime when the binary was build -var buildTime = "" - -// qualifier returns the version qualifier like alpha1. -var qualifier = "" + libbeatVersion "github.com/elastic/beats/v7/libbeat/version" +) // snapshot is a flag marking build as a snapshot. var snapshot = "" // Commit returns the current build hash or unknown if it was not injected in the build process. func Commit() string { - return commit + return libbeatVersion.Commit() } // BuildTime returns the build time of the binaries. func BuildTime() time.Time { - t, err := time.Parse(time.RFC3339, buildTime) - if err != nil { - return time.Time{} - } - return t + return libbeatVersion.BuildTime() } // Version returns the version of the application. func Version() string { - if qualifier == "" { - return version - } - return version + "-" + qualifier + return libbeatVersion.GetDefaultVersion() } // Snapshot returns true if binary was built as snapshot. diff --git a/x-pack/elastic-agent/pkg/release/version_test.go b/x-pack/elastic-agent/pkg/release/version_test.go deleted file mode 100644 index 279644c6750..00000000000 --- a/x-pack/elastic-agent/pkg/release/version_test.go +++ /dev/null @@ -1,42 +0,0 @@ -// 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. - -package release - -import ( - "testing" - "time" - - "github.com/stretchr/testify/assert" -) - -func TestVersion(t *testing.T) { - t.Run("set version without qualifier", func(t *testing.T) { - old := version - defer func() { version = old }() - version = "8.x.x" - assert.Equal(t, Version(), version) - }) - - t.Run("set version with qualifier", func(t *testing.T) { - old := version - defer func() { version = old }() - version = "8.x.x" - qualifier = "alpha1" - assert.Equal(t, Version(), version+"-"+qualifier) - }) - - t.Run("get commit hash", func(t *testing.T) { - commit = "abc1234" - assert.Equal(t, Commit(), commit) - }) - - t.Run("get build time", func(t *testing.T) { - ts := time.Now().Format(time.RFC3339) - old := buildTime - defer func() { buildTime = old }() - buildTime = ts - assert.Equal(t, ts, BuildTime().Format(time.RFC3339)) - }) -}