diff --git a/builder/vmware/iso/step_configure_vmx.go b/builder/vmware/common/step_configure_vmx.go similarity index 99% rename from builder/vmware/iso/step_configure_vmx.go rename to builder/vmware/common/step_configure_vmx.go index 3b467eeaf..0d53c44e9 100644 --- a/builder/vmware/iso/step_configure_vmx.go +++ b/builder/vmware/common/step_configure_vmx.go @@ -1,4 +1,4 @@ -package iso +package common import ( "fmt" diff --git a/builder/vmware/iso/step_configure_vmx_test.go b/builder/vmware/common/step_configure_vmx_test.go similarity index 99% rename from builder/vmware/iso/step_configure_vmx_test.go rename to builder/vmware/common/step_configure_vmx_test.go index 851841311..293269e3f 100644 --- a/builder/vmware/iso/step_configure_vmx_test.go +++ b/builder/vmware/common/step_configure_vmx_test.go @@ -1,4 +1,4 @@ -package iso +package common import ( "io/ioutil" diff --git a/builder/vmware/common/step_test.go b/builder/vmware/common/step_test.go new file mode 100644 index 000000000..27c37495e --- /dev/null +++ b/builder/vmware/common/step_test.go @@ -0,0 +1,17 @@ +package common + +import ( + "bytes" + "github.com/mitchellh/multistep" + "github.com/mitchellh/packer/packer" + "testing" +) + +func testState(t *testing.T) multistep.StateBag { + state := new(multistep.BasicStateBag) + state.Put("ui", &packer.BasicUi{ + Reader: new(bytes.Buffer), + Writer: new(bytes.Buffer), + }) + return state +} diff --git a/builder/vmware/common/vmx.go b/builder/vmware/common/vmx.go new file mode 100644 index 000000000..455da4cec --- /dev/null +++ b/builder/vmware/common/vmx.go @@ -0,0 +1,70 @@ +package common + +import ( + "bytes" + "fmt" + "io" + "log" + "os" + "regexp" + "sort" + "strings" +) + +// ParseVMX parses the keys and values from a VMX file and returns +// them as a Go map. +func ParseVMX(contents string) map[string]string { + results := make(map[string]string) + + lineRe := regexp.MustCompile(`^(.+?)\s*=\s*"(.*?)"\s*$`) + + for _, line := range strings.Split(contents, "\n") { + matches := lineRe.FindStringSubmatch(line) + if matches == nil { + continue + } + + key := strings.ToLower(matches[1]) + results[key] = matches[2] + } + + return results +} + +// EncodeVMX takes a map and turns it into valid VMX contents. +func EncodeVMX(contents map[string]string) string { + var buf bytes.Buffer + + i := 0 + keys := make([]string, len(contents)) + for k, _ := range contents { + keys[i] = k + i++ + } + + sort.Strings(keys) + for _, k := range keys { + buf.WriteString(fmt.Sprintf("%s = \"%s\"\n", k, contents[k])) + } + + return buf.String() +} + +// WriteVMX takes a path to a VMX file and contents in the form of a +// map and writes it out. +func WriteVMX(path string, data map[string]string) (err error) { + log.Printf("Writing VMX to: %s", path) + f, err := os.Create(path) + if err != nil { + return + } + defer f.Close() + + var buf bytes.Buffer + buf.WriteString(EncodeVMX(data)) + if _, err = io.Copy(f, &buf); err != nil { + return + } + + return +} diff --git a/builder/vmware/common/vmx_test.go b/builder/vmware/common/vmx_test.go new file mode 100644 index 000000000..da07b2a10 --- /dev/null +++ b/builder/vmware/common/vmx_test.go @@ -0,0 +1,39 @@ +package common + +import "testing" + +func TestParseVMX(t *testing.T) { + contents := ` +.encoding = "UTF-8" +config.version = "8" +` + + results := ParseVMX(contents) + if len(results) != 2 { + t.Fatalf("not correct number of results: %d", len(results)) + } + + if results[".encoding"] != "UTF-8" { + t.Errorf("invalid .encoding: %s", results[".encoding"]) + } + + if results["config.version"] != "8" { + t.Errorf("invalid config.version: %s", results["config.version"]) + } +} + +func TestEncodeVMX(t *testing.T) { + contents := map[string]string{ + ".encoding": "UTF-8", + "config.version": "8", + } + + expected := `.encoding = "UTF-8" +config.version = "8" +` + + result := EncodeVMX(contents) + if result != expected { + t.Errorf("invalid results: %s", result) + } +} diff --git a/builder/vmware/iso/builder.go b/builder/vmware/iso/builder.go index a65176d2c..068a675dd 100644 --- a/builder/vmware/iso/builder.go +++ b/builder/vmware/iso/builder.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "github.com/mitchellh/multistep" + vmwcommon "github.com/mitchellh/packer/builder/vmware/common" "github.com/mitchellh/packer/common" "github.com/mitchellh/packer/packer" "io/ioutil" @@ -405,7 +406,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe }, &stepCreateDisk{}, &stepCreateVMX{}, - &StepConfigureVMX{ + &vmwcommon.StepConfigureVMX{ CustomData: b.config.VMXData, }, &stepSuppressMessages{},