Pass floppy path via state bag

This commit is contained in:
Michael Kuzmin 2018-04-25 11:43:47 +03:00
parent 115811d410
commit 775d0007fa
4 changed files with 26 additions and 22 deletions

View File

@ -32,12 +32,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
var steps []multistep.Step var steps []multistep.Step
var stepAddFloppy = &StepAddFloppy{
Config: &b.config.FloppyConfig,
Datastore: b.config.Datastore,
Host: b.config.Host,
}
steps = append(steps, steps = append(steps,
&common.StepConnect{ &common.StepConnect{
Config: &b.config.ConnectConfig, Config: &b.config.ConnectConfig,
@ -52,7 +46,11 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
Files: b.config.FloppyFiles, Files: b.config.FloppyFiles,
Directories: b.config.FloppyDirectories, Directories: b.config.FloppyDirectories,
}, },
stepAddFloppy, &StepAddFloppy{
Config: &b.config.FloppyConfig,
Datastore: b.config.Datastore,
Host: b.config.Host,
},
&StepConfigParams{ &StepConfigParams{
Config: &b.config.ConfigParamsConfig, Config: &b.config.ConfigParamsConfig,
}, },
@ -82,9 +80,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
steps = append(steps, steps = append(steps,
&StepRemoveCDRom{}, &StepRemoveCDRom{},
&StepRemoveFloppy{ &StepRemoveFloppy{
Datastore: b.config.Datastore, Datastore: b.config.Datastore,
Host: b.config.Host, Host: b.config.Host,
UploadedFloppyPath: stepAddFloppy.uploadedFloppyPath,
}, },
&common.StepCreateSnapshot{ &common.StepCreateSnapshot{
CreateSnapshot: b.config.CreateSnapshot, CreateSnapshot: b.config.CreateSnapshot,

View File

@ -30,8 +30,6 @@ type StepAddFloppy struct {
Config *FloppyConfig Config *FloppyConfig
Datastore string Datastore string
Host string Host string
uploadedFloppyPath string
} }
func (s *StepAddFloppy) Run(state multistep.StateBag) multistep.StepAction { func (s *StepAddFloppy) Run(state multistep.StateBag) multistep.StepAction {
@ -65,9 +63,8 @@ func (s *StepAddFloppy) runImpl(state multistep.StateBag) error {
if err := ds.UploadFile(tmpFloppy.(string), uploadPath); err != nil { if err := ds.UploadFile(tmpFloppy.(string), uploadPath); err != nil {
return fmt.Errorf("error uploading floppy image: %v", err) return fmt.Errorf("error uploading floppy image: %v", err)
} }
state.Put("uploaded_floppy_path", uploadPath)
// remember the path to the temporary floppy image to remove it after the build is finished
s.uploadedFloppyPath = uploadPath
floppyIMGPath := ds.ResolvePath(uploadPath) floppyIMGPath := ds.ResolvePath(uploadPath)
ui.Say("Adding generated Floppy...") ui.Say("Adding generated Floppy...")
err = vm.AddFloppy(floppyIMGPath) err = vm.AddFloppy(floppyIMGPath)

View File

@ -15,6 +15,7 @@ func (s *StepRemoveCDRom) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
vm := state.Get("vm").(*driver.VirtualMachine) vm := state.Get("vm").(*driver.VirtualMachine)
ui.Say("Deleting CD-ROM drives...")
devices, err := vm.Devices() devices, err := vm.Devices()
if err != nil { if err != nil {
ui.Error(fmt.Sprintf("error removing cdroms: %v", err)) ui.Error(fmt.Sprintf("error removing cdroms: %v", err))

View File

@ -10,9 +10,8 @@ import (
) )
type StepRemoveFloppy struct { type StepRemoveFloppy struct {
Datastore string Datastore string
Host string Host string
UploadedFloppyPath string
} }
func (s *StepRemoveFloppy) Run(state multistep.StateBag) multistep.StepAction { func (s *StepRemoveFloppy) Run(state multistep.StateBag) multistep.StepAction {
@ -20,25 +19,35 @@ func (s *StepRemoveFloppy) Run(state multistep.StateBag) multistep.StepAction {
vm := state.Get("vm").(*driver.VirtualMachine) vm := state.Get("vm").(*driver.VirtualMachine)
d := state.Get("driver").(*driver.Driver) d := state.Get("driver").(*driver.Driver)
var UploadedFloppyPath string
switch s := state.Get("uploaded_floppy_path").(type) {
case string:
UploadedFloppyPath = s
case nil:
UploadedFloppyPath = ""
}
ui.Say("Deleting Floppy drives...")
devices, err := vm.Devices() devices, err := vm.Devices()
if err != nil { if err != nil {
ui.Error(fmt.Sprintf("error removing floppy: %v", err)) ui.Error(fmt.Sprintf("error removing floppy: %v", err))
return multistep.ActionHalt return multistep.ActionHalt
} }
cdroms := devices.SelectByType((*types.VirtualFloppy)(nil)) floppies := devices.SelectByType((*types.VirtualFloppy)(nil))
if err = vm.RemoveDevice(false, cdroms...); err != nil { if err = vm.RemoveDevice(false, floppies...); err != nil {
ui.Error(fmt.Sprintf("error removing floppy: %v", err)) ui.Error(fmt.Sprintf("error removing floppy: %v", err))
return multistep.ActionHalt return multistep.ActionHalt
} }
if s.UploadedFloppyPath != "" { if UploadedFloppyPath != "" {
ui.Say("Deleting Floppy image...")
ds, err := d.FindDatastore(s.Datastore, s.Host) ds, err := d.FindDatastore(s.Datastore, s.Host)
if err != nil { if err != nil {
ui.Error(err.Error()) ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt
} }
if err := ds.Delete(s.UploadedFloppyPath); err != nil { if err := ds.Delete(UploadedFloppyPath); err != nil {
ui.Error(fmt.Sprintf("Error deleting floppy image '%v': %v", s.UploadedFloppyPath, err.Error())) ui.Error(fmt.Sprintf("Error deleting floppy image '%v': %v", UploadedFloppyPath, err.Error()))
return multistep.ActionHalt return multistep.ActionHalt
} }
} }