From fe12fdd1c9219791d69541fa18306666b7e1bd04 Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Thu, 14 May 2020 11:35:52 +0200 Subject: [PATCH] [Elastic Agent] Pick up version from libbeat (#18350) [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 c0cb50bb6ae..1c0f62cff7f 100644 --- a/x-pack/elastic-agent/CHANGELOG.asciidoc +++ b/x-pack/elastic-agent/CHANGELOG.asciidoc @@ -55,3 +55,4 @@ - Enable introspecting configuration {pull}18124[18124] - Follow home path for all config files {pull}18161[18161] - Use nested objects so fleet can handle metadata correctly {pull}18234[18234] +- 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 fdb19bdf4f9..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 = "8.0.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)) - }) -}