2018-01-24 06:04:39 -05:00
|
|
|
package clone
|
2017-05-09 10:23:57 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"fmt"
|
2017-08-23 15:40:57 -04:00
|
|
|
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
|
2017-05-09 10:23:57 -04:00
|
|
|
)
|
|
|
|
|
2017-07-01 20:52:10 -04:00
|
|
|
type CloneConfig struct {
|
|
|
|
Template string `mapstructure:"template"`
|
|
|
|
VMName string `mapstructure:"vm_name"`
|
2017-07-02 16:29:50 -04:00
|
|
|
Folder string `mapstructure:"folder"`
|
2017-07-01 20:52:10 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-06-27 23:04:25 -04:00
|
|
|
type StepCloneVM struct {
|
2017-07-01 23:46:38 -04:00
|
|
|
config *CloneConfig
|
2017-05-09 10:23:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepCloneVM) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2017-08-23 15:40:57 -04:00
|
|
|
d := state.Get("driver").(*driver.Driver)
|
2017-07-01 20:52:10 -04:00
|
|
|
|
2017-07-01 23:46:38 -04:00
|
|
|
ui.Say("Cloning VM...")
|
2017-05-09 10:23:57 -04:00
|
|
|
|
2017-08-23 20:06:50 -04:00
|
|
|
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,
|
2017-08-23 15:40:57 -04:00
|
|
|
Folder: s.config.Folder,
|
|
|
|
Host: s.config.Host,
|
|
|
|
ResourcePool: s.config.ResourcePool,
|
|
|
|
Datastore: s.config.Datastore,
|
|
|
|
LinkedClone: s.config.LinkedClone,
|
|
|
|
})
|
2017-05-09 10:23:57 -04:00
|
|
|
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)
|
2017-07-01 20:52:10 -04:00
|
|
|
if !cancelled && !halted {
|
|
|
|
return
|
|
|
|
}
|
2017-05-09 10:23:57 -04:00
|
|
|
|
2017-08-23 20:06:50 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2017-08-24 14:54:01 -04:00
|
|
|
|
|
|
|
st := state.Get("vm")
|
|
|
|
if st == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
vm := st.(*driver.VirtualMachine)
|
2017-05-09 10:23:57 -04:00
|
|
|
|
2017-08-23 20:06:50 -04:00
|
|
|
ui.Say("Destroying VM...")
|
2017-05-09 10:23:57 -04:00
|
|
|
|
2017-08-23 20:06:50 -04:00
|
|
|
err := vm.Destroy()
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(err.Error())
|
2017-05-09 10:23:57 -04:00
|
|
|
}
|
|
|
|
}
|