builder/googlecompute: -force

This commit is contained in:
Valentin Tjoncke 2016-09-23 14:21:43 +02:00
parent fcaae838fc
commit 6f545af7f7
3 changed files with 29 additions and 13 deletions

View File

@ -49,10 +49,11 @@ type Config struct {
UseInternalIP bool `mapstructure:"use_internal_ip"` UseInternalIP bool `mapstructure:"use_internal_ip"`
Zone string `mapstructure:"zone"` Zone string `mapstructure:"zone"`
Account AccountFile Account AccountFile
privateKeyBytes []byte privateKeyBytes []byte
stateTimeout time.Duration stateTimeout time.Duration
ctx interpolate.Context imageAlreadyExists bool
ctx interpolate.Context
} }
func NewConfig(raws ...interface{}) (*Config, []string, error) { func NewConfig(raws ...interface{}) (*Config, []string, error) {
@ -191,4 +192,4 @@ func (c *Config) CalcTimeout() error {
} }
c.stateTimeout = stateTimeout c.stateTimeout = stateTimeout
return nil return nil
} }

View File

@ -17,15 +17,17 @@ func (s *StepCheckExistingImage) Run(state multistep.StateBag) multistep.StepAct
d := state.Get("driver").(Driver) d := state.Get("driver").(Driver)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
ui.Say("Checking image does not exist...") if !c.PackerForce {
exists := d.ImageExists(c.ImageName) ui.Say("Checking image does not exist...")
if exists { c.imageAlreadyExists = d.ImageExists(c.ImageName)
err := fmt.Errorf("Image %s already exists", c.ImageName) if c.imageAlreadyExists {
state.Put("error", err) err := fmt.Errorf("Image %s already exists.\n"+
ui.Error(err.Error()) "Use the force flag to delete it prior to building.", c.ImageName)
return multistep.ActionHalt state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
} }
return multistep.ActionContinue return multistep.ActionContinue
} }

View File

@ -22,6 +22,19 @@ func (s *StepCreateImage) Run(state multistep.StateBag) multistep.StepAction {
driver := state.Get("driver").(Driver) driver := state.Get("driver").(Driver)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
if config.PackerForce && config.imageAlreadyExists {
ui.Say("Deleting previous image...")
errCh := driver.DeleteImage(config.ImageName)
err := <-errCh
if err != nil {
err := fmt.Errorf("Error deleting image: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
}
ui.Say("Creating image...") ui.Say("Creating image...")
imageCh, errCh := driver.CreateImage( imageCh, errCh := driver.CreateImage(