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

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"log"
     7  	"regexp"
     8  	"strings"
     9  
    10  	"github.com/mitchellh/multistep"
    11  	"github.com/mitchellh/packer/packer"
    12  )
    13  
    14  // This step configures a VMX by setting some default settings as well
    15  // as taking in custom data to set, attaching a floppy if it exists, etc.
    16  //
    17  // Uses:
    18  //   vmx_path string
    19  type StepConfigureVMX struct {
    20  	CustomData map[string]string
    21  }
    22  
    23  func (s *StepConfigureVMX) Run(state multistep.StateBag) multistep.StepAction {
    24  	ui := state.Get("ui").(packer.Ui)
    25  	vmxPath := state.Get("vmx_path").(string)
    26  
    27  	vmxContents, err := ioutil.ReadFile(vmxPath)
    28  	if err != nil {
    29  		err := fmt.Errorf("Error reading VMX file: %s", err)
    30  		state.Put("error", err)
    31  		ui.Error(err.Error())
    32  		return multistep.ActionHalt
    33  	}
    34  
    35  	vmxData := ParseVMX(string(vmxContents))
    36  
    37  	// Set this so that no dialogs ever appear from Packer.
    38  	vmxData["msg.autoanswer"] = "true"
    39  
    40  	// Create a new UUID for this VM, since it is a new VM
    41  	vmxData["uuid.action"] = "create"
    42  
    43  	// Delete any generated addresses since we want to regenerate
    44  	// them. Conflicting MAC addresses is a bad time.
    45  	addrRegex := regexp.MustCompile(`(?i)^ethernet\d+\.generatedAddress`)
    46  	for k, _ := range vmxData {
    47  		if addrRegex.MatchString(k) {
    48  			delete(vmxData, k)
    49  		}
    50  	}
    51  
    52  	// Set custom data
    53  	for k, v := range s.CustomData {
    54  		log.Printf("Setting VMX: '%s' = '%s'", k, v)
    55  		k = strings.ToLower(k)
    56  		vmxData[k] = v
    57  	}
    58  
    59  	// Set a floppy disk if we have one
    60  	if floppyPathRaw, ok := state.GetOk("floppy_path"); ok {
    61  		log.Println("Floppy path present, setting in VMX")
    62  		vmxData["floppy0.present"] = "TRUE"
    63  		vmxData["floppy0.filetype"] = "file"
    64  		vmxData["floppy0.filename"] = floppyPathRaw.(string)
    65  	}
    66  
    67  	if err := WriteVMX(vmxPath, vmxData); err != nil {
    68  		err := fmt.Errorf("Error writing VMX file: %s", err)
    69  		state.Put("error", err)
    70  		ui.Error(err.Error())
    71  		return multistep.ActionHalt
    72  	}
    73  
    74  	return multistep.ActionContinue
    75  }
    76  
    77  func (s *StepConfigureVMX) Cleanup(state multistep.StateBag) {
    78  }