2020-01-07 19:59:31 -05:00
|
|
|
//go:generate struct-markdown
|
2020-07-08 04:33:45 -04:00
|
|
|
//go:generate mapstructure-to-hcl2 -type CloneConfig,vAppConfig
|
2020-01-07 19:59:31 -05:00
|
|
|
|
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"
|
2020-04-23 08:07:07 -04:00
|
|
|
"path"
|
2020-02-14 11:42:29 -05:00
|
|
|
|
2020-01-07 19:59:31 -05:00
|
|
|
"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"
|
2017-05-09 10:23:57 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
)
|
|
|
|
|
2020-07-08 04:33:45 -04:00
|
|
|
type vAppConfig struct {
|
|
|
|
// Set values for the available vApp Properties to supply configuration parameters to a virtual machine cloned from
|
|
|
|
// a template that came from an imported OVF or OVA file.
|
|
|
|
//
|
|
|
|
// -> **Note:** The only supported usage path for vApp properties is for existing user-configurable keys.
|
|
|
|
// These generally come from an existing template that was created from an imported OVF or OVA file.
|
|
|
|
// You cannot set values for vApp properties on virtual machines created from scratch,
|
|
|
|
// virtual machines lacking a vApp configuration, or on property keys that do not exist.
|
|
|
|
Properties map[string]string `mapstructure:"properties"`
|
|
|
|
}
|
|
|
|
|
2017-07-01 20:52:10 -04:00
|
|
|
type CloneConfig struct {
|
2020-01-07 19:59:31 -05:00
|
|
|
// 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"`
|
2020-08-13 11:26:40 -04:00
|
|
|
// Set the network in which the VM will be connected to. If no network is
|
|
|
|
// specified, `host` must be specified to allow Packer to look for the
|
|
|
|
// available network. If the network is inside a network folder in vCenter,
|
|
|
|
// you need to provide the full path to the network.
|
2020-01-07 19:59:31 -05:00
|
|
|
Network string `mapstructure:"network"`
|
2020-09-14 07:18:39 -04:00
|
|
|
// Sets a custom Mac Address to the network adapter. If set, the [network](#network) must be also specified.
|
|
|
|
MacAddress string `mapstructure:"mac_address"`
|
2020-01-07 19:59:31 -05:00
|
|
|
// VM notes.
|
|
|
|
Notes string `mapstructure:"notes"`
|
2020-07-08 04:33:45 -04:00
|
|
|
// Set the vApp Options to a virtual machine.
|
|
|
|
// See the [vApp Options Configuration](/docs/builders/vmware/vsphere-clone#vapp-options-configuration)
|
|
|
|
// to know the available options and how to use it.
|
|
|
|
VAppConfig vAppConfig `mapstructure:"vapp"`
|
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
|
|
|
}
|
|
|
|
|
2020-09-14 07:18:39 -04:00
|
|
|
if c.MacAddress != "" && c.Network == "" {
|
|
|
|
errs = append(errs, fmt.Errorf("'network' is required when 'mac_address' is specified"))
|
|
|
|
}
|
|
|
|
|
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)
|
2020-08-31 04:34:41 -04:00
|
|
|
d := state.Get("driver").(*driver.VCenterDriver)
|
2020-04-23 08:07:07 -04:00
|
|
|
vmPath := path.Join(s.Location.Folder, s.Location.VMName)
|
2017-07-01 20:52:10 -04:00
|
|
|
|
2020-04-23 08:07:07 -04:00
|
|
|
err := d.PreCleanVM(ui, vmPath, s.Force)
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
2018-11-16 10:37:54 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-04-23 08:07:07 -04:00
|
|
|
vm, err := template.Clone(ctx, &driver.CloneConfig{
|
2020-07-08 04:33:45 -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,
|
2020-09-14 07:18:39 -04:00
|
|
|
MacAddress: s.Config.MacAddress,
|
2020-07-08 04:33:45 -04:00
|
|
|
Annotation: s.Config.Notes,
|
|
|
|
VAppProperties: s.Config.VAppConfig.Properties,
|
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) {
|
2020-10-27 09:07:08 -04:00
|
|
|
common.CleanupVM(state)
|
2017-05-09 10:23:57 -04:00
|
|
|
}
|