Code cleanup

This commit is contained in:
Michael Kuzmin 2018-05-07 00:26:04 +03:00
parent e59c1326f9
commit f46f373e3a
23 changed files with 173 additions and 239 deletions

View File

@ -37,7 +37,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
Config: &b.config.ConnectConfig,
},
&StepCloneVM{
config: &b.config.CloneConfig,
Config: &b.config.CloneConfig,
Location: &b.config.LocationConfig,
},
&common.StepConfigureHardware{
Config: &b.config.HardwareConfig,
@ -77,10 +78,13 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
b.runner = packerCommon.NewRunner(steps, b.config.PackerConfig, ui)
b.runner.Run(state)
if err := common.CheckRunStatus(state); err != nil {
return nil, err
if rawErr, ok := state.GetOk("error"); ok {
return nil, rawErr.(error)
}
if _, ok := state.GetOk("vm"); !ok {
return nil, nil
}
artifact := &common.Artifact{
Name: b.config.VMName,
VM: state.Get("vm").(*driver.VirtualMachine),

View File

@ -6,34 +6,45 @@ import (
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
"github.com/jetbrains-infra/packer-builder-vsphere/common"
"github.com/hashicorp/packer/helper/config"
)
type Config struct {
packerCommon.PackerConfig `mapstructure:",squash"`
common.ConnectConfig `mapstructure:",squash"`
common.RunConfig `mapstructure:",squash"`
CloneConfig `mapstructure:",squash"`
common.HardwareConfig `mapstructure:",squash"`
Comm communicator.Config `mapstructure:",squash"`
common.ShutdownConfig `mapstructure:",squash"`
CreateSnapshot bool `mapstructure:"create_snapshot"`
ConvertToTemplate bool `mapstructure:"convert_to_template"`
common.ConfigParamsConfig `mapstructure:",squash"`
packerCommon.PackerConfig `mapstructure:",squash"`
common.ConnectConfig `mapstructure:",squash"`
CloneConfig `mapstructure:",squash"`
common.LocationConfig `mapstructure:",squash"`
common.HardwareConfig `mapstructure:",squash"`
common.ConfigParamsConfig `mapstructure:",squash"`
common.RunConfig `mapstructure:",squash"`
Comm communicator.Config `mapstructure:",squash"`
common.ShutdownConfig `mapstructure:",squash"`
CreateSnapshot bool `mapstructure:"create_snapshot"`
ConvertToTemplate bool `mapstructure:"convert_to_template"`
ctx interpolate.Context
}
func NewConfig(raws ...interface{}) (*Config, []string, error) {
c := new(Config)
if err := common.DecodeConfig(c, &c.ctx, raws...); err != nil {
err := config.Decode(c, &config.DecodeOpts{
Interpolate: true,
InterpolateContext: &c.ctx,
}, raws...)
if err != nil {
return nil, nil, err
}
errs := new(packer.MultiError)
errs = packer.MultiErrorAppend(errs, c.Comm.Prepare(&c.ctx)...)
errs = packer.MultiErrorAppend(errs, c.ConnectConfig.Prepare()...)
errs = packer.MultiErrorAppend(errs, c.CloneConfig.Prepare()...)
errs = packer.MultiErrorAppend(errs, c.LocationConfig.Prepare()...)
errs = packer.MultiErrorAppend(errs, c.HardwareConfig.Prepare()...)
errs = packer.MultiErrorAppend(errs, c.Comm.Prepare(&c.ctx)...)
errs = packer.MultiErrorAppend(errs, c.ShutdownConfig.Prepare()...)
if len(errs.Errors) > 0 {

View File

@ -11,23 +11,27 @@ import (
type CloneConfig struct {
Template string `mapstructure:"template"`
common.VMConfig `mapstructure:",squash"`
LinkedClone bool `mapstructure:"linked_clone"`
DiskSize int64 `mapstructure:"disk_size"`
LinkedClone bool `mapstructure:"linked_clone"`
}
func (c *CloneConfig) Prepare() []error {
errs := c.VMConfig.Prepare()
var errs []error
if c.Template == "" {
errs = append(errs, fmt.Errorf("Template name is required"))
errs = append(errs, fmt.Errorf("'template' is required"))
}
if c.LinkedClone == true && c.DiskSize != 0 {
errs = append(errs, fmt.Errorf("'linked_clone' and 'disk_size' cannot be used together"))
}
return errs
}
type StepCloneVM struct {
config *CloneConfig
Config *CloneConfig
Location *common.LocationConfig
}
func (s *StepCloneVM) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
@ -36,20 +40,20 @@ func (s *StepCloneVM) Run(_ context.Context, state multistep.StateBag) multistep
ui.Say("Cloning VM...")
template, err := d.FindVM(s.config.Template)
template, err := d.FindVM(s.Config.Template)
if err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
vm, err := template.Clone(&driver.CloneConfig{
Name: s.config.VMName,
Folder: s.config.Folder,
Cluster: s.config.Cluster,
Host: s.config.Host,
ResourcePool: s.config.ResourcePool,
Datastore: s.config.Datastore,
LinkedClone: s.config.LinkedClone,
Name: s.Location.VMName,
Folder: s.Location.Folder,
Cluster: s.Location.Cluster,
Host: s.Location.Host,
ResourcePool: s.Location.ResourcePool,
Datastore: s.Location.Datastore,
LinkedClone: s.Config.LinkedClone,
})
if err != nil {
state.Put("error", err)
@ -57,8 +61,8 @@ func (s *StepCloneVM) Run(_ context.Context, state multistep.StateBag) multistep
}
state.Put("vm", vm)
if s.config.DiskSize > 0 {
err = vm.ResizeDisk(s.config.DiskSize)
if s.Config.DiskSize > 0 {
err = vm.ResizeDisk(s.Config.DiskSize)
if err != nil {
state.Put("error", err)
return multistep.ActionHalt

View File

@ -32,9 +32,5 @@ func (a *Artifact) State(name string) interface{} {
}
func (a *Artifact) Destroy() error {
err := a.VM.Destroy()
if err != nil {
return err
}
return nil
return a.VM.Destroy()
}

View File

@ -1,24 +0,0 @@
package common
import (
"github.com/hashicorp/packer/helper/multistep"
"errors"
)
func CheckRunStatus(state *multistep.BasicStateBag) error {
// If there was an error, return that
if rawErr, ok := state.GetOk("error"); ok {
return rawErr.(error)
}
// If we were interrupted or cancelled, then just exit.
if _, ok := state.GetOk(multistep.StateCancelled); ok {
return errors.New("Build was cancelled.")
}
if _, ok := state.GetOk(multistep.StateHalted); ok {
return errors.New("Build was halted.")
}
return nil
}

25
common/config_location.go Normal file
View File

@ -0,0 +1,25 @@
package common
import "fmt"
type LocationConfig struct {
VMName string `mapstructure:"vm_name"`
Folder string `mapstructure:"folder"`
Cluster string `mapstructure:"cluster"`
Host string `mapstructure:"host"`
ResourcePool string `mapstructure:"resource_pool"`
Datastore string `mapstructure:"datastore"`
}
func (c *LocationConfig) Prepare() []error {
var errs []error
if c.VMName == "" {
errs = append(errs, fmt.Errorf("'vm_name' is required"))
}
if c.Cluster == "" && c.Host == "" {
errs = append(errs, fmt.Errorf("'host' or 'cluster' is required"))
}
return errs
}

View File

@ -1,14 +0,0 @@
package common
import (
"github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/template/interpolate"
)
func DecodeConfig(cfg interface{}, ctx *interpolate.Context, raws ...interface{}) error {
err := config.Decode(cfg, &config.DecodeOpts{
Interpolate: true,
InterpolateContext: ctx,
}, raws...)
return err
}

View File

@ -12,10 +12,6 @@ type ConfigParamsConfig struct {
ConfigParams map[string]string `mapstructure:"configuration_parameters"`
}
func (c *ConfigParamsConfig) Prepare() []error {
return nil
}
type StepConfigParams struct {
Config *ConfigParamsConfig
}
@ -24,11 +20,12 @@ func (s *StepConfigParams) Run(_ context.Context, state multistep.StateBag) mult
ui := state.Get("ui").(packer.Ui)
vm := state.Get("vm").(*driver.VirtualMachine)
ui.Say("Adding configuration parameters...")
if err := vm.AddConfigParams(s.Config.ConfigParams); err != nil {
state.Put("error", fmt.Errorf("error adding configuration parameters: %v", err))
return multistep.ActionHalt
if s.Config.ConfigParams != nil {
ui.Say("Adding configuration parameters...")
if err := vm.AddConfigParams(s.Config.ConfigParams); err != nil {
state.Put("error", fmt.Errorf("error adding configuration parameters: %v", err))
return multistep.ActionHalt
}
}
return multistep.ActionContinue

View File

@ -19,13 +19,13 @@ func (c *ConnectConfig) Prepare() []error {
var errs []error
if c.VCenterServer == "" {
errs = append(errs, fmt.Errorf("vCenter hostname is required"))
errs = append(errs, fmt.Errorf("'vcenter_server' is required"))
}
if c.Username == "" {
errs = append(errs, fmt.Errorf("Username is required"))
errs = append(errs, fmt.Errorf("'username' is required"))
}
if c.Password == "" {
errs = append(errs, fmt.Errorf("Password is required"))
errs = append(errs, fmt.Errorf("'password' is required"))
}
return errs

View File

@ -12,12 +12,14 @@ type HardwareConfig struct {
CPUs int32 `mapstructure:"CPUs"`
CPUReservation int64 `mapstructure:"CPU_reservation"`
CPULimit int64 `mapstructure:"CPU_limit"`
CpuHotAddEnabled bool `mapstructure:"CPU_hot_plug"`
RAM int64 `mapstructure:"RAM"`
RAMReservation int64 `mapstructure:"RAM_reservation"`
RAMReserveAll bool `mapstructure:"RAM_reserve_all"`
NestedHV bool `mapstructure:"NestedHV"`
CpuHotAddEnabled bool `mapstructure:"CPU_hot_plug"`
MemoryHotAddEnabled bool `mapstructure:"RAM_hot_plug"`
NestedHV bool `mapstructure:"NestedHV"`
}
func (c *HardwareConfig) Prepare() []error {
@ -39,7 +41,7 @@ func (s *StepConfigureHardware) Run(_ context.Context, state multistep.StateBag)
vm := state.Get("vm").(*driver.VirtualMachine)
if *s.Config != (HardwareConfig{}) {
ui.Say("Customizing hardware parameters...")
ui.Say("Customizing hardware...")
err := vm.Configure(&driver.HardwareConfig{
CPUs: s.Config.CPUs,

View File

@ -3,7 +3,6 @@ package common
import (
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"fmt"
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
"strings"
"context"
@ -29,10 +28,11 @@ func (s *StepRun) Run(_ context.Context, state multistep.StateBag) multistep.Ste
ui := state.Get("ui").(packer.Ui)
vm := state.Get("vm").(*driver.VirtualMachine)
ui.Say("Set boot order...")
if s.Config.BootOrder != "" {
if err := vm.SetBootOrder(strings.Split(s.Config.BootOrder, ",")); err != nil {
state.Put("error", fmt.Errorf("error selecting boot order: %v", err))
ui.Say("Set boot order...")
order := strings.Split(s.Config.BootOrder, ",")
if err := vm.SetBootOrder(order); err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
}
@ -40,7 +40,7 @@ func (s *StepRun) Run(_ context.Context, state multistep.StateBag) multistep.Ste
ui.Say("Power on VM...")
err := vm.PowerOn()
if err != nil {
state.Put("error", fmt.Errorf("error powering on VM: %v", err))
state.Put("error",err)
return multistep.ActionHalt
}

View File

@ -14,6 +14,7 @@ import (
type ShutdownConfig struct {
Command string `mapstructure:"shutdown_command"`
RawTimeout string `mapstructure:"shutdown_timeout"`
Timeout time.Duration
}
@ -75,7 +76,6 @@ func (s *StepShutdown) Run(_ context.Context, state multistep.StateBag) multiste
return multistep.ActionHalt
}
ui.Say("VM stopped")
return multistep.ActionContinue
}

View File

@ -45,6 +45,4 @@ func (s *StepWaitForIp) Run(_ context.Context, state multistep.StateBag) multist
}
}
func (s *StepWaitForIp) Cleanup(state multistep.StateBag) {
// nothing
}
func (s *StepWaitForIp) Cleanup(state multistep.StateBag) {}

View File

@ -1,25 +0,0 @@
package common
import "fmt"
type VMConfig struct {
VMName string `mapstructure:"vm_name"`
Folder string `mapstructure:"folder"`
Cluster string `mapstructure:"cluster"`
Host string `mapstructure:"host"`
ResourcePool string `mapstructure:"resource_pool"`
Datastore string `mapstructure:"datastore"`
}
func (c *VMConfig) Prepare() []error {
var errs []error
if c.VMName == "" {
errs = append(errs, fmt.Errorf("Target VM name is required"))
}
if c.Cluster == "" && c.Host == "" {
errs = append(errs, fmt.Errorf("vSphere host or cluster is required"))
}
return errs
}

View File

@ -53,7 +53,7 @@ type CreateConfig struct {
Network string // "" for default network
NetworkCard string // example: vmxnet3
USBController bool
Version int // example: 10
Version uint // example: 10
}
func (d *Driver) NewVM(ref *types.ManagedObjectReference) *VirtualMachine {

View File

@ -37,7 +37,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
Config: &b.config.ConnectConfig,
},
&StepCreateVM{
Config: &b.config.CreateConfig,
Config: &b.config.CreateConfig,
Location: &b.config.LocationConfig,
},
&common.StepConfigureHardware{
Config: &b.config.HardwareConfig,
@ -97,10 +98,13 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
b.runner = packerCommon.NewRunner(steps, b.config.PackerConfig, ui)
b.runner.Run(state)
if err := common.CheckRunStatus(state); err != nil {
return nil, err
if rawErr, ok := state.GetOk("error"); ok {
return nil, rawErr.(error)
}
if _, ok := state.GetOk("vm"); !ok {
return nil, nil
}
artifact := &common.Artifact{
Name: b.config.VMName,
VM: state.Get("vm").(*driver.VirtualMachine),

View File

@ -6,41 +6,51 @@ import (
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
"github.com/jetbrains-infra/packer-builder-vsphere/common"
"github.com/hashicorp/packer/helper/config"
)
type Config struct {
packerCommon.PackerConfig `mapstructure:",squash"`
common.RunConfig `mapstructure:",squash"`
BootConfig `mapstructure:",squash"`
common.ConnectConfig `mapstructure:",squash"`
Comm communicator.Config `mapstructure:",squash"`
common.ShutdownConfig `mapstructure:",squash"`
CreateSnapshot bool `mapstructure:"create_snapshot"`
ConvertToTemplate bool `mapstructure:"convert_to_template"`
packerCommon.PackerConfig `mapstructure:",squash"`
common.ConnectConfig `mapstructure:",squash"`
CreateConfig `mapstructure:",squash"`
common.LocationConfig `mapstructure:",squash"`
common.HardwareConfig `mapstructure:",squash"`
CDRomConfig `mapstructure:",squash"`
FloppyConfig `mapstructure:",squash"`
common.ConfigParamsConfig `mapstructure:",squash"`
CDRomConfig `mapstructure:",squash"`
FloppyConfig `mapstructure:",squash"`
common.RunConfig `mapstructure:",squash"`
BootConfig `mapstructure:",squash"`
Comm communicator.Config `mapstructure:",squash"`
common.ShutdownConfig `mapstructure:",squash"`
CreateSnapshot bool `mapstructure:"create_snapshot"`
ConvertToTemplate bool `mapstructure:"convert_to_template"`
ctx interpolate.Context
}
func NewConfig(raws ...interface{}) (*Config, []string, error) {
c := new(Config)
if err := common.DecodeConfig(c, &c.ctx, raws...); err != nil {
err := config.Decode(c, &config.DecodeOpts{
Interpolate: true,
InterpolateContext: &c.ctx,
}, raws...)
if err != nil {
return nil, nil, err
}
errs := new(packer.MultiError)
errs = packer.MultiErrorAppend(errs, c.Comm.Prepare(&c.ctx)...)
errs = packer.MultiErrorAppend(errs, c.ConnectConfig.Prepare()...)
errs = packer.MultiErrorAppend(errs, c.CreateConfig.Prepare()...)
errs = packer.MultiErrorAppend(errs, c.LocationConfig.Prepare()...)
errs = packer.MultiErrorAppend(errs, c.HardwareConfig.Prepare()...)
errs = packer.MultiErrorAppend(errs, c.RunConfig.Prepare()...)
errs = packer.MultiErrorAppend(errs, c.BootConfig.Prepare()...)
errs = packer.MultiErrorAppend(errs, c.ConnectConfig.Prepare()...)
errs = packer.MultiErrorAppend(errs, c.HardwareConfig.Prepare()...)
errs = packer.MultiErrorAppend(errs, c.Comm.Prepare(&c.ctx)...)
errs = packer.MultiErrorAppend(errs, c.ShutdownConfig.Prepare()...)
errs = packer.MultiErrorAppend(errs, c.CreateConfig.Prepare()...)
if len(errs.Errors) > 0 {
return nil, nil, errs

View File

@ -12,10 +12,6 @@ type CDRomConfig struct {
ISOPaths []string `mapstructure:"iso_paths"`
}
func (c *CDRomConfig) Prepare() []error {
return nil
}
type StepAddCDRom struct {
Config *CDRomConfig
}
@ -24,7 +20,7 @@ func (s *StepAddCDRom) Run(_ context.Context, state multistep.StateBag) multiste
ui := state.Get("ui").(packer.Ui)
vm := state.Get("vm").(*driver.VirtualMachine)
ui.Say("Adding CDRoms...")
ui.Say("Adding CD-ROM drives...")
if err := vm.AddSATAController(); err != nil {
state.Put("error", fmt.Errorf("error adding SATA controller: %v", err))
return multistep.ActionHalt

View File

@ -14,18 +14,6 @@ type FloppyConfig struct {
FloppyDirectories []string `mapstructure:"floppy_dirs"`
}
func (c *FloppyConfig) Prepare() []error {
var errs []error
if c.FloppyIMGPath != "" && (c.FloppyFiles != nil || c.FloppyDirectories != nil) {
errs = append(errs,
fmt.Errorf("'floppy_img_path' cannot be used together with 'floppy_files' and 'floppy_dirs'"),
)
}
return errs
}
type StepAddFloppy struct {
Config *FloppyConfig
Datastore string
@ -33,56 +21,51 @@ type StepAddFloppy struct {
}
func (s *StepAddFloppy) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
err := s.runImpl(state)
if err != nil {
state.Put("error", fmt.Errorf("error adding floppy: %v", err))
return multistep.ActionHalt
}
return multistep.ActionContinue
}
func (s *StepAddFloppy) runImpl(state multistep.StateBag) error {
ui := state.Get("ui").(packer.Ui)
vm := state.Get("vm").(*driver.VirtualMachine)
d := state.Get("driver").(*driver.Driver)
tmpFloppy := state.Get("floppy_path")
if tmpFloppy != nil {
tmpFloppy := state.Get("floppy_path").(string)
if tmpFloppy != "" {
ui.Say("Uploading created floppy image")
ds, err := d.FindDatastore(s.Datastore, s.Host)
if err != nil {
return err
state.Put("error", err)
return multistep.ActionHalt
}
vmDir, err := vm.GetDir()
if err != nil {
return err
state.Put("error", err)
return multistep.ActionHalt
}
uploadPath := fmt.Sprintf("%v/packer-tmp-created-floppy.img", vmDir)
if err := ds.UploadFile(tmpFloppy.(string), uploadPath); err != nil {
return fmt.Errorf("error uploading floppy image: %v", err)
uploadPath := fmt.Sprintf("%v/packer-tmp-created-floppy.flp", vmDir)
if err := ds.UploadFile(tmpFloppy, uploadPath); err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
state.Put("uploaded_floppy_path", uploadPath)
floppyIMGPath := ds.ResolvePath(uploadPath)
ui.Say("Adding generated Floppy...")
floppyIMGPath := ds.ResolvePath(uploadPath)
err = vm.AddFloppy(floppyIMGPath)
if err != nil {
return err
state.Put("error", err)
return multistep.ActionHalt
}
}
if s.Config.FloppyIMGPath != "" {
floppyIMGPath := s.Config.FloppyIMGPath
ui.Say("Adding Floppy image...")
err := vm.AddFloppy(floppyIMGPath)
err := vm.AddFloppy(s.Config.FloppyIMGPath)
if err != nil {
return err
state.Put("error", err)
return multistep.ActionHalt
}
}
return nil
return multistep.ActionContinue
}
func (s *StepAddFloppy) Cleanup(state multistep.StateBag) {}

View File

@ -10,54 +10,35 @@ import (
)
type CreateConfig struct {
common.VMConfig `mapstructure:",squash"`
Version uint `mapstructure:"vm_version"`
GuestOSType string `mapstructure:"guest_os_type"`
DiskThinProvisioned bool `mapstructure:"disk_thin_provisioned"`
DiskControllerType string `mapstructure:"disk_controller_type"`
DiskSize int64 `mapstructure:"disk_size"`
DiskThinProvisioned bool `mapstructure:"disk_thin_provisioned"`
GuestOSType string `mapstructure:"guest_os_type"`
Network string `mapstructure:"network"`
NetworkCard string `mapstructure:"network_card"`
USBController bool `mapstructure:"usb_controller"`
Version int `mapstructure:"vm_version"`
}
func (c *CreateConfig) Prepare() []error {
var errs []error
// needed to avoid changing the original config in case of errors
tmp := *c
// do recursive calls
errs = append(errs, tmp.VMConfig.Prepare()...)
if tmp.Version < 0 {
errs = append(errs, fmt.Errorf("'vm_version' cannot be a negative number"))
if c.DiskSize == 0 {
errs = append(errs, fmt.Errorf("'disk_size' is required"))
}
if tmp.DiskSize == 0 {
errs = append(errs, fmt.Errorf("'disk_size' must be provided"))
if c.GuestOSType == "" {
c.GuestOSType = "otherGuest"
}
if len(errs) > 0 {
return errs
}
// set default values
if tmp.GuestOSType == "" {
tmp.GuestOSType = "otherGuest"
}
// change the original config
*c = tmp
return []error{}
return errs
}
type StepCreateVM struct {
Config *CreateConfig
Config *CreateConfig
Location *common.LocationConfig
}
func (s *StepCreateVM) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
@ -65,30 +46,28 @@ func (s *StepCreateVM) Run(_ context.Context, state multistep.StateBag) multiste
d := state.Get("driver").(*driver.Driver)
ui.Say("Creating VM...")
vm, err := d.CreateVM(&driver.CreateConfig{
DiskThinProvisioned: s.Config.DiskThinProvisioned,
DiskControllerType: s.Config.DiskControllerType,
DiskSize: s.Config.DiskSize,
Name: s.Config.VMName,
Folder: s.Config.Folder,
Cluster: s.Config.Cluster,
Host: s.Config.Host,
ResourcePool: s.Config.ResourcePool,
Datastore: s.Config.Datastore,
Name: s.Location.VMName,
Folder: s.Location.Folder,
Cluster: s.Location.Cluster,
Host: s.Location.Host,
ResourcePool: s.Location.ResourcePool,
Datastore: s.Location.Datastore,
GuestOS: s.Config.GuestOSType,
Network: s.Config.Network,
NetworkCard: s.Config.NetworkCard,
USBController: s.Config.USBController,
Version: s.Config.Version,
})
if err != nil {
state.Put("error", fmt.Errorf("error creating vm: %v", err))
return multistep.ActionHalt
}
state.Put("vm", vm)
return multistep.ActionContinue
}
@ -108,7 +87,6 @@ func (s *StepCreateVM) Cleanup(state multistep.StateBag) {
vm := st.(*driver.VirtualMachine)
ui.Say("Destroying VM...")
err := vm.Destroy()
if err != nil {
ui.Error(err.Error())

View File

@ -1,8 +1,6 @@
package iso
import (
"fmt"
"github.com/hashicorp/packer/packer"
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
"github.com/hashicorp/packer/helper/multistep"
@ -19,12 +17,12 @@ func (s *StepRemoveCDRom) Run(_ context.Context, state multistep.StateBag) multi
ui.Say("Deleting CD-ROM drives...")
devices, err := vm.Devices()
if err != nil {
ui.Error(fmt.Sprintf("error removing cdroms: %v", err))
state.Put("error", 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))
if err = vm.RemoveDevice(true, cdroms...); err != nil {
state.Put("error", err)
return multistep.ActionHalt
}

View File

@ -1,8 +1,6 @@
package iso
import (
"fmt"
"github.com/hashicorp/packer/packer"
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
"github.com/hashicorp/packer/helper/multistep"
@ -20,35 +18,28 @@ func (s *StepRemoveFloppy) Run(_ context.Context, state multistep.StateBag) mult
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))
state.Put("error", err)
return multistep.ActionHalt
}
floppies := devices.SelectByType((*types.VirtualFloppy)(nil))
if err = vm.RemoveDevice(false, floppies...); err != nil {
ui.Error(fmt.Sprintf("error removing floppy: %v", err))
if err = vm.RemoveDevice(true, floppies...); err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
UploadedFloppyPath := state.Get("uploaded_floppy_path").(string)
if UploadedFloppyPath != "" {
ui.Say("Deleting Floppy image...")
ds, err := d.FindDatastore(s.Datastore, s.Host)
if err != nil {
ui.Error(err.Error())
state.Put("error", err)
return multistep.ActionHalt
}
if err := ds.Delete(UploadedFloppyPath); err != nil {
ui.Error(fmt.Sprintf("Error deleting floppy image '%v': %v", UploadedFloppyPath, err.Error()))
state.Put("error", err)
return multistep.ActionHalt
}
}