2018-01-30 11:45:56 -05:00
|
|
|
package iso
|
|
|
|
|
|
|
|
import (
|
2018-04-25 05:23:37 -04:00
|
|
|
"fmt"
|
|
|
|
|
2018-01-30 11:45:56 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
|
2018-04-25 05:23:37 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2018-01-30 11:45:56 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
2018-02-01 06:47:09 -05:00
|
|
|
Config *FloppyConfig
|
|
|
|
Datastore string
|
2018-04-25 05:23:37 -04:00
|
|
|
Host string
|
2018-01-30 11:45:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepAddFloppy) Run(state multistep.StateBag) multistep.StepAction {
|
2018-02-01 06:47:09 -05:00
|
|
|
err := s.runImpl(state)
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", fmt.Errorf("error adding floppy: %v", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepAddFloppy) runImpl(state multistep.StateBag) error {
|
2018-01-30 11:45:56 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2018-02-01 06:47:09 -05:00
|
|
|
vm := state.Get("vm").(*driver.VirtualMachine)
|
|
|
|
d := state.Get("driver").(*driver.Driver)
|
2018-01-30 11:45:56 -05:00
|
|
|
|
2018-02-01 06:47:09 -05:00
|
|
|
tmpFloppy := state.Get("floppy_path")
|
|
|
|
if tmpFloppy != nil {
|
|
|
|
ui.Say("Uploading created floppy image")
|
2018-01-30 11:45:56 -05:00
|
|
|
|
2018-03-20 19:03:47 -04:00
|
|
|
ds, err := d.FindDatastore(s.Datastore, s.Host)
|
2018-01-30 11:45:56 -05:00
|
|
|
if err != nil {
|
2018-02-01 06:47:09 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
vmDir, err := vm.GetDir()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
uploadPath := fmt.Sprintf("%v/packer-tmp-created-floppy.img", vmDir)
|
|
|
|
if err := ds.UploadFile(tmpFloppy.(string), uploadPath); err != nil {
|
|
|
|
return fmt.Errorf("error uploading floppy image: %v", err)
|
2018-01-30 11:45:56 -05:00
|
|
|
}
|
2018-04-25 04:43:47 -04:00
|
|
|
state.Put("uploaded_floppy_path", uploadPath)
|
2018-02-01 06:47:09 -05:00
|
|
|
|
2018-02-14 03:01:41 -05:00
|
|
|
floppyIMGPath := ds.ResolvePath(uploadPath)
|
|
|
|
ui.Say("Adding generated Floppy...")
|
|
|
|
err = vm.AddFloppy(floppyIMGPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-01-30 11:45:56 -05:00
|
|
|
}
|
|
|
|
|
2018-02-14 03:01:41 -05:00
|
|
|
if s.Config.FloppyIMGPath != "" {
|
|
|
|
floppyIMGPath := s.Config.FloppyIMGPath
|
|
|
|
ui.Say("Adding Floppy image...")
|
|
|
|
err := vm.AddFloppy(floppyIMGPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-01-30 11:45:56 -05:00
|
|
|
}
|
|
|
|
|
2018-02-01 06:47:09 -05:00
|
|
|
return nil
|
2018-01-30 11:45:56 -05:00
|
|
|
}
|
|
|
|
|
2018-04-25 05:23:37 -04:00
|
|
|
func (s *StepAddFloppy) Cleanup(state multistep.StateBag) {}
|