Makes sure version is always displayed consistently.

This commit is contained in:
James Phillips 2016-07-19 15:06:32 -07:00
parent dab7905cab
commit 53877aa260
4 changed files with 54 additions and 74 deletions

View File

@ -37,20 +37,18 @@ var validDatacenter = regexp.MustCompile("^[a-zA-Z0-9_-]+$")
// ShutdownCh. If two messages are sent on the ShutdownCh it will forcibly // ShutdownCh. If two messages are sent on the ShutdownCh it will forcibly
// exit. // exit.
type Command struct { type Command struct {
Revision string Version string
Version string Ui cli.Ui
VersionPrerelease string ShutdownCh <-chan struct{}
Ui cli.Ui args []string
ShutdownCh <-chan struct{} logFilter *logutils.LevelFilter
args []string logOutput io.Writer
logFilter *logutils.LevelFilter agent *Agent
logOutput io.Writer rpcServer *AgentRPC
agent *Agent httpServers []*HTTPServer
rpcServer *AgentRPC dnsServer *DNSServer
httpServers []*HTTPServer scadaProvider *scada.Provider
dnsServer *DNSServer scadaHttp *HTTPServer
scadaProvider *scada.Provider
scadaHttp *HTTPServer
} }
// readConfig is responsible for setup of our configuration using // readConfig is responsible for setup of our configuration using
@ -310,11 +308,6 @@ func (c *Command) readConfig() *Config {
c.Ui.Error("WARNING: Bootstrap mode enabled! Do not enable unless necessary") c.Ui.Error("WARNING: Bootstrap mode enabled! Do not enable unless necessary")
} }
// Set the version info
config.Revision = c.Revision
config.Version = c.Version
config.VersionPrerelease = c.VersionPrerelease
return config return config
} }
@ -406,19 +399,7 @@ func (c *Command) setupLoggers(config *Config) (*GatedWriter, *logWriter, io.Wri
// setupAgent is used to start the agent and various interfaces // setupAgent is used to start the agent and various interfaces
func (c *Command) setupAgent(config *Config, logOutput io.Writer, logWriter *logWriter) error { func (c *Command) setupAgent(config *Config, logOutput io.Writer, logWriter *logWriter) error {
var version string c.Ui.Output("Starting Consul agent...")
version = "v" + config.Version
if len(config.VersionPrerelease) != 0 {
version += " " + config.VersionPrerelease
if len(config.Revision) != 0 {
version += " " + config.Revision
}
}
c.Ui.Output("Starting Consul agent (" + version + ")...")
agent, err := Create(config, logOutput) agent, err := Create(config, logOutput)
if err != nil { if err != nil {
c.Ui.Error(fmt.Sprintf("Error starting agent: %s", err)) c.Ui.Error(fmt.Sprintf("Error starting agent: %s", err))
@ -533,12 +514,7 @@ func (c *Command) checkpointResults(results *checkpoint.CheckResponse, err error
return return
} }
if results.Outdated { if results.Outdated {
versionStr := c.Version c.Ui.Error(fmt.Sprintf("Newer Consul version available: %s (currently running: %s)", results.CurrentVersion, c.Version))
if c.VersionPrerelease != "" {
versionStr += fmt.Sprintf("-%s", c.VersionPrerelease)
}
c.Ui.Error(fmt.Sprintf("Newer Consul version available: %s (currently running: %s)", results.CurrentVersion, versionStr))
} }
for _, alert := range results.Alerts { for _, alert := range results.Alerts {
switch alert.Level { switch alert.Level {
@ -836,6 +812,7 @@ func (c *Command) Run(args []string) int {
c.agent.StartSync() c.agent.StartSync()
c.Ui.Output("Consul agent running!") c.Ui.Output("Consul agent running!")
c.Ui.Info(fmt.Sprintf(" Version: '%s'", c.Version))
c.Ui.Info(fmt.Sprintf(" Node name: '%s'", config.NodeName)) c.Ui.Info(fmt.Sprintf(" Node name: '%s'", config.NodeName))
c.Ui.Info(fmt.Sprintf(" Datacenter: '%s'", config.Datacenter)) c.Ui.Info(fmt.Sprintf(" Datacenter: '%s'", config.Datacenter))
c.Ui.Info(fmt.Sprintf(" Server: %v (bootstrap: %v)", config.Server, config.Bootstrap)) c.Ui.Info(fmt.Sprintf(" Server: %v (bootstrap: %v)", config.Server, config.Bootstrap))

View File

@ -1,7 +1,6 @@
package command package command
import ( import (
"bytes"
"fmt" "fmt"
"github.com/hashicorp/consul/consul" "github.com/hashicorp/consul/consul"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
@ -9,10 +8,8 @@ import (
// VersionCommand is a Command implementation prints the version. // VersionCommand is a Command implementation prints the version.
type VersionCommand struct { type VersionCommand struct {
Revision string Version string
Version string Ui cli.Ui
VersionPrerelease string
Ui cli.Ui
} }
func (c *VersionCommand) Help() string { func (c *VersionCommand) Help() string {
@ -20,19 +17,9 @@ func (c *VersionCommand) Help() string {
} }
func (c *VersionCommand) Run(_ []string) int { func (c *VersionCommand) Run(_ []string) int {
var versionString bytes.Buffer c.Ui.Output(fmt.Sprintf("Consul Version: %s", c.Version))
fmt.Fprintf(&versionString, "Consul %s", c.Version) c.Ui.Output(fmt.Sprintf("Supported Protocol Version(s): %d to %d",
if c.VersionPrerelease != "" { consul.ProtocolVersionMin, consul.ProtocolVersionMax))
fmt.Fprintf(&versionString, ".%s", c.VersionPrerelease)
if c.Revision != "" {
fmt.Fprintf(&versionString, " (%s)", c.Revision)
}
}
c.Ui.Output(versionString.String())
c.Ui.Output(fmt.Sprintf("Consul Protocol: %d (Understands back to: %d)",
consul.ProtocolVersionMax, consul.ProtocolVersionMin))
return 0 return 0
} }

View File

@ -19,11 +19,9 @@ func init() {
Commands = map[string]cli.CommandFactory{ Commands = map[string]cli.CommandFactory{
"agent": func() (cli.Command, error) { "agent": func() (cli.Command, error) {
return &agent.Command{ return &agent.Command{
Revision: GitCommit, Version: GetVersion(),
Version: Version, Ui: ui,
VersionPrerelease: VersionPrerelease, ShutdownCh: make(chan struct{}),
Ui: ui,
ShutdownCh: make(chan struct{}),
}, nil }, nil
}, },
@ -121,20 +119,9 @@ func init() {
}, },
"version": func() (cli.Command, error) { "version": func() (cli.Command, error) {
ver := Version
rel := VersionPrerelease
if GitDescribe != "" {
ver = GitDescribe
}
if GitDescribe == "" && rel == "" {
rel = "dev"
}
return &command.VersionCommand{ return &command.VersionCommand{
Revision: GitCommit, Version: GetVersion(),
Version: ver, Ui: ui,
VersionPrerelease: rel,
Ui: ui,
}, nil }, nil
}, },

View File

@ -1,5 +1,10 @@
package main package main
import (
"fmt"
"strings"
)
// The git commit that was compiled. This will be filled in by the compiler. // The git commit that was compiled. This will be filled in by the compiler.
var ( var (
GitCommit string GitCommit string
@ -13,3 +18,27 @@ const Version = "0.7.0"
// then it means that it is a final release. Otherwise, this is a pre-release // then it means that it is a final release. Otherwise, this is a pre-release
// such as "dev" (in development), "beta", "rc1", etc. // such as "dev" (in development), "beta", "rc1", etc.
const VersionPrerelease = "dev" const VersionPrerelease = "dev"
// GetVersion returns the full version of Consul.
func GetVersion() string {
version := Version
if GitDescribe != "" {
version = GitDescribe
}
release := VersionPrerelease
if GitDescribe == "" && release == "" {
release = "dev"
}
fullVersion := fmt.Sprintf("%s", version)
if release != "" {
fullVersion += fmt.Sprintf("-%s", release)
if GitCommit != "" {
fullVersion += fmt.Sprintf(" (%s)", GitCommit)
}
}
// Strip off any single quotes added by the git information.
return strings.Replace(fullVersion, "'", "", -1)
}