packer: check for latest version

This commit is contained in:
Mitchell Hashimoto 2014-09-08 15:25:50 -07:00
parent d51364a897
commit 9c93c9e874
2 changed files with 71 additions and 2 deletions

View File

@ -9,18 +9,27 @@ import (
"github.com/mitchellh/packer/packer"
)
func init() {
packer.VersionChecker = packerVersionCheck
checkpointResult = make(chan *checkpoint.CheckResponse, 1)
}
var checkpointResult chan *checkpoint.CheckResponse
// runCheckpoint runs a HashiCorp Checkpoint request. You can read about
// Checkpoint here: https://github.com/hashicorp/go-checkpoint.
func runCheckpoint(c *config) {
// If the user doesn't want checkpoint at all, then return.
if c.DisableCheckpoint {
log.Printf("[INFO] Checkpoint disabled. Not running.")
checkpointResult <- nil
return
}
configDir, err := ConfigDir()
if err != nil {
log.Printf("[ERR] Checkpoint setup error: %s", err)
checkpointResult <- nil
return
}
@ -35,7 +44,7 @@ func runCheckpoint(c *config) {
signaturePath = ""
}
_, err = checkpoint.Check(&checkpoint.CheckParams{
resp, err := checkpoint.Check(&checkpoint.CheckParams{
Product: "packer",
Version: version,
SignatureFile: signaturePath,
@ -43,5 +52,29 @@ func runCheckpoint(c *config) {
})
if err != nil {
log.Printf("[ERR] Checkpoint error: %s", err)
resp = nil
}
checkpointResult <- resp
}
// packerVersionCheck implements packer.VersionCheckFunc and is used
// as the version checker.
func packerVersionCheck(current string) (packer.VersionCheckInfo, error) {
info := <-checkpointResult
if info == nil {
var zero packer.VersionCheckInfo
return zero, nil
}
alerts := make([]string, len(info.Alerts))
for i, a := range info.Alerts {
alerts[i] = a.Message
}
return packer.VersionCheckInfo{
Outdated: info.Outdated,
Latest: info.CurrentVersion,
Alerts: alerts,
}, nil
}

View File

@ -9,6 +9,12 @@ import (
// compiler for source builds.
var GitCommit string
// This should be check to a callback to check for the latest version.
//
// The global nature of this variable is dirty, but a version checker
// really shouldn't change anyways.
var VersionChecker VersionCheckFunc
// The version of packer.
const Version = "0.6.2"
@ -17,6 +23,18 @@ const Version = "0.6.2"
// pre-release marker.
const VersionPrerelease = "dev"
// VersionCheckFunc is the callback that is called to check the latest
// version of Packer.
type VersionCheckFunc func(string) (VersionCheckInfo, error)
// VersionCheckInfo is the return value for the VersionCheckFunc that
// contains the latest version information.
type VersionCheckInfo struct {
Outdated bool
Latest string
Alerts []string
}
type versionCommand byte
func (versionCommand) Help() string {
@ -30,8 +48,26 @@ func (versionCommand) Run(env Environment, args []string) int {
env.Ui().Machine("version", Version)
env.Ui().Machine("version-prelease", VersionPrerelease)
env.Ui().Machine("version-commit", GitCommit)
env.Ui().Say(VersionString())
if VersionChecker != nil {
current := Version
if VersionPrerelease != "" {
current += fmt.Sprintf(".%s", VersionPrerelease)
}
info, err := VersionChecker(current)
if err != nil {
env.Ui().Say(fmt.Sprintf("\nError checking latest version: %s", err))
}
if info.Outdated {
env.Ui().Say(fmt.Sprintf(
"\nYour version of Packer is out of date! The latest version\n"+
"is %s. You can update by downloading from www.packer.io.",
info.Latest))
}
}
return 0
}