github.com/sneal/packer@v0.5.2/builder/googlecompute/step_update_gsutil.go (about)

     1  package googlecompute
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/mitchellh/multistep"
     7  	"github.com/mitchellh/packer/packer"
     8  )
     9  
    10  // StepUpdateGsutil represents a Packer build step that updates the gsutil
    11  // utility to the latest version available.
    12  type StepUpdateGsutil int
    13  
    14  // Run executes the Packer build step that updates the gsutil utility to the
    15  // latest version available.
    16  //
    17  // This step is required to prevent the image creation process from hanging;
    18  // the image creation process utilizes the gcimagebundle cli tool which will
    19  // prompt to update gsutil if a newer version is available.
    20  func (s *StepUpdateGsutil) Run(state multistep.StateBag) multistep.StepAction {
    21  	comm := state.Get("communicator").(packer.Communicator)
    22  	config := state.Get("config").(*Config)
    23  	ui := state.Get("ui").(packer.Ui)
    24  
    25  	sudoPrefix := ""
    26  
    27  	if config.SSHUsername != "root" {
    28  		sudoPrefix = "sudo "
    29  	}
    30  
    31  	gsutilUpdateCmd := "/usr/local/bin/gsutil update -n -f"
    32  	cmd := new(packer.RemoteCmd)
    33  	cmd.Command = fmt.Sprintf("%s%s", sudoPrefix, gsutilUpdateCmd)
    34  
    35  	ui.Say("Updating gsutil...")
    36  	err := cmd.StartWithUi(comm, ui)
    37  	if err == nil && cmd.ExitStatus != 0 {
    38  		err = fmt.Errorf(
    39  			"gsutil update exited with non-zero exit status: %d", cmd.ExitStatus)
    40  	}
    41  	if err != nil {
    42  		err := fmt.Errorf("Error updating gsutil: %s", err)
    43  		state.Put("error", err)
    44  		ui.Error(err.Error())
    45  		return multistep.ActionHalt
    46  	}
    47  
    48  	return multistep.ActionContinue
    49  }
    50  
    51  // Cleanup.
    52  func (s *StepUpdateGsutil) Cleanup(state multistep.StateBag) {}