2013-12-24 01:07:43 -05:00
|
|
|
package common
|
2013-12-23 17:38:54 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"regexp"
|
2015-08-10 17:52:34 -04:00
|
|
|
"strings"
|
2013-12-23 17:38:54 -05:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-12-23 17:38:54 -05:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This step configures a VMX by setting some default settings as well
|
|
|
|
// as taking in custom data to set, attaching a floppy if it exists, etc.
|
|
|
|
//
|
|
|
|
// Uses:
|
|
|
|
// vmx_path string
|
|
|
|
type StepConfigureVMX struct {
|
|
|
|
CustomData map[string]string
|
2014-09-04 00:27:54 -04:00
|
|
|
SkipFloppy bool
|
2013-12-23 17:38:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepConfigureVMX) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
vmxPath := state.Get("vmx_path").(string)
|
|
|
|
|
|
|
|
vmxContents, err := ioutil.ReadFile(vmxPath)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error reading VMX file: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
vmxData := ParseVMX(string(vmxContents))
|
|
|
|
|
|
|
|
// Set this so that no dialogs ever appear from Packer.
|
|
|
|
vmxData["msg.autoanswer"] = "true"
|
|
|
|
|
|
|
|
// Create a new UUID for this VM, since it is a new VM
|
|
|
|
vmxData["uuid.action"] = "create"
|
|
|
|
|
|
|
|
// Delete any generated addresses since we want to regenerate
|
|
|
|
// them. Conflicting MAC addresses is a bad time.
|
|
|
|
addrRegex := regexp.MustCompile(`(?i)^ethernet\d+\.generatedAddress`)
|
2016-11-01 17:08:04 -04:00
|
|
|
for k := range vmxData {
|
2013-12-23 17:38:54 -05:00
|
|
|
if addrRegex.MatchString(k) {
|
|
|
|
delete(vmxData, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set custom data
|
|
|
|
for k, v := range s.CustomData {
|
|
|
|
log.Printf("Setting VMX: '%s' = '%s'", k, v)
|
2015-08-10 17:52:34 -04:00
|
|
|
k = strings.ToLower(k)
|
2013-12-23 17:38:54 -05:00
|
|
|
vmxData[k] = v
|
|
|
|
}
|
|
|
|
|
2014-06-08 19:02:21 -04:00
|
|
|
// Set a floppy disk, but only if we should
|
2014-09-04 00:27:54 -04:00
|
|
|
if !s.SkipFloppy {
|
2014-06-08 19:02:21 -04:00
|
|
|
// Set a floppy disk if we have one
|
|
|
|
if floppyPathRaw, ok := state.GetOk("floppy_path"); ok {
|
|
|
|
log.Println("Floppy path present, setting in VMX")
|
|
|
|
vmxData["floppy0.present"] = "TRUE"
|
|
|
|
vmxData["floppy0.filetype"] = "file"
|
|
|
|
vmxData["floppy0.filename"] = floppyPathRaw.(string)
|
|
|
|
}
|
2013-12-23 17:38:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := WriteVMX(vmxPath, vmxData); err != nil {
|
|
|
|
err := fmt.Errorf("Error writing VMX file: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepConfigureVMX) Cleanup(state multistep.StateBag) {
|
|
|
|
}
|