2017-07-10 20:52:48 -04:00
|
|
|
package vsphere_template
|
2017-07-09 14:56:39 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-08-14 22:00:19 -04:00
|
|
|
"fmt"
|
|
|
|
"path"
|
2017-11-06 00:57:41 -05:00
|
|
|
"regexp"
|
2017-08-14 22:00:19 -04:00
|
|
|
"strings"
|
2017-07-09 14:56:39 -04:00
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-07-09 14:56:39 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2017-08-14 22:00:19 -04:00
|
|
|
"github.com/vmware/govmomi"
|
2017-07-09 14:56:39 -04:00
|
|
|
"github.com/vmware/govmomi/object"
|
2017-11-06 00:57:41 -05:00
|
|
|
"github.com/vmware/govmomi/vim25/types"
|
2017-07-09 14:56:39 -04:00
|
|
|
)
|
|
|
|
|
2017-08-14 22:00:19 -04:00
|
|
|
type stepMarkAsTemplate struct {
|
|
|
|
VMName string
|
|
|
|
}
|
2017-07-09 14:56:39 -04:00
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *stepMarkAsTemplate) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2017-07-09 14:56:39 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2017-08-14 22:00:19 -04:00
|
|
|
cli := state.Get("client").(*govmomi.Client)
|
|
|
|
folder := state.Get("folder").(*object.Folder)
|
|
|
|
dcPath := state.Get("dcPath").(string)
|
|
|
|
|
|
|
|
ui.Message("Marking as a template...")
|
|
|
|
|
|
|
|
vm, err := findRuntimeVM(cli, dcPath, s.VMName)
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2017-11-06 00:57:41 -05:00
|
|
|
if err := unregisterPreviousVM(cli, folder, s.VMName); err != nil {
|
2017-08-14 22:00:19 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2017-11-08 13:57:34 -05:00
|
|
|
dsPath, err := datastorePath(vm)
|
2017-11-06 00:57:41 -05:00
|
|
|
if err != nil {
|
2017-08-14 22:00:19 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2017-11-06 00:57:41 -05:00
|
|
|
host, err := vm.HostSystem(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2017-08-14 22:00:19 -04:00
|
|
|
|
2017-11-06 00:57:41 -05:00
|
|
|
if err := vm.Unregister(context.Background()); err != nil {
|
2017-08-14 22:00:19 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2017-07-09 14:56:39 -04:00
|
|
|
|
2017-11-08 13:57:34 -05:00
|
|
|
task, err := folder.RegisterVM(context.Background(), dsPath.String(), s.VMName, true, nil, host)
|
2017-08-14 22:00:19 -04:00
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2017-07-09 14:56:39 -04:00
|
|
|
|
2017-08-14 22:00:19 -04:00
|
|
|
if err = task.Wait(context.Background()); err != nil {
|
2017-07-09 14:56:39 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2017-08-14 22:00:19 -04:00
|
|
|
|
2017-07-09 14:56:39 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2017-11-08 13:57:34 -05:00
|
|
|
func datastorePath(vm *object.VirtualMachine) (*object.DatastorePath, error) {
|
2017-11-06 00:57:41 -05:00
|
|
|
devices, err := vm.Device(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
disk := ""
|
|
|
|
for _, device := range devices {
|
|
|
|
if d, ok := device.(*types.VirtualDisk); ok {
|
|
|
|
if b, ok := d.Backing.(types.BaseVirtualDeviceFileBackingInfo); ok {
|
|
|
|
disk = b.GetVirtualDeviceFileBackingInfo().FileName
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if disk == "" {
|
2017-11-08 13:57:34 -05:00
|
|
|
return nil, fmt.Errorf("disk not found in '%v'", vm.Name())
|
2017-11-06 00:57:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
re := regexp.MustCompile("\\[(.*?)\\]")
|
|
|
|
|
|
|
|
datastore := re.FindStringSubmatch(disk)[1]
|
2017-11-08 13:57:34 -05:00
|
|
|
vmxPath := path.Join("/", path.Dir(strings.Split(disk, " ")[1]), vm.Name()+".vmx")
|
2017-11-06 00:57:41 -05:00
|
|
|
|
2017-11-13 14:45:31 -05:00
|
|
|
return &object.DatastorePath{
|
|
|
|
Datastore: datastore,
|
|
|
|
Path: vmxPath,
|
|
|
|
}, nil
|
2017-11-06 00:57:41 -05:00
|
|
|
}
|
|
|
|
|
2017-08-14 22:00:19 -04:00
|
|
|
// We will use the virtual machine created by vmware-iso builder
|
|
|
|
func findRuntimeVM(cli *govmomi.Client, dcPath, name string) (*object.VirtualMachine, error) {
|
|
|
|
si := object.NewSearchIndex(cli.Client)
|
|
|
|
fullPath := path.Join(dcPath, "vm", "Discovered virtual machine", name)
|
|
|
|
|
|
|
|
ref, err := si.FindByInventoryPath(context.Background(), fullPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ref == nil {
|
|
|
|
return nil, fmt.Errorf("VM at path %s not found", fullPath)
|
|
|
|
}
|
|
|
|
|
2017-11-08 13:57:34 -05:00
|
|
|
vm := ref.(*object.VirtualMachine)
|
|
|
|
if vm.InventoryPath == "" {
|
|
|
|
vm.SetInventoryPath(fullPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
return vm, nil
|
2017-08-14 22:00:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// If in the target folder a virtual machine or template already exists
|
|
|
|
// it will be removed to maintain consistency
|
|
|
|
func unregisterPreviousVM(cli *govmomi.Client, folder *object.Folder, name string) error {
|
|
|
|
si := object.NewSearchIndex(cli.Client)
|
|
|
|
fullPath := path.Join(folder.InventoryPath, name)
|
|
|
|
|
|
|
|
ref, err := si.FindByInventoryPath(context.Background(), fullPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ref != nil {
|
|
|
|
if vm, ok := ref.(*object.VirtualMachine); ok {
|
|
|
|
return vm.Unregister(context.Background())
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("an object name '%v' already exists", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-07-18 23:10:05 -04:00
|
|
|
func (s *stepMarkAsTemplate) Cleanup(multistep.StateBag) {}
|