Commit aa694072 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

post-processor/vagrant: simplify logic, only send overrides to PP

[GH-413] /cc @jasonberanek
parent 6fc89e95
...@@ -24,15 +24,12 @@ type Config struct { ...@@ -24,15 +24,12 @@ type Config struct {
} }
type PostProcessor struct { type PostProcessor struct {
config Config config Config
premade map[string]packer.PostProcessor premade map[string]packer.PostProcessor
rawConfigs []interface{} extraConfig map[string]interface{}
} }
func (p *PostProcessor) Configure(raws ...interface{}) error { func (p *PostProcessor) Configure(raws ...interface{}) error {
// Store the raw configs for usage later
p.rawConfigs = raws
_, err := common.DecodeConfig(&p.config, raws...) _, err := common.DecodeConfig(&p.config, raws...)
if err != nil { if err != nil {
return err return err
...@@ -57,11 +54,8 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { ...@@ -57,11 +54,8 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
} }
// Store extra configuration we'll send to each post-processor type // Store extra configuration we'll send to each post-processor type
ppExtraConfig := make(map[string]interface{}) p.extraConfig = make(map[string]interface{})
ppExtraConfig["output"] = p.config.OutputPath p.extraConfig["output"] = p.config.OutputPath
// Store the extra configuration for post-processors
p.rawConfigs = append(p.rawConfigs, ppExtraConfig)
// TODO(mitchellh): Properly handle multiple raw configs. This isn't // TODO(mitchellh): Properly handle multiple raw configs. This isn't
// very pressing at the moment because at the time of this comment // very pressing at the moment because at the time of this comment
...@@ -75,18 +69,14 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { ...@@ -75,18 +69,14 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
p.premade = make(map[string]packer.PostProcessor) p.premade = make(map[string]packer.PostProcessor)
for k, raw := range mapConfig { for k, raw := range mapConfig {
pp := keyToPostProcessor(k) pp, err := p.subPostProcessor(k, raw, p.extraConfig)
if pp == nil { if err != nil {
errs = packer.MultiErrorAppend(errs, err)
continue continue
} }
// Create the proper list of configurations if pp == nil {
ppConfigs := make([]interface{}, len(p.rawConfigs), len(p.rawConfigs)+1) continue
copy(ppConfigs, p.rawConfigs)
ppConfigs = append(ppConfigs, raw)
if err := pp.Configure(ppConfigs...); err != nil {
errs = packer.MultiErrorAppend(errs, err)
} }
p.premade[k] = pp p.premade[k] = pp
...@@ -110,20 +100,37 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac ...@@ -110,20 +100,37 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
pp, ok := p.premade[ppName] pp, ok := p.premade[ppName]
if !ok { if !ok {
log.Printf("Premade post-processor for '%s' not found. Creating.", ppName) log.Printf("Premade post-processor for '%s' not found. Creating.", ppName)
pp = keyToPostProcessor(ppName)
if pp == nil {
return nil, false, fmt.Errorf("Vagrant box post-processor not found: %s", ppName)
}
if err := pp.Configure(p.rawConfigs...); err != nil { var err error
pp, err = p.subPostProcessor(ppName, nil, p.extraConfig)
if err != nil {
return nil, false, err return nil, false, err
} }
if pp == nil {
return nil, false, fmt.Errorf("Vagrant box post-processor not found: %s", ppName)
}
} }
ui.Say(fmt.Sprintf("Creating Vagrant box for '%s' provider", ppName)) ui.Say(fmt.Sprintf("Creating Vagrant box for '%s' provider", ppName))
return pp.PostProcess(ui, artifact) return pp.PostProcess(ui, artifact)
} }
func (p *PostProcessor) subPostProcessor(key string, specific interface{}, extra map[string]interface{}) (packer.PostProcessor, error) {
pp := keyToPostProcessor(key)
if pp == nil {
return nil, nil
}
if err := pp.Configure(extra, specific); err != nil {
return nil, err
}
return pp, nil
}
// keyToPostProcessor maps a configuration key to the actual post-processor
// it will be configuring. This returns a new instance of that post-processor.
func keyToPostProcessor(key string) packer.PostProcessor { func keyToPostProcessor(key string) packer.PostProcessor {
switch key { switch key {
case "aws": case "aws":
......
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