github.com/rothwerx/packer@v0.9.0/builder/googlecompute/step_check_existing_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  // StepCheckExistingImage represents a Packer build step that checks if the
    11  // target image already exists, and aborts immediately if so.
    12  type StepCheckExistingImage int
    13  
    14  // Run executes the Packer build step that checks if the image already exists.
    15  func (s *StepCheckExistingImage) Run(state multistep.StateBag) multistep.StepAction {
    16  	config := state.Get("config").(*Config)
    17  	driver := state.Get("driver").(Driver)
    18  	ui := state.Get("ui").(packer.Ui)
    19  
    20  	ui.Say("Checking image does not exist...")
    21  	exists := driver.ImageExists(config.ImageName)
    22  	if exists {
    23  		err := fmt.Errorf("Image %s already exists", config.ImageName)
    24  		state.Put("error", err)
    25  		ui.Error(err.Error())
    26  		return multistep.ActionHalt
    27  	}
    28  
    29  	return multistep.ActionContinue
    30  }
    31  
    32  // Cleanup.
    33  func (s *StepCheckExistingImage) Cleanup(state multistep.StateBag) {}