From 807896d63bba8a8267dfedb5bed648b246816105 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 1 Jul 2013 14:59:23 -0700 Subject: [PATCH] packer: PostProcessor can take multiple configs --- packer/build_test.go | 2 +- packer/plugin/post_processor.go | 4 ++-- packer/plugin/post_processor_test.go | 2 +- packer/post_processor.go | 2 +- packer/post_processor_test.go | 4 ++-- packer/rpc/post_processor.go | 13 +++++++++---- packer/rpc/post_processor_test.go | 7 ++++--- post-processor/vagrant/aws.go | 10 ++++++---- post-processor/vagrant/post-processor.go | 15 +++++++++------ post-processor/vagrant/virtualbox.go | 12 +++++++----- post-processor/vagrant/vmware.go | 11 +++++++---- 11 files changed, 49 insertions(+), 33 deletions(-) diff --git a/packer/build_test.go b/packer/build_test.go index 28e2fd43a..82e8c6e10 100644 --- a/packer/build_test.go +++ b/packer/build_test.go @@ -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) { diff --git a/packer/plugin/post_processor.go b/packer/plugin/post_processor.go index d32701532..f511748a6 100644 --- a/packer/plugin/post_processor.go +++ b/packer/plugin/post_processor.go @@ -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) { diff --git a/packer/plugin/post_processor_test.go b/packer/plugin/post_processor_test.go index 0e3cb0a7c..df8a799f7 100644 --- a/packer/plugin/post_processor_test.go +++ b/packer/plugin/post_processor_test.go @@ -8,7 +8,7 @@ import ( type helperPostProcessor byte -func (helperPostProcessor) Configure(interface{}) error { +func (helperPostProcessor) Configure(...interface{}) error { return nil } diff --git a/packer/post_processor.go b/packer/post_processor.go index 606581f42..3def4a0dd 100644 --- a/packer/post_processor.go +++ b/packer/post_processor.go @@ -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` diff --git a/packer/post_processor_test.go b/packer/post_processor_test.go index 3c373a913..fa6dbdbf9 100644 --- a/packer/post_processor_test.go +++ b/packer/post_processor_test.go @@ -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 diff --git a/packer/rpc/post_processor.go b/packer/rpc/post_processor.go index fea078b04..fb43cb7d9 100644 --- a/packer/rpc/post_processor.go +++ b/packer/rpc/post_processor.go @@ -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) } diff --git a/packer/rpc/post_processor_test.go b/packer/rpc/post_processor_test.go index 26e49b022..75f3deca0 100644 --- a/packer/rpc/post_processor_test.go +++ b/packer/rpc/post_processor_test.go @@ -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) } diff --git a/post-processor/vagrant/aws.go b/post-processor/vagrant/aws.go index c6e9f9120..c4284c3f8 100644 --- a/post-processor/vagrant/aws.go +++ b/post-processor/vagrant/aws.go @@ -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 diff --git a/post-processor/vagrant/post-processor.go b/post-processor/vagrant/post-processor.go index 8ce2f9842..0dd1e4bf2 100644 --- a/post-processor/vagrant/post-processor.go +++ b/post-processor/vagrant/post-processor.go @@ -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 } diff --git a/post-processor/vagrant/virtualbox.go b/post-processor/vagrant/virtualbox.go index 068c759e3..50aad8c71 100644 --- a/post-processor/vagrant/virtualbox.go +++ b/post-processor/vagrant/virtualbox.go @@ -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 ` diff --git a/post-processor/vagrant/vmware.go b/post-processor/vagrant/vmware.go index 33f4998e8..310192f39 100644 --- a/post-processor/vagrant/vmware.go +++ b/post-processor/vagrant/vmware.go @@ -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 }