Eject CD-ROM images, don't delete devices (#173)

This commit is contained in:
Michael Kuzmin 2018-10-21 10:47:35 +03:00 committed by GitHub
parent 912aec563c
commit 75cabff278
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 15 deletions

View File

@ -46,3 +46,22 @@ func (vm *VirtualMachine) CreateCdrom(c *types.VirtualAHCIController) (*types.Vi
return device, nil return device, nil
} }
func (vm *VirtualMachine) EjectCdroms() error {
devices, err := vm.Devices()
if err != nil {
return err
}
cdroms := devices.SelectByType((*types.VirtualCdrom)(nil))
for _, cd := range cdroms {
c := cd.(*types.VirtualCdrom)
c.Backing = &types.VirtualCdromRemotePassthroughBackingInfo{}
c.Connectable = &types.VirtualDeviceConnectInfo{}
err := vm.vm.EditDevice(vm.driver.ctx, c)
if err != nil {
return err
}
}
return nil
}

View File

@ -353,6 +353,18 @@ func checkFull(t *testing.T) builderT.TestCheckFunc {
t.Errorf("Boot order must be empty") t.Errorf("Boot order must be empty")
} }
devices, err := vm.Devices()
if err != nil {
t.Fatalf("Cannot read devices: %v", err)
}
cdroms := devices.SelectByType((*types.VirtualCdrom)(nil))
for _, cd := range cdroms {
_, ok := cd.(*types.VirtualCdrom).Backing.(*types.VirtualCdromRemotePassthroughBackingInfo)
if !ok {
t.Errorf("wrong cdrom backing")
}
}
return nil return nil
} }
} }

View File

@ -4,7 +4,6 @@ import (
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
"github.com/jetbrains-infra/packer-builder-vsphere/driver" "github.com/jetbrains-infra/packer-builder-vsphere/driver"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
"github.com/vmware/govmomi/vim25/types"
"context" "context"
) )
@ -14,24 +13,12 @@ func (s *StepRemoveCDRom) Run(_ context.Context, state multistep.StateBag) multi
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
vm := state.Get("vm").(*driver.VirtualMachine) vm := state.Get("vm").(*driver.VirtualMachine)
ui.Say("Deleting CD-ROM drives...") ui.Say("Eject CD-ROM drives...")
devices, err := vm.Devices() err := vm.EjectCdroms()
if err != nil { if err != nil {
state.Put("error", err) state.Put("error", err)
return multistep.ActionHalt return multistep.ActionHalt
} }
cdroms := devices.SelectByType((*types.VirtualCdrom)(nil))
if err = vm.RemoveDevice(true, cdroms...); err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
ui.Say("Deleting SATA controller...")
sata := devices.SelectByType((*types.VirtualAHCIController)(nil))
if err = vm.RemoveDevice(true, sata...); err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
return multistep.ActionContinue return multistep.ActionContinue
} }