Added support for Parallels Desktop for Mac [GH-233] in the vagrant post-processor.
Fixes https://github.com/rickard-von-essen/packer-parallels/issues/3
This commit is contained in:
parent
75e26ee955
commit
d08ee4adfd
|
@ -2,18 +2,15 @@ package vagrant
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/going/toolkit/xmlpath"
|
|
||||||
"github.com/mitchellh/packer/packer"
|
"github.com/mitchellh/packer/packer"
|
||||||
)
|
)
|
||||||
|
|
||||||
// These are the extensions of files that are unnecessary for the function
|
// These are the extensions of files and directories that are unnecessary for the function
|
||||||
// of a Parallels virtual machine.
|
// of a Parallels virtual machine.
|
||||||
var UnnecessaryFileExtensions = []string{".log", ".backup", ".Backup"}
|
var UnnecessaryFilesPatterns = []string{"\\.log$", "\\.backup$", "\\.Backup$", "\\.app/"}
|
||||||
|
|
||||||
type ParallelsProvider struct{}
|
type ParallelsProvider struct{}
|
||||||
|
|
||||||
|
@ -24,18 +21,14 @@ 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) {
|
func (p *ParallelsProvider) Process(ui packer.Ui, artifact packer.Artifact, dir string) (vagrantfile string, metadata map[string]interface{}, err error) {
|
||||||
// Create the metadata
|
// Create the metadata
|
||||||
metadata = map[string]interface{}{"provider": "parallels"}
|
metadata = map[string]interface{}{"provider": "parallels"}
|
||||||
var configPath string
|
|
||||||
|
|
||||||
// Copy all of the original contents into the temporary directory
|
// Copy all of the original contents into the temporary directory
|
||||||
for _, path := range artifact.Files() {
|
for _, path := range artifact.Files() {
|
||||||
// If the file isn't critical to the function of the
|
// If the file isn't critical to the function of the
|
||||||
// virtual machine, we get rid of it.
|
// 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
|
unnecessary := false
|
||||||
ext := filepath.Ext(path)
|
for _, unnecessaryPat := range UnnecessaryFilesPatterns {
|
||||||
for _, unnecessaryExt := range UnnecessaryFileExtensions {
|
if matched, _ := regexp.MatchString(unnecessaryPat, path); matched {
|
||||||
if unnecessaryExt == ext {
|
|
||||||
unnecessary = true
|
unnecessary = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -59,40 +52,15 @@ func (p *ParallelsProvider) Process(ui packer.Ui, artifact packer.Artifact, dir
|
||||||
if err = CopyContents(dstPath, path); err != nil {
|
if err = CopyContents(dstPath, path); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if strings.HasSuffix(dstPath, "/config.pvs") {
|
|
||||||
configPath = dstPath
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the Vagrantfile from the template
|
// Create the Vagrantfile from the template
|
||||||
var baseMacAddress string
|
vagrantfile = fmt.Sprintf(parallelsVagrantfile)
|
||||||
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
|
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 = `
|
var parallelsVagrantfile = `
|
||||||
Vagrant.configure("2") do |config|
|
Vagrant.configure("2") do |config|
|
||||||
config.vm.base_mac = "%s"
|
|
||||||
end
|
end
|
||||||
`
|
`
|
||||||
|
|
|
@ -21,6 +21,14 @@ func CopyContents(dst, src string) error {
|
||||||
}
|
}
|
||||||
defer srcF.Close()
|
defer srcF.Close()
|
||||||
|
|
||||||
|
dstDir, _ := filepath.Split(dst)
|
||||||
|
if dstDir != "" {
|
||||||
|
err := os.MkdirAll(dstDir, os.ModePerm)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
dstF, err := os.Create(dst)
|
dstF, err := os.Create(dst)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -97,7 +97,7 @@ In the example above, the compression level will be set to 1 except for
|
||||||
VMware, where it will be set to 0.
|
VMware, where it will be set to 0.
|
||||||
|
|
||||||
The available provider names are: `aws`, `digitalocean`, `virtualbox`,
|
The available provider names are: `aws`, `digitalocean`, `virtualbox`,
|
||||||
and `vmware`.
|
`vmware`, and `parallels`.
|
||||||
|
|
||||||
## Input Artifacts
|
## Input Artifacts
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue