Add inserting floppy image to iso builder

This commit is contained in:
Andrei Tonkikh 2018-01-30 19:45:56 +03:00
parent 6d9933d804
commit 50625ddcad
6 changed files with 100 additions and 16 deletions

View File

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

View File

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

View File

@ -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,15 +39,13 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&StepCreateVM{
config: &b.config.CreateConfig,
},
)
if b.config.CDRomConfig.ISOPath != "" {
steps = append(steps,
&StepAddCDRom{
config: &b.config.CDRomConfig,
},
&StepAddFloppy{
config: &b.config.FloppyConfig,
},
)
}
// Run!
b.runner = packerCommon.NewRunner(steps, b.config.PackerConfig, ui)

View File

@ -18,6 +18,7 @@ type Config struct {
CreateConfig `mapstructure:",squash"`
CDRomConfig `mapstructure:",squash"`
FloppyConfig `mapstructure:",squash"`
ctx interpolate.Context
}

View File

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

63
iso/step_add_floppy.go Normal file
View File

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