From 39e3a1ac3e1f5cd1dce64e696b881fb9367f3709 Mon Sep 17 00:00:00 2001 From: Daniel Bennett Date: Mon, 27 Feb 2023 11:27:40 -0600 Subject: [PATCH] build/cli: Add BuildDate (#16216) * build: add BuildDate to version info will be used in enterprise to compare to license expiration time * cli: multi-line version output, add BuildDate before: $ nomad version Nomad v1.4.3 (coolfakecommithashomgoshsuchacoolonewoww) after: $ nomad version Nomad v1.5.0-dev BuildDate 2023-02-17T19:29:26Z Revision coolfakecommithashomgoshsuchacoolonewoww compare consul: $ consul version Consul v1.14.4 Revision dae670fe Build Date 2023-01-26T15:47:10Z Protocol 2 spoken by default, blah blah blah... and vault: $ vault version Vault v1.12.3 (209b3dd99fe8ca320340d08c70cff5f620261f9b), built 2023-02-02T09:07:27Z * docs: update version command output --- .changelog/16216.txt | 3 +++ GNUmakefile | 11 +++++++++-- command/agent/agent.go | 1 + nomad/config.go | 3 +++ nomad/server_setup.go | 5 +++++ version/version.go | 16 +++++++++++++++- website/content/docs/commands/version.mdx | 15 ++++++++++----- 7 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 .changelog/16216.txt diff --git a/.changelog/16216.txt b/.changelog/16216.txt new file mode 100644 index 000000000..c189e15c1 --- /dev/null +++ b/.changelog/16216.txt @@ -0,0 +1,3 @@ +```release-note:improvement +cli: multi-line `nomad version` output, add BuildDate +``` diff --git a/GNUmakefile b/GNUmakefile index 50fb65280..5a9d46264 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -3,10 +3,17 @@ PROJECT_ROOT := $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST))))) THIS_OS := $(shell uname | cut -d- -f1) THIS_ARCH := $(shell uname -m) +GO_MODULE = github.com/hashicorp/nomad + GIT_COMMIT := $(shell git rev-parse HEAD) GIT_DIRTY := $(if $(shell git status --porcelain),+CHANGES) +GIT_COMMIT_FLAG = $(GO_MODULE)/version.GitCommit=$(GIT_COMMIT)$(GIT_DIRTY) -GO_LDFLAGS := "-X github.com/hashicorp/nomad/version.GitCommit=$(GIT_COMMIT)$(GIT_DIRTY)" +# build date is based on most recent commit, in RFC3339 format +BUILD_DATE ?= $(shell TZ=UTC0 git show -s --format=%cd --date=format-local:'%Y-%m-%dT%H:%M:%SZ' HEAD) +BUILD_DATE_FLAG = $(GO_MODULE)/version.BuildDate=$(BUILD_DATE) + +GO_LDFLAGS = -X $(GIT_COMMIT_FLAG) -X $(BUILD_DATE_FLAG) ifneq (MSYS_NT,$(THIS_OS)) # GOPATH supports PATH style multi-paths; assume the first entry is favorable. @@ -91,7 +98,7 @@ endif GOOS=$(firstword $(subst _, ,$*)) \ GOARCH=$(lastword $(subst _, ,$*)) \ CC=$(CC) \ - go build -trimpath -ldflags $(GO_LDFLAGS) -tags "$(GO_TAGS)" -o $(GO_OUT) + go build -trimpath -ldflags "$(GO_LDFLAGS)" -tags "$(GO_TAGS)" -o $(GO_OUT) ifneq (armv7l,$(THIS_ARCH)) pkg/linux_arm/nomad: CC = arm-linux-gnueabihf-gcc diff --git a/command/agent/agent.go b/command/agent/agent.go index 2b964eec7..33e865fed 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -171,6 +171,7 @@ func convertServerConfig(agentConfig *Config) (*nomad.Config, error) { conf.EnableDebug = agentConfig.EnableDebug conf.Build = agentConfig.Version.VersionNumber() + conf.BuildDate = agentConfig.Version.BuildDate conf.Revision = agentConfig.Version.Revision if agentConfig.Region != "" { conf.Region = agentConfig.Region diff --git a/nomad/config.go b/nomad/config.go index b59b14352..6c789aef4 100644 --- a/nomad/config.go +++ b/nomad/config.go @@ -127,6 +127,9 @@ type Config struct { // operators track which versions are actively deployed Build string + // BuildDate is the time of the git commit used to build the program. + BuildDate time.Time + // Revision is a string that carries the version.GitCommit of Nomad that // was compiled. Revision string diff --git a/nomad/server_setup.go b/nomad/server_setup.go index e56a9b745..a37d8b517 100644 --- a/nomad/server_setup.go +++ b/nomad/server_setup.go @@ -1,6 +1,8 @@ package nomad import ( + "time" + "github.com/hashicorp/go-hclog" "golang.org/x/exp/slices" ) @@ -8,6 +10,9 @@ import ( // LicenseConfig allows for tunable licensing config // primarily used for enterprise testing type LicenseConfig struct { + // BuildDate is the time of the git commit used to build the program. + BuildDate time.Time + // LicenseEnvBytes is the license bytes to use for the server's license LicenseEnvBytes string diff --git a/version/version.go b/version/version.go index 200741d0b..4bcb9f734 100644 --- a/version/version.go +++ b/version/version.go @@ -3,9 +3,14 @@ package version import ( "bytes" "fmt" + "time" ) var ( + // BuildDate is the time of the git commit used to build the program, + // in RFC3339 format. It is filled in by the compiler via makefile. + BuildDate string + // The git commit that was compiled. This will be filled in by the compiler. GitCommit string GitDescribe string @@ -24,6 +29,7 @@ var ( // VersionInfo type VersionInfo struct { + BuildDate time.Time Revision string Version string VersionPrerelease string @@ -50,7 +56,11 @@ func GetVersion() *VersionInfo { rel = "dev" } + // on parse error, will be zero value time.Time{} + built, _ := time.Parse(time.RFC3339, BuildDate) + return &VersionInfo{ + BuildDate: built, Revision: GitCommit, Version: ver, VersionPrerelease: rel, @@ -84,8 +94,12 @@ func (c *VersionInfo) FullVersionNumber(rev bool) string { fmt.Fprintf(&versionString, "+%s", c.VersionMetadata) } + if !c.BuildDate.IsZero() { + fmt.Fprintf(&versionString, "\nBuildDate %s", c.BuildDate.Format(time.RFC3339)) + } + if rev && c.Revision != "" { - fmt.Fprintf(&versionString, " (%s)", c.Revision) + fmt.Fprintf(&versionString, "\nRevision %s", c.Revision) } return versionString.String() diff --git a/website/content/docs/commands/version.mdx b/website/content/docs/commands/version.mdx index e5b4ef616..b58b9ae22 100644 --- a/website/content/docs/commands/version.mdx +++ b/website/content/docs/commands/version.mdx @@ -8,7 +8,7 @@ description: | # Command: version The `version` command displays build information about the running binary, -including the release version and the exact revision. +including the release version, build date, and the exact revision. ## Usage @@ -18,13 +18,18 @@ nomad version ## Output -This command prints both the version number as well as the exact commit SHA used -during the build. The SHA may also have the string `+CHANGES` appended to the -end, indicating that local, uncommitted changes were detected at build time. +This command prints the version number and info about the git commit that was +used to build the binary. `BuildDate` is when the commit was made, +and `Revision` is the exact commit SHA. + +The SHA may also have the string `+CHANGES` appended to the end, +indicating that local, uncommitted changes were detected at build time. ## Examples ```shell-session $ nomad version -Nomad v0.0.0-615-gcf3c6aa-dev (cf3c6aa8a75a689987b689d75ae2ba73458465cb+CHANGES) +Nomad v1.5.0 +BuildDate 2023-02-17T19:29:26Z +Revision a536284ebcfb4ff26065955abae446d81cc92b87+CHANGES ```