mirror of
https://github.com/hashicorp/packer.git
synced 2026-10-12 17:22:01 -04:00
this regroups the calls of tmp func calls into one package. the tmp pkg allows to store tmp files under a common directory for easier managment Squashed commit of the following: commit74b674c015Merge:fc94b571466001525dAuthor: Matthew Patton <[email protected]> Date: Fri Nov 2 02:02:38 2018 -0400 Merge branch 'remove_TMPDIR_docs' of github.com:tb3088/packer into remove_TMPDIR_docs commitfc94b57142Author: Matthew Patton <[email protected]> Date: Fri Nov 2 01:42:38 2018 -0400 update tests to honor configured temporary directory commit5a562359f4Author: Matthew Patton <[email protected]> Date: Thu Nov 1 18:52:38 2018 -0400 sync straggler to refactor. implement tests at packer/configfile instead of buried in docker. commit9d5a4a0935Author: Matthew Patton <[email protected]> Date: Thu Nov 1 15:39:46 2018 -0400 insert URL to GoLang API for os.TempDir() commitfbae4c2e05Author: Matthew Patton <[email protected]> Date: Thu Nov 1 07:41:29 2018 -0400 revise documentation to match code commit3590fae8bdAuthor: Matthew Patton <[email protected]> Date: Thu Nov 1 07:21:59 2018 -0400 refacter config_file. replace all hard-coded os.TempDir with wrapper commitd5c5306a97Author: Matthew Patton <[email protected]> Date: Wed Oct 31 20:11:36 2018 -0400 close massive file overwrite hole with TempDir commit0a72297da8Author: Matthew Patton <[email protected]> Date: Wed Oct 31 01:06:00 2018 -0400 adjust var declaration scope commit20f68228b6Author: Matthew Patton <[email protected]> Date: Wed Oct 31 00:54:35 2018 -0400 use mktemp() equivalent to create temporary directory commitc73ebe3d83Author: Matthew Patton <[email protected]> Date: Tue Oct 30 20:40:58 2018 -0400 remove extraneous variable declaration, fix FOR loop commit63549b8bd7Author: Matthew Patton <[email protected]> Date: Tue Oct 30 19:30:44 2018 -0400 match styistic convension with rest of docs commit9761010749Author: Matthew Patton <[email protected]> Date: Tue Oct 30 19:08:39 2018 -0400 revert dangling config file change commit95159afbc0Author: Matthew Patton <[email protected]> Date: Fri Dec 29 23:53:43 2017 -0500 replace invalid TMPDIR variable wth PACKER_TMP_DIR. update ConfigTmpDir() to try common temporary paths first and only write to configDir() as a last resort. commit66001525d7Author: Matthew Patton <[email protected]> Date: Fri Nov 2 01:42:38 2018 -0400 update tests to honor configured temporary directory commite9b6adefeaAuthor: Matthew Patton <[email protected]> Date: Thu Nov 1 18:52:38 2018 -0400 sync straggler to refactor. implement tests at packer/configfile instead of buried in docker. commit852113ed07Author: Matthew Patton <[email protected]> Date: Thu Nov 1 15:39:46 2018 -0400 insert URL to GoLang API for os.TempDir() commit54add38d1dAuthor: Matthew Patton <[email protected]> Date: Thu Nov 1 07:41:29 2018 -0400 revise documentation to match code commit6b5b8f6d4eAuthor: Matthew Patton <[email protected]> Date: Thu Nov 1 07:21:59 2018 -0400 refacter config_file. replace all hard-coded os.TempDir with wrapper commitc22092c601Author: Matthew Patton <[email protected]> Date: Wed Oct 31 20:11:36 2018 -0400 close massive file overwrite hole with TempDir commit7a73045091Author: Matthew Patton <[email protected]> Date: Wed Oct 31 01:06:00 2018 -0400 adjust var declaration scope commit0f2933adb6Author: Matthew Patton <[email protected]> Date: Wed Oct 31 00:54:35 2018 -0400 use mktemp() equivalent to create temporary directory commitd74839ede0Author: Matthew Patton <[email protected]> Date: Tue Oct 30 20:40:58 2018 -0400 remove extraneous variable declaration, fix FOR loop commiteb65416619Author: Matthew Patton <[email protected]> Date: Tue Oct 30 19:30:44 2018 -0400 match styistic convension with rest of docs commitacaa2b31edAuthor: Matthew Patton <[email protected]> Date: Tue Oct 30 19:08:39 2018 -0400 revert dangling config file change commite573fde668Author: Matthew Patton <[email protected]> Date: Fri Dec 29 23:53:43 2017 -0500 replace invalid TMPDIR variable wth PACKER_TMP_DIR. update ConfigTmpDir() to try common temporary paths first and only write to configDir() as a last resort. commit39a9874afcMerge:8a413cfe83315812c2Author: Matthew Patton <[email protected]> Date: Tue Oct 30 18:19:26 2018 -0400 Merge branch 'master' of https://github.com/hashicorp/packer into prestine commit8a413cfe83Merge:e07491de54e14710a6Author: Matthew Patton <[email protected]> Date: Mon Oct 1 20:18:10 2018 -0400 Merge remote-tracking branch 'upstream/master' into prestine commite07491de59Merge:42610a35da1fa35dffAuthor: Matthew Patton <[email protected]> Date: Tue Aug 21 13:26:19 2018 -0400 Merge remote-tracking branch 'upstream/master' into prestine commit42610a35d5Merge:5298142da0d63cf7bcAuthor: Matthew Patton <[email protected]> Date: Tue Jun 19 22:45:05 2018 -0400 Merge remote-tracking branch 'upstream/master' into prestine commit5298142da6Merge:7bb110bc79d9736552Author: Matthew Patton <[email protected]> Date: Mon Jun 11 15:10:09 2018 -0400 Merge remote-tracking branch 'upstream/master' into prestine commit7bb110bc74Merge:a61c869ca6189d66e7Author: Matthew Patton <[email protected]> Date: Wed May 9 23:41:22 2018 -0400 Merge remote-tracking branch 'upstream/master' into prestine commita61c869ca7Merge:098101dd94be5f072cAuthor: Matthew Patton <[email protected]> Date: Fri May 4 10:13:04 2018 -0400 Merge remote-tracking branch 'upstream/master' into prestine commit098101dd99Merge:fefaf0fa6554b2b4a5Author: Matthew Patton <[email protected]> Date: Tue May 1 20:52:30 2018 -0400 Merge branch 'EOL-handling' into prestine Co-Authored-By: Matthew Patton <[email protected]>
130 lines
4.1 KiB
Go
130 lines
4.1 KiB
Go
package vmx
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
|
|
vmwcommon "github.com/hashicorp/packer/builder/vmware/common"
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
"github.com/hashicorp/packer/packer"
|
|
"github.com/hashicorp/packer/packer/tmp"
|
|
)
|
|
|
|
// StepCloneVMX takes a VMX file and clones the VM into the output directory.
|
|
type StepCloneVMX struct {
|
|
OutputDir string
|
|
Path string
|
|
VMName string
|
|
Linked bool
|
|
tempDir string
|
|
}
|
|
|
|
func (s *StepCloneVMX) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
|
halt := func(err error) multistep.StepAction {
|
|
state.Put("error", err)
|
|
return multistep.ActionHalt
|
|
}
|
|
|
|
driver := state.Get("driver").(vmwcommon.Driver)
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
// Set the path we want for the new .vmx file and clone
|
|
vmxPath := filepath.Join(s.OutputDir, s.VMName+".vmx")
|
|
ui.Say("Cloning source VM...")
|
|
log.Printf("Cloning from: %s", s.Path)
|
|
log.Printf("Cloning to: %s", vmxPath)
|
|
|
|
if err := driver.Clone(vmxPath, s.Path, s.Linked); err != nil {
|
|
return halt(err)
|
|
}
|
|
|
|
// 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
|
|
if remoteDriver, ok := driver.(vmwcommon.RemoteDriver); ok {
|
|
remoteVmxPath := vmxPath
|
|
tempDir, err := tmp.Dir("packer-vmx")
|
|
if err != nil {
|
|
return halt(err)
|
|
}
|
|
s.tempDir = tempDir
|
|
vmxPath = filepath.Join(tempDir, s.VMName+".vmx")
|
|
if err = remoteDriver.Download(remoteVmxPath, vmxPath); err != nil {
|
|
return halt(err)
|
|
}
|
|
}
|
|
|
|
vmxData, err := vmwcommon.ReadVMX(vmxPath)
|
|
if err != nil {
|
|
return halt(err)
|
|
}
|
|
|
|
var diskFilenames []string
|
|
// 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)
|
|
}
|
|
}
|
|
|
|
// Write out the relative, host filesystem paths to the disks
|
|
var diskFullPaths []string
|
|
for _, diskFilename := range diskFilenames {
|
|
log.Printf("Found attached disk with filename: %s", diskFilename)
|
|
diskFullPaths = append(diskFullPaths, filepath.Join(s.OutputDir, diskFilename))
|
|
}
|
|
|
|
if len(diskFullPaths) == 0 {
|
|
return halt(fmt.Errorf("Could not enumerate disk info from the vmx file"))
|
|
}
|
|
|
|
// Determine the network type by reading out of the .vmx
|
|
var networkType string
|
|
if _, ok := vmxData["ethernet0.connectiontype"]; ok {
|
|
networkType = vmxData["ethernet0.connectiontype"]
|
|
log.Printf("Discovered the network type: %s", networkType)
|
|
}
|
|
if networkType == "" {
|
|
networkType = "nat"
|
|
log.Printf("Defaulting to network type: %s", networkType)
|
|
}
|
|
|
|
// Stash all required information in our state bag
|
|
state.Put("vmx_path", vmxPath)
|
|
state.Put("disk_full_paths", diskFullPaths)
|
|
state.Put("vmnetwork", networkType)
|
|
|
|
return multistep.ActionContinue
|
|
}
|
|
|
|
func (s *StepCloneVMX) Cleanup(state multistep.StateBag) {
|
|
if s.tempDir != "" {
|
|
os.RemoveAll(s.tempDir)
|
|
}
|
|
}
|