github.com/rothwerx/packer@v0.9.0/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  	SkipFloppy bool
    22  }
    23  
    24  func (s *StepConfigureVMX) Run(state multistep.StateBag) multistep.StepAction {
    25  	ui := state.Get("ui").(packer.Ui)
    26  	vmxPath := state.Get("vmx_path").(string)
    27  
    28  	vmxContents, err := ioutil.ReadFile(vmxPath)
    29  	if err != nil {
    30  		err := fmt.Errorf("Error reading VMX file: %s", err)
    31  		state.Put("error", err)
    32  		ui.Error(err.Error())
    33  		return multistep.ActionHalt
    34  	}
    35  
    36  	vmxData := ParseVMX(string(vmxContents))
    37  
    38  	// Set this so that no dialogs ever appear from Packer.
    39  	vmxData["msg.autoanswer"] = "true"
    40  
    41  	// Create a new UUID for this VM, since it is a new VM
    42  	vmxData["uuid.action"] = "create"
    43  
    44  	// Delete any generated addresses since we want to regenerate
    45  	// them. Conflicting MAC addresses is a bad time.
    46  	addrRegex := regexp.MustCompile(`(?i)^ethernet\d+\.generatedAddress`)
    47  	for k, _ := range vmxData {
    48  		if addrRegex.MatchString(k) {
    49  			delete(vmxData, k)
    50  		}
    51  	}
    52  
    53  	// Set custom data
    54  	for k, v := range s.CustomData {
    55  		log.Printf("Setting VMX: '%s' = '%s'", k, v)
    56  		k = strings.ToLower(k)
    57  		vmxData[k] = v
    58  	}
    59  
    60  	// Set a floppy disk, but only if we should
    61  	if !s.SkipFloppy {
    62  		// Set a floppy disk if we have one
    63  		if floppyPathRaw, ok := state.GetOk("floppy_path"); ok {
    64  			log.Println("Floppy path present, setting in VMX")
    65  			vmxData["floppy0.present"] = "TRUE"
    66  			vmxData["floppy0.filetype"] = "file"
    67  			vmxData["floppy0.filename"] = floppyPathRaw.(string)
    68  		}
    69  	}
    70  
    71  	if err := WriteVMX(vmxPath, vmxData); err != nil {
    72  		err := fmt.Errorf("Error writing VMX file: %s", err)
    73  		state.Put("error", err)
    74  		ui.Error(err.Error())
    75  		return multistep.ActionHalt
    76  	}
    77  
    78  	return multistep.ActionContinue
    79  }
    80  
    81  func (s *StepConfigureVMX) Cleanup(state multistep.StateBag) {
    82  }