github.com/mitchellh/packer@v1.3.2/builder/vmware/common/step_clean_vmx.go (about)

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