packer-cn/builder/docker/builder.go

90 lines
1.8 KiB
Go
Raw Normal View History

2013-11-08 19:55:02 -05:00
package docker
import (
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/common"
"github.com/mitchellh/packer/packer"
"log"
)
const BuilderId = "packer.docker"
type Builder struct {
config Config
runner multistep.Runner
}
func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
md, err := common.DecodeConfig(&b.config, raws...)
if err != nil {
return nil, err
}
b.config.tpl, err = packer.NewConfigTemplate()
if err != nil {
return nil, err
}
2013-11-09 14:47:32 -05:00
// Accumulate any errors
2013-11-08 19:55:02 -05:00
errs := common.CheckUnusedConfig(md)
warnings := make([]string, 0)
2013-11-09 14:47:32 -05:00
// Validate the configuration
cwarns, cerrs := b.config.Prepare()
errs = packer.MultiErrorAppend(errs, cerrs...)
warnings = append(warnings, cwarns...)
2013-11-08 19:55:02 -05:00
if errs != nil && len(errs.Errors) > 0 {
return warnings, errs
}
return warnings, nil
}
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
2013-11-09 01:00:57 -05:00
steps := []multistep.Step{
&StepTempDir{},
2013-11-09 01:00:57 -05:00
&StepPull{},
2013-11-09 01:17:46 -05:00
&StepRun{},
&StepProvision{},
2013-11-09 12:48:36 -05:00
&StepExport{},
2013-11-09 01:00:57 -05:00
}
2013-11-08 19:55:02 -05:00
// Setup the state bag and initial state for the steps
state := new(multistep.BasicStateBag)
state.Put("config", &b.config)
2013-11-09 01:00:57 -05:00
state.Put("hook", hook)
state.Put("ui", ui)
2013-11-08 19:55:02 -05:00
// Setup the driver that will talk to Docker
state.Put("driver", &DockerDriver{
Ui: ui,
})
2013-11-08 19:55:02 -05:00
// Run!
if b.config.PackerDebug {
b.runner = &multistep.DebugRunner{
Steps: steps,
PauseFn: common.MultistepDebugFn(ui),
}
} else {
b.runner = &multistep.BasicRunner{Steps: steps}
}
b.runner.Run(state)
// If there was an error, return that
if rawErr, ok := state.GetOk("error"); ok {
return nil, rawErr.(error)
}
return nil, nil
}
func (b *Builder) Cancel() {
if b.runner != nil {
log.Println("Cancelling the step runner...")
b.runner.Cancel()
}
}