Commit 75e26ee9 authored by YungSang's avatar YungSang Committed by Rickard von Essen

Added support for Parallels Desktop for Mac [GH-233] in the vagrant post-processor.

The code originate from https://github.com/YungSang/packer-parallels
parent c5e9fbcb
package vagrant
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/going/toolkit/xmlpath"
"github.com/mitchellh/packer/packer"
)
// These are the extensions of files that are unnecessary for the function
// of a Parallels virtual machine.
var UnnecessaryFileExtensions = []string{".log", ".backup", ".Backup"}
type ParallelsProvider struct{}
func (p *ParallelsProvider) KeepInputArtifact() bool {
return false
}
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() {
// If the file isn't critical to the function of the
// virtual machine, we get rid of it.
// It's done by the builder, but we need one more time
// because unregistering a vm creates config.pvs.backup again.
unnecessary := false
ext := filepath.Ext(path)
for _, unnecessaryExt := range UnnecessaryFileExtensions {
if unnecessaryExt == ext {
unnecessary = true
break
}
}
if unnecessary {
continue
}
tmpPath := filepath.ToSlash(path)
pathRe := regexp.MustCompile(`^(.+?)([^/]+\.pvm/.+?)$`)
matches := pathRe.FindStringSubmatch(tmpPath)
var pvmPath string
if matches != nil {
pvmPath = filepath.FromSlash(matches[2])
} else {
continue // Just copy a pvm
}
dstPath := filepath.Join(dir, pvmPath)
ui.Message(fmt.Sprintf("Copying: %s", path))
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
`
package vagrant
import (
"testing"
)
func TestParallelsProvider_impl(t *testing.T) {
var _ Provider = new(ParallelsProvider)
}
...@@ -16,11 +16,12 @@ import ( ...@@ -16,11 +16,12 @@ import (
) )
var builtins = map[string]string{ var builtins = map[string]string{
"mitchellh.amazonebs": "aws", "mitchellh.amazonebs": "aws",
"mitchellh.amazon.instance": "aws", "mitchellh.amazon.instance": "aws",
"mitchellh.virtualbox": "virtualbox", "mitchellh.virtualbox": "virtualbox",
"mitchellh.vmware": "vmware", "mitchellh.vmware": "vmware",
"pearkes.digitalocean": "digitalocean", "pearkes.digitalocean": "digitalocean",
"rickard-von-essen.parallels": "parallels",
} }
type Config struct { type Config struct {
...@@ -63,6 +64,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { ...@@ -63,6 +64,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
} }
func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
name, ok := builtins[artifact.BuilderId()] name, ok := builtins[artifact.BuilderId()]
if !ok { if !ok {
return nil, false, fmt.Errorf( return nil, false, fmt.Errorf(
...@@ -216,6 +218,8 @@ func providerForName(name string) Provider { ...@@ -216,6 +218,8 @@ func providerForName(name string) Provider {
return new(VBoxProvider) return new(VBoxProvider)
case "vmware": case "vmware":
return new(VMwareProvider) return new(VMwareProvider)
case "parallels":
return new(ParallelsProvider)
default: default:
return nil return nil
} }
......
...@@ -30,6 +30,7 @@ providers. ...@@ -30,6 +30,7 @@ providers.
* DigitalOcean * DigitalOcean
* VirtualBox * VirtualBox
* VMware * VMware
* Parallels
<div class="alert alert-block alert-info"> <div class="alert alert-block alert-info">
<strong>Support for additional providers</strong> is planned. If the <strong>Support for additional providers</strong> is planned. If the
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment