packer-cn/builder/vsphere/clone/step_clone.go

121 lines
2.8 KiB
Go
Raw Normal View History

//go:generate struct-markdown
//go:generate mapstructure-to-hcl2 -type CloneConfig
package clone
import (
2018-10-31 17:42:24 -04:00
"context"
"fmt"
"path"
"github.com/hashicorp/packer/builder/vsphere/common"
"github.com/hashicorp/packer/builder/vsphere/driver"
2018-04-25 07:22:38 -04:00
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
)
type CloneConfig struct {
// Name of source VM. Path is optional.
Template string `mapstructure:"template"`
// The size of the disk in MB.
DiskSize int64 `mapstructure:"disk_size"`
// Create VM as a linked clone from latest snapshot. Defaults to `false`.
LinkedClone bool `mapstructure:"linked_clone"`
// Set network VM will be connected to.
Network string `mapstructure:"network"`
// VM notes.
Notes string `mapstructure:"notes"`
}
func (c *CloneConfig) Prepare() []error {
2018-05-06 17:26:04 -04:00
var errs []error
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"))
}
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
Force bool
}
2018-05-16 09:05:08 -04:00
func (s *StepCloneVM) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
d := state.Get("driver").(*driver.Driver)
vmPath := path.Join(s.Location.Folder, s.Location.VMName)
err := d.PreCleanVM(ui, vmPath, s.Force)
if err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
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
}
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,
Network: s.Config.Network,
2018-11-08 11:50:52 -05:00
Annotation: s.Config.Notes,
})
if err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
2018-05-16 09:05:08 -04:00
if vm == nil {
return multistep.ActionHalt
}
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
}
}
return multistep.ActionContinue
}
func (s *StepCloneVM) Cleanup(state multistep.StateBag) {
_, cancelled := state.GetOk(multistep.StateCancelled)
_, halted := state.GetOk(multistep.StateHalted)
if !cancelled && !halted {
return
}
2017-08-23 20:06:50 -04:00
ui := state.Get("ui").(packer.Ui)
st := state.Get("vm")
if st == nil {
return
}
vm := st.(*driver.VirtualMachine)
2017-08-23 20:06:50 -04:00
ui.Say("Destroying VM...")
2017-08-23 20:06:50 -04:00
err := vm.Destroy()
if err != nil {
ui.Error(err.Error())
}
}