2018-04-25 05:23:37 -04:00
|
|
|
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 {
|
2018-04-25 04:43:47 -04:00
|
|
|
Datastore string
|
|
|
|
Host string
|
2018-04-25 05:23:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2018-04-25 04:43:47 -04:00
|
|
|
var UploadedFloppyPath string
|
|
|
|
switch s := state.Get("uploaded_floppy_path").(type) {
|
|
|
|
case string:
|
|
|
|
UploadedFloppyPath = s
|
|
|
|
case nil:
|
|
|
|
UploadedFloppyPath = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Deleting Floppy drives...")
|
2018-04-25 05:23:37 -04:00
|
|
|
devices, err := vm.Devices()
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("error removing floppy: %v", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2018-04-25 04:43:47 -04:00
|
|
|
floppies := devices.SelectByType((*types.VirtualFloppy)(nil))
|
|
|
|
if err = vm.RemoveDevice(false, floppies...); err != nil {
|
2018-04-25 05:23:37 -04:00
|
|
|
ui.Error(fmt.Sprintf("error removing floppy: %v", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2018-04-25 04:43:47 -04:00
|
|
|
if UploadedFloppyPath != "" {
|
|
|
|
ui.Say("Deleting Floppy image...")
|
2018-04-25 05:23:37 -04:00
|
|
|
ds, err := d.FindDatastore(s.Datastore, s.Host)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2018-04-25 04:43:47 -04:00
|
|
|
if err := ds.Delete(UploadedFloppyPath); err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error deleting floppy image '%v': %v", UploadedFloppyPath, err.Error()))
|
2018-04-25 05:23:37 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepRemoveFloppy) Cleanup(state multistep.StateBag) {}
|