packer-cn/checkpoint.go

84 lines
2.1 KiB
Go
Raw Permalink Normal View History

package main
import (
"fmt"
"log"
"path/filepath"
"github.com/hashicorp/go-checkpoint"
2020-12-17 16:29:25 -05:00
"github.com/hashicorp/packer-plugin-sdk/pathing"
2017-04-04 16:39:01 -04:00
"github.com/hashicorp/packer/command"
packerVersion "github.com/hashicorp/packer/version"
)
2014-09-08 18:25:50 -04:00
func init() {
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.
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
}
2020-12-02 16:19:45 -05:00
configDir, err := pathing.ConfigDir()
if err != nil {
log.Printf("[ERR] Checkpoint setup error: %s", err)
2014-09-08 18:25:50 -04:00
checkpointResult <- nil
return
}
version := packerVersion.Version
if packerVersion.VersionPrerelease != "" {
version += fmt.Sprintf("-%s", packerVersion.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{
Product: "packer",
Version: version,
2014-09-08 17:20:13 -04:00
SignatureFile: signaturePath,
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
}
2014-10-27 23:51:34 -04:00
// commandVersionCheck implements command.VersionCheckFunc and is used
2014-09-08 18:25:50 -04:00
// as the version checker.
2014-10-27 23:51:34 -04:00
func commandVersionCheck() (command.VersionCheckInfo, error) {
// Wait for the result to come through
2014-09-08 18:25:50 -04:00
info := <-checkpointResult
if info == nil {
2014-10-27 23:51:34 -04:00
var zero command.VersionCheckInfo
2014-09-08 18:25:50 -04:00
return zero, nil
}
2014-09-08 18:25:50 -04:00
2014-10-27 23:51:34 -04:00
// Build the alerts that we may have received about our version
2014-09-08 18:25:50 -04:00
alerts := make([]string, len(info.Alerts))
for i, a := range info.Alerts {
alerts[i] = a.Message
}
2014-10-27 23:51:34 -04:00
return command.VersionCheckInfo{
2014-09-08 18:25:50 -04:00
Outdated: info.Outdated,
Latest: info.CurrentVersion,
Alerts: alerts,
}, nil
}