GH 1508 - Ensure Packer VMX is updated and saved

We need to ensure the VMWare process has exited before attempting to run VMX file cleanup steps, otherwise VMWare may overwrite our changes. While Packer does its best to ensure VMWare has exited, there's still a race condition on some OSs between VMWare flushing the VMX and Packer updating it. The workaround is to artifically wait 5 seconds.

When using the VMX builder its possible for the source machine to have a floppy and/or CD-ROM mounted which gets cloned to the new VM Packer spins up, but have no Packer configuration for those devices. With this change we always attempt to remove the mounted devices regardless of the Packer configuration.
This commit is contained in:
Shawn Neal 2014-09-17 11:06:56 -07:00
parent 7d9c252b3a
commit 5fd9651982
3 changed files with 16 additions and 21 deletions

View File

@ -42,26 +42,17 @@ func (s StepCleanVMX) Run(state multistep.StateBag) multistep.StepAction {
}
vmxData["floppy0.present"] = "FALSE"
if isoPathRaw, ok := state.GetOk("iso_path"); ok {
isoPath := isoPathRaw.(string)
devRe := regexp.MustCompile(`^ide\d:\d\.`)
for k, v := range vmxData {
ide := devRe.FindString(k)
if ide == "" || v != "cdrom-image" {
continue
}
ui.Message("Detaching ISO from CD-ROM device...")
devRe := regexp.MustCompile(`^ide\d:\d\.`)
for k, _ := range vmxData {
match := devRe.FindString(k)
if match == "" {
continue
}
filenameKey := match + "filename"
if filename, ok := vmxData[filenameKey]; ok {
if filename == isoPath {
// Change the CD-ROM device back to auto-detect to eject
vmxData[filenameKey] = "auto detect"
vmxData[match+"devicetype"] = "cdrom-raw"
}
}
}
vmxData[ide+"devicetype"] = "cdrom-raw"
vmxData[ide+"filename"] = "auto detect"
}
// Rewrite the VMX

View File

@ -88,7 +88,6 @@ func TestStepCleanVMX_isoPath(t *testing.T) {
t.Fatalf("err: %s", err)
}
state.Put("iso_path", "foo")
state.Put("vmx_path", vmxPath)
// Test the run
@ -135,6 +134,7 @@ floppy0.filetype = "file"
`
const testVMXISOPath = `
ide0:0.devicetype = "cdrom-image"
ide0:0.filename = "foo"
ide0:1.filename = "bar"
foo = "bar"

View File

@ -137,10 +137,14 @@ LockWaitLoop:
}
}
if runtime.GOOS == "windows" && !s.Testing {
if runtime.GOOS != "darwin" && !s.Testing {
// Windows takes a while to yield control of the files when the
// process is exiting. We just sleep here. In the future, it'd be
// nice to find a better solution to this.
// process is exiting. Ubuntu will yield control of the files but
// VMWare may overwrite the VMX cleanup steps that run after this,
// so we wait to ensure VMWare has exited and flushed the VMX.
// We just sleep here. In the future, it'd be nice to find a better
// solution to this.
time.Sleep(5 * time.Second)
}