diff --git a/post-processor/vagrant/aws.go b/post-processor/vagrant/aws.go index aaacc23d9..a7dc8baa0 100644 --- a/post-processor/vagrant/aws.go +++ b/post-processor/vagrant/aws.go @@ -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 = ` diff --git a/post-processor/vagrant/util.go b/post-processor/vagrant/util.go index 8f18a6221..5cc52bfb3 100644 --- a/post-processor/vagrant/util.go +++ b/post-processor/vagrant/util.go @@ -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")) diff --git a/post-processor/vagrant/virtualbox.go b/post-processor/vagrant/virtualbox.go index c76726629..ad5f30081 100644 --- a/post-processor/vagrant/virtualbox.go +++ b/post-processor/vagrant/virtualbox.go @@ -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 = `