Remove cdroms and floppy in the cleanup stage of iso builder

This commit is contained in:
Andrei Tonkikh 2018-02-02 21:04:32 +03:00
parent 1851223ad2
commit 78d53c81ce
4 changed files with 35 additions and 44 deletions

View File

@ -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{}

View File

@ -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{},

View File

@ -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))
}
}

View File

@ -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
}
}
}