From dc1e67b6d27d233313d62b659f43778d0d7581e4 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 27 May 2015 14:56:22 -0700 Subject: [PATCH] post-processor/*: interpolation --- post-processor/atlas/post-processor.go | 36 +++++------------ post-processor/compress/post-processor.go | 39 +++++------------- .../vagrant-cloud/post-processor.go | 40 +++++++++---------- post-processor/vsphere/post-processor.go | 36 ++++++----------- 4 files changed, 50 insertions(+), 101 deletions(-) diff --git a/post-processor/atlas/post-processor.go b/post-processor/atlas/post-processor.go index 26ecf5318..8ac16d273 100644 --- a/post-processor/atlas/post-processor.go +++ b/post-processor/atlas/post-processor.go @@ -10,7 +10,9 @@ import ( "github.com/hashicorp/atlas-go/v1" "github.com/mitchellh/mapstructure" "github.com/mitchellh/packer/common" + "github.com/mitchellh/packer/helper/config" "github.com/mitchellh/packer/packer" + "github.com/mitchellh/packer/template/interpolate" ) const BuildEnvKey = "ATLAS_BUILD_ID" @@ -39,7 +41,7 @@ type Config struct { // This shouldn't ever be set outside of unit tests. Test bool `mapstructure:"test"` - tpl *packer.ConfigTemplate + ctx interpolate.Context user, name string buildId int } @@ -50,38 +52,22 @@ type PostProcessor struct { } func (p *PostProcessor) Configure(raws ...interface{}) error { - _, err := common.DecodeConfig(&p.config, raws...) + err := config.Decode(&p.config, &config.DecodeOpts{ + Interpolate: true, + InterpolateFilter: &interpolate.RenderFilter{ + Exclude: []string{}, + }, + }, raws...) if err != nil { return err } - p.config.tpl, err = packer.NewConfigTemplate() - if err != nil { - return err - } - p.config.tpl.UserVars = p.config.PackerUserVars - - templates := map[string]*string{ - "artifact": &p.config.Artifact, - "type": &p.config.Type, - "server_address": &p.config.ServerAddr, - "token": &p.config.Token, - } - - errs := new(packer.MultiError) - for key, ptr := range templates { - *ptr, err = p.config.tpl.Process(*ptr, nil) - if err != nil { - errs = packer.MultiErrorAppend( - errs, fmt.Errorf("Error processing %s: %s", key, err)) - } - } - required := map[string]*string{ "artifact": &p.config.Artifact, "artifact_type": &p.config.Type, } + var errs *packer.MultiError for key, ptr := range required { if *ptr == "" { errs = packer.MultiErrorAppend( @@ -89,7 +75,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { } } - if len(errs.Errors) > 0 { + if errs != nil && len(errs.Errors) > 0 { return errs } diff --git a/post-processor/compress/post-processor.go b/post-processor/compress/post-processor.go index 003afd2a9..ccf300946 100644 --- a/post-processor/compress/post-processor.go +++ b/post-processor/compress/post-processor.go @@ -8,7 +8,9 @@ import ( "os" "github.com/mitchellh/packer/common" + "github.com/mitchellh/packer/helper/config" "github.com/mitchellh/packer/packer" + "github.com/mitchellh/packer/template/interpolate" ) type Config struct { @@ -16,7 +18,7 @@ type Config struct { OutputPath string `mapstructure:"output"` - tpl *packer.ConfigTemplate + ctx interpolate.Context } type PostProcessor struct { @@ -24,39 +26,16 @@ type PostProcessor struct { } func (self *PostProcessor) Configure(raws ...interface{}) error { - _, err := common.DecodeConfig(&self.config, raws...) + err := config.Decode(&self.config, &config.DecodeOpts{ + Interpolate: true, + InterpolateFilter: &interpolate.RenderFilter{ + Exclude: []string{}, + }, + }, raws...) if err != nil { return err } - self.config.tpl, err = packer.NewConfigTemplate() - if err != nil { - return err - } - self.config.tpl.UserVars = self.config.PackerUserVars - - templates := map[string]*string{ - "output": &self.config.OutputPath, - } - - errs := new(packer.MultiError) - for key, ptr := range templates { - if *ptr == "" { - errs = packer.MultiErrorAppend( - errs, fmt.Errorf("%s must be set", key)) - } - - *ptr, err = self.config.tpl.Process(*ptr, nil) - if err != nil { - errs = packer.MultiErrorAppend( - errs, fmt.Errorf("Error processing %s: %s", key, err)) - } - } - - if len(errs.Errors) > 0 { - return errs - } - return nil } diff --git a/post-processor/vagrant-cloud/post-processor.go b/post-processor/vagrant-cloud/post-processor.go index 516bea5d9..ace2a5c71 100644 --- a/post-processor/vagrant-cloud/post-processor.go +++ b/post-processor/vagrant-cloud/post-processor.go @@ -6,11 +6,14 @@ package vagrantcloud import ( "fmt" - "github.com/mitchellh/multistep" - "github.com/mitchellh/packer/common" - "github.com/mitchellh/packer/packer" "log" "strings" + + "github.com/mitchellh/multistep" + "github.com/mitchellh/packer/common" + "github.com/mitchellh/packer/helper/config" + "github.com/mitchellh/packer/packer" + "github.com/mitchellh/packer/template/interpolate" ) const VAGRANT_CLOUD_URL = "https://vagrantcloud.com/api/v1" @@ -28,7 +31,7 @@ type Config struct { BoxDownloadUrl string `mapstructure:"box_download_url"` - tpl *packer.ConfigTemplate + ctx interpolate.Context } type boxDownloadUrlTemplate struct { @@ -43,17 +46,18 @@ type PostProcessor struct { } func (p *PostProcessor) Configure(raws ...interface{}) error { - _, err := common.DecodeConfig(&p.config, raws...) + err := config.Decode(&p.config, &config.DecodeOpts{ + Interpolate: true, + InterpolateFilter: &interpolate.RenderFilter{ + Exclude: []string{ + "box_download_url", + }, + }, + }, raws...) if err != nil { return err } - p.config.tpl, err = packer.NewConfigTemplate() - if err != nil { - return err - } - p.config.tpl.UserVars = p.config.PackerUserVars - // Default configuration if p.config.VagrantCloudUrl == "" { p.config.VagrantCloudUrl = VAGRANT_CLOUD_URL @@ -76,15 +80,6 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { } } - // Template process - for key, ptr := range templates { - *ptr, err = p.config.tpl.Process(*ptr, nil) - if err != nil { - errs = packer.MultiErrorAppend( - errs, fmt.Errorf("Error processing %s: %s", key, err)) - } - } - if len(errs.Errors) > 0 { return errs } @@ -111,10 +106,11 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac // The name of the provider for vagrant cloud, and vagrant providerName := providerFromBuilderName(artifact.Id()) - boxDownloadUrl, err := p.config.tpl.Process(p.config.BoxDownloadUrl, &boxDownloadUrlTemplate{ + p.config.ctx.Data = &boxDownloadUrlTemplate{ ArtifactId: artifact.Id(), Provider: providerName, - }) + } + boxDownloadUrl, err := interpolate.Render(p.config.BoxDownloadUrl, &p.config.ctx) if err != nil { return nil, false, fmt.Errorf("Error processing box_download_url: %s", err) } diff --git a/post-processor/vsphere/post-processor.go b/post-processor/vsphere/post-processor.go index 38bff43aa..f9a6c37c3 100644 --- a/post-processor/vsphere/post-processor.go +++ b/post-processor/vsphere/post-processor.go @@ -3,12 +3,15 @@ package vsphere import ( "bytes" "fmt" - "github.com/mitchellh/packer/common" - "github.com/mitchellh/packer/packer" "log" "net/url" "os/exec" "strings" + + "github.com/mitchellh/packer/common" + "github.com/mitchellh/packer/helper/config" + "github.com/mitchellh/packer/packer" + "github.com/mitchellh/packer/template/interpolate" ) var builtins = map[string]string{ @@ -31,7 +34,7 @@ type Config struct { VMName string `mapstructure:"vm_name"` VMNetwork string `mapstructure:"vm_network"` - tpl *packer.ConfigTemplate + ctx interpolate.Context } type PostProcessor struct { @@ -39,17 +42,16 @@ type PostProcessor struct { } func (p *PostProcessor) Configure(raws ...interface{}) error { - _, err := common.DecodeConfig(&p.config, raws...) + err := config.Decode(&p.config, &config.DecodeOpts{ + Interpolate: true, + InterpolateFilter: &interpolate.RenderFilter{ + Exclude: []string{}, + }, + }, raws...) if err != nil { return err } - p.config.tpl, err = packer.NewConfigTemplate() - if err != nil { - return err - } - p.config.tpl.UserVars = p.config.PackerUserVars - // Defaults if p.config.DiskMode == "" { p.config.DiskMode = "thick" @@ -81,20 +83,6 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { } } - // Then define the ones that are optional - templates["datastore"] = &p.config.Datastore - templates["vm_network"] = &p.config.VMNetwork - templates["vm_folder"] = &p.config.VMFolder - - // Template process - for key, ptr := range templates { - *ptr, err = p.config.tpl.Process(*ptr, nil) - if err != nil { - errs = packer.MultiErrorAppend( - errs, fmt.Errorf("Error processing %s: %s", key, err)) - } - } - if len(errs.Errors) > 0 { return errs }