packer: PostProcessor can take multiple configs

This commit is contained in:
Mitchell Hashimoto 2013-07-01 14:59:23 -07:00
parent 944d59afcd
commit 807896d63b
11 changed files with 49 additions and 33 deletions

View File

@ -59,7 +59,7 @@ func TestBuild_Prepare(t *testing.T) {
corePP := build.postProcessors[0][0]
pp := corePP.processor.(*TestPostProcessor)
assert.True(pp.configCalled, "config should be called")
assert.Equal(pp.configVal, 42, "config should have right value")
assert.Equal(pp.configVal, []interface{}{42}, "config should have right value")
}
func TestBuild_Prepare_Twice(t *testing.T) {

View File

@ -10,13 +10,13 @@ type cmdPostProcessor struct {
client *Client
}
func (c *cmdPostProcessor) Configure(config interface{}) error {
func (c *cmdPostProcessor) Configure(config ...interface{}) error {
defer func() {
r := recover()
c.checkExit(r, nil)
}()
return c.p.Configure(config)
return c.p.Configure(config...)
}
func (c *cmdPostProcessor) PostProcess(ui packer.Ui, a packer.Artifact) (packer.Artifact, bool, error) {

View File

@ -8,7 +8,7 @@ import (
type helperPostProcessor byte
func (helperPostProcessor) Configure(interface{}) error {
func (helperPostProcessor) Configure(...interface{}) error {
return nil
}

View File

@ -9,7 +9,7 @@ type PostProcessor interface {
// Configure is responsible for setting up configuration, storing
// the state for later, and returning and errors, such as validation
// errors.
Configure(interface{}) error
Configure(...interface{}) error
// PostProcess takes a previously created Artifact and produces another
// Artifact. If an error occurs, it should return that error. If `keep`

View File

@ -4,13 +4,13 @@ type TestPostProcessor struct {
artifactId string
keep bool
configCalled bool
configVal interface{}
configVal []interface{}
ppCalled bool
ppArtifact Artifact
ppUi Ui
}
func (pp *TestPostProcessor) Configure(v interface{}) error {
func (pp *TestPostProcessor) Configure(v ...interface{}) error {
pp.configCalled = true
pp.configVal = v
return nil

View File

@ -17,6 +17,10 @@ type PostProcessorServer struct {
p packer.PostProcessor
}
type PostProcessorConfigureArgs struct {
Configs []interface{}
}
type PostProcessorProcessResponse struct {
Err error
Keep bool
@ -26,8 +30,9 @@ type PostProcessorProcessResponse struct {
func PostProcessor(client *rpc.Client) *postProcessor {
return &postProcessor{client}
}
func (p *postProcessor) Configure(raw interface{}) (err error) {
if cerr := p.client.Call("PostProcessor.Configure", &raw, &err); cerr != nil {
func (p *postProcessor) Configure(raw ...interface{}) (err error) {
args := &PostProcessorConfigureArgs{Configs: raw}
if cerr := p.client.Call("PostProcessor.Configure", args, &err); cerr != nil {
err = cerr
}
@ -60,8 +65,8 @@ func (p *postProcessor) PostProcess(ui packer.Ui, a packer.Artifact) (packer.Art
return Artifact(client), response.Keep, nil
}
func (p *PostProcessorServer) Configure(raw *interface{}, reply *error) error {
*reply = p.p.Configure(*raw)
func (p *PostProcessorServer) Configure(args *PostProcessorConfigureArgs, reply *error) error {
*reply = p.p.Configure(args.Configs...)
if *reply != nil {
*reply = NewBasicError(*reply)
}

View File

@ -3,6 +3,7 @@ package rpc
import (
"github.com/mitchellh/packer/packer"
"net/rpc"
"reflect"
"testing"
)
@ -10,13 +11,13 @@ var testPostProcessorArtifact = new(testArtifact)
type TestPostProcessor struct {
configCalled bool
configVal interface{}
configVal []interface{}
ppCalled bool
ppArtifact packer.Artifact
ppUi packer.Ui
}
func (pp *TestPostProcessor) Configure(v interface{}) error {
func (pp *TestPostProcessor) Configure(v ...interface{}) error {
pp.configCalled = true
pp.configVal = v
return nil
@ -56,7 +57,7 @@ func TestPostProcessorRPC(t *testing.T) {
t.Fatal("config should be called")
}
if p.configVal != 42 {
if !reflect.DeepEqual(p.configVal, []interface{}{42}) {
t.Fatalf("unknown config value: %#v", p.configVal)
}

View File

@ -24,10 +24,12 @@ type AWSBoxPostProcessor struct {
config AWSBoxConfig
}
func (p *AWSBoxPostProcessor) Configure(raw interface{}) error {
err := mapstructure.Decode(raw, &p.config)
if err != nil {
return err
func (p *AWSBoxPostProcessor) Configure(raws ...interface{}) error {
for _, raw := range raws {
err := mapstructure.Decode(raw, &p.config)
if err != nil {
return err
}
}
return nil

View File

@ -26,23 +26,26 @@ type PostProcessor struct {
premade map[string]packer.PostProcessor
}
func (p *PostProcessor) Configure(raw interface{}) error {
err := mapstructure.Decode(raw, &p.config)
if err != nil {
return err
func (p *PostProcessor) Configure(raws ...interface{}) error {
for _, raw := range raws {
err := mapstructure.Decode(raw, &p.config)
if err != nil {
return err
}
}
if p.config.OutputPath == "" {
p.config.OutputPath = "packer_{{.Provider}}.box"
}
_, err = template.New("output").Parse(p.config.OutputPath)
_, err := template.New("output").Parse(p.config.OutputPath)
if err != nil {
return fmt.Errorf("output invalid template: %s", err)
}
// TODO(mitchellh): Properly handle multiple raw configs
var mapConfig map[string]interface{}
if err := mapstructure.Decode(raw, &mapConfig); err != nil {
if err := mapstructure.Decode(raws[0], &mapConfig); err != nil {
return err
}

View File

@ -28,10 +28,12 @@ type VBoxBoxPostProcessor struct {
config VBoxBoxConfig
}
func (p *VBoxBoxPostProcessor) Configure(raw interface{}) error {
err := mapstructure.Decode(raw, &p.config)
if err != nil {
return err
func (p *VBoxBoxPostProcessor) Configure(raws ...interface{}) error {
for _, raw := range raws {
err := mapstructure.Decode(raw, &p.config)
if err != nil {
return err
}
}
return nil
@ -179,6 +181,6 @@ func (p *VBoxBoxPostProcessor) renameOVF(dir string) error {
var defaultVBoxVagrantfile = `
Vagrant.configure("2") do |config|
config.vm.base_mac = "{{ .BaseMacAddress }}"
config.vm.base_mac = "{{ .BaseMacAddress }}"
end
`

View File

@ -20,12 +20,15 @@ type VMwareBoxPostProcessor struct {
config VMwareBoxConfig
}
func (p *VMwareBoxPostProcessor) Configure(raw interface{}) error {
err := mapstructure.Decode(raw, &p.config)
if err != nil {
return err
func (p *VMwareBoxPostProcessor) Configure(raws ...interface{}) error {
for _, raw := range raws {
err := mapstructure.Decode(raw, &p.config)
if err != nil {
return err
}
}
return nil
}