2013-12-24 20:40:52 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-12-24 20:40:52 -05:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
2015-08-10 17:52:34 -04:00
|
|
|
|
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"
|
2013-12-24 20:40:52 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// This step cleans up the VMX by removing or changing this prior to
|
|
|
|
// being ready for use.
|
|
|
|
//
|
|
|
|
// Uses:
|
|
|
|
// ui packer.Ui
|
|
|
|
// vmx_path string
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// <nothing>
|
2017-05-24 19:44:35 -04:00
|
|
|
type StepCleanVMX struct {
|
|
|
|
RemoveEthernetInterfaces bool
|
2017-10-12 19:38:18 -04:00
|
|
|
VNCEnabled bool
|
2017-05-24 19:44:35 -04:00
|
|
|
}
|
2013-12-24 20:40:52 -05:00
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s StepCleanVMX) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2013-12-24 20:40:52 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
vmxPath := state.Get("vmx_path").(string)
|
|
|
|
|
|
|
|
ui.Say("Cleaning VMX prior to finishing up...")
|
|
|
|
|
|
|
|
vmxData, err := ReadVMX(vmxPath)
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", fmt.Errorf("Error reading VMX: %s", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2014-09-16 21:27:00 -04:00
|
|
|
// Delete the floppy0 entries so the floppy is no longer mounted
|
|
|
|
ui.Message("Unmounting floppy from VMX...")
|
2016-11-01 17:08:04 -04:00
|
|
|
for k := range vmxData {
|
2014-09-16 21:27:00 -04:00
|
|
|
if strings.HasPrefix(k, "floppy0.") {
|
|
|
|
log.Printf("Deleting key: %s", k)
|
|
|
|
delete(vmxData, k)
|
2013-12-24 20:40:52 -05:00
|
|
|
}
|
|
|
|
}
|
2014-09-16 21:27:00 -04:00
|
|
|
vmxData["floppy0.present"] = "FALSE"
|
2013-12-24 20:40:52 -05:00
|
|
|
|
2014-09-17 14:06:56 -04:00
|
|
|
devRe := regexp.MustCompile(`^ide\d:\d\.`)
|
|
|
|
for k, v := range vmxData {
|
|
|
|
ide := devRe.FindString(k)
|
|
|
|
if ide == "" || v != "cdrom-image" {
|
|
|
|
continue
|
|
|
|
}
|
2013-12-24 20:40:52 -05:00
|
|
|
|
|
|
|
ui.Message("Detaching ISO from CD-ROM device...")
|
|
|
|
|
2015-08-10 17:52:34 -04:00
|
|
|
vmxData[ide+"devicetype"] = "cdrom-raw"
|
|
|
|
vmxData[ide+"filename"] = "auto detect"
|
2015-02-13 05:13:58 -05:00
|
|
|
vmxData[ide+"clientdevice"] = "TRUE"
|
2013-12-24 20:40:52 -05:00
|
|
|
}
|
|
|
|
|
2017-10-12 19:38:18 -04:00
|
|
|
if s.VNCEnabled {
|
2017-09-20 23:07:21 -04:00
|
|
|
ui.Message("Disabling VNC server...")
|
|
|
|
vmxData["remotedisplay.vnc.enabled"] = "FALSE"
|
|
|
|
}
|
2015-06-22 17:50:25 -04:00
|
|
|
|
2017-05-24 19:44:35 -04:00
|
|
|
if s.RemoveEthernetInterfaces {
|
|
|
|
ui.Message("Removing Ethernet Interfaces...")
|
|
|
|
for k := range vmxData {
|
|
|
|
if strings.HasPrefix(k, "ethernet") {
|
|
|
|
log.Printf("Deleting key: %s", k)
|
|
|
|
delete(vmxData, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-24 20:40:52 -05:00
|
|
|
// Rewrite the VMX
|
|
|
|
if err := WriteVMX(vmxPath, vmxData); err != nil {
|
|
|
|
state.Put("error", fmt.Errorf("Error writing VMX: %s", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (StepCleanVMX) Cleanup(multistep.StateBag) {}
|