2020-01-07 19:59:31 -05:00
|
|
|
//go:generate struct-markdown
|
|
|
|
//go:generate mapstructure-to-hcl2 -type FloppyConfig
|
|
|
|
|
2018-01-30 11:45:56 -05:00
|
|
|
package iso
|
|
|
|
|
|
|
|
import (
|
2018-10-31 17:42:24 -04:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-02-14 11:42:29 -05:00
|
|
|
|
2020-01-07 19:59:31 -05:00
|
|
|
"github.com/hashicorp/packer/builder/vsphere/driver"
|
2018-04-25 07:22:38 -04:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2018-01-30 11:45:56 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FloppyConfig struct {
|
2020-01-07 19:59:31 -05:00
|
|
|
// Datastore path to a floppy image that will be mounted to the VM.
|
|
|
|
// Example: `[datastore1] ISO/pvscsi-Windows8.flp`.
|
|
|
|
FloppyIMGPath string `mapstructure:"floppy_img_path"`
|
|
|
|
// List of local files to be mounted to the VM floppy drive. Can be used to
|
|
|
|
// make Debian preseed or RHEL kickstart files available to the VM.
|
|
|
|
FloppyFiles []string `mapstructure:"floppy_files"`
|
|
|
|
// List of directories to copy files from.
|
2018-01-30 11:45:56 -05:00
|
|
|
FloppyDirectories []string `mapstructure:"floppy_dirs"`
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-04-25 07:22:38 -04:00
|
|
|
func (s *StepAddFloppy) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
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-05-13 16:06:21 -04:00
|
|
|
if floppyPath, ok := state.GetOk("floppy_path"); ok {
|
2018-02-01 06:47:09 -05:00
|
|
|
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-05-06 17:26:04 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
2018-02-01 06:47:09 -05:00
|
|
|
}
|
|
|
|
vmDir, err := vm.GetDir()
|
|
|
|
if err != nil {
|
2018-05-06 17:26:04 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
2018-02-01 06:47:09 -05:00
|
|
|
}
|
|
|
|
|
2018-05-06 17:26:04 -04:00
|
|
|
uploadPath := fmt.Sprintf("%v/packer-tmp-created-floppy.flp", vmDir)
|
2018-11-13 12:22:19 -05:00
|
|
|
if err := ds.UploadFile(floppyPath.(string), uploadPath, s.Host); err != nil {
|
2018-05-06 17:26:04 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
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
|
|
|
ui.Say("Adding generated Floppy...")
|
2018-05-06 17:26:04 -04:00
|
|
|
floppyIMGPath := ds.ResolvePath(uploadPath)
|
2018-02-14 03:01:41 -05:00
|
|
|
err = vm.AddFloppy(floppyIMGPath)
|
|
|
|
if err != nil {
|
2018-05-06 17:26:04 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
2018-02-14 03:01:41 -05:00
|
|
|
}
|
2018-01-30 11:45:56 -05:00
|
|
|
}
|
|
|
|
|
2018-02-14 03:01:41 -05:00
|
|
|
if s.Config.FloppyIMGPath != "" {
|
|
|
|
ui.Say("Adding Floppy image...")
|
2018-05-06 17:26:04 -04:00
|
|
|
err := vm.AddFloppy(s.Config.FloppyIMGPath)
|
2018-02-14 03:01:41 -05:00
|
|
|
if err != nil {
|
2018-05-06 17:26:04 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
2018-02-14 03:01:41 -05:00
|
|
|
}
|
2018-01-30 11:45:56 -05:00
|
|
|
}
|
|
|
|
|
2018-05-06 17:26:04 -04:00
|
|
|
return multistep.ActionContinue
|
2018-01-30 11:45:56 -05:00
|
|
|
}
|
|
|
|
|
2018-10-18 09:43:17 -04:00
|
|
|
func (s *StepAddFloppy) Cleanup(state multistep.StateBag) {
|
|
|
|
_, cancelled := state.GetOk(multistep.StateCancelled)
|
|
|
|
_, halted := state.GetOk(multistep.StateHalted)
|
|
|
|
if !cancelled && !halted {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
d := state.Get("driver").(*driver.Driver)
|
|
|
|
|
|
|
|
if UploadedFloppyPath, ok := state.GetOk("uploaded_floppy_path"); ok {
|
|
|
|
ui.Say("Deleting Floppy image ...")
|
|
|
|
|
|
|
|
ds, err := d.FindDatastore(s.Datastore, s.Host)
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-08 10:48:37 -04:00
|
|
|
err = ds.Delete(UploadedFloppyPath.(string))
|
|
|
|
if err != nil {
|
2018-10-18 09:43:17 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|