From c26ecb7092fa2e726937deb3b9dd31d3512a144f Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Wed, 16 Aug 2017 15:42:15 -0700 Subject: [PATCH] Add version package This PR adds a version package and consolidates version strings into a Version struct. --- client/alloc_runner.go | 2 +- client/config/config.go | 6 +-- client/driver/docker.go | 2 +- client/driver/exec.go | 2 +- client/driver/java.go | 2 +- client/driver/qemu.go | 2 +- client/driver/raw_exec.go | 2 +- client/fingerprint/nomad.go | 4 +- client/task_runner.go | 2 +- command/agent/agent.go | 5 +-- command/agent/command.go | 28 +++++-------- command/agent/config.go | 5 +-- command/version.go | 5 ++- commands.go | 11 +++-- main.go | 3 +- scripts/build-dev.sh | 2 +- version.go | 54 ------------------------ version/version.go | 84 +++++++++++++++++++++++++++++++++++++ 18 files changed, 120 insertions(+), 101 deletions(-) delete mode 100644 version.go create mode 100644 version/version.go diff --git a/client/alloc_runner.go b/client/alloc_runner.go index 877fc95ee..d6486734e 100644 --- a/client/alloc_runner.go +++ b/client/alloc_runner.go @@ -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 { diff --git a/client/config/config.go b/client/config/config.go index ed98bdc63..4f9929b20 100644 --- a/client/config/config.go +++ b/client/config/config.go @@ -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 diff --git a/client/driver/docker.go b/client/driver/docker.go index df87a07d7..ee52c229a 100644 --- a/client/driver/docker.go +++ b/client/driver/docker.go @@ -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), diff --git a/client/driver/exec.go b/client/driver/exec.go index f882492a0..751c2081e 100644 --- a/client/driver/exec.go +++ b/client/driver/exec.go @@ -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, diff --git a/client/driver/java.go b/client/driver/java.go index 4bd4dd2c2..601ed5263 100644 --- a/client/driver/java.go +++ b/client/driver/java.go @@ -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), diff --git a/client/driver/qemu.go b/client/driver/qemu.go index b0a53b594..8e744940d 100644 --- a/client/driver/qemu.go +++ b/client/driver/qemu.go @@ -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), diff --git a/client/driver/raw_exec.go b/client/driver/raw_exec.go index a2e29d6f2..c12c8cdbe 100644 --- a/client/driver/raw_exec.go +++ b/client/driver/raw_exec.go @@ -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), diff --git a/client/fingerprint/nomad.go b/client/fingerprint/nomad.go index 5ac78d091..0db894196 100644 --- a/client/fingerprint/nomad.go +++ b/client/fingerprint/nomad.go @@ -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 } diff --git a/client/task_runner.go b/client/task_runner.go index d945d4dbf..544d2af5f 100644 --- a/client/task_runner.go +++ b/client/task_runner.go @@ -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, diff --git a/command/agent/agent.go b/command/agent/agent.go index 251121cdb..bc238443a 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -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") diff --git a/command/agent/command.go b/command/agent/command.go index 181eb8e38..56712a362 100644 --- a/command/agent/command.go +++ b/command/agent/command.go @@ -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), diff --git a/command/agent/config.go b/command/agent/config.go index 4c07e3436..331a8febf 100644 --- a/command/agent/config.go +++ b/command/agent/config.go @@ -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:"-"` diff --git a/command/version.go b/command/version.go index 0e126a527..c7527862a 100644 --- a/command/version.go +++ b/command/version.go @@ -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 } diff --git a/commands.go b/commands.go index fd0426a8c..cdf92b84d 100644 --- a/commands.go +++ b/commands.go @@ -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 }, diff --git a/main.go b/main.go index dbe27b92e..d04d272da 100644 --- a/main.go +++ b/main.go @@ -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, diff --git a/scripts/build-dev.sh b/scripts/build-dev.sh index e109a2cf8..9668a343d 100755 --- a/scripts/build-dev.sh +++ b/scripts/build-dev.sh @@ -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 diff --git a/version.go b/version.go deleted file mode 100644 index 75319ed5b..000000000 --- a/version.go +++ /dev/null @@ -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() -} diff --git a/version/version.go b/version/version.go new file mode 100644 index 000000000..54c0f9be6 --- /dev/null +++ b/version/version.go @@ -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() +}