open-nomad/version/version.go
Tim Gross bb7f0edd6a
node pools: prevent panic on upsert during upgrades (#17474)
Whenever we write a Raft log entry for node pools, we need to first make sure
that all servers can safely apply the log without panicking. Gate upsert and
delete RPCs on all servers being upgraded to the minimum version.
2023-06-12 09:01:30 -04:00

110 lines
2.4 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
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
// The main version number that is being run at the moment.
Version = "1.6.0"
// A pre-release marker for the version. If this is "" (empty string)
// then it means that it is a final release. Otherwise, this is a pre-release
// such as "dev" (in development), "beta", "rc1", etc.
VersionPrerelease = "dev"
// VersionMetadata is metadata further describing the build type.
VersionMetadata = ""
)
// VersionInfo
type VersionInfo struct {
BuildDate time.Time
Revision string
Version string
VersionPrerelease string
VersionMetadata string
}
func (v *VersionInfo) Copy() *VersionInfo {
if v == nil {
return nil
}
nv := *v
return &nv
}
func GetVersion() *VersionInfo {
ver := Version
rel := VersionPrerelease
md := VersionMetadata
if GitDescribe != "" {
ver = GitDescribe
}
if GitDescribe == "" && rel == "" && VersionPrerelease != "" {
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,
VersionMetadata: md,
}
}
func (c *VersionInfo) VersionNumber() string {
version := c.Version
if c.VersionPrerelease != "" {
version = fmt.Sprintf("%s-%s", version, c.VersionPrerelease)
}
if c.VersionMetadata != "" {
version = fmt.Sprintf("%s+%s", version, c.VersionMetadata)
}
return version
}
func (c *VersionInfo) FullVersionNumber(rev bool) string {
var versionString bytes.Buffer
fmt.Fprintf(&versionString, "Nomad v%s", c.Version)
if c.VersionPrerelease != "" {
fmt.Fprintf(&versionString, "-%s", c.VersionPrerelease)
}
if c.VersionMetadata != "" {
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, "\nRevision %s", c.Revision)
}
return versionString.String()
}