From 6f545af7f7c2c1cd23623ff47576d16327365d58 Mon Sep 17 00:00:00 2001 From: Valentin Tjoncke Date: Fri, 23 Sep 2016 14:21:43 +0200 Subject: [PATCH 1/3] builder/googlecompute: -force --- builder/googlecompute/config.go | 11 ++++++----- .../googlecompute/step_check_existing_image.go | 18 ++++++++++-------- builder/googlecompute/step_create_image.go | 13 +++++++++++++ 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/builder/googlecompute/config.go b/builder/googlecompute/config.go index 7c5eb6956..9c80b7271 100644 --- a/builder/googlecompute/config.go +++ b/builder/googlecompute/config.go @@ -49,10 +49,11 @@ type Config struct { UseInternalIP bool `mapstructure:"use_internal_ip"` Zone string `mapstructure:"zone"` - Account AccountFile - privateKeyBytes []byte - stateTimeout time.Duration - ctx interpolate.Context + Account AccountFile + privateKeyBytes []byte + stateTimeout time.Duration + imageAlreadyExists bool + ctx interpolate.Context } func NewConfig(raws ...interface{}) (*Config, []string, error) { @@ -191,4 +192,4 @@ func (c *Config) CalcTimeout() error { } c.stateTimeout = stateTimeout return nil -} \ No newline at end of file +} diff --git a/builder/googlecompute/step_check_existing_image.go b/builder/googlecompute/step_check_existing_image.go index 9b87e8337..171e503ce 100644 --- a/builder/googlecompute/step_check_existing_image.go +++ b/builder/googlecompute/step_check_existing_image.go @@ -17,15 +17,17 @@ func (s *StepCheckExistingImage) Run(state multistep.StateBag) multistep.StepAct d := state.Get("driver").(Driver) ui := state.Get("ui").(packer.Ui) - ui.Say("Checking image does not exist...") - exists := d.ImageExists(c.ImageName) - if exists { - err := fmt.Errorf("Image %s already exists", c.ImageName) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt + if !c.PackerForce { + ui.Say("Checking image does not exist...") + c.imageAlreadyExists = d.ImageExists(c.ImageName) + if c.imageAlreadyExists { + err := fmt.Errorf("Image %s already exists.\n"+ + "Use the force flag to delete it prior to building.", c.ImageName) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } } - return multistep.ActionContinue } diff --git a/builder/googlecompute/step_create_image.go b/builder/googlecompute/step_create_image.go index c8d5b4834..79d0c3df6 100644 --- a/builder/googlecompute/step_create_image.go +++ b/builder/googlecompute/step_create_image.go @@ -22,6 +22,19 @@ func (s *StepCreateImage) Run(state multistep.StateBag) multistep.StepAction { driver := state.Get("driver").(Driver) 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...") imageCh, errCh := driver.CreateImage( From 4605f9559f6035cda2cfb3024324044c7b114fe0 Mon Sep 17 00:00:00 2001 From: nouney Date: Fri, 23 Sep 2016 20:38:28 +0200 Subject: [PATCH 2/3] Fix unreachable code --- .../googlecompute/step_check_existing_image.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/builder/googlecompute/step_check_existing_image.go b/builder/googlecompute/step_check_existing_image.go index 171e503ce..8d43c98c1 100644 --- a/builder/googlecompute/step_check_existing_image.go +++ b/builder/googlecompute/step_check_existing_image.go @@ -19,14 +19,15 @@ func (s *StepCheckExistingImage) Run(state multistep.StateBag) multistep.StepAct if !c.PackerForce { ui.Say("Checking image does not exist...") - c.imageAlreadyExists = d.ImageExists(c.ImageName) - if c.imageAlreadyExists { - err := fmt.Errorf("Image %s already exists.\n"+ - "Use the force flag to delete it prior to building.", c.ImageName) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt - } + } + + c.imageAlreadyExists = d.ImageExists(c.ImageName) + if !c.PackerForce && c.imageAlreadyExists { + err := fmt.Errorf("Image %s already exists.\n"+ + "Use the force flag to delete it prior to building.", c.ImageName) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt } return multistep.ActionContinue } From ef1517064c5e2041dcc747ef3b7866438ecfbe1d Mon Sep 17 00:00:00 2001 From: Valentin Tjoncke Date: Sat, 24 Sep 2016 21:12:25 +0200 Subject: [PATCH 3/3] googlecompute/builder: Always show message when checking image exists --- builder/googlecompute/step_check_existing_image.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/builder/googlecompute/step_check_existing_image.go b/builder/googlecompute/step_check_existing_image.go index 8d43c98c1..dbad5cf1c 100644 --- a/builder/googlecompute/step_check_existing_image.go +++ b/builder/googlecompute/step_check_existing_image.go @@ -17,10 +17,7 @@ func (s *StepCheckExistingImage) Run(state multistep.StateBag) multistep.StepAct d := state.Get("driver").(Driver) ui := state.Get("ui").(packer.Ui) - if !c.PackerForce { - ui.Say("Checking image does not exist...") - } - + ui.Say("Checking image does not exist...") c.imageAlreadyExists = d.ImageExists(c.ImageName) if !c.PackerForce && c.imageAlreadyExists { err := fmt.Errorf("Image %s already exists.\n"+