Add version package

This PR adds a version package and consolidates version strings into a
Version struct.
This commit is contained in:
Alex Dadgar 2017-08-16 15:42:15 -07:00
parent 755cc33f13
commit 1a86aecf55
18 changed files with 120 additions and 101 deletions

View File

@ -419,7 +419,7 @@ func (r *AllocRunner) saveAllocRunnerState() error {
// Write immutable data iff it hasn't been written yet
if !r.immutablePersisted {
immutable := &allocRunnerImmutableState{
Version: r.config.Version,
Version: r.config.Version.VersionNumber(),
}
if err := putObject(allocBkt, allocRunnerStateImmutableKey, &immutable); err != nil {

View File

@ -12,6 +12,7 @@ import (
"github.com/hashicorp/nomad/helper/tlsutil"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/nomad/structs/config"
"github.com/hashicorp/nomad/version"
)
var (
@ -129,10 +130,7 @@ type Config struct {
Options map[string]string
// Version is the version of the Nomad client
Version string
// Revision is the commit number of the Nomad client
Revision string
Version *version.VersionInfo
// ConsulConfig is this Agent's Consul configuration
ConsulConfig *config.ConsulConfig

View File

@ -627,7 +627,7 @@ func (d *DockerDriver) Start(ctx *ExecContext, task *structs.Task) (*StartRespon
Image: d.driverConfig.ImageName,
ImageID: d.imageID,
containerID: container.ID,
version: d.config.Version,
version: d.config.Version.VersionNumber(),
killTimeout: GetKillTimeout(task.KillTimeout, maxKill),
maxKillTimeout: maxKill,
doneCh: make(chan bool),

View File

@ -162,7 +162,7 @@ func (d *ExecDriver) Start(ctx *ExecContext, task *structs.Task) (*StartResponse
killTimeout: GetKillTimeout(task.KillTimeout, maxKill),
maxKillTimeout: maxKill,
logger: d.logger,
version: d.config.Version,
version: d.config.Version.VersionNumber(),
doneCh: make(chan struct{}),
waitCh: make(chan *dstructs.WaitResult, 1),
taskDir: ctx.TaskDir,

View File

@ -290,7 +290,7 @@ func (d *JavaDriver) Start(ctx *ExecContext, task *structs.Task) (*StartResponse
taskDir: ctx.TaskDir.Dir,
killTimeout: GetKillTimeout(task.KillTimeout, maxKill),
maxKillTimeout: maxKill,
version: d.config.Version,
version: d.config.Version.VersionNumber(),
logger: d.logger,
doneCh: make(chan struct{}),
waitCh: make(chan *dstructs.WaitResult, 1),

View File

@ -280,7 +280,7 @@ func (d *QemuDriver) Start(ctx *ExecContext, task *structs.Task) (*StartResponse
userPid: ps.Pid,
killTimeout: GetKillTimeout(task.KillTimeout, maxKill),
maxKillTimeout: maxKill,
version: d.config.Version,
version: d.config.Version.VersionNumber(),
logger: d.logger,
doneCh: make(chan struct{}),
waitCh: make(chan *dstructs.WaitResult, 1),

View File

@ -165,7 +165,7 @@ func (d *RawExecDriver) Start(ctx *ExecContext, task *structs.Task) (*StartRespo
userPid: ps.Pid,
killTimeout: GetKillTimeout(task.KillTimeout, maxKill),
maxKillTimeout: maxKill,
version: d.config.Version,
version: d.config.Version.VersionNumber(),
logger: d.logger,
doneCh: make(chan struct{}),
waitCh: make(chan *dstructs.WaitResult, 1),

View File

@ -20,7 +20,7 @@ func NewNomadFingerprint(logger *log.Logger) Fingerprint {
}
func (f *NomadFingerprint) Fingerprint(config *client.Config, node *structs.Node) (bool, error) {
node.Attributes["nomad.version"] = config.Version
node.Attributes["nomad.revision"] = config.Revision
node.Attributes["nomad.version"] = config.Version.VersionNumber()
node.Attributes["nomad.revision"] = config.Version.Revision
return true, nil
}

View File

@ -435,7 +435,7 @@ func (r *TaskRunner) SaveState() error {
r.persistLock.Lock()
defer r.persistLock.Unlock()
snap := taskRunnerState{
Version: r.config.Version,
Version: r.config.Version.VersionNumber(),
ArtifactDownloaded: r.artifactsDownloaded,
TaskDirBuilt: r.taskDirBuilt,
PayloadRendered: r.payloadRendered,

View File

@ -102,7 +102,7 @@ func convertServerConfig(agentConfig *Config, logOutput io.Writer) (*nomad.Confi
}
conf.LogOutput = logOutput
conf.DevMode = agentConfig.DevMode
conf.Build = fmt.Sprintf("%s%s", agentConfig.Version, agentConfig.VersionPrerelease)
conf.Build = agentConfig.Version.VersionNumber()
if agentConfig.Region != "" {
conf.Region = agentConfig.Region
}
@ -308,8 +308,7 @@ func (a *Agent) clientConfig() (*clientconfig.Config, error) {
r.IOPS = a.config.Client.Reserved.IOPS
conf.GloballyReservedPorts = a.config.Client.Reserved.ParsedReservedPorts
conf.Version = fmt.Sprintf("%s%s", a.config.Version, a.config.VersionPrerelease)
conf.Revision = a.config.Revision
conf.Version = a.config.Version
if *a.config.Consul.AutoAdvertise && a.config.Consul.ClientServiceName == "" {
return nil, fmt.Errorf("client_service_name must be set when auto_advertise is enabled")

View File

@ -25,6 +25,7 @@ import (
"github.com/hashicorp/nomad/helper/flag-helpers"
"github.com/hashicorp/nomad/helper/gated-writer"
"github.com/hashicorp/nomad/nomad/structs/config"
"github.com/hashicorp/nomad/version"
"github.com/hashicorp/scada-client/scada"
"github.com/mitchellh/cli"
"github.com/posener/complete"
@ -38,11 +39,9 @@ const gracefulTimeout = 5 * time.Second
// ShutdownCh. If two messages are sent on the ShutdownCh it will forcibly
// exit.
type Command struct {
Revision string
Version string
VersionPrerelease string
Ui cli.Ui
ShutdownCh <-chan struct{}
Version *version.VersionInfo
Ui cli.Ui
ShutdownCh <-chan struct{}
args []string
agent *Agent
@ -199,9 +198,7 @@ func (c *Command) readConfig() *Config {
config = config.Merge(cmdConfig)
// Set the version info
config.Revision = c.Revision
config.Version = c.Version
config.VersionPrerelease = c.VersionPrerelease
// Normalize binds, ports, addresses, and advertise
if err := config.normalizeAddrs(); err != nil {
@ -361,9 +358,9 @@ func (c *Command) setupAgent(config *Config, logOutput io.Writer) error {
// Setup update checking
if !config.DisableUpdateCheck {
version := config.Version
if config.VersionPrerelease != "" {
version += fmt.Sprintf("-%s", config.VersionPrerelease)
version := config.Version.Version
if config.Version.VersionPrerelease != "" {
version += fmt.Sprintf("-%s", config.Version.VersionPrerelease)
}
updateParams := &checkpoint.CheckParams{
Product: "nomad",
@ -392,12 +389,7 @@ func (c *Command) checkpointResults(results *checkpoint.CheckResponse, err error
return
}
if results.Outdated {
versionStr := c.Version
if c.VersionPrerelease != "" {
versionStr += fmt.Sprintf("-%s", c.VersionPrerelease)
}
c.Ui.Error(fmt.Sprintf("Newer Nomad version available: %s (currently running: %s)", results.CurrentVersion, versionStr))
c.Ui.Error(fmt.Sprintf("Newer Nomad version available: %s (currently running: %s)", results.CurrentVersion, c.Version.VersionNumber()))
}
for _, alert := range results.Alerts {
switch alert.Level {
@ -485,7 +477,7 @@ func (c *Command) Run(args []string) int {
// Compile agent information for output later
info := make(map[string]string)
info["version"] = fmt.Sprintf("%s%s", config.Version, config.VersionPrerelease)
info["version"] = config.Version.VersionNumber()
info["client"] = strconv.FormatBool(config.Client.Enabled)
info["log level"] = config.LogLevel
info["server"] = strconv.FormatBool(config.Server.Enabled)
@ -746,7 +738,7 @@ func (c *Command) setupSCADA(config *Config) error {
scadaConfig := &scada.Config{
Service: "nomad",
Version: fmt.Sprintf("%s%s", config.Version, config.VersionPrerelease),
Version: config.Version.VersionNumber(),
ResourceType: "nomad-cluster",
Meta: map[string]string{
"auto-join": strconv.FormatBool(config.Atlas.Join),

View File

@ -20,6 +20,7 @@ import (
"github.com/hashicorp/nomad/helper"
"github.com/hashicorp/nomad/nomad"
"github.com/hashicorp/nomad/nomad/structs/config"
"github.com/hashicorp/nomad/version"
)
// Config is the configuration for the Nomad agent.
@ -114,9 +115,7 @@ type Config struct {
DevMode bool `mapstructure:"-"`
// Version information is set at compilation time
Revision string
Version string
VersionPrerelease string
Version *version.VersionInfo
// List of config files that have been loaded (in order)
Files []string `mapstructure:"-"`

View File

@ -1,12 +1,13 @@
package command
import (
"github.com/hashicorp/nomad/version"
"github.com/mitchellh/cli"
)
// VersionCommand is a Command implementation prints the version.
type VersionCommand struct {
Version string
Version *version.VersionInfo
Ui cli.Ui
}
@ -15,7 +16,7 @@ func (c *VersionCommand) Help() string {
}
func (c *VersionCommand) Run(_ []string) int {
c.Ui.Output(c.Version)
c.Ui.Output(c.Version.FullVersionNumber(true))
return 0
}

View File

@ -5,6 +5,7 @@ import (
"github.com/hashicorp/nomad/command"
"github.com/hashicorp/nomad/command/agent"
"github.com/hashicorp/nomad/version"
"github.com/mitchellh/cli"
)
@ -32,11 +33,9 @@ func Commands(metaPtr *command.Meta) map[string]cli.CommandFactory {
},
"agent": func() (cli.Command, error) {
return &agent.Command{
Revision: GitCommit,
Version: Version,
VersionPrerelease: VersionPrerelease,
Ui: meta.Ui,
ShutdownCh: make(chan struct{}),
Version: version.GetVersion(),
Ui: meta.Ui,
ShutdownCh: make(chan struct{}),
}, nil
},
"agent-info": func() (cli.Command, error) {
@ -237,7 +236,7 @@ func Commands(metaPtr *command.Meta) map[string]cli.CommandFactory {
},
"version": func() (cli.Command, error) {
return &command.VersionCommand{
Version: PrettyVersion(GetVersionParts()),
Version: version.GetVersion(),
Ui: meta.Ui,
}, nil
},

View File

@ -7,6 +7,7 @@ import (
"sort"
"strings"
"github.com/hashicorp/nomad/version"
"github.com/mitchellh/cli"
"github.com/sean-/seed"
)
@ -43,7 +44,7 @@ func RunCustom(args []string, commands map[string]cli.CommandFactory) int {
cli := &cli.CLI{
Name: "nomad",
Version: PrettyVersion(GetVersionParts()),
Version: version.GetVersion().FullVersionNumber(true),
Args: args,
Commands: commands,
Autocomplete: true,

View File

@ -3,7 +3,7 @@ set -e
GIT_COMMIT="$(git rev-parse HEAD)"
GIT_DIRTY="$(test -n "`git status --porcelain`" && echo "+CHANGES" || true)"
LDFLAG="main.GitCommit=${GIT_COMMIT}${GIT_DIRTY}"
LDFLAG="github.com/hashicorp/nomad/version.GitCommit=${GIT_COMMIT}${GIT_DIRTY}"
TAGS="nomad_test"
if [[ $(uname) == "Linux" ]]; then

View File

@ -1,54 +0,0 @@
package main
import (
"bytes"
"fmt"
)
// The git commit that was compiled. This will be filled in by the compiler.
var GitCommit string
var GitDescribe string
// The main version number that is being run at the moment.
const Version = "0.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.
const VersionPrerelease = "dev"
// GetVersionParts returns the Nomad version strings. Printing of the Nomad
// version should be used in conjunction with the PrettyVersion method.
func GetVersionParts() (rev, ver, rel string) {
ver = Version
rel = VersionPrerelease
if GitDescribe != "" {
ver = GitDescribe
// Trim off a leading 'v', we append it anyways.
if ver[0] == 'v' {
ver = ver[1:]
}
}
if GitDescribe == "" && rel == "" && VersionPrerelease != "" {
rel = "dev"
}
return GitCommit, ver, rel
}
// PrettyVersion takes the version parts and formats it in a human readable
// string.
func PrettyVersion(revision, version, versionPrerelease string) string {
var versionString bytes.Buffer
fmt.Fprintf(&versionString, "Nomad v%s", version)
if versionPrerelease != "" {
fmt.Fprintf(&versionString, "-%s", versionPrerelease)
if revision != "" {
fmt.Fprintf(&versionString, " (%s)", revision)
}
}
return versionString.String()
}

84
version/version.go Normal file
View File

@ -0,0 +1,84 @@
package version
import (
"bytes"
"fmt"
)
var (
// 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 = "0.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 {
Revision string
Version string
VersionPrerelease string
VersionMetadata string
}
func GetVersion() *VersionInfo {
ver := Version
rel := VersionPrerelease
md := VersionMetadata
if GitDescribe != "" {
ver = GitDescribe
}
if GitDescribe == "" && rel == "" && VersionPrerelease != "" {
rel = "dev"
}
return &VersionInfo{
Revision: GitCommit,
Version: ver,
VersionPrerelease: rel,
VersionMetadata: md,
}
}
func (c *VersionInfo) VersionNumber() string {
version := fmt.Sprintf("%s", 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 rev && c.Revision != "" {
fmt.Fprintf(&versionString, " (%s)", c.Revision)
}
return versionString.String()
}