packer-cn/builder/docker/builder.go

78 lines
1.5 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 {
2013-11-09 20:07:14 -05:00
config *Config
2013-11-08 19:55:02 -05:00
runner multistep.Runner
}
func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
2013-11-09 20:07:14 -05:00
c, warnings, errs := NewConfig(raws...)
if errs != nil {
2013-11-08 19:55:02 -05:00
return warnings, errs
}
2013-11-09 20:07:14 -05:00
b.config = c
2013-11-08 19:55:02 -05:00
return warnings, nil
}
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
driver := &DockerDriver{Ui: ui}
if err := driver.Verify(); err != nil {
return nil, err
}
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)
2013-11-09 20:07:14 -05:00
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", driver)
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)
}
2013-11-09 16:22:13 -05:00
// No errors, must've worked
artifact := &ExportArtifact{path: b.config.ExportPath}
return artifact, nil
2013-11-08 19:55:02 -05:00
}
func (b *Builder) Cancel() {
if b.runner != nil {
log.Println("Cancelling the step runner...")
b.runner.Cancel()
}
}