github.com/rothwerx/packer@v0.9.0/checkpoint.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"path/filepath"
     7  
     8  	"github.com/hashicorp/go-checkpoint"
     9  	"github.com/mitchellh/packer/command"
    10  	"github.com/mitchellh/packer/packer"
    11  )
    12  
    13  func init() {
    14  	checkpointResult = make(chan *checkpoint.CheckResponse, 1)
    15  }
    16  
    17  var checkpointResult chan *checkpoint.CheckResponse
    18  
    19  // runCheckpoint runs a HashiCorp Checkpoint request. You can read about
    20  // Checkpoint here: https://github.com/hashicorp/go-checkpoint.
    21  func runCheckpoint(c *config) {
    22  	// If the user doesn't want checkpoint at all, then return.
    23  	if c.DisableCheckpoint {
    24  		log.Printf("[INFO] Checkpoint disabled. Not running.")
    25  		checkpointResult <- nil
    26  		return
    27  	}
    28  
    29  	configDir, err := packer.ConfigDir()
    30  	if err != nil {
    31  		log.Printf("[ERR] Checkpoint setup error: %s", err)
    32  		checkpointResult <- nil
    33  		return
    34  	}
    35  
    36  	version := Version
    37  	if VersionPrerelease != "" {
    38  		version += fmt.Sprintf("-%s", VersionPrerelease)
    39  	}
    40  
    41  	signaturePath := filepath.Join(configDir, "checkpoint_signature")
    42  	if c.DisableCheckpointSignature {
    43  		log.Printf("[INFO] Checkpoint signature disabled")
    44  		signaturePath = ""
    45  	}
    46  
    47  	resp, err := checkpoint.Check(&checkpoint.CheckParams{
    48  		Product:       "packer",
    49  		Version:       version,
    50  		SignatureFile: signaturePath,
    51  		CacheFile:     filepath.Join(configDir, "checkpoint_cache"),
    52  	})
    53  	if err != nil {
    54  		log.Printf("[ERR] Checkpoint error: %s", err)
    55  		resp = nil
    56  	}
    57  
    58  	checkpointResult <- resp
    59  }
    60  
    61  // commandVersionCheck implements command.VersionCheckFunc and is used
    62  // as the version checker.
    63  func commandVersionCheck() (command.VersionCheckInfo, error) {
    64  	// Wait for the result to come through
    65  	info := <-checkpointResult
    66  	if info == nil {
    67  		var zero command.VersionCheckInfo
    68  		return zero, nil
    69  	}
    70  
    71  	// Build the alerts that we may have received about our version
    72  	alerts := make([]string, len(info.Alerts))
    73  	for i, a := range info.Alerts {
    74  		alerts[i] = a.Message
    75  	}
    76  
    77  	return command.VersionCheckInfo{
    78  		Outdated: info.Outdated,
    79  		Latest:   info.CurrentVersion,
    80  		Alerts:   alerts,
    81  	}, nil
    82  }