github.com/tonnydourado/packer@v0.6.1-0.20140701134019-5d0cd9676a37/post-processor/vagrant/parallels.go (about)

     1  package vagrant
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  	"regexp"
     7  
     8  	"github.com/mitchellh/packer/packer"
     9  )
    10  
    11  // These are the extensions of files and directories that are unnecessary for the function
    12  // of a Parallels virtual machine.
    13  var UnnecessaryFilesPatterns = []string{"\\.log$", "\\.backup$", "\\.Backup$", "\\.app/"}
    14  
    15  type ParallelsProvider struct{}
    16  
    17  func (p *ParallelsProvider) KeepInputArtifact() bool {
    18  	return false
    19  }
    20  
    21  func (p *ParallelsProvider) Process(ui packer.Ui, artifact packer.Artifact, dir string) (vagrantfile string, metadata map[string]interface{}, err error) {
    22  	// Create the metadata
    23  	metadata = map[string]interface{}{"provider": "parallels"}
    24  
    25  	// Copy all of the original contents into the temporary directory
    26  	for _, path := range artifact.Files() {
    27  		// If the file isn't critical to the function of the
    28  		// virtual machine, we get rid of it.
    29  		unnecessary := false
    30  		for _, unnecessaryPat := range UnnecessaryFilesPatterns {
    31  			if matched, _ := regexp.MatchString(unnecessaryPat, path); matched {
    32  				unnecessary = true
    33  				break
    34  			}
    35  		}
    36  		if unnecessary {
    37  			continue
    38  		}
    39  
    40  		tmpPath := filepath.ToSlash(path)
    41  		pathRe := regexp.MustCompile(`^(.+?)([^/]+\.pvm/.+?)$`)
    42  		matches := pathRe.FindStringSubmatch(tmpPath)
    43  		var pvmPath string
    44  		if matches != nil {
    45  			pvmPath = filepath.FromSlash(matches[2])
    46  		} else {
    47  			continue // Just copy a pvm
    48  		}
    49  		dstPath := filepath.Join(dir, pvmPath)
    50  
    51  		ui.Message(fmt.Sprintf("Copying: %s", path))
    52  		if err = CopyContents(dstPath, path); err != nil {
    53  			return
    54  		}
    55  	}
    56  
    57  	// Create the Vagrantfile from the template
    58  	vagrantfile = fmt.Sprintf(parallelsVagrantfile)
    59  
    60  	return
    61  }
    62  
    63  var parallelsVagrantfile = `
    64  Vagrant.configure("2") do |config|
    65  end
    66  `