2014-09-08 17:17:27 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-checkpoint"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
2014-09-08 18:25:50 -04:00
|
|
|
func init() {
|
|
|
|
packer.VersionChecker = packerVersionCheck
|
|
|
|
checkpointResult = make(chan *checkpoint.CheckResponse, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
var checkpointResult chan *checkpoint.CheckResponse
|
|
|
|
|
2014-09-08 17:20:13 -04:00
|
|
|
// runCheckpoint runs a HashiCorp Checkpoint request. You can read about
|
|
|
|
// Checkpoint here: https://github.com/hashicorp/go-checkpoint.
|
2014-09-08 17:17:27 -04:00
|
|
|
func runCheckpoint(c *config) {
|
2014-09-08 17:20:13 -04:00
|
|
|
// If the user doesn't want checkpoint at all, then return.
|
|
|
|
if c.DisableCheckpoint {
|
2014-09-08 17:21:13 -04:00
|
|
|
log.Printf("[INFO] Checkpoint disabled. Not running.")
|
2014-09-08 18:25:50 -04:00
|
|
|
checkpointResult <- nil
|
2014-09-08 17:20:13 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-08 17:17:27 -04:00
|
|
|
configDir, err := ConfigDir()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERR] Checkpoint setup error: %s", err)
|
2014-09-08 18:25:50 -04:00
|
|
|
checkpointResult <- nil
|
2014-09-08 17:17:27 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
version := packer.Version
|
|
|
|
if packer.VersionPrerelease != "" {
|
|
|
|
version += fmt.Sprintf(".%s", packer.VersionPrerelease)
|
|
|
|
}
|
|
|
|
|
2014-09-08 17:20:13 -04:00
|
|
|
signaturePath := filepath.Join(configDir, "checkpoint_signature")
|
|
|
|
if c.DisableCheckpointSignature {
|
2014-09-08 17:21:13 -04:00
|
|
|
log.Printf("[INFO] Checkpoint signature disabled")
|
2014-09-08 17:20:13 -04:00
|
|
|
signaturePath = ""
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:25:50 -04:00
|
|
|
resp, err := checkpoint.Check(&checkpoint.CheckParams{
|
2014-09-08 17:17:27 -04:00
|
|
|
Product: "packer",
|
|
|
|
Version: version,
|
2014-09-08 17:20:13 -04:00
|
|
|
SignatureFile: signaturePath,
|
2014-09-08 17:17:27 -04:00
|
|
|
CacheFile: filepath.Join(configDir, "checkpoint_cache"),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERR] Checkpoint error: %s", err)
|
2014-09-08 18:25:50 -04:00
|
|
|
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
|
2014-09-08 17:17:27 -04:00
|
|
|
}
|
2014-09-08 18:25:50 -04:00
|
|
|
|
|
|
|
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
|
2014-09-08 17:17:27 -04:00
|
|
|
}
|