package iso import ( "fmt" "github.com/hashicorp/packer/packer" "github.com/jetbrains-infra/packer-builder-vsphere/common" "github.com/jetbrains-infra/packer-builder-vsphere/driver" "github.com/hashicorp/packer/helper/multistep" "context" ) type CreateConfig struct { common.VMConfig `mapstructure:",squash"` DiskThinProvisioned bool `mapstructure:"disk_thin_provisioned"` DiskControllerType string `mapstructure:"disk_controller_type"` DiskSize int64 `mapstructure:"disk_size"` 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 tmp.DiskSize == 0 { errs = append(errs, fmt.Errorf("'disk_size' must be provided")) } if len(errs) > 0 { return errs } // set default values if tmp.GuestOSType == "" { tmp.GuestOSType = "otherGuest" } // change the original config *c = tmp return []error{} } type StepCreateVM struct { Config *CreateConfig } func (s *StepCreateVM) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { ui := state.Get("ui").(packer.Ui) 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, 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 } func (s *StepCreateVM) Cleanup(state multistep.StateBag) { _, cancelled := state.GetOk(multistep.StateCancelled) _, halted := state.GetOk(multistep.StateHalted) if !cancelled && !halted { return } ui := state.Get("ui").(packer.Ui) st := state.Get("vm") if st == nil { return } vm := st.(*driver.VirtualMachine) ui.Say("Destroying VM...") err := vm.Destroy() if err != nil { ui.Error(err.Error()) } }