github.com/daniellockard/packer@v0.7.6-0.20141210173435-5a9390934716/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  	// Delete the floppy0 entries so the floppy is no longer mounted
    36  	ui.Message("Unmounting floppy from VMX...")
    37  	for k, _ := range vmxData {
    38  		if strings.HasPrefix(k, "floppy0.") {
    39  			log.Printf("Deleting key: %s", k)
    40  			delete(vmxData, k)
    41  		}
    42  	}
    43  	vmxData["floppy0.present"] = "FALSE"
    44  
    45  	devRe := regexp.MustCompile(`^ide\d:\d\.`)
    46  	for k, v := range vmxData {
    47  		ide := devRe.FindString(k)
    48  		if ide == "" || v != "cdrom-image" {
    49  			continue
    50  		}
    51  
    52  		ui.Message("Detaching ISO from CD-ROM device...")
    53  
    54  		vmxData[ide+"devicetype"] = "cdrom-raw"
    55  		vmxData[ide+"filename"] = "auto detect"
    56  	}
    57  
    58  	// Rewrite the VMX
    59  	if err := WriteVMX(vmxPath, vmxData); err != nil {
    60  		state.Put("error", fmt.Errorf("Error writing VMX: %s", err))
    61  		return multistep.ActionHalt
    62  	}
    63  
    64  	return multistep.ActionContinue
    65  }
    66  
    67  func (StepCleanVMX) Cleanup(multistep.StateBag) {}