2018-01-24 09:56:14 -05:00
|
|
|
package iso
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/jetbrains-infra/packer-builder-vsphere/common"
|
|
|
|
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
|
2018-04-25 07:22:38 -04:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
"context"
|
2018-01-24 09:56:14 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type CreateConfig struct {
|
2018-05-06 17:26:04 -04:00
|
|
|
Version uint `mapstructure:"vm_version"`
|
|
|
|
GuestOSType string `mapstructure:"guest_os_type"`
|
2018-10-16 19:42:02 -04:00
|
|
|
Firmware string `mapstructure:"firmware"`
|
2018-01-24 09:56:14 -05:00
|
|
|
|
|
|
|
DiskControllerType string `mapstructure:"disk_controller_type"`
|
2018-05-05 17:41:14 -04:00
|
|
|
DiskSize int64 `mapstructure:"disk_size"`
|
2018-05-06 17:26:04 -04:00
|
|
|
DiskThinProvisioned bool `mapstructure:"disk_thin_provisioned"`
|
2018-01-24 09:56:14 -05:00
|
|
|
|
2018-02-26 19:22:52 -05:00
|
|
|
Network string `mapstructure:"network"`
|
|
|
|
NetworkCard string `mapstructure:"network_card"`
|
|
|
|
USBController bool `mapstructure:"usb_controller"`
|
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-16 19:42:02 -04:00
|
|
|
if (c.Firmware == "") {
|
|
|
|
c.Firmware = "bios"
|
|
|
|
}
|
|
|
|
|
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-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)
|
|
|
|
|
|
|
|
ui.Say("Creating VM...")
|
|
|
|
vm, err := d.CreateVM(&driver.CreateConfig{
|
2018-02-01 06:47:09 -05:00
|
|
|
DiskThinProvisioned: s.Config.DiskThinProvisioned,
|
|
|
|
DiskControllerType: s.Config.DiskControllerType,
|
2018-05-05 17:41:14 -04:00
|
|
|
DiskSize: s.Config.DiskSize,
|
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,
|
2018-02-01 06:47:09 -05:00
|
|
|
GuestOS: s.Config.GuestOSType,
|
|
|
|
Network: s.Config.Network,
|
|
|
|
NetworkCard: s.Config.NetworkCard,
|
2018-02-26 19:22:52 -05:00
|
|
|
USBController: s.Config.USBController,
|
2018-03-20 05:09:36 -04:00
|
|
|
Version: s.Config.Version,
|
2018-10-16 19:42:02 -04:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|