Skip to content

Commit

Permalink
[Elastic Agent] Pick up version from libbeat (#18350) (#18559)
Browse files Browse the repository at this point in the history
[Elastic Agent] Pick up version from libbeat (#18350)
  • Loading branch information
michalpristas authored May 15, 2020
1 parent 75f78d0 commit c003285
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 118 deletions.
1 change: 1 addition & 0 deletions x-pack/elastic-agent/CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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]
67 changes: 29 additions & 38 deletions x-pack/elastic-agent/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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
}
Expand All @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}
16 changes: 0 additions & 16 deletions x-pack/elastic-agent/pkg/release/release_dev.go

This file was deleted.

27 changes: 5 additions & 22 deletions x-pack/elastic-agent/pkg/release/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<unknown>"

// buildTime when the binary was build
var buildTime = "<unknown>"

// 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.
Expand Down
42 changes: 0 additions & 42 deletions x-pack/elastic-agent/pkg/release/version_test.go

This file was deleted.

0 comments on commit c003285

Please sign in to comment.