post-processor/vagrant: interpolation
This commit is contained in:
parent
4bb16ac223
commit
5b343ca98c
|
@ -11,8 +11,11 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
|
"github.com/mitchellh/mapstructure"
|
||||||
"github.com/mitchellh/packer/common"
|
"github.com/mitchellh/packer/common"
|
||||||
|
"github.com/mitchellh/packer/helper/config"
|
||||||
"github.com/mitchellh/packer/packer"
|
"github.com/mitchellh/packer/packer"
|
||||||
|
"github.com/mitchellh/packer/template/interpolate"
|
||||||
)
|
)
|
||||||
|
|
||||||
var builtins = map[string]string{
|
var builtins = map[string]string{
|
||||||
|
@ -35,7 +38,7 @@ type Config struct {
|
||||||
Override map[string]interface{}
|
Override map[string]interface{}
|
||||||
VagrantfileTemplate string `mapstructure:"vagrantfile_template"`
|
VagrantfileTemplate string `mapstructure:"vagrantfile_template"`
|
||||||
|
|
||||||
tpl *packer.ConfigTemplate
|
ctx interpolate.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
type PostProcessor struct {
|
type PostProcessor struct {
|
||||||
|
@ -73,10 +76,12 @@ func (p *PostProcessor) PostProcessProvider(name string, provider Provider, ui p
|
||||||
|
|
||||||
ui.Say(fmt.Sprintf("Creating Vagrant box for '%s' provider", name))
|
ui.Say(fmt.Sprintf("Creating Vagrant box for '%s' provider", name))
|
||||||
|
|
||||||
outputPath, err := config.tpl.Process(config.OutputPath, &outputPathTemplate{
|
outputPath, err := interpolate.Render(config.OutputPath, &interpolate.Context{
|
||||||
ArtifactId: artifact.Id(),
|
Data: &outputPathTemplate{
|
||||||
BuildName: config.PackerBuildName,
|
ArtifactId: artifact.Id(),
|
||||||
Provider: name,
|
BuildName: config.PackerBuildName,
|
||||||
|
Provider: name,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, err
|
return nil, false, err
|
||||||
|
@ -162,21 +167,24 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
|
||||||
return p.PostProcessProvider(name, provider, ui, artifact)
|
return p.PostProcessProvider(name, provider, ui, artifact)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PostProcessor) configureSingle(config *Config, raws ...interface{}) error {
|
func (p *PostProcessor) configureSingle(c *Config, raws ...interface{}) error {
|
||||||
md, err := common.DecodeConfig(config, raws...)
|
var md mapstructure.Metadata
|
||||||
|
err := config.Decode(c, &config.DecodeOpts{
|
||||||
|
Metadata: &md,
|
||||||
|
Interpolate: true,
|
||||||
|
InterpolateFilter: &interpolate.RenderFilter{
|
||||||
|
Exclude: []string{
|
||||||
|
"output",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}, raws...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
config.tpl, err = packer.NewConfigTemplate()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
config.tpl.UserVars = config.PackerUserVars
|
|
||||||
|
|
||||||
// Defaults
|
// Defaults
|
||||||
if config.OutputPath == "" {
|
if c.OutputPath == "" {
|
||||||
config.OutputPath = "packer_{{ .BuildName }}_{{.Provider}}.box"
|
c.OutputPath = "packer_{{ .BuildName }}_{{.Provider}}.box"
|
||||||
}
|
}
|
||||||
|
|
||||||
found := false
|
found := false
|
||||||
|
@ -188,39 +196,15 @@ func (p *PostProcessor) configureSingle(config *Config, raws ...interface{}) err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !found {
|
if !found {
|
||||||
config.CompressionLevel = flate.DefaultCompression
|
c.CompressionLevel = flate.DefaultCompression
|
||||||
}
|
}
|
||||||
|
|
||||||
// Accumulate any errors
|
var errs *packer.MultiError
|
||||||
errs := common.CheckUnusedConfig(md)
|
if c.VagrantfileTemplate != "" {
|
||||||
|
_, err := os.Stat(c.VagrantfileTemplate)
|
||||||
templates := map[string]*string{
|
|
||||||
"vagrantfile_template": &config.VagrantfileTemplate,
|
|
||||||
}
|
|
||||||
|
|
||||||
for key, ptr := range templates {
|
|
||||||
*ptr, err = config.tpl.Process(*ptr, nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Error processing %s: %s", key, err))
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf(
|
||||||
}
|
"vagrantfile_template '%s' does not exist", c.VagrantfileTemplate))
|
||||||
}
|
|
||||||
|
|
||||||
validates := map[string]*string{
|
|
||||||
"output": &config.OutputPath,
|
|
||||||
"vagrantfile_template": &config.VagrantfileTemplate,
|
|
||||||
}
|
|
||||||
|
|
||||||
if config.VagrantfileTemplate != "" {
|
|
||||||
_, err := os.Stat(config.VagrantfileTemplate)
|
|
||||||
if err != nil {
|
|
||||||
errs = packer.MultiErrorAppend(errs, fmt.Errorf("vagrantfile_template '%s' does not exist", config.VagrantfileTemplate))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for n, ptr := range validates {
|
|
||||||
if err := config.tpl.Validate(*ptr); err != nil {
|
|
||||||
errs = packer.MultiErrorAppend(
|
|
||||||
errs, fmt.Errorf("Error parsing %s: %s", n, err))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue