github.phpd.cn/hashicorp/packer@v1.3.2/builder/googlecompute/step_check_existing_image.go (about)

     1  package googlecompute
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/hashicorp/packer/helper/multistep"
     8  	"github.com/hashicorp/packer/packer"
     9  )
    10  
    11  // StepCheckExistingImage represents a Packer build step that checks if the
    12  // target image already exists, and aborts immediately if so.
    13  type StepCheckExistingImage int
    14  
    15  // Run executes the Packer build step that checks if the image already exists.
    16  func (s *StepCheckExistingImage) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    17  	c := state.Get("config").(*Config)
    18  	d := state.Get("driver").(Driver)
    19  	ui := state.Get("ui").(packer.Ui)
    20  
    21  	ui.Say("Checking image does not exist...")
    22  	c.imageAlreadyExists = d.ImageExists(c.ImageName)
    23  	if !c.PackerForce && c.imageAlreadyExists {
    24  		err := fmt.Errorf("Image %s already exists.\n"+
    25  			"Use the force flag to delete it prior to building.", c.ImageName)
    26  		state.Put("error", err)
    27  		ui.Error(err.Error())
    28  		return multistep.ActionHalt
    29  	}
    30  	return multistep.ActionContinue
    31  }
    32  
    33  // Cleanup.
    34  func (s *StepCheckExistingImage) Cleanup(state multistep.StateBag) {}