diff --git a/driver/vm.go b/driver/vm.go index 0244cf04d..74f076370 100644 --- a/driver/vm.go +++ b/driver/vm.go @@ -442,9 +442,35 @@ func (vm *VirtualMachine) AddCdrom(isoPath string) error { cdrom = devices.InsertIso(cdrom, isoPath) } - newDevices := object.VirtualDeviceList{cdrom} + return vm.addDevice(cdrom) +} + +func (vm *VirtualMachine) AddFloppy(imgPath string) error { + devices, err := vm.vm.Device(vm.driver.ctx) + if err != nil { + return err + } + + floppy, err := devices.CreateFloppy() + if err != nil { + return err + } + + if imgPath != "" { + floppy = devices.InsertImg(floppy, imgPath) + } + + return vm.addDevice(floppy) +} + +func (vm *VirtualMachine) addDevice(device types.BaseVirtualDevice) error { + newDevices := object.VirtualDeviceList{device} confSpec := types.VirtualMachineConfigSpec{} + var err error confSpec.DeviceChange, err = newDevices.ConfigSpec(types.VirtualDeviceConfigSpecOperationAdd) + if err != nil { + return err + } task, err := vm.vm.Reconfigure(vm.driver.ctx, confSpec) if err != nil { diff --git a/driver/vm_create_acc_test.go b/driver/vm_create_acc_test.go index 5084d3737..cc8dcb051 100644 --- a/driver/vm_create_acc_test.go +++ b/driver/vm_create_acc_test.go @@ -31,9 +31,6 @@ func TestVMAcc_create(t *testing.T) { defer destroyVM(t, vm, tc.config.Name) - log.Printf("[DEBUG] Adding CDRom to the created VM") - vm.AddCdrom("[datastore1] ISO/alpine-standard-3.6.2-x86_64.iso") - log.Printf("[DEBUG] Running check function") tc.checkFunction(t, vm, tc.config) }) diff --git a/iso/builder.go b/iso/builder.go index 18bd4e450..6ebdf8b42 100644 --- a/iso/builder.go +++ b/iso/builder.go @@ -6,6 +6,7 @@ import ( "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 { @@ -38,16 +39,14 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe &StepCreateVM{ config: &b.config.CreateConfig, }, + &StepAddCDRom{ + config: &b.config.CDRomConfig, + }, + &StepAddFloppy{ + config: &b.config.FloppyConfig, + }, ) - if b.config.CDRomConfig.ISOPath != "" { - steps = append(steps, - &StepAddCDRom{ - config: &b.config.CDRomConfig, - }, - ) - } - // Run! b.runner = packerCommon.NewRunner(steps, b.config.PackerConfig, ui) b.runner.Run(state) diff --git a/iso/config.go b/iso/config.go index aec7c90fb..c3ca72d5d 100644 --- a/iso/config.go +++ b/iso/config.go @@ -18,6 +18,7 @@ type Config struct { CreateConfig `mapstructure:",squash"` CDRomConfig `mapstructure:",squash"` + FloppyConfig `mapstructure:",squash"` ctx interpolate.Context } diff --git a/iso/step_add_cdrom.go b/iso/step_add_cdrom.go index 136bc6ab5..668055e30 100644 --- a/iso/step_add_cdrom.go +++ b/iso/step_add_cdrom.go @@ -11,9 +11,7 @@ type CDRomConfig struct { } func (c *CDRomConfig) Prepare() []error { - var errs []error - - return errs + return nil } type StepAddCDRom struct { @@ -23,7 +21,7 @@ type StepAddCDRom struct { func (s *StepAddCDRom) Run(state multistep.StateBag) multistep.StepAction { ui := state.Get("ui").(packer.Ui) - ui.Say("Adding CDRom ...") + ui.Say("Adding CDRom...") vm := state.Get("vm").(*driver.VirtualMachine) err := vm.AddCdrom(s.config.ISOPath) diff --git a/iso/step_add_floppy.go b/iso/step_add_floppy.go new file mode 100644 index 000000000..e8c8e2fd0 --- /dev/null +++ b/iso/step_add_floppy.go @@ -0,0 +1,63 @@ +package iso + +import ( + "github.com/mitchellh/multistep" + "github.com/hashicorp/packer/packer" + "github.com/jetbrains-infra/packer-builder-vsphere/driver" + "fmt" +) + +type FloppyConfig struct { + FloppyIMGPath string `mapstructure:"floppy_img_path"` + FloppyFiles []string `mapstructure:"floppy_files"` + FloppyDirectories []string `mapstructure:"floppy_dirs"` +} + +func (c *FloppyConfig) Prepare() []error { + var errs []error + + if c.FloppyIMGPath != "" && (c.FloppyFiles != nil || c.FloppyDirectories != nil) { + errs = append(errs, + fmt.Errorf("'floppy_img_path' cannot be used together with 'floppy_files' and 'floppy_dirs'"), + ) + } + + return errs +} + +type StepAddFloppy struct { + config *FloppyConfig +} + +func (s *StepAddFloppy) Run(state multistep.StateBag) multistep.StepAction { + ui := state.Get("ui").(packer.Ui) + + ui.Say("Adding Floppy...") + + floppyIMGPath := s.config.FloppyIMGPath + if s.config.FloppyFiles != nil || s.config.FloppyDirectories != nil { + var err error + floppyIMGPath, err = s.createFloppy() + if err != nil { + state.Put("error", fmt.Errorf("Error creating floppy image: %v", err)) + } + } + + vm := state.Get("vm").(*driver.VirtualMachine) + err := vm.AddFloppy(floppyIMGPath) + if err != nil { + state.Put("error", err) + return multistep.ActionHalt + } + + return multistep.ActionContinue +} + +func (s *StepAddFloppy) Cleanup(state multistep.StateBag) { + // nothing +} + +func (s *StepAddFloppy) createFloppy() (string, error) { + return "", fmt.Errorf("Not implemented") + // TODO +}