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 stepAddFloppy = &StepAddFloppy{
Config: &b.config.FloppyConfig,
Datastore: b.config.Datastore,
Host: b.config.Host,
}
steps = append(steps,
&common.StepConnect{
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,
Directories: b.config.FloppyDirectories,
},
stepAddFloppy,
&StepAddFloppy{
Config: &b.config.FloppyConfig,
Datastore: b.config.Datastore,
Host: b.config.Host,
},
&StepConfigParams{
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,
&StepRemoveCDRom{},
&StepRemoveFloppy{
Datastore: b.config.Datastore,
Host: b.config.Host,
UploadedFloppyPath: stepAddFloppy.uploadedFloppyPath,
Datastore: b.config.Datastore,
Host: b.config.Host,
},
&common.StepCreateSnapshot{
CreateSnapshot: b.config.CreateSnapshot,

View File

@ -30,8 +30,6 @@ type StepAddFloppy struct {
Config *FloppyConfig
Datastore string
Host string
uploadedFloppyPath string
}
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 {
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)
ui.Say("Adding generated Floppy...")
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)
vm := state.Get("vm").(*driver.VirtualMachine)
ui.Say("Deleting CD-ROM drives...")
devices, err := vm.Devices()
if err != nil {
ui.Error(fmt.Sprintf("error removing cdroms: %v", err))

View File

@ -10,9 +10,8 @@ import (
)
type StepRemoveFloppy struct {
Datastore string
Host string
UploadedFloppyPath string
Datastore string
Host string
}
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)
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()
if err != nil {
ui.Error(fmt.Sprintf("error removing floppy: %v", err))
return multistep.ActionHalt
}
cdroms := devices.SelectByType((*types.VirtualFloppy)(nil))
if err = vm.RemoveDevice(false, cdroms...); err != nil {
floppies := devices.SelectByType((*types.VirtualFloppy)(nil))
if err = vm.RemoveDevice(false, floppies...); err != nil {
ui.Error(fmt.Sprintf("error removing floppy: %v", err))
return multistep.ActionHalt
}
if s.UploadedFloppyPath != "" {
if UploadedFloppyPath != "" {
ui.Say("Deleting Floppy image...")
ds, err := d.FindDatastore(s.Datastore, s.Host)
if err != nil {
ui.Error(err.Error())
return multistep.ActionHalt
}
if err := ds.Delete(s.UploadedFloppyPath); err != nil {
ui.Error(fmt.Sprintf("Error deleting floppy image '%v': %v", s.UploadedFloppyPath, err.Error()))
if err := ds.Delete(UploadedFloppyPath); err != nil {
ui.Error(fmt.Sprintf("Error deleting floppy image '%v': %v", UploadedFloppyPath, err.Error()))
return multistep.ActionHalt
}
}