2018-01-24 06:04:39 -05:00
|
|
|
package clone
|
2017-05-09 10:23:57 -04:00
|
|
|
|
|
|
|
import (
|
2018-10-31 17:42:24 -04:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2018-04-25 07:22:38 -04:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-05-09 10:23:57 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2018-03-15 17:53:25 -04:00
|
|
|
"github.com/jetbrains-infra/packer-builder-vsphere/common"
|
2018-10-31 17:42:24 -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 {
|
2018-05-05 17:41:14 -04:00
|
|
|
Template string `mapstructure:"template"`
|
|
|
|
DiskSize int64 `mapstructure:"disk_size"`
|
2018-05-06 17:26:04 -04:00
|
|
|
LinkedClone bool `mapstructure:"linked_clone"`
|
2019-03-13 16:07:30 -04:00
|
|
|
Network string `mapstructure:"network"`
|
2018-11-08 11:50:52 -05:00
|
|
|
Notes string `mapstructure:"notes"`
|
2017-07-01 20:52:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CloneConfig) Prepare() []error {
|
2018-05-06 17:26:04 -04:00
|
|
|
var errs []error
|
2017-07-01 20:52:10 -04:00
|
|
|
|
|
|
|
if c.Template == "" {
|
2018-05-06 17:26:04 -04:00
|
|
|
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"))
|
2017-07-01 20:52:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
2017-06-27 23:04:25 -04:00
|
|
|
type StepCloneVM struct {
|
2018-05-06 17:26:04 -04:00
|
|
|
Config *CloneConfig
|
|
|
|
Location *common.LocationConfig
|
2018-11-16 10:37:54 -05:00
|
|
|
Force bool
|
2017-05-09 10:23:57 -04:00
|
|
|
}
|
|
|
|
|
2018-05-16 09:05:08 -04:00
|
|
|
func (s *StepCloneVM) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2017-05-09 10:23:57 -04:00
|
|
|
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
|
|
|
|
2019-07-08 10:48:37 -04:00
|
|
|
vm, err := d.FindVM(s.Location.VMName)
|
2017-05-09 10:23:57 -04:00
|
|
|
|
2018-11-16 10:37:54 -05:00
|
|
|
if s.Force == false && err == nil {
|
2019-08-08 07:49:36 -04:00
|
|
|
state.Put("error", fmt.Errorf("%s already exists, you can use -force flag to destroy it", s.Location.VMName))
|
2018-11-16 10:37:54 -05:00
|
|
|
return multistep.ActionHalt
|
|
|
|
} else if s.Force == true && err == nil {
|
|
|
|
ui.Say(fmt.Sprintf("the vm/template %s already exists, but deleting it due to -force flag", s.Location.VMName))
|
2019-07-08 10:48:37 -04:00
|
|
|
err := vm.Destroy()
|
2018-11-16 10:37:54 -05:00
|
|
|
if err != nil {
|
|
|
|
state.Put("error", fmt.Errorf("error destroying %s: %v", s.Location.VMName, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Cloning VM...")
|
2018-05-06 17:26:04 -04:00
|
|
|
template, err := d.FindVM(s.Config.Template)
|
2017-08-23 20:06:50 -04:00
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2019-07-08 10:48:37 -04:00
|
|
|
vm, err = template.Clone(ctx, &driver.CloneConfig{
|
2018-05-06 17:26:04 -04:00
|
|
|
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,
|
2019-03-13 16:07:30 -04:00
|
|
|
Network: s.Config.Network,
|
2018-11-08 11:50:52 -05:00
|
|
|
Annotation: s.Config.Notes,
|
2017-08-23 15:40:57 -04:00
|
|
|
})
|
2017-05-09 10:23:57 -04:00
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2018-05-16 09:05:08 -04:00
|
|
|
if vm == nil {
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2017-05-09 10:23:57 -04:00
|
|
|
state.Put("vm", vm)
|
2018-05-05 17:41:14 -04:00
|
|
|
|
2018-05-06 17:26:04 -04:00
|
|
|
if s.Config.DiskSize > 0 {
|
|
|
|
err = vm.ResizeDisk(s.Config.DiskSize)
|
2018-05-05 17:41:14 -04:00
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-09 10:23:57 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|