diff --git a/clone/builder.go b/clone/builder.go index bf3410135..757e9b2c5 100644 --- a/clone/builder.go +++ b/clone/builder.go @@ -39,6 +39,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe &StepCloneVM{ Config: &b.config.CloneConfig, Location: &b.config.LocationConfig, + Force: b.config.PackerConfig.PackerForce, }, &common.StepConfigureHardware{ Config: &b.config.HardwareConfig, diff --git a/clone/step_clone.go b/clone/step_clone.go index 07f65f224..3db1010e0 100644 --- a/clone/step_clone.go +++ b/clone/step_clone.go @@ -33,14 +33,27 @@ func (c *CloneConfig) Prepare() []error { type StepCloneVM struct { Config *CloneConfig Location *common.LocationConfig + Force bool } func (s *StepCloneVM) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { ui := state.Get("ui").(packer.Ui) d := state.Get("driver").(*driver.Driver) - ui.Say("Cloning VM...") + find_vm, err := d.FindVM(s.Location.VMName) + if s.Force == false && err == nil { + state.Put("error", fmt.Errorf("%s already exists, you can use -force flag to destroy it: %v", s.Location.VMName, err)) + return multistep.ActionHalt + } else if s.Force == true && err == nil { + ui.Say(fmt.Sprintf("the vm/template %s already exists, but deleting it due to -force flag", s.Location.VMName)) + err := find_vm.Destroy() + if err != nil { + state.Put("error", fmt.Errorf("error destroying %s: %v", s.Location.VMName, err)) + } + } + + ui.Say("Cloning VM...") template, err := d.FindVM(s.Config.Template) if err != nil { state.Put("error", err) diff --git a/iso/builder.go b/iso/builder.go index 113cbc3ce..fb2b424bb 100644 --- a/iso/builder.go +++ b/iso/builder.go @@ -39,6 +39,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe &StepCreateVM{ Config: &b.config.CreateConfig, Location: &b.config.LocationConfig, + Force: b.config.PackerConfig.PackerForce, }, &common.StepConfigureHardware{ Config: &b.config.HardwareConfig, diff --git a/iso/step_create.go b/iso/step_create.go index a58010373..122e542e8 100644 --- a/iso/step_create.go +++ b/iso/step_create.go @@ -46,12 +46,26 @@ func (c *CreateConfig) Prepare() []error { type StepCreateVM struct { Config *CreateConfig Location *common.LocationConfig + Force bool } func (s *StepCreateVM) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { ui := state.Get("ui").(packer.Ui) d := state.Get("driver").(*driver.Driver) + find_vm, err := d.FindVM(s.Location.VMName) + + if s.Force == false && err == nil { + state.Put("error", fmt.Errorf("%s already exists, you can use -force flag to destroy it: %v", s.Location.VMName, err)) + return multistep.ActionHalt + } else if s.Force == true && err == nil { + ui.Say(fmt.Sprintf("the vm/template %s already exists, but deleting it due to -force flag", s.Location.VMName)) + err := find_vm.Destroy() + if err != nil { + state.Put("error", fmt.Errorf("error destroying %s: %v", s.Location.VMName, err)) + } + } + ui.Say("Creating VM...") vm, err := d.CreateVM(&driver.CreateConfig{ DiskThinProvisioned: s.Config.DiskThinProvisioned,