2020-01-07 19:59:31 -05:00
|
|
|
//go:generate struct-markdown
|
2020-02-21 10:47:46 -05:00
|
|
|
//go:generate mapstructure-to-hcl2 -type NIC,CreateConfig,DiskConfig
|
2020-01-07 19:59:31 -05:00
|
|
|
|
2018-01-24 09:56:14 -05:00
|
|
|
package iso
|
|
|
|
|
|
|
|
import (
|
2018-10-31 17:42:24 -04:00
|
|
|
"context"
|
2018-01-24 09:56:14 -05:00
|
|
|
"fmt"
|
2020-01-08 18:28:26 -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-10-31 17:42:24 -04:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2018-01-24 09:56:14 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
)
|
|
|
|
|
2020-02-14 11:51:57 -05:00
|
|
|
type NIC struct {
|
|
|
|
// Set network VM will be connected to.
|
|
|
|
Network string `mapstructure:"network"`
|
|
|
|
// Set VM network card type. Example `vmxnet3`.
|
|
|
|
NetworkCard string `mapstructure:"network_card" required:"true"`
|
|
|
|
// Set network card MAC address
|
|
|
|
MacAddress string `mapstructure:"mac_address"`
|
|
|
|
// Enable DirectPath I/O passthrough
|
|
|
|
Passthrough *bool `mapstructure:"passthrough"`
|
|
|
|
}
|
|
|
|
|
2020-02-21 10:47:46 -05:00
|
|
|
type DiskConfig struct {
|
|
|
|
// Set the size of the disk
|
|
|
|
DiskSize int64 `mapstructure:"disk_size" required:"true"`
|
|
|
|
// Enable VMDK thin provisioning for VM. Defaults to `false`.
|
|
|
|
DiskThinProvisioned bool `mapstructure:"disk_thin_provisioned"`
|
|
|
|
// Enable VMDK eager scrubbing for VM. Defaults to `false`.
|
|
|
|
DiskEagerlyScrub bool `mapstructure:"disk_eagerly_scrub"`
|
|
|
|
}
|
|
|
|
|
2018-01-24 09:56:14 -05:00
|
|
|
type CreateConfig struct {
|
2020-01-07 19:59:31 -05:00
|
|
|
// Set VM hardware version. Defaults to the most current VM hardware
|
|
|
|
// version supported by vCenter. See
|
|
|
|
// [VMWare article 1003746](https://kb.vmware.com/s/article/1003746) for
|
|
|
|
// the full list of supported VM hardware versions.
|
|
|
|
Version uint `mapstructure:"vm_version"`
|
|
|
|
// Set VM OS type. Defaults to `otherGuest`. See [
|
|
|
|
// here](https://pubs.vmware.com/vsphere-6-5/index.jsp?topic=%2Fcom.vmware.wssdk.apiref.doc%2Fvim.vm.GuestOsDescriptor.GuestOsIdentifier.html)
|
|
|
|
// for a full list of possible values.
|
2018-05-06 17:26:04 -04:00
|
|
|
GuestOSType string `mapstructure:"guest_os_type"`
|
2020-01-07 19:59:31 -05:00
|
|
|
// Set the Firmware at machine creation. Example `efi`. Defaults to `bios`.
|
|
|
|
Firmware string `mapstructure:"firmware"`
|
|
|
|
// Set VM disk controller type. Example `pvscsi`.
|
|
|
|
DiskControllerType string `mapstructure:"disk_controller_type"`
|
|
|
|
// The size of the disk in MB.
|
|
|
|
DiskSize int64 `mapstructure:"disk_size"`
|
|
|
|
// Enable VMDK thin provisioning for VM. Defaults to `false`.
|
|
|
|
DiskThinProvisioned bool `mapstructure:"disk_thin_provisioned"`
|
2020-02-17 02:58:06 -05:00
|
|
|
// Enable VMDK eager scrubbing for VM. Defaults to `false`.
|
|
|
|
DiskEagerlyScrub bool `mapstructure:"disk_eagerly_scrub"`
|
2020-02-21 10:47:46 -05:00
|
|
|
// Storage
|
|
|
|
Storage []DiskConfig `mapstructure:"storage"`
|
2020-01-08 18:28:26 -05:00
|
|
|
// Set network VM will be connected to.
|
2020-01-07 19:59:31 -05:00
|
|
|
Network string `mapstructure:"network"`
|
|
|
|
// Set VM network card type. Example `vmxnet3`.
|
|
|
|
NetworkCard string `mapstructure:"network_card"`
|
2020-02-14 11:51:57 -05:00
|
|
|
// Network adapters
|
|
|
|
NICs []NIC `mapstructure:"network_adapters"`
|
2020-01-07 19:59:31 -05:00
|
|
|
// Create USB controller for virtual machine. Defaults to `false`.
|
|
|
|
USBController bool `mapstructure:"usb_controller"`
|
|
|
|
// VM notes.
|
2018-11-08 11:50:52 -05:00
|
|
|
Notes string `mapstructure:"notes"`
|
2018-01-24 09:56:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CreateConfig) Prepare() []error {
|
|
|
|
var errs []error
|
|
|
|
|
2018-05-06 17:26:04 -04:00
|
|
|
if c.DiskSize == 0 {
|
|
|
|
errs = append(errs, fmt.Errorf("'disk_size' is required"))
|
2018-03-20 05:09:36 -04:00
|
|
|
}
|
|
|
|
|
2018-05-06 17:26:04 -04:00
|
|
|
if c.GuestOSType == "" {
|
|
|
|
c.GuestOSType = "otherGuest"
|
2018-05-05 17:41:14 -04:00
|
|
|
}
|
|
|
|
|
2018-10-31 17:42:24 -04:00
|
|
|
if c.Firmware != "" && c.Firmware != "bios" && c.Firmware != "efi" {
|
2018-10-16 19:15:27 -04:00
|
|
|
errs = append(errs, fmt.Errorf("'firmware' must be 'bios' or 'efi'"))
|
2018-10-16 19:42:02 -04:00
|
|
|
}
|
|
|
|
|
2018-05-06 17:26:04 -04:00
|
|
|
return errs
|
2018-01-24 09:56:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type StepCreateVM struct {
|
2018-05-06 17:26:04 -04:00
|
|
|
Config *CreateConfig
|
|
|
|
Location *common.LocationConfig
|
2018-11-16 10:37:54 -05:00
|
|
|
Force bool
|
2018-01-24 09:56:14 -05:00
|
|
|
}
|
|
|
|
|
2018-04-25 07:22:38 -04:00
|
|
|
func (s *StepCreateVM) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2018-01-24 09:56:14 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
d := state.Get("driver").(*driver.Driver)
|
|
|
|
|
2019-07-08 10:48:37 -04:00
|
|
|
vm, err := d.FindVM(s.Location.VMName)
|
2018-11-16 10:37:54 -05:00
|
|
|
|
|
|
|
if s.Force == false && err == nil {
|
|
|
|
state.Put("error", fmt.Errorf("%s already exists, you can use -force flag to destroy it: %v", s.Location.VMName, err))
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-24 09:56:14 -05:00
|
|
|
ui.Say("Creating VM...")
|
2020-02-14 11:51:57 -05:00
|
|
|
|
|
|
|
// add network/network card an the first nic for backwards compatibility in the type is defined
|
|
|
|
var networkCards []driver.NIC
|
|
|
|
if s.Config.NetworkCard != "" {
|
|
|
|
networkCards = append(networkCards, driver.NIC{
|
|
|
|
NetworkCard: s.Config.NetworkCard,
|
|
|
|
Network: s.Config.Network})
|
|
|
|
}
|
|
|
|
for _, nic := range s.Config.NICs {
|
|
|
|
networkCards = append(networkCards, driver.NIC{
|
|
|
|
Network: nic.Network,
|
|
|
|
NetworkCard: nic.NetworkCard,
|
|
|
|
MacAddress: nic.MacAddress,
|
|
|
|
Passthrough: nic.Passthrough,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-21 10:47:46 -05:00
|
|
|
// add disk as the first drive for backwards compatibility if the type is defined
|
|
|
|
var disks []driver.Disk
|
|
|
|
if s.Config.DiskSize != 0 {
|
|
|
|
disks = append(disks, driver.Disk{
|
|
|
|
DiskSize: s.Config.DiskSize,
|
|
|
|
DiskEagerlyScrub: s.Config.DiskEagerlyScrub,
|
|
|
|
DiskThinProvisioned: s.Config.DiskThinProvisioned,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for _, disk := range s.Config.Storage {
|
|
|
|
disks = append(disks, driver.Disk{
|
|
|
|
DiskSize: disk.DiskSize,
|
|
|
|
DiskEagerlyScrub: disk.DiskEagerlyScrub,
|
|
|
|
DiskThinProvisioned: disk.DiskThinProvisioned,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-08 10:48:37 -04:00
|
|
|
vm, err = d.CreateVM(&driver.CreateConfig{
|
2020-02-21 10:47:46 -05:00
|
|
|
DiskControllerType: s.Config.DiskControllerType,
|
|
|
|
Storage: disks,
|
|
|
|
Annotation: s.Config.Notes,
|
|
|
|
Name: s.Location.VMName,
|
|
|
|
Folder: s.Location.Folder,
|
|
|
|
Cluster: s.Location.Cluster,
|
|
|
|
Host: s.Location.Host,
|
|
|
|
ResourcePool: s.Location.ResourcePool,
|
|
|
|
Datastore: s.Location.Datastore,
|
|
|
|
GuestOS: s.Config.GuestOSType,
|
|
|
|
NICs: networkCards,
|
|
|
|
USBController: s.Config.USBController,
|
|
|
|
Version: s.Config.Version,
|
|
|
|
Firmware: s.Config.Firmware,
|
2018-01-24 09:56:14 -05:00
|
|
|
})
|
|
|
|
if err != nil {
|
2018-02-01 06:47:09 -05:00
|
|
|
state.Put("error", fmt.Errorf("error creating vm: %v", err))
|
2018-01-24 09:56:14 -05:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
state.Put("vm", vm)
|
2018-05-06 17:26:04 -04:00
|
|
|
|
2018-01-24 09:56:14 -05:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepCreateVM) 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())
|
|
|
|
}
|
|
|
|
}
|