github.com/sneal/packer@v0.5.2/builder/vmware/common/step_clean_vmx.go (about)

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