2017-05-04 19:29:21 -04:00
|
|
|
package packer
|
|
|
|
|
|
|
|
import (
|
2017-06-15 04:15:32 -04:00
|
|
|
"context"
|
2017-05-04 19:29:21 -04:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
checkpoint "github.com/hashicorp/go-checkpoint"
|
|
|
|
packerVersion "github.com/hashicorp/packer/version"
|
|
|
|
)
|
|
|
|
|
|
|
|
const TelemetryVersion string = "beta/packer/4"
|
|
|
|
const TelemetryPanicVersion string = "beta/packer_panic/4"
|
|
|
|
|
|
|
|
var CheckpointReporter CheckpointTelemetry
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
CheckpointReporter.startTime = time.Now().UTC()
|
|
|
|
}
|
|
|
|
|
|
|
|
type PackerReport struct {
|
|
|
|
Spans []*TelemetrySpan `json:"spans"`
|
|
|
|
ExitCode int `json:"exit_code"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
Command string `json:"command"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type CheckpointTelemetry struct {
|
|
|
|
enabled bool
|
|
|
|
spans []*TelemetrySpan
|
|
|
|
signatureFile string
|
|
|
|
startTime time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CheckpointTelemetry) Enable(disableSignature bool) {
|
|
|
|
configDir, err := ConfigDir()
|
|
|
|
if err != nil {
|
2017-06-21 17:07:12 -04:00
|
|
|
log.Printf("[WARN] (telemetry) setup error: %s", err)
|
2017-05-04 19:29:21 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
signatureFile := ""
|
|
|
|
if disableSignature {
|
2017-06-21 17:07:12 -04:00
|
|
|
log.Printf("[INFO] (telemetry) Checkpoint signature disabled")
|
2017-05-04 19:29:21 -04:00
|
|
|
} else {
|
|
|
|
signatureFile = filepath.Join(configDir, "checkpoint_signature")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.signatureFile = signatureFile
|
|
|
|
c.enabled = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CheckpointTelemetry) baseParams(prefix string) *checkpoint.ReportParams {
|
|
|
|
version := packerVersion.Version
|
|
|
|
if packerVersion.VersionPrerelease != "" {
|
2017-06-16 20:23:41 -04:00
|
|
|
version += "-" + packerVersion.VersionPrerelease
|
2017-05-04 19:29:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return &checkpoint.ReportParams{
|
|
|
|
Product: "packer",
|
|
|
|
SchemaVersion: prefix,
|
|
|
|
StartTime: c.startTime,
|
|
|
|
Version: version,
|
|
|
|
RunID: os.Getenv("PACKER_RUN_UUID"),
|
|
|
|
SignatureFile: c.signatureFile,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CheckpointTelemetry) ReportPanic(m string) error {
|
|
|
|
if !c.enabled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
panicParams := c.baseParams(TelemetryPanicVersion)
|
|
|
|
panicParams.Payload = m
|
|
|
|
panicParams.EndTime = time.Now().UTC()
|
2017-06-15 04:15:32 -04:00
|
|
|
|
2017-06-21 18:56:29 -04:00
|
|
|
// This timeout can be longer because it runs in the real main.
|
|
|
|
// We're also okay waiting a bit longer to collect panic information
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
2017-06-15 04:15:32 -04:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
return checkpoint.Report(ctx, panicParams)
|
2017-05-04 19:29:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CheckpointTelemetry) AddSpan(name, pluginType string) *TelemetrySpan {
|
2017-06-21 17:07:12 -04:00
|
|
|
log.Printf("[INFO] (telemetry) Starting %s %s", pluginType, name)
|
2017-05-04 19:29:21 -04:00
|
|
|
ts := &TelemetrySpan{
|
|
|
|
Name: name,
|
|
|
|
Type: pluginType,
|
|
|
|
StartTime: time.Now().UTC(),
|
|
|
|
}
|
|
|
|
c.spans = append(c.spans, ts)
|
|
|
|
return ts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CheckpointTelemetry) Finalize(command string, errCode int, err error) error {
|
|
|
|
if !c.enabled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
params := c.baseParams(TelemetryVersion)
|
|
|
|
params.EndTime = time.Now().UTC()
|
|
|
|
|
|
|
|
extra := &PackerReport{
|
|
|
|
Spans: c.spans,
|
|
|
|
ExitCode: errCode,
|
|
|
|
Command: command,
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
extra.Error = err.Error()
|
|
|
|
}
|
|
|
|
params.Payload = extra
|
|
|
|
|
2017-06-26 13:36:31 -04:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 550*time.Millisecond)
|
2017-06-15 04:15:32 -04:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
return checkpoint.Report(ctx, params)
|
2017-05-04 19:29:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type TelemetrySpan struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
StartTime time.Time `json:"start_time"`
|
|
|
|
EndTime time.Time `json:"end_time"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TelemetrySpan) End(err error) {
|
|
|
|
s.EndTime = time.Now().UTC()
|
2017-06-21 17:07:12 -04:00
|
|
|
log.Printf("[INFO] (telemetry) ending %s", s.Name)
|
2017-05-04 19:29:21 -04:00
|
|
|
if err != nil {
|
|
|
|
s.Error = err.Error()
|
2017-06-21 17:07:12 -04:00
|
|
|
log.Printf("[INFO] (telemetry) found error: %s", err.Error())
|
2017-05-04 19:29:21 -04:00
|
|
|
}
|
|
|
|
}
|