fix cdrom and floppy drive unmounting issue when creating templates (#82)

This commit is contained in:
Asier Marruedo 2018-04-25 11:23:37 +02:00 committed by Michael Kuzmin
parent 6029bfe9e3
commit 115811d410
5 changed files with 104 additions and 52 deletions

View File

@ -2,11 +2,11 @@ package iso
import (
packerCommon "github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/packer"
"github.com/jetbrains-infra/packer-builder-vsphere/common"
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
"github.com/mitchellh/multistep"
"github.com/hashicorp/packer/helper/communicator"
)
type Builder struct {
@ -32,6 +32,12 @@ 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,
@ -46,11 +52,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
Files: b.config.FloppyFiles,
Directories: b.config.FloppyDirectories,
},
&StepAddFloppy{
Config: &b.config.FloppyConfig,
Datastore: b.config.Datastore,
Host: b.config.Host,
},
stepAddFloppy,
&StepConfigParams{
Config: &b.config.ConfigParamsConfig,
},
@ -78,6 +80,12 @@ 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,
},
&common.StepCreateSnapshot{
CreateSnapshot: b.config.CreateSnapshot,
},

View File

@ -1,11 +1,11 @@
package iso
import (
"fmt"
"github.com/hashicorp/packer/packer"
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
"github.com/mitchellh/multistep"
"fmt"
"github.com/vmware/govmomi/vim25/types"
)
type CDRomConfig struct {
@ -40,16 +40,4 @@ func (s *StepAddCDRom) Run(state multistep.StateBag) multistep.StepAction {
return multistep.ActionContinue
}
func (s *StepAddCDRom) Cleanup(state multistep.StateBag) {
ui := state.Get("ui").(packer.Ui)
vm := state.Get("vm").(*driver.VirtualMachine)
devices, err := vm.Devices()
if err != nil {
ui.Error(fmt.Sprintf("error removing cdroms: %v", err))
}
cdroms := devices.SelectByType((*types.VirtualCdrom)(nil))
if err = vm.RemoveDevice(false, cdroms...); err != nil {
ui.Error(fmt.Sprintf("error removing cdroms: %v", err))
}
}
func (s *StepAddCDRom) Cleanup(state multistep.StateBag) {}

View File

@ -1,11 +1,11 @@
package iso
import (
"github.com/mitchellh/multistep"
"fmt"
"github.com/hashicorp/packer/packer"
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
"fmt"
"github.com/vmware/govmomi/vim25/types"
"github.com/mitchellh/multistep"
)
type FloppyConfig struct {
@ -88,29 +88,4 @@ func (s *StepAddFloppy) runImpl(state multistep.StateBag) error {
return nil
}
func (s *StepAddFloppy) Cleanup(state multistep.StateBag) {
ui := state.Get("ui").(packer.Ui)
vm := state.Get("vm").(*driver.VirtualMachine)
d := state.Get("driver").(*driver.Driver)
devices, err := vm.Devices()
if err != nil {
ui.Error(fmt.Sprintf("error removing floppy: %v", err))
}
cdroms := devices.SelectByType((*types.VirtualFloppy)(nil))
if err = vm.RemoveDevice(false, cdroms...); err != nil {
ui.Error(fmt.Sprintf("error removing floppy: %v", err))
}
if s.uploadedFloppyPath != "" {
ds, err := d.FindDatastore(s.Datastore, s.Host)
if err != nil {
ui.Error(err.Error())
return
}
if err := ds.Delete(s.uploadedFloppyPath); err != nil {
ui.Error(fmt.Sprintf("Error deleting floppy image '%v': %v", s.uploadedFloppyPath, err.Error()))
return
}
}
}
func (s *StepAddFloppy) Cleanup(state multistep.StateBag) {}

32
iso/step_remove_cdrom.go Normal file
View File

@ -0,0 +1,32 @@
package iso
import (
"fmt"
"github.com/hashicorp/packer/packer"
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
"github.com/mitchellh/multistep"
"github.com/vmware/govmomi/vim25/types"
)
type StepRemoveCDRom struct{}
func (s *StepRemoveCDRom) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
vm := state.Get("vm").(*driver.VirtualMachine)
devices, err := vm.Devices()
if err != nil {
ui.Error(fmt.Sprintf("error removing cdroms: %v", err))
return multistep.ActionHalt
}
cdroms := devices.SelectByType((*types.VirtualCdrom)(nil))
if err = vm.RemoveDevice(false, cdroms...); err != nil {
ui.Error(fmt.Sprintf("error removing cdroms: %v", err))
return multistep.ActionHalt
}
return multistep.ActionContinue
}
func (s *StepRemoveCDRom) Cleanup(state multistep.StateBag) {}

49
iso/step_remove_floppy.go Normal file
View File

@ -0,0 +1,49 @@
package iso
import (
"fmt"
"github.com/hashicorp/packer/packer"
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
"github.com/mitchellh/multistep"
"github.com/vmware/govmomi/vim25/types"
)
type StepRemoveFloppy struct {
Datastore string
Host string
UploadedFloppyPath string
}
func (s *StepRemoveFloppy) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
vm := state.Get("vm").(*driver.VirtualMachine)
d := state.Get("driver").(*driver.Driver)
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 {
ui.Error(fmt.Sprintf("error removing floppy: %v", err))
return multistep.ActionHalt
}
if s.UploadedFloppyPath != "" {
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()))
return multistep.ActionHalt
}
}
return multistep.ActionContinue
}
func (s *StepRemoveFloppy) Cleanup(state multistep.StateBag) {}