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"
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2018-12-03 18:47:23 -05:00
|
|
|
// Grab our list of devices added during the build out of the statebag
|
|
|
|
for _, device := range state.Get("temporaryDevices").([]string) {
|
|
|
|
// Instead of doing this in one pass which would be more efficient,
|
|
|
|
// we do it per device-type so that the logic appears to be the same
|
|
|
|
// as the prior implementation.
|
2013-12-24 20:40:52 -05:00
|
|
|
|
2018-12-03 18:47:23 -05:00
|
|
|
// Walk through all the devices that were temporarily added and figure
|
|
|
|
// out which type it is in order to figure out how to disable it.
|
|
|
|
// Right now only floppy, cdrom devices, ethernet, and devices that use
|
|
|
|
// ".present" are supported.
|
|
|
|
if strings.HasPrefix(device, "floppy") {
|
|
|
|
// We can identify a floppy device because it begins with "floppy"
|
|
|
|
ui.Message(fmt.Sprintf("Unmounting %s from VMX...", device))
|
|
|
|
|
|
|
|
// Delete the floppy%d entries so the floppy is no longer mounted
|
|
|
|
for k := range vmxData {
|
|
|
|
if strings.HasPrefix(k, fmt.Sprintf("%s.", device)) {
|
|
|
|
log.Printf("Deleting key for floppy device: %s", k)
|
|
|
|
delete(vmxData, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vmxData[fmt.Sprintf("%s.present", device)] = "FALSE"
|
|
|
|
|
|
|
|
} else if strings.HasPrefix(vmxData[fmt.Sprintf("%s.devicetype", device)], "cdrom-") {
|
|
|
|
// We can identify something is a cdrom if it has a ".devicetype"
|
|
|
|
// attribute that begins with "cdrom-"
|
|
|
|
ui.Message(fmt.Sprintf("Detaching ISO from CD-ROM device %s...", device))
|
2013-12-24 20:40:52 -05:00
|
|
|
|
2018-12-03 18:47:23 -05:00
|
|
|
// Simply turn the CDROM device into a native cdrom instead of an iso
|
|
|
|
vmxData[fmt.Sprintf("%s.devicetype", device)] = "cdrom-raw"
|
|
|
|
vmxData[fmt.Sprintf("%s.filename", device)] = "auto detect"
|
|
|
|
vmxData[fmt.Sprintf("%s.clientdevice", device)] = "TRUE"
|
2013-12-24 20:40:52 -05:00
|
|
|
|
2018-12-03 18:47:23 -05:00
|
|
|
} else if strings.HasPrefix(device, "ethernet") && s.RemoveEthernetInterfaces {
|
|
|
|
// We can identify an ethernet device because it begins with "ethernet"
|
|
|
|
// Although we're supporting this, as of now it's not in use due
|
|
|
|
// to these interfaces not ever being added to the "temporaryDevices" statebag.
|
|
|
|
ui.Message(fmt.Sprintf("Removing %s interface...", device))
|
|
|
|
|
|
|
|
// Delete the ethernet%d entries so the ethernet interface is removed.
|
|
|
|
// This corresponds to the same logic defined below.
|
|
|
|
for k := range vmxData {
|
|
|
|
if strings.HasPrefix(k, fmt.Sprintf("%s.", device)) {
|
|
|
|
log.Printf("Deleting key for ethernet device: %s", k)
|
|
|
|
delete(vmxData, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// First check to see if we can simply disable the device
|
|
|
|
if _, ok := vmxData[fmt.Sprintf("%s.present", device)]; ok {
|
|
|
|
ui.Message(fmt.Sprintf("Disabling device %s of an unknown device type...", device))
|
|
|
|
vmxData[fmt.Sprintf("%s.present", device)] = "FALSE"
|
|
|
|
} else {
|
|
|
|
// Okay, so this wasn't so simple. Let's just log info about the
|
|
|
|
// device and not tamper with any of its keys
|
|
|
|
log.Printf("Refusing to remove device due to being of an unsupported type: %s\n", device)
|
|
|
|
for k := range vmxData {
|
|
|
|
if strings.HasPrefix(k, fmt.Sprintf("%s.", device)) {
|
|
|
|
log.Printf("Leaving unsupported device key: %s\n", k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-24 20:40:52 -05:00
|
|
|
}
|
|
|
|
|
2018-12-03 18:47:23 -05:00
|
|
|
// Disable the VNC server if necessary
|
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
|
|
|
|
2018-12-03 18:47:23 -05:00
|
|
|
// Disable any ethernet devices if necessary
|
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") {
|
2018-12-03 18:47:23 -05:00
|
|
|
log.Printf("Deleting key for ethernet device: %s", k)
|
2017-05-24 19:44:35 -04:00
|
|
|
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) {}
|