Skip to content

Commit

Permalink
Added base MAC address into vagrant boxes for Parallels provider.
Browse files Browse the repository at this point in the history
  • Loading branch information
rickard-von-essen committed Mar 20, 2014
1 parent 39127c9 commit 8b8cd12
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions post-processor/vagrant/parallels.go
Expand Up @@ -2,9 +2,12 @@ package vagrant

import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/going/toolkit/xmlpath"
"github.com/mitchellh/packer/packer"
)

Expand All @@ -21,6 +24,7 @@ func (p *ParallelsProvider) KeepInputArtifact() bool {
func (p *ParallelsProvider) Process(ui packer.Ui, artifact packer.Artifact, dir string) (vagrantfile string, metadata map[string]interface{}, err error) {
// Create the metadata
metadata = map[string]interface{}{"provider": "parallels"}
var configPath string

// Copy all of the original contents into the temporary directory
for _, path := range artifact.Files() {
Expand All @@ -40,11 +44,10 @@ func (p *ParallelsProvider) Process(ui packer.Ui, artifact packer.Artifact, dir
continue
}

var pvmPath string

tmpPath := filepath.ToSlash(path)
pathRe := regexp.MustCompile(`^(.+?)([^/]+\.pvm/.+?)$`)
matches := pathRe.FindStringSubmatch(tmpPath)
var pvmPath string
if matches != nil {
pvmPath = filepath.FromSlash(matches[2])
} else {
Expand All @@ -56,7 +59,40 @@ func (p *ParallelsProvider) Process(ui packer.Ui, artifact packer.Artifact, dir
if err = CopyContents(dstPath, path); err != nil {
return
}
if strings.HasSuffix(dstPath, "/config.pvs") {
configPath = dstPath
}
}

// Create the Vagrantfile from the template
var baseMacAddress string
baseMacAddress, err = findBaseMacAddress(configPath)
if err != nil {
ui.Message(fmt.Sprintf("Problem determining Vagarant Box MAC address: %s", err))
}

vagrantfile = fmt.Sprintf(parallelsVagrantfile, baseMacAddress)

return
}

func findBaseMacAddress(path string) (string, error) {
xpath := "/ParallelsVirtualMachine/Hardware/NetworkAdapter[@id='0']/MAC"
file, err := os.Open(path)
if err != nil {
return "", err
}
xpathComp := xmlpath.MustCompile(xpath)
root, err := xmlpath.Parse(file)
if err != nil {
return "", err
}
value, _ := xpathComp.String(root)
return value, nil
}

var parallelsVagrantfile = `
Vagrant.configure("2") do |config|
config.vm.base_mac = "%s"
end
`

0 comments on commit 8b8cd12

Please sign in to comment.