Commit dc1e67b6 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

post-processor/*: interpolation

parent 2b4df93f
...@@ -10,7 +10,9 @@ import ( ...@@ -10,7 +10,9 @@ import (
"github.com/hashicorp/atlas-go/v1" "github.com/hashicorp/atlas-go/v1"
"github.com/mitchellh/mapstructure" "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"
) )
const BuildEnvKey = "ATLAS_BUILD_ID" const BuildEnvKey = "ATLAS_BUILD_ID"
...@@ -39,7 +41,7 @@ type Config struct { ...@@ -39,7 +41,7 @@ type Config struct {
// This shouldn't ever be set outside of unit tests. // This shouldn't ever be set outside of unit tests.
Test bool `mapstructure:"test"` Test bool `mapstructure:"test"`
tpl *packer.ConfigTemplate ctx interpolate.Context
user, name string user, name string
buildId int buildId int
} }
...@@ -50,38 +52,22 @@ type PostProcessor struct { ...@@ -50,38 +52,22 @@ type PostProcessor struct {
} }
func (p *PostProcessor) Configure(raws ...interface{}) error { 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 { if err != nil {
return err 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{ required := map[string]*string{
"artifact": &p.config.Artifact, "artifact": &p.config.Artifact,
"artifact_type": &p.config.Type, "artifact_type": &p.config.Type,
} }
var errs *packer.MultiError
for key, ptr := range required { for key, ptr := range required {
if *ptr == "" { if *ptr == "" {
errs = packer.MultiErrorAppend( errs = packer.MultiErrorAppend(
...@@ -89,7 +75,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { ...@@ -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 return errs
} }
......
...@@ -8,7 +8,9 @@ import ( ...@@ -8,7 +8,9 @@ import (
"os" "os"
"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"
) )
type Config struct { type Config struct {
...@@ -16,7 +18,7 @@ type Config struct { ...@@ -16,7 +18,7 @@ type Config struct {
OutputPath string `mapstructure:"output"` OutputPath string `mapstructure:"output"`
tpl *packer.ConfigTemplate ctx interpolate.Context
} }
type PostProcessor struct { type PostProcessor struct {
...@@ -24,39 +26,16 @@ type PostProcessor struct { ...@@ -24,39 +26,16 @@ type PostProcessor struct {
} }
func (self *PostProcessor) Configure(raws ...interface{}) error { 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 { if err != nil {
return err 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 return nil
} }
......
...@@ -6,11 +6,14 @@ package vagrantcloud ...@@ -6,11 +6,14 @@ package vagrantcloud
import ( import (
"fmt" "fmt"
"log"
"strings"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
"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"
"log" "github.com/mitchellh/packer/template/interpolate"
"strings"
) )
const VAGRANT_CLOUD_URL = "https://vagrantcloud.com/api/v1" const VAGRANT_CLOUD_URL = "https://vagrantcloud.com/api/v1"
...@@ -28,7 +31,7 @@ type Config struct { ...@@ -28,7 +31,7 @@ type Config struct {
BoxDownloadUrl string `mapstructure:"box_download_url"` BoxDownloadUrl string `mapstructure:"box_download_url"`
tpl *packer.ConfigTemplate ctx interpolate.Context
} }
type boxDownloadUrlTemplate struct { type boxDownloadUrlTemplate struct {
...@@ -43,17 +46,18 @@ type PostProcessor struct { ...@@ -43,17 +46,18 @@ type PostProcessor struct {
} }
func (p *PostProcessor) Configure(raws ...interface{}) error { 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 { if err != nil {
return err return err
} }
p.config.tpl, err = packer.NewConfigTemplate()
if err != nil {
return err
}
p.config.tpl.UserVars = p.config.PackerUserVars
// Default configuration // Default configuration
if p.config.VagrantCloudUrl == "" { if p.config.VagrantCloudUrl == "" {
p.config.VagrantCloudUrl = VAGRANT_CLOUD_URL p.config.VagrantCloudUrl = VAGRANT_CLOUD_URL
...@@ -76,15 +80,6 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { ...@@ -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 { if len(errs.Errors) > 0 {
return errs return errs
} }
...@@ -111,10 +106,11 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac ...@@ -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 // The name of the provider for vagrant cloud, and vagrant
providerName := providerFromBuilderName(artifact.Id()) providerName := providerFromBuilderName(artifact.Id())
boxDownloadUrl, err := p.config.tpl.Process(p.config.BoxDownloadUrl, &boxDownloadUrlTemplate{ p.config.ctx.Data = &boxDownloadUrlTemplate{
ArtifactId: artifact.Id(), ArtifactId: artifact.Id(),
Provider: providerName, Provider: providerName,
}) }
boxDownloadUrl, err := interpolate.Render(p.config.BoxDownloadUrl, &p.config.ctx)
if err != nil { if err != nil {
return nil, false, fmt.Errorf("Error processing box_download_url: %s", err) return nil, false, fmt.Errorf("Error processing box_download_url: %s", err)
} }
......
...@@ -3,12 +3,15 @@ package vsphere ...@@ -3,12 +3,15 @@ package vsphere
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"github.com/mitchellh/packer/common"
"github.com/mitchellh/packer/packer"
"log" "log"
"net/url" "net/url"
"os/exec" "os/exec"
"strings" "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{ var builtins = map[string]string{
...@@ -31,7 +34,7 @@ type Config struct { ...@@ -31,7 +34,7 @@ type Config struct {
VMName string `mapstructure:"vm_name"` VMName string `mapstructure:"vm_name"`
VMNetwork string `mapstructure:"vm_network"` VMNetwork string `mapstructure:"vm_network"`
tpl *packer.ConfigTemplate ctx interpolate.Context
} }
type PostProcessor struct { type PostProcessor struct {
...@@ -39,17 +42,16 @@ type PostProcessor struct { ...@@ -39,17 +42,16 @@ type PostProcessor struct {
} }
func (p *PostProcessor) Configure(raws ...interface{}) error { 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 { if err != nil {
return err return err
} }
p.config.tpl, err = packer.NewConfigTemplate()
if err != nil {
return err
}
p.config.tpl.UserVars = p.config.PackerUserVars
// Defaults // Defaults
if p.config.DiskMode == "" { if p.config.DiskMode == "" {
p.config.DiskMode = "thick" p.config.DiskMode = "thick"
...@@ -81,20 +83,6 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { ...@@ -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 { if len(errs.Errors) > 0 {
return errs return errs
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment