github.com/hashicorp/packer@v1.14.3/checkpoint.go (about)

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