diff --git a/builder/vmware/iso/ssh.go b/builder/vmware/iso/ssh.go index 3c27c387b..32c7cb962 100644 --- a/builder/vmware/iso/ssh.go +++ b/builder/vmware/iso/ssh.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "github.com/mitchellh/multistep" + vmwcommon "github.com/mitchellh/packer/builder/vmware/common" "github.com/mitchellh/packer/communicator/ssh" "io/ioutil" "log" @@ -28,7 +29,7 @@ func sshAddress(state multistep.StateBag) (string, error) { return "", err } - vmxData := ParseVMX(string(vmxBytes)) + vmxData := vmwcommon.ParseVMX(string(vmxBytes)) var ok bool macAddress := "" diff --git a/builder/vmware/iso/step_clean_vmx.go b/builder/vmware/iso/step_clean_vmx.go index 2b9d6f90c..119173a5b 100644 --- a/builder/vmware/iso/step_clean_vmx.go +++ b/builder/vmware/iso/step_clean_vmx.go @@ -3,6 +3,7 @@ package iso import ( "fmt" "github.com/mitchellh/multistep" + vmwcommon "github.com/mitchellh/packer/builder/vmware/common" "github.com/mitchellh/packer/packer" "io/ioutil" "log" @@ -66,7 +67,7 @@ func (s stepCleanVMX) Run(state multistep.StateBag) multistep.StepAction { } // Rewrite the VMX - if err := WriteVMX(vmxPath, vmxData); err != nil { + if err := vmwcommon.WriteVMX(vmxPath, vmxData); err != nil { state.Put("error", fmt.Errorf("Error writing VMX: %s", err)) return multistep.ActionHalt } @@ -88,5 +89,5 @@ func (stepCleanVMX) readVMX(vmxPath string) (map[string]string, error) { return nil, err } - return ParseVMX(string(vmxBytes)), nil + return vmwcommon.ParseVMX(string(vmxBytes)), nil } diff --git a/builder/vmware/iso/step_configure_vnc.go b/builder/vmware/iso/step_configure_vnc.go index 6e6c39a22..676439213 100644 --- a/builder/vmware/iso/step_configure_vnc.go +++ b/builder/vmware/iso/step_configure_vnc.go @@ -3,6 +3,7 @@ package iso import ( "fmt" "github.com/mitchellh/multistep" + vmwcommon "github.com/mitchellh/packer/builder/vmware/common" "github.com/mitchellh/packer/packer" "io/ioutil" "log" @@ -84,11 +85,11 @@ func (s *stepConfigureVNC) Run(state multistep.StateBag) multistep.StepAction { log.Printf("Found available VNC port: %d", vncPort) - vmxData := ParseVMX(string(vmxBytes)) + vmxData := vmwcommon.ParseVMX(string(vmxBytes)) vmxData["remotedisplay.vnc.enabled"] = "TRUE" vmxData["remotedisplay.vnc.port"] = fmt.Sprintf("%d", vncPort) - if err := WriteVMX(vmxPath, vmxData); err != nil { + if err := vmwcommon.WriteVMX(vmxPath, vmxData); err != nil { err := fmt.Errorf("Error writing VMX data: %s", err) state.Put("error", err) ui.Error(err.Error()) diff --git a/builder/vmware/iso/step_create_vmx.go b/builder/vmware/iso/step_create_vmx.go index 46bbf77e1..8544a1405 100644 --- a/builder/vmware/iso/step_create_vmx.go +++ b/builder/vmware/iso/step_create_vmx.go @@ -3,6 +3,7 @@ package iso import ( "fmt" "github.com/mitchellh/multistep" + vmwcommon "github.com/mitchellh/packer/builder/vmware/common" "github.com/mitchellh/packer/packer" "io/ioutil" "os" @@ -90,7 +91,7 @@ func (s *stepCreateVMX) Run(state multistep.StateBag) multistep.StepAction { } vmxPath := filepath.Join(vmxDir, config.VMName+".vmx") - if err := WriteVMX(vmxPath, ParseVMX(vmxContents)); err != nil { + if err := vmwcommon.WriteVMX(vmxPath, vmwcommon.ParseVMX(vmxContents)); err != nil { err := fmt.Errorf("Error creating VMX file: %s", err) state.Put("error", err) ui.Error(err.Error()) diff --git a/builder/vmware/iso/vmx.go b/builder/vmware/iso/vmx.go deleted file mode 100644 index c4fc6d724..000000000 --- a/builder/vmware/iso/vmx.go +++ /dev/null @@ -1,70 +0,0 @@ -package iso - -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/iso/vmx_test.go b/builder/vmware/iso/vmx_test.go deleted file mode 100644 index 4b189b67b..000000000 --- a/builder/vmware/iso/vmx_test.go +++ /dev/null @@ -1,39 +0,0 @@ -package iso - -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) - } -}