Skip to content

Commit

Permalink
Add checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Oct 13, 2014
1 parent 5a3f805 commit 70191d2
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 1 deletion.
81 changes: 81 additions & 0 deletions checkpoint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package main

import (
"fmt"
"log"
"path/filepath"

"github.com/hashicorp/go-checkpoint"
"github.com/hashicorp/terraform/command"
)

func init() {
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
}

version := Version
if VersionPrerelease != "" {
version += fmt.Sprintf(".%s", VersionPrerelease)
}

signaturePath := filepath.Join(configDir, "checkpoint_signature")
if c.DisableCheckpointSignature {
log.Printf("[INFO] Checkpoint signature disabled")
signaturePath = ""
}

resp, err := checkpoint.Check(&checkpoint.CheckParams{
Product: "terraform",
Version: version,
SignatureFile: signaturePath,
CacheFile: filepath.Join(configDir, "checkpoint_cache"),
})
if err != nil {
log.Printf("[ERR] Checkpoint error: %s", err)
resp = nil
}

checkpointResult <- resp
}

// commandVersionCheck implements command.VersionCheckFunc and is used
// as the version checker.
func commandVersionCheck() (command.VersionCheckInfo, error) {
// Wait for the result to come through
info := <-checkpointResult
if info == nil {
var zero command.VersionCheckInfo
return zero, nil
}

// Build the alerts that we may have received about our version
alerts := make([]string, len(info.Alerts))
for i, a := range info.Alerts {
alerts[i] = a.Message
}

return command.VersionCheckInfo{
Outdated: info.Outdated,
Latest: info.CurrentVersion,
Alerts: alerts,
}, nil
}
36 changes: 35 additions & 1 deletion command/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ type VersionCommand struct {
Revision string
Version string
VersionPrerelease string
CheckFunc VersionCheckFunc
}

// VersionCheckFunc is the callback called by the Version command to
// check if there is a new version of Terraform.
type VersionCheckFunc func() (VersionCheckInfo, error)

// VersionCheckInfo is the return value for the VersionCheckFunc callback
// and tells the Version command information about the latest version
// of Terraform.
type VersionCheckInfo struct {
Outdated bool
Latest string
Alerts []string
}

func (c *VersionCommand) Help() string {
Expand All @@ -20,7 +34,6 @@ func (c *VersionCommand) Help() string {

func (c *VersionCommand) Run(args []string) int {
var versionString bytes.Buffer

args = c.Meta.process(args, false)

fmt.Fprintf(&versionString, "Terraform v%s", c.Version)
Expand All @@ -33,6 +46,27 @@ func (c *VersionCommand) Run(args []string) int {
}

c.Ui.Output(versionString.String())

// If we have a version check function, then let's check for
// the latest version as well.
if c.CheckFunc != nil {
// Separate the prior output with a newline
c.Ui.Output("")

// Check the latest version
info, err := c.CheckFunc()
if err != nil {
c.Ui.Error(fmt.Sprintf(
"Error checking latest version: %s", err))
}
if info.Outdated {
c.Ui.Output(fmt.Sprintf(
"Your version of Terraform is out of date! The latest version\n"+
"is %s. You can update by downloading from www.terraform.io",
info.Latest))
}
}

return 0
}

Expand Down
1 change: 1 addition & 0 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func init() {
Revision: GitCommit,
Version: Version,
VersionPrerelease: VersionPrerelease,
CheckFunc: commandVersionCheck,
}, nil
},
}
Expand Down
3 changes: 3 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import (
type Config struct {
Providers map[string]string
Provisioners map[string]string

DisableCheckpoint bool `hcl:"disable_checkpoint"`
DisableCheckpointSignature bool `hcl:"disable_checkpoint_signature"`
}

// BuiltinConfig is the built-in defaults for the configuration. These
Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ func wrappedMain() int {
return 1
}

// Run checkpoint
go runCheckpoint(&config)

// Make sure we clean up any managed plugins at the end of this
defer plugin.CleanupClients()

Expand Down

0 comments on commit 70191d2

Please sign in to comment.