diff --git a/post-processor/vagrant/virtualbox.go b/post-processor/vagrant/virtualbox.go index 08b454243..376fce917 100644 --- a/post-processor/vagrant/virtualbox.go +++ b/post-processor/vagrant/virtualbox.go @@ -1,13 +1,17 @@ package vagrant import ( + "errors" "fmt" "github.com/mitchellh/mapstructure" "github.com/mitchellh/packer/packer" "io" "io/ioutil" + "log" "os" "path/filepath" + "regexp" + "strings" "text/template" ) @@ -34,8 +38,12 @@ func (p *VBoxBoxPostProcessor) Configure(raw interface{}) error { } func (p *VBoxBoxPostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, error) { - // TODO(mitchellh): Actually parse the base mac address + var err error tplData := &VBoxVagrantfileTemplate{} + tplData.BaseMacAddress, err = p.findBaseMacAddress(artifact) + if err != nil { + return nil, err + } // Compile the output path outputPath, err := ProcessOutputPath(p.config.OutputPath, "virtualbox", artifact) @@ -112,10 +120,44 @@ func (p *VBoxBoxPostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifac return NewArtifact("virtualbox", outputPath), nil } +func (p *VBoxBoxPostProcessor) findBaseMacAddress(a packer.Artifact) (string, error) { + log.Println("Looking for OVF for base mac address...") + var ovf string + for _, f := range a.Files() { + if strings.HasSuffix(f, ".ovf") { + log.Printf("OVF found: %s", f) + ovf = f + break + } + } + + if ovf == "" { + return "", errors.New("ovf file couldn't be found") + } + + f, err := os.Open(ovf) + if err != nil { + return "", err + } + defer f.Close() + + data, err := ioutil.ReadAll(f) + if err != nil { + return "", err + } + + re := regexp.MustCompile(`