package main import ( "github.com/mitchellh/multistep" "github.com/hashicorp/packer/packer" "fmt" "github.com/jetbrains-infra/packer-builder-vsphere/driver" ) type CloneConfig struct { Template string `mapstructure:"template"` VMName string `mapstructure:"vm_name"` Folder string `mapstructure:"folder"` Host string `mapstructure:"host"` ResourcePool string `mapstructure:"resource_pool"` Datastore string `mapstructure:"datastore"` LinkedClone bool `mapstructure:"linked_clone"` } func (c *CloneConfig) Prepare() []error { var errs []error if c.Template == "" { errs = append(errs, fmt.Errorf("Template name is required")) } if c.VMName == "" { errs = append(errs, fmt.Errorf("Target VM name is required")) } if c.Host == "" { errs = append(errs, fmt.Errorf("vSphere host is required")) } return errs } type StepCloneVM struct { config *CloneConfig } func (s *StepCloneVM) Run(state multistep.StateBag) multistep.StepAction { ui := state.Get("ui").(packer.Ui) d := state.Get("driver").(*driver.Driver) ui.Say("Cloning VM...") 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, Host: s.config.Host, ResourcePool: s.config.ResourcePool, Datastore: s.config.Datastore, LinkedClone: s.config.LinkedClone, }) if err != nil { state.Put("error", err) return multistep.ActionHalt } state.Put("vm", vm) return multistep.ActionContinue } func (s *StepCloneVM) 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()) } }