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-08-22 21:19:03 -04:00
|
|
|
"github.com/mitchellh/packer/common"
|
2013-06-26 20:37:46 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-06-27 10:14:15 -04:00
|
|
|
"log"
|
2013-06-26 20:37:46 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var builtins = map[string]string{
|
2013-10-05 18:32:41 -04:00
|
|
|
"mitchellh.amazonebs": "aws",
|
|
|
|
"mitchellh.amazon.instance": "aws",
|
|
|
|
"mitchellh.virtualbox": "virtualbox",
|
|
|
|
"mitchellh.vmware": "vmware",
|
2013-10-05 22:29:02 -04:00
|
|
|
"pearkes.digitalocean": "digitalocean",
|
2013-06-26 20:37:46 -04:00
|
|
|
}
|
|
|
|
|
2013-06-26 22:09:39 -04:00
|
|
|
type Config struct {
|
2013-08-22 21:19:03 -04:00
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
|
2013-06-26 22:09:24 -04:00
|
|
|
OutputPath string `mapstructure:"output"`
|
|
|
|
}
|
2013-06-26 20:37:46 -04:00
|
|
|
|
|
|
|
type PostProcessor struct {
|
2013-09-18 18:01:06 -04:00
|
|
|
config Config
|
|
|
|
premade map[string]packer.PostProcessor
|
|
|
|
extraConfig map[string]interface{}
|
2013-06-26 20:37:46 -04:00
|
|
|
}
|
|
|
|
|
2013-07-01 17:59:23 -04:00
|
|
|
func (p *PostProcessor) Configure(raws ...interface{}) error {
|
2013-08-23 18:31:21 -04:00
|
|
|
_, err := common.DecodeConfig(&p.config, raws...)
|
2013-08-22 21:19:03 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tpl, err := packer.NewConfigTemplate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2013-06-26 22:09:24 -04:00
|
|
|
}
|
2013-08-22 21:19:03 -04:00
|
|
|
tpl.UserVars = p.config.PackerUserVars
|
|
|
|
|
2013-08-23 18:31:21 -04:00
|
|
|
// Defaults
|
2013-06-26 22:09:24 -04:00
|
|
|
if p.config.OutputPath == "" {
|
2013-07-01 18:07:09 -04:00
|
|
|
p.config.OutputPath = "packer_{{ .BuildName }}_{{.Provider}}.box"
|
2013-06-27 13:53:43 -04:00
|
|
|
}
|
|
|
|
|
2013-08-23 18:31:21 -04:00
|
|
|
// Accumulate any errors
|
|
|
|
errs := new(packer.MultiError)
|
2013-08-22 21:19:03 -04:00
|
|
|
if err := tpl.Validate(p.config.OutputPath); err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Error parsing output template: %s", err))
|
2013-06-26 22:09:24 -04:00
|
|
|
}
|
|
|
|
|
2013-09-18 17:51:51 -04:00
|
|
|
// Store extra configuration we'll send to each post-processor type
|
2013-09-18 18:01:06 -04:00
|
|
|
p.extraConfig = make(map[string]interface{})
|
|
|
|
p.extraConfig["output"] = p.config.OutputPath
|
2013-09-18 23:56:00 -04:00
|
|
|
p.extraConfig["packer_build_name"] = p.config.PackerBuildName
|
|
|
|
p.extraConfig["packer_builder_type"] = p.config.PackerBuilderType
|
|
|
|
p.extraConfig["packer_debug"] = p.config.PackerDebug
|
|
|
|
p.extraConfig["packer_force"] = p.config.PackerForce
|
|
|
|
p.extraConfig["packer_user_variables"] = p.config.PackerUserVars
|
2013-07-05 14:11:54 -04:00
|
|
|
|
2013-09-18 17:51:51 -04:00
|
|
|
// TODO(mitchellh): Properly handle multiple raw configs. This isn't
|
|
|
|
// very pressing at the moment because at the time of this comment
|
|
|
|
// only the first member of raws can contain the actual type-overrides.
|
2013-06-28 10:16:38 -04:00
|
|
|
var mapConfig map[string]interface{}
|
2013-07-01 17:59:23 -04:00
|
|
|
if err := mapstructure.Decode(raws[0], &mapConfig); err != nil {
|
2013-08-23 18:31:21 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("Failed to decode config: %s", err))
|
|
|
|
return errs
|
2013-06-28 10:16:38 -04:00
|
|
|
}
|
2013-06-27 10:14:15 -04:00
|
|
|
|
2013-06-28 10:16:38 -04:00
|
|
|
p.premade = make(map[string]packer.PostProcessor)
|
|
|
|
for k, raw := range mapConfig {
|
2013-09-18 18:01:06 -04:00
|
|
|
pp, err := p.subPostProcessor(k, raw, p.extraConfig)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs, err)
|
2013-06-28 10:16:38 -04:00
|
|
|
continue
|
2013-06-27 17:06:14 -04:00
|
|
|
}
|
|
|
|
|
2013-09-18 18:01:06 -04:00
|
|
|
if pp == nil {
|
|
|
|
continue
|
2013-06-27 17:06:14 -04:00
|
|
|
}
|
2013-06-28 10:16:38 -04:00
|
|
|
|
|
|
|
p.premade[k] = pp
|
|
|
|
}
|
|
|
|
|
2013-08-23 18:31:21 -04:00
|
|
|
if len(errs.Errors) > 0 {
|
|
|
|
return errs
|
2013-06-28 10:16:38 -04:00
|
|
|
}
|
2013-06-27 10:14:15 -04:00
|
|
|
|
2013-06-26 20:37:46 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-07-01 14:30:39 -04:00
|
|
|
func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
|
2013-06-26 22:09:24 -04:00
|
|
|
ppName, ok := builtins[artifact.BuilderId()]
|
|
|
|
if !ok {
|
2013-07-01 14:30:39 -04:00
|
|
|
return nil, false, fmt.Errorf("Unknown artifact type, can't build box: %s", artifact.BuilderId())
|
2013-06-26 22:09:24 -04:00
|
|
|
}
|
|
|
|
|
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)
|
2013-06-26 22:09:24 -04:00
|
|
|
|
2013-09-18 18:01:06 -04:00
|
|
|
var err error
|
|
|
|
pp, err = p.subPostProcessor(ppName, nil, p.extraConfig)
|
|
|
|
if err != nil {
|
2013-07-01 14:30:39 -04:00
|
|
|
return nil, false, err
|
2013-06-27 10:14:15 -04:00
|
|
|
}
|
2013-09-18 18:01:06 -04:00
|
|
|
|
|
|
|
if pp == nil {
|
|
|
|
return nil, false, fmt.Errorf("Vagrant box post-processor not found: %s", ppName)
|
|
|
|
}
|
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
|
|
|
|
2013-09-18 18:01:06 -04:00
|
|
|
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.
|
2013-06-27 10:14:15 -04:00
|
|
|
func keyToPostProcessor(key string) packer.PostProcessor {
|
|
|
|
switch key {
|
|
|
|
case "aws":
|
|
|
|
return new(AWSBoxPostProcessor)
|
2013-10-05 22:29:02 -04:00
|
|
|
case "digitalocean":
|
|
|
|
return new(DigitalOceanBoxPostProcessor)
|
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
|
|
|
|
}
|
|
|
|
}
|