post-processor/*: interpolation

This commit is contained in:
Mitchell Hashimoto 2015-05-27 14:56:22 -07:00
parent 2b4df93f2f
commit dc1e67b6d2
4 changed files with 50 additions and 101 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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)
}

View File

@ -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
}