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