builder/vmware: add more stuff to common

This commit is contained in:
Mitchell Hashimoto 2013-12-23 23:07:43 -07:00
parent d73844c3ef
commit 33452c2dfd
6 changed files with 130 additions and 3 deletions

View File

@ -1,4 +1,4 @@
package iso
package common
import (
"fmt"

View File

@ -1,4 +1,4 @@
package iso
package common
import (
"io/ioutil"

View File

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

View File

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

View File

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

View File

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