builder/vmware/iso: move VMX methods out to the common

This commit is contained in:
Mitchell Hashimoto 2013-12-23 23:09:47 -07:00
parent 33452c2dfd
commit 6d83ef5499
6 changed files with 10 additions and 115 deletions

View File

@ -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 := ""

View File

@ -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
}

View File

@ -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())

View File

@ -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())

View File

@ -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
}

View File

@ -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)
}
}