2013-12-26 10:34:27 -05:00
|
|
|
package vmx
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-12-26 17:44:39 -05:00
|
|
|
"fmt"
|
2013-12-26 10:34:27 -05:00
|
|
|
"log"
|
2017-01-27 09:11:55 -05:00
|
|
|
"os"
|
2013-12-26 10:34:27 -05:00
|
|
|
"path/filepath"
|
2018-05-01 19:22:09 -04:00
|
|
|
"regexp"
|
2013-12-26 10:34:27 -05:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
vmwcommon "github.com/hashicorp/packer/builder/vmware/common"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2018-12-12 09:45:00 -05:00
|
|
|
"github.com/hashicorp/packer/packer/tmp"
|
2013-12-26 10:34:27 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// StepCloneVMX takes a VMX file and clones the VM into the output directory.
|
|
|
|
type StepCloneVMX struct {
|
|
|
|
OutputDir string
|
|
|
|
Path string
|
|
|
|
VMName string
|
2018-06-16 20:38:42 -04:00
|
|
|
Linked bool
|
2017-01-27 09:11:55 -05:00
|
|
|
tempDir string
|
2013-12-26 10:34:27 -05:00
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepCloneVMX) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2017-01-27 09:11:55 -05:00
|
|
|
halt := func(err error) multistep.StepAction {
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-12-26 16:39:41 -05:00
|
|
|
driver := state.Get("driver").(vmwcommon.Driver)
|
2013-12-26 10:34:27 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
2018-03-30 16:59:38 -04:00
|
|
|
// Set the path we want for the new .vmx file and clone
|
2013-12-26 10:34:27 -05:00
|
|
|
vmxPath := filepath.Join(s.OutputDir, s.VMName+".vmx")
|
2013-12-26 16:39:41 -05:00
|
|
|
ui.Say("Cloning source VM...")
|
2013-12-26 10:34:27 -05:00
|
|
|
log.Printf("Cloning from: %s", s.Path)
|
|
|
|
log.Printf("Cloning to: %s", vmxPath)
|
2017-01-27 09:11:55 -05:00
|
|
|
|
2018-06-16 20:38:42 -04:00
|
|
|
if err := driver.Clone(vmxPath, s.Path, s.Linked); err != nil {
|
2017-02-06 02:13:37 -05:00
|
|
|
return halt(err)
|
2013-12-26 10:34:27 -05:00
|
|
|
}
|
|
|
|
|
2018-03-30 16:59:38 -04:00
|
|
|
// Read in the machine configuration from the cloned VMX file
|
|
|
|
//
|
|
|
|
// * The main driver needs the path to the vmx (set above) and the
|
|
|
|
// network type so that it can work out things like IP's and MAC
|
|
|
|
// addresses
|
|
|
|
// * The disk compaction step needs the paths to all attached disks
|
2017-01-27 09:11:55 -05:00
|
|
|
if remoteDriver, ok := driver.(vmwcommon.RemoteDriver); ok {
|
2017-02-06 02:13:37 -05:00
|
|
|
remoteVmxPath := vmxPath
|
2018-12-12 09:45:00 -05:00
|
|
|
tempDir, err := tmp.Dir("packer-vmx")
|
2017-01-27 09:11:55 -05:00
|
|
|
if err != nil {
|
|
|
|
return halt(err)
|
|
|
|
}
|
|
|
|
s.tempDir = tempDir
|
|
|
|
vmxPath = filepath.Join(tempDir, s.VMName+".vmx")
|
2017-02-06 02:13:37 -05:00
|
|
|
if err = remoteDriver.Download(remoteVmxPath, vmxPath); err != nil {
|
2017-01-27 09:11:55 -05:00
|
|
|
return halt(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-26 17:44:39 -05:00
|
|
|
vmxData, err := vmwcommon.ReadVMX(vmxPath)
|
|
|
|
if err != nil {
|
2017-01-27 09:11:55 -05:00
|
|
|
return halt(err)
|
2013-12-26 17:44:39 -05:00
|
|
|
}
|
|
|
|
|
2018-03-30 16:59:38 -04:00
|
|
|
var diskFilenames []string
|
2018-05-27 05:43:01 -04:00
|
|
|
// The VMX file stores the path to a configured disk, and information
|
|
|
|
// about that disks attachment to a virtual adapter/controller, as a
|
|
|
|
// key/value pair.
|
|
|
|
// For a virtual disk attached to bus ID 3 of the virtual machines
|
|
|
|
// first SCSI adapter the key/value pair would look something like:
|
|
|
|
// scsi0:3.fileName = "relative/path/to/scsiDisk.vmdk"
|
|
|
|
// The supported adapter types and configuration maximums for each type
|
|
|
|
// vary according to the VMware platform type and version, and the
|
|
|
|
// Virtual Machine Hardware version used. See the 'Virtual Machine
|
|
|
|
// Maximums' section within VMware's 'Configuration Maximums'
|
|
|
|
// documentation for each platform:
|
|
|
|
// https://kb.vmware.com/s/article/1003497
|
|
|
|
// Information about the supported Virtual Machine Hardware versions:
|
|
|
|
// https://kb.vmware.com/s/article/1003746
|
|
|
|
// The following regexp is used to match all possible disk attachment
|
|
|
|
// points that may be found in the VMX file across all VMware
|
|
|
|
// platforms/versions and Virtual Machine Hardware versions
|
|
|
|
diskPathKeyRe := regexp.MustCompile(`(?i)^(scsi|sata|ide|nvme)[[:digit:]]:[[:digit:]]{1,2}\.fileName`)
|
|
|
|
for k, v := range vmxData {
|
|
|
|
match := diskPathKeyRe.FindString(k)
|
|
|
|
if match != "" && filepath.Ext(v) == ".vmdk" {
|
|
|
|
diskFilenames = append(diskFilenames, v)
|
|
|
|
}
|
2014-10-27 19:53:25 -04:00
|
|
|
}
|
2018-03-30 16:59:38 -04:00
|
|
|
|
|
|
|
// Write out the relative, host filesystem paths to the disks
|
2018-04-27 15:09:19 -04:00
|
|
|
var diskFullPaths []string
|
2018-03-30 16:59:38 -04:00
|
|
|
for _, diskFilename := range diskFilenames {
|
|
|
|
log.Printf("Found attached disk with filename: %s", diskFilename)
|
2018-04-27 15:09:19 -04:00
|
|
|
diskFullPaths = append(diskFullPaths, filepath.Join(s.OutputDir, diskFilename))
|
2014-10-28 03:58:46 -04:00
|
|
|
}
|
2018-03-30 16:59:38 -04:00
|
|
|
|
2018-04-27 15:09:19 -04:00
|
|
|
if len(diskFullPaths) == 0 {
|
2018-12-10 17:24:27 -05:00
|
|
|
return halt(fmt.Errorf("Could not enumerate disk info from the vmx file"))
|
2013-12-26 17:44:39 -05:00
|
|
|
}
|
|
|
|
|
2018-03-30 16:59:38 -04:00
|
|
|
// Determine the network type by reading out of the .vmx
|
2015-11-11 07:39:06 -05:00
|
|
|
var networkType string
|
2018-03-25 19:51:37 -04:00
|
|
|
if _, ok := vmxData["ethernet0.connectiontype"]; ok {
|
|
|
|
networkType = vmxData["ethernet0.connectiontype"]
|
|
|
|
log.Printf("Discovered the network type: %s", networkType)
|
2015-11-11 07:39:06 -05:00
|
|
|
}
|
|
|
|
if networkType == "" {
|
|
|
|
networkType = "nat"
|
2018-03-25 19:51:37 -04:00
|
|
|
log.Printf("Defaulting to network type: %s", networkType)
|
2015-11-11 07:39:06 -05:00
|
|
|
}
|
|
|
|
|
2018-03-30 16:59:38 -04:00
|
|
|
// Stash all required information in our state bag
|
2013-12-26 10:34:27 -05:00
|
|
|
state.Put("vmx_path", vmxPath)
|
2018-04-27 15:09:19 -04:00
|
|
|
state.Put("disk_full_paths", diskFullPaths)
|
2018-03-25 19:51:37 -04:00
|
|
|
state.Put("vmnetwork", networkType)
|
|
|
|
|
2013-12-26 10:34:27 -05:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepCloneVMX) Cleanup(state multistep.StateBag) {
|
2017-01-27 09:11:55 -05:00
|
|
|
if s.tempDir != "" {
|
|
|
|
os.RemoveAll(s.tempDir)
|
|
|
|
}
|
2013-12-26 10:34:27 -05:00
|
|
|
}
|