github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/vmware/common/step_clean_vmx.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"regexp"
     7  	"strings"
     8  
     9  	"github.com/hashicorp/packer/packer"
    10  	"github.com/mitchellh/multistep"
    11  )
    12  
    13  // This step cleans up the VMX by removing or changing this prior to
    14  // being ready for use.
    15  //
    16  // Uses:
    17  //   ui     packer.Ui
    18  //   vmx_path string
    19  //
    20  // Produces:
    21  //   <nothing>
    22  type StepCleanVMX struct {
    23  	RemoveEthernetInterfaces bool
    24  }
    25  
    26  func (s StepCleanVMX) Run(state multistep.StateBag) multistep.StepAction {
    27  	ui := state.Get("ui").(packer.Ui)
    28  	vmxPath := state.Get("vmx_path").(string)
    29  
    30  	ui.Say("Cleaning VMX prior to finishing up...")
    31  
    32  	vmxData, err := ReadVMX(vmxPath)
    33  	if err != nil {
    34  		state.Put("error", fmt.Errorf("Error reading VMX: %s", err))
    35  		return multistep.ActionHalt
    36  	}
    37  
    38  	// Delete the floppy0 entries so the floppy is no longer mounted
    39  	ui.Message("Unmounting floppy from VMX...")
    40  	for k := range vmxData {
    41  		if strings.HasPrefix(k, "floppy0.") {
    42  			log.Printf("Deleting key: %s", k)
    43  			delete(vmxData, k)
    44  		}
    45  	}
    46  	vmxData["floppy0.present"] = "FALSE"
    47  
    48  	devRe := regexp.MustCompile(`^ide\d:\d\.`)
    49  	for k, v := range vmxData {
    50  		ide := devRe.FindString(k)
    51  		if ide == "" || v != "cdrom-image" {
    52  			continue
    53  		}
    54  
    55  		ui.Message("Detaching ISO from CD-ROM device...")
    56  
    57  		vmxData[ide+"devicetype"] = "cdrom-raw"
    58  		vmxData[ide+"filename"] = "auto detect"
    59  		vmxData[ide+"clientdevice"] = "TRUE"
    60  	}
    61  
    62  	ui.Message("Disabling VNC server...")
    63  	vmxData["remotedisplay.vnc.enabled"] = "FALSE"
    64  
    65  	if s.RemoveEthernetInterfaces {
    66  		ui.Message("Removing Ethernet Interfaces...")
    67  		for k := range vmxData {
    68  			if strings.HasPrefix(k, "ethernet") {
    69  				log.Printf("Deleting key: %s", k)
    70  				delete(vmxData, k)
    71  			}
    72  		}
    73  	}
    74  
    75  	// Rewrite the VMX
    76  	if err := WriteVMX(vmxPath, vmxData); err != nil {
    77  		state.Put("error", fmt.Errorf("Error writing VMX: %s", err))
    78  		return multistep.ActionHalt
    79  	}
    80  
    81  	return multistep.ActionContinue
    82  }
    83  
    84  func (StepCleanVMX) Cleanup(multistep.StateBag) {}