diff --git a/driver/vm.go b/driver/vm.go index e9a3d0727..bfa642b1a 100644 --- a/driver/vm.go +++ b/driver/vm.go @@ -494,6 +494,10 @@ func (vm *VirtualMachine) SetBootOrder(order []string) error { return vm.vm.SetBootOptions(vm.driver.ctx, &bootOptions) } +func (vm *VirtualMachine) RemoveDevice(keepFiles bool, device ...types.BaseVirtualDevice) error { + return vm.vm.RemoveDevice(vm.driver.ctx, keepFiles, device...) +} + func (vm *VirtualMachine) addDevice(device types.BaseVirtualDevice) error { newDevices := object.VirtualDeviceList{device} confSpec := types.VirtualMachineConfigSpec{} diff --git a/iso/builder_acc_test.go b/iso/builder_acc_test.go index 14cc6a6cc..5e05d4c0e 100644 --- a/iso/builder_acc_test.go +++ b/iso/builder_acc_test.go @@ -157,7 +157,6 @@ func TestISOBuilderAcc_cdrom(t *testing.T) { builderT.Test(t, builderT.TestCase{ Builder: &Builder{}, Template: cdromConfig(), - Check: checkCDRom(t), }) } @@ -170,35 +169,6 @@ func cdromConfig() string { return commonT.RenderConfig(config) } -func checkCDRom(t *testing.T) builderT.TestCheckFunc { - return func(artifacts []packer.Artifact) error { - d := commonT.TestConn(t) - - vm := commonT.GetVM(t, d, artifacts) - devices, err := vm.Devices() - if err != nil { - t.Fatalf("cannot read VM properties: %v", err) - } - - cdroms := devices.SelectByType((*types.VirtualCdrom)(nil)) - if len(cdroms) != 2 { - t.Fatalf("expected 2 cdroms, found %v", len(cdroms)) - } - for i := range cdroms { - iso, ok := cdroms[i].(*types.VirtualCdrom).Backing.(*types.VirtualCdromIsoBackingInfo) - if !ok { - t.Fatalf("the iso 'test%v.iso' is not connected", i) - } - expectedFileName := fmt.Sprintf("[datastore1] test%v.iso", i) - if iso.FileName != expectedFileName { - t.Fatalf("invalid iso filename: expected '%v', got '%v'", expectedFileName, iso.FileName) - } - } - - return nil - } -} - func TestISOBuilderAcc_networkCard(t *testing.T) { builderT.Test(t, builderT.TestCase{ Builder: &Builder{}, diff --git a/iso/step_add_cdrom.go b/iso/step_add_cdrom.go index ebe94a90d..1dd7f6e17 100644 --- a/iso/step_add_cdrom.go +++ b/iso/step_add_cdrom.go @@ -5,6 +5,7 @@ import ( "github.com/jetbrains-infra/packer-builder-vsphere/driver" "github.com/mitchellh/multistep" "fmt" + "github.com/vmware/govmomi/vim25/types" ) type CDRomConfig struct { @@ -21,10 +22,9 @@ type StepAddCDRom struct { func (s *StepAddCDRom) Run(state multistep.StateBag) multistep.StepAction { ui := state.Get("ui").(packer.Ui) + vm := state.Get("vm").(*driver.VirtualMachine) ui.Say("Adding CDRoms...") - - vm := state.Get("vm").(*driver.VirtualMachine) for _, path := range s.Config.ISOPaths { if err := vm.AddCdrom(path); err != nil { state.Put("error", fmt.Errorf("error adding a cdrom: %v", err)) @@ -36,5 +36,15 @@ func (s *StepAddCDRom) Run(state multistep.StateBag) multistep.StepAction { } func (s *StepAddCDRom) Cleanup(state multistep.StateBag) { - // nothing + 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)) + } } diff --git a/iso/step_add_floppy.go b/iso/step_add_floppy.go index a43eeb54e..3b996458f 100644 --- a/iso/step_add_floppy.go +++ b/iso/step_add_floppy.go @@ -5,6 +5,7 @@ import ( "github.com/hashicorp/packer/packer" "github.com/jetbrains-infra/packer-builder-vsphere/driver" "fmt" + "github.com/vmware/govmomi/vim25/types" ) type FloppyConfig struct { @@ -86,22 +87,28 @@ func (s *StepAddFloppy) runImpl(state multistep.StateBag) error { } func (s *StepAddFloppy) Cleanup(state multistep.StateBag) { - if s.uploadedFloppyPath == "" { - return - } - ui := state.Get("ui").(packer.Ui) + vm := state.Get("vm").(*driver.VirtualMachine) d := state.Get("driver").(*driver.Driver) - ds, err := d.FindDatastore(s.Datastore) + devices, err := vm.Devices() if err != nil { - ui.Error(err.Error()) - return + ui.Error(fmt.Sprintf("error removing floppy: %v", err)) } - if err := ds.Delete(s.uploadedFloppyPath); err != nil { - ui.Error(fmt.Sprintf("Error deleting floppy image '%v': %v", s.uploadedFloppyPath, err.Error())) - return + cdroms := devices.SelectByType((*types.VirtualFloppy)(nil)) + if err = vm.RemoveDevice(false, cdroms...); err != nil { + ui.Error(fmt.Sprintf("error removing floppy: %v", err)) } - // the img will be automatically ejected + if s.uploadedFloppyPath != "" { + ds, err := d.FindDatastore(s.Datastore) + 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 + } + } }