From 115811d410a2e12814ba77f7aa15552728022236 Mon Sep 17 00:00:00 2001 From: Asier Marruedo Date: Wed, 25 Apr 2018 11:23:37 +0200 Subject: [PATCH] fix cdrom and floppy drive unmounting issue when creating templates (#82) --- iso/builder.go | 22 ++++++++++++------ iso/step_add_cdrom.go | 18 +++----------- iso/step_add_floppy.go | 35 ++++------------------------ iso/step_remove_cdrom.go | 32 +++++++++++++++++++++++++ iso/step_remove_floppy.go | 49 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 104 insertions(+), 52 deletions(-) create mode 100644 iso/step_remove_cdrom.go create mode 100644 iso/step_remove_floppy.go diff --git a/iso/builder.go b/iso/builder.go index 5e00117df..e92a0c548 100644 --- a/iso/builder.go +++ b/iso/builder.go @@ -2,11 +2,11 @@ package iso import ( packerCommon "github.com/hashicorp/packer/common" + "github.com/hashicorp/packer/helper/communicator" "github.com/hashicorp/packer/packer" "github.com/jetbrains-infra/packer-builder-vsphere/common" "github.com/jetbrains-infra/packer-builder-vsphere/driver" "github.com/mitchellh/multistep" - "github.com/hashicorp/packer/helper/communicator" ) type Builder struct { @@ -32,6 +32,12 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe var steps []multistep.Step + var stepAddFloppy = &StepAddFloppy{ + Config: &b.config.FloppyConfig, + Datastore: b.config.Datastore, + Host: b.config.Host, + } + steps = append(steps, &common.StepConnect{ Config: &b.config.ConnectConfig, @@ -46,13 +52,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe Files: b.config.FloppyFiles, Directories: b.config.FloppyDirectories, }, - &StepAddFloppy{ - Config: &b.config.FloppyConfig, - Datastore: b.config.Datastore, - Host: b.config.Host, - }, + stepAddFloppy, &StepConfigParams{ - Config: &b.config.ConfigParamsConfig, + Config: &b.config.ConfigParamsConfig, }, ) @@ -78,6 +80,12 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe } steps = append(steps, + &StepRemoveCDRom{}, + &StepRemoveFloppy{ + Datastore: b.config.Datastore, + Host: b.config.Host, + UploadedFloppyPath: stepAddFloppy.uploadedFloppyPath, + }, &common.StepCreateSnapshot{ CreateSnapshot: b.config.CreateSnapshot, }, diff --git a/iso/step_add_cdrom.go b/iso/step_add_cdrom.go index 387e76a3f..91fefb0dd 100644 --- a/iso/step_add_cdrom.go +++ b/iso/step_add_cdrom.go @@ -1,11 +1,11 @@ package iso import ( + "fmt" + "github.com/hashicorp/packer/packer" "github.com/jetbrains-infra/packer-builder-vsphere/driver" "github.com/mitchellh/multistep" - "fmt" - "github.com/vmware/govmomi/vim25/types" ) type CDRomConfig struct { @@ -40,16 +40,4 @@ func (s *StepAddCDRom) Run(state multistep.StateBag) multistep.StepAction { return multistep.ActionContinue } -func (s *StepAddCDRom) Cleanup(state multistep.StateBag) { - ui := state.Get("ui").(packer.Ui) - vm := state.Get("vm").(*driver.VirtualMachine) - - devices, err := vm.Devices() - if err != nil { - ui.Error(fmt.Sprintf("error removing cdroms: %v", err)) - } - cdroms := devices.SelectByType((*types.VirtualCdrom)(nil)) - if err = vm.RemoveDevice(false, cdroms...); err != nil { - ui.Error(fmt.Sprintf("error removing cdroms: %v", err)) - } -} +func (s *StepAddCDRom) Cleanup(state multistep.StateBag) {} diff --git a/iso/step_add_floppy.go b/iso/step_add_floppy.go index 04d337716..cde7c630e 100644 --- a/iso/step_add_floppy.go +++ b/iso/step_add_floppy.go @@ -1,11 +1,11 @@ package iso import ( - "github.com/mitchellh/multistep" + "fmt" + "github.com/hashicorp/packer/packer" "github.com/jetbrains-infra/packer-builder-vsphere/driver" - "fmt" - "github.com/vmware/govmomi/vim25/types" + "github.com/mitchellh/multistep" ) type FloppyConfig struct { @@ -29,7 +29,7 @@ func (c *FloppyConfig) Prepare() []error { type StepAddFloppy struct { Config *FloppyConfig Datastore string - Host string + Host string uploadedFloppyPath string } @@ -88,29 +88,4 @@ func (s *StepAddFloppy) runImpl(state multistep.StateBag) error { return nil } -func (s *StepAddFloppy) Cleanup(state multistep.StateBag) { - ui := state.Get("ui").(packer.Ui) - vm := state.Get("vm").(*driver.VirtualMachine) - d := state.Get("driver").(*driver.Driver) - - devices, err := vm.Devices() - if err != nil { - ui.Error(fmt.Sprintf("error removing floppy: %v", err)) - } - cdroms := devices.SelectByType((*types.VirtualFloppy)(nil)) - if err = vm.RemoveDevice(false, cdroms...); err != nil { - ui.Error(fmt.Sprintf("error removing floppy: %v", err)) - } - - if s.uploadedFloppyPath != "" { - ds, err := d.FindDatastore(s.Datastore, s.Host) - if err != nil { - ui.Error(err.Error()) - return - } - if err := ds.Delete(s.uploadedFloppyPath); err != nil { - ui.Error(fmt.Sprintf("Error deleting floppy image '%v': %v", s.uploadedFloppyPath, err.Error())) - return - } - } -} +func (s *StepAddFloppy) Cleanup(state multistep.StateBag) {} diff --git a/iso/step_remove_cdrom.go b/iso/step_remove_cdrom.go new file mode 100644 index 000000000..43b57b3ca --- /dev/null +++ b/iso/step_remove_cdrom.go @@ -0,0 +1,32 @@ +package iso + +import ( + "fmt" + + "github.com/hashicorp/packer/packer" + "github.com/jetbrains-infra/packer-builder-vsphere/driver" + "github.com/mitchellh/multistep" + "github.com/vmware/govmomi/vim25/types" +) + +type StepRemoveCDRom struct{} + +func (s *StepRemoveCDRom) Run(state multistep.StateBag) multistep.StepAction { + ui := state.Get("ui").(packer.Ui) + vm := state.Get("vm").(*driver.VirtualMachine) + + devices, err := vm.Devices() + if err != nil { + ui.Error(fmt.Sprintf("error removing cdroms: %v", err)) + return multistep.ActionHalt + } + cdroms := devices.SelectByType((*types.VirtualCdrom)(nil)) + if err = vm.RemoveDevice(false, cdroms...); err != nil { + ui.Error(fmt.Sprintf("error removing cdroms: %v", err)) + return multistep.ActionHalt + } + + return multistep.ActionContinue +} + +func (s *StepRemoveCDRom) Cleanup(state multistep.StateBag) {} diff --git a/iso/step_remove_floppy.go b/iso/step_remove_floppy.go new file mode 100644 index 000000000..9018d3a5f --- /dev/null +++ b/iso/step_remove_floppy.go @@ -0,0 +1,49 @@ +package iso + +import ( + "fmt" + + "github.com/hashicorp/packer/packer" + "github.com/jetbrains-infra/packer-builder-vsphere/driver" + "github.com/mitchellh/multistep" + "github.com/vmware/govmomi/vim25/types" +) + +type StepRemoveFloppy struct { + Datastore string + Host string + UploadedFloppyPath string +} + +func (s *StepRemoveFloppy) Run(state multistep.StateBag) multistep.StepAction { + ui := state.Get("ui").(packer.Ui) + vm := state.Get("vm").(*driver.VirtualMachine) + d := state.Get("driver").(*driver.Driver) + + devices, err := vm.Devices() + if err != nil { + ui.Error(fmt.Sprintf("error removing floppy: %v", err)) + return multistep.ActionHalt + } + cdroms := devices.SelectByType((*types.VirtualFloppy)(nil)) + if err = vm.RemoveDevice(false, cdroms...); err != nil { + ui.Error(fmt.Sprintf("error removing floppy: %v", err)) + return multistep.ActionHalt + } + + if s.UploadedFloppyPath != "" { + ds, err := d.FindDatastore(s.Datastore, s.Host) + if err != nil { + ui.Error(err.Error()) + return multistep.ActionHalt + } + if err := ds.Delete(s.UploadedFloppyPath); err != nil { + ui.Error(fmt.Sprintf("Error deleting floppy image '%v': %v", s.UploadedFloppyPath, err.Error())) + return multistep.ActionHalt + } + } + + return multistep.ActionContinue +} + +func (s *StepRemoveFloppy) Cleanup(state multistep.StateBag) {}