packer-cn/iso/step_add_floppy.go

97 lines
2.3 KiB
Go
Raw Normal View History

package iso
import (
2018-04-25 07:22:38 -04:00
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
2018-04-25 07:22:38 -04:00
"fmt"
"context"
)
type FloppyConfig struct {
FloppyIMGPath string `mapstructure:"floppy_img_path"`
FloppyFiles []string `mapstructure:"floppy_files"`
FloppyDirectories []string `mapstructure:"floppy_dirs"`
}
type StepAddFloppy struct {
Config *FloppyConfig
Datastore string
Host string
}
2018-04-25 07:22:38 -04:00
func (s *StepAddFloppy) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
vm := state.Get("vm").(*driver.VirtualMachine)
d := state.Get("driver").(*driver.Driver)
if floppyPath, ok := state.GetOk("floppy_path"); ok {
ui.Say("Uploading created floppy image")
2018-03-20 19:03:47 -04:00
ds, err := d.FindDatastore(s.Datastore, s.Host)
if err != nil {
2018-05-06 17:26:04 -04:00
state.Put("error", err)
return multistep.ActionHalt
}
vmDir, err := vm.GetDir()
if err != nil {
2018-05-06 17:26:04 -04:00
state.Put("error", err)
return multistep.ActionHalt
}
2018-05-06 17:26:04 -04:00
uploadPath := fmt.Sprintf("%v/packer-tmp-created-floppy.flp", vmDir)
if err := ds.UploadFile(floppyPath.(string), uploadPath); err != nil {
2018-05-06 17:26:04 -04:00
state.Put("error", err)
return multistep.ActionHalt
}
2018-04-25 04:43:47 -04:00
state.Put("uploaded_floppy_path", uploadPath)
ui.Say("Adding generated Floppy...")
2018-05-06 17:26:04 -04:00
floppyIMGPath := ds.ResolvePath(uploadPath)
err = vm.AddFloppy(floppyIMGPath)
if err != nil {
2018-05-06 17:26:04 -04:00
state.Put("error", err)
return multistep.ActionHalt
}
}
if s.Config.FloppyIMGPath != "" {
ui.Say("Adding Floppy image...")
2018-05-06 17:26:04 -04:00
err := vm.AddFloppy(s.Config.FloppyIMGPath)
if err != nil {
2018-05-06 17:26:04 -04:00
state.Put("error", err)
return multistep.ActionHalt
}
}
2018-05-06 17:26:04 -04:00
return multistep.ActionContinue
}
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
}
err_del := ds.Delete(UploadedFloppyPath.(string))
if err_del != nil {
state.Put("error", err)
return
}
}
}