Add checkpoint
This commit is contained in:
parent
5a3f80559c
commit
70191d22a6
|
@ -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
|
||||||
|
}
|
|
@ -12,6 +12,20 @@ type VersionCommand struct {
|
||||||
Revision string
|
Revision string
|
||||||
Version string
|
Version string
|
||||||
VersionPrerelease 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 {
|
func (c *VersionCommand) Help() string {
|
||||||
|
@ -20,7 +34,6 @@ func (c *VersionCommand) Help() string {
|
||||||
|
|
||||||
func (c *VersionCommand) Run(args []string) int {
|
func (c *VersionCommand) Run(args []string) int {
|
||||||
var versionString bytes.Buffer
|
var versionString bytes.Buffer
|
||||||
|
|
||||||
args = c.Meta.process(args, false)
|
args = c.Meta.process(args, false)
|
||||||
|
|
||||||
fmt.Fprintf(&versionString, "Terraform v%s", c.Version)
|
fmt.Fprintf(&versionString, "Terraform v%s", c.Version)
|
||||||
|
@ -33,6 +46,27 @@ func (c *VersionCommand) Run(args []string) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Ui.Output(versionString.String())
|
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
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -96,6 +96,7 @@ func init() {
|
||||||
Revision: GitCommit,
|
Revision: GitCommit,
|
||||||
Version: Version,
|
Version: Version,
|
||||||
VersionPrerelease: VersionPrerelease,
|
VersionPrerelease: VersionPrerelease,
|
||||||
|
CheckFunc: commandVersionCheck,
|
||||||
}, nil
|
}, nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,9 @@ import (
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Providers map[string]string
|
Providers map[string]string
|
||||||
Provisioners 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
|
// BuiltinConfig is the built-in defaults for the configuration. These
|
||||||
|
|
3
main.go
3
main.go
|
@ -92,6 +92,9 @@ func wrappedMain() int {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Run checkpoint
|
||||||
|
go runCheckpoint(&config)
|
||||||
|
|
||||||
// Make sure we clean up any managed plugins at the end of this
|
// Make sure we clean up any managed plugins at the end of this
|
||||||
defer plugin.CleanupClients()
|
defer plugin.CleanupClients()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue