github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/googlecompute/step_check_existing_image.go (about)

     1  package googlecompute
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/packer/packer"
     7  	"github.com/mitchellh/multistep"
     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  	c := state.Get("config").(*Config)
    17  	d := state.Get("driver").(Driver)
    18  	ui := state.Get("ui").(packer.Ui)
    19  
    20  	ui.Say("Checking image does not exist...")
    21  	c.imageAlreadyExists = d.ImageExists(c.ImageName)
    22  	if !c.PackerForce && c.imageAlreadyExists {
    23  		err := fmt.Errorf("Image %s already exists.\n"+
    24  			"Use the force flag to delete it prior to building.", c.ImageName)
    25  		state.Put("error", err)
    26  		ui.Error(err.Error())
    27  		return multistep.ActionHalt
    28  	}
    29  	return multistep.ActionContinue
    30  }
    31  
    32  // Cleanup.
    33  func (s *StepCheckExistingImage) Cleanup(state multistep.StateBag) {}