2013-06-26 20:37:46 -04:00
|
|
|
// vagrant implements the packer.PostProcessor interface and adds a
|
|
|
|
// post-processor that turns artifacts of known builders into Vagrant
|
|
|
|
// boxes.
|
|
|
|
package vagrant
|
|
|
|
|
|
|
|
import (
|
2013-06-26 22:09:24 -04:00
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
2013-06-26 20:37:46 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
var builtins = map[string]string{
|
|
|
|
"mitchellh.amazonebs": "aws",
|
|
|
|
}
|
|
|
|
|
2013-06-26 22:09:39 -04:00
|
|
|
type Config struct {
|
2013-06-26 22:09:24 -04:00
|
|
|
OutputPath string `mapstructure:"output"`
|
|
|
|
}
|
2013-06-26 20:37:46 -04:00
|
|
|
|
|
|
|
type PostProcessor struct {
|
|
|
|
config Config
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PostProcessor) Configure(raw interface{}) error {
|
2013-06-26 22:09:24 -04:00
|
|
|
err := mapstructure.Decode(raw, &p.config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.OutputPath == "" {
|
|
|
|
return fmt.Errorf("`output` must be specified.")
|
|
|
|
}
|
|
|
|
|
2013-06-26 20:37:46 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, error) {
|
2013-06-26 22:09:24 -04:00
|
|
|
ppName, ok := builtins[artifact.BuilderId()]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Unknown artifact type, can't build box: %s", artifact.BuilderId())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the actual PostProcessor implementation for this type
|
|
|
|
var pp packer.PostProcessor
|
|
|
|
switch ppName {
|
|
|
|
case "aws":
|
|
|
|
pp = new(AWSBoxPostProcessor)
|
|
|
|
default:
|
|
|
|
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}
|
|
|
|
if err := pp.Configure(config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return pp.PostProcess(ui, artifact)
|
2013-06-26 20:37:46 -04:00
|
|
|
}
|