post-processor/vagrant: compile the output path

This commit is contained in:
Mitchell Hashimoto 2013-06-27 10:51:13 -07:00
parent faa7505151
commit 6f3d0f6bcd
3 changed files with 46 additions and 5 deletions

View File

@ -12,7 +12,7 @@ import (
)
type AWSBoxConfig struct {
OutputPath string `mapstructure:"output"`
OutputPath string `mapstructure:"output"`
VagrantfileTemplate string `mapstructure:"vagrantfile_template"`
}
@ -48,6 +48,12 @@ func (p *AWSBoxPostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact
tplData.Images[parts[0]] = parts[1]
}
// Compile the output path
outputPath, err := ProcessOutputPath(p.config.OutputPath, "aws", artifact)
if err != nil {
return nil, err
}
// Create a temporary directory for us to build the contents of the box in
dir, err := ioutil.TempDir("", "packer")
if err != nil {
@ -89,11 +95,11 @@ func (p *AWSBoxPostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact
}
// Compress the directory to the given output path
if err := DirToBox(p.config.OutputPath, dir); err != nil {
if err := DirToBox(outputPath, dir); err != nil {
return nil, err
}
return NewArtifact("aws", p.config.OutputPath), nil
return NewArtifact("aws", outputPath), nil
}
var defaultAWSVagrantfile = `

View File

@ -2,13 +2,23 @@ package vagrant
import (
"archive/tar"
"bytes"
"compress/gzip"
"encoding/json"
"github.com/mitchellh/packer/packer"
"io"
"os"
"path/filepath"
"text/template"
)
// OutputPathTemplate is the structure that is availalable within the
// OutputPath variables.
type OutputPathTemplate struct {
ArtifactId string
Provider string
}
// DirToBox takes the directory and compresses it into a Vagrant-compatible
// box. This function does not perform checks to verify that dir is
// actually a proper box. This is an expected precondition.
@ -62,6 +72,25 @@ func DirToBox(dst, dir string) error {
return filepath.Walk(dir, tarWalk)
}
// ProcessOutputPath takes an output path template and executes it,
// replacing variables with their respective values.
func ProcessOutputPath(path string, provider string, artifact packer.Artifact) (string, error) {
var buf bytes.Buffer
tplData := &OutputPathTemplate{
ArtifactId: artifact.Id(),
Provider: provider,
}
t, err := template.New("output").Parse(path)
if err != nil {
return "", err
}
err = t.Execute(&buf, tplData)
return buf.String(), err
}
// WriteMetadata writes the "metadata.json" file for a Vagrant box.
func WriteMetadata(dir string, contents interface{}) error {
f, err := os.Create(filepath.Join(dir, "metadata.json"))

View File

@ -37,6 +37,12 @@ func (p *VBoxBoxPostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifac
// TODO(mitchellh): Actually parse the base mac address
tplData := &VBoxVagrantfileTemplate{}
// Compile the output path
outputPath, err := ProcessOutputPath(p.config.OutputPath, "aws", artifact)
if err != nil {
return nil, err
}
// Create a temporary directory for us to build the contents of the box in
dir, err := ioutil.TempDir("", "packer")
if err != nil {
@ -99,11 +105,11 @@ func (p *VBoxBoxPostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifac
// Compress the directory to the given output path
ui.Message(fmt.Sprintf("Compressing box..."))
if err := DirToBox(p.config.OutputPath, dir); err != nil {
if err := DirToBox(outputPath, dir); err != nil {
return nil, err
}
return NewArtifact("virtualbox", p.config.OutputPath), nil
return NewArtifact("virtualbox", outputPath), nil
}
var defaultVBoxVagrantfile = `