post-processor/vagrant: allow config of individual pp's

This commit is contained in:
Mitchell Hashimoto 2013-06-27 07:14:15 -07:00
parent face87d1f1
commit 637bcbc943
1 changed files with 48 additions and 12 deletions

View File

@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"log"
) )
var builtins = map[string]string{ var builtins = map[string]string{
@ -15,10 +16,12 @@ var builtins = map[string]string{
type Config struct { type Config struct {
OutputPath string `mapstructure:"output"` OutputPath string `mapstructure:"output"`
} }
type PostProcessor struct { type PostProcessor struct {
config Config config Config
premade map[string]packer.PostProcessor
} }
func (p *PostProcessor) Configure(raw interface{}) error { func (p *PostProcessor) Configure(raw interface{}) error {
@ -31,6 +34,29 @@ func (p *PostProcessor) Configure(raw interface{}) error {
return fmt.Errorf("`output` must be specified.") return fmt.Errorf("`output` must be specified.")
} }
mapConfig, ok := raw.(map[string]interface{})
if !ok {
panic("Raw configuration not a map")
}
errors := make([]error, 0)
for k, raw := range mapConfig {
pp := keyToPostProcessor(k)
if pp == nil {
continue
}
if err := pp.Configure(raw); err != nil {
errors = append(errors, err)
}
p.premade[k] = pp
}
if len(errors) > 0 {
return &packer.MultiError{errors}
}
return nil return nil
} }
@ -40,20 +66,30 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
return nil, fmt.Errorf("Unknown artifact type, can't build box: %s", artifact.BuilderId()) return nil, fmt.Errorf("Unknown artifact type, can't build box: %s", artifact.BuilderId())
} }
// Get the actual PostProcessor implementation for this type // Use the premade PostProcessor if we have one. Otherwise, we
var pp packer.PostProcessor // create it and configure it here.
switch ppName { pp, ok := p.premade[ppName]
case "aws": if !ok {
pp = new(AWSBoxPostProcessor) log.Printf("Premade post-processor for '%s' not found. Creating.", ppName)
default: pp = keyToPostProcessor(ppName)
if pp == nil {
return nil, fmt.Errorf("Vagrant box post-processor not found: %s", ppName) return nil, fmt.Errorf("Vagrant box post-processor not found: %s", ppName)
} }
// Prepare and run the post-processor
config := map[string]string{"output": p.config.OutputPath} config := map[string]string{"output": p.config.OutputPath}
if err := pp.Configure(config); err != nil { if err := pp.Configure(config); err != nil {
return nil, err return nil, err
} }
}
return pp.PostProcess(ui, artifact) return pp.PostProcess(ui, artifact)
} }
func keyToPostProcessor(key string) packer.PostProcessor {
switch key {
case "aws":
return new(AWSBoxPostProcessor)
default:
return nil
}
}