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"
|
2013-06-27 10:14:15 -04:00
|
|
|
"log"
|
2013-06-27 13:53:43 -04:00
|
|
|
"text/template"
|
2013-06-26 20:37:46 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var builtins = map[string]string{
|
2013-06-27 10:39:11 -04:00
|
|
|
"mitchellh.amazonebs": "aws",
|
|
|
|
"mitchellh.virtualbox": "virtualbox",
|
2013-06-27 19:21:03 -04:00
|
|
|
"mitchellh.vmware": "vmware",
|
2013-06-26 20:37:46 -04:00
|
|
|
}
|
|
|
|
|
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 {
|
2013-06-27 10:38:33 -04:00
|
|
|
config Config
|
2013-06-27 10:14:15 -04:00
|
|
|
premade map[string]packer.PostProcessor
|
2013-06-26 20:37:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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 == "" {
|
2013-06-27 13:53:43 -04:00
|
|
|
p.config.OutputPath = "packer_{{.Provider}}.box"
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = template.New("output").Parse(p.config.OutputPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("output invalid template: %s", err)
|
2013-06-26 22:09:24 -04:00
|
|
|
}
|
|
|
|
|
2013-06-27 17:06:14 -04:00
|
|
|
/*
|
|
|
|
TODO(mitchellh): We need a way to get the keys...
|
|
|
|
mapConfig, ok := raw.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
panic("Raw configuration not a map")
|
2013-06-27 10:14:15 -04:00
|
|
|
}
|
|
|
|
|
2013-06-27 17:06:14 -04:00
|
|
|
errors := make([]error, 0)
|
|
|
|
for k, raw := range mapConfig {
|
|
|
|
pp := keyToPostProcessor(k)
|
|
|
|
if pp == nil {
|
|
|
|
continue
|
|
|
|
}
|
2013-06-27 10:14:15 -04:00
|
|
|
|
2013-06-27 17:06:14 -04:00
|
|
|
if err := pp.Configure(raw); err != nil {
|
|
|
|
errors = append(errors, err)
|
|
|
|
}
|
2013-06-27 10:14:15 -04:00
|
|
|
|
2013-06-27 17:06:14 -04:00
|
|
|
p.premade[k] = pp
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errors) > 0 {
|
|
|
|
return &packer.MultiError{errors}
|
|
|
|
}
|
|
|
|
*/
|
2013-06-27 10:14:15 -04:00
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2013-06-27 10:14:15 -04:00
|
|
|
// Use the premade PostProcessor if we have one. Otherwise, we
|
|
|
|
// create it and configure it here.
|
|
|
|
pp, ok := p.premade[ppName]
|
|
|
|
if !ok {
|
|
|
|
log.Printf("Premade post-processor for '%s' not found. Creating.", ppName)
|
|
|
|
pp = keyToPostProcessor(ppName)
|
|
|
|
if pp == nil {
|
|
|
|
return nil, fmt.Errorf("Vagrant box post-processor not found: %s", ppName)
|
|
|
|
}
|
2013-06-26 22:09:24 -04:00
|
|
|
|
2013-06-27 10:14:15 -04:00
|
|
|
config := map[string]string{"output": p.config.OutputPath}
|
|
|
|
if err := pp.Configure(config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-06-26 22:09:24 -04:00
|
|
|
}
|
|
|
|
|
2013-06-27 10:17:08 -04:00
|
|
|
ui.Say(fmt.Sprintf("Creating Vagrant box for '%s' provider", ppName))
|
2013-06-26 22:09:24 -04:00
|
|
|
return pp.PostProcess(ui, artifact)
|
2013-06-26 20:37:46 -04:00
|
|
|
}
|
2013-06-27 10:14:15 -04:00
|
|
|
|
|
|
|
func keyToPostProcessor(key string) packer.PostProcessor {
|
|
|
|
switch key {
|
|
|
|
case "aws":
|
|
|
|
return new(AWSBoxPostProcessor)
|
2013-06-27 10:39:11 -04:00
|
|
|
case "virtualbox":
|
|
|
|
return new(VBoxBoxPostProcessor)
|
2013-06-27 19:21:03 -04:00
|
|
|
case "vmware":
|
|
|
|
return new(VMwareBoxPostProcessor)
|
2013-06-27 10:14:15 -04:00
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|