github.com/sneal/packer@v0.5.2/builder/googlecompute/step_upload_image.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  // StepUploadImage represents a Packer build step that uploads GCE machine images.
    11  type StepUploadImage int
    12  
    13  // Run executes the Packer build step that uploads a GCE machine image.
    14  func (s *StepUploadImage) Run(state multistep.StateBag) multistep.StepAction {
    15  	comm := state.Get("communicator").(packer.Communicator)
    16  	config := state.Get("config").(*Config)
    17  	imageFilename := state.Get("image_file_name").(string)
    18  	ui := state.Get("ui").(packer.Ui)
    19  
    20  	sudoPrefix := ""
    21  	if config.SSHUsername != "root" {
    22  		sudoPrefix = "sudo "
    23  	}
    24  
    25  	ui.Say("Uploading image...")
    26  	cmd := new(packer.RemoteCmd)
    27  	cmd.Command = fmt.Sprintf("%s/usr/local/bin/gsutil cp %s gs://%s",
    28  		sudoPrefix, imageFilename, config.BucketName)
    29  	err := cmd.StartWithUi(comm, ui)
    30  	if err == nil && cmd.ExitStatus != 0 {
    31  		err = fmt.Errorf(
    32  			"gsutil exited with non-zero exit status: %d", cmd.ExitStatus)
    33  	}
    34  	if err != nil {
    35  		err := fmt.Errorf("Error uploading image: %s", err)
    36  		state.Put("error", err)
    37  		ui.Error(err.Error())
    38  		return multistep.ActionHalt
    39  	}
    40  
    41  	return multistep.ActionContinue
    42  }
    43  
    44  // Cleanup.
    45  func (s *StepUploadImage) Cleanup(state multistep.StateBag) {}