packer-cn/builder/docker/builder.go

93 lines
1.9 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"
const BuilderIdImport = "packer.post-processor.docker-import"
2013-11-08 19:55:02 -05:00
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{Tpl: b.config.tpl, 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{},
}
if b.config.Commit {
steps = append(steps, new(StepCommit))
} else {
steps = append(steps, new(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)
}
var artifact packer.Artifact
2013-11-09 16:22:13 -05:00
// No errors, must've worked
if b.config.Commit {
artifact = &ImportArtifact{
IdValue: state.Get("image_id").(string),
BuilderIdValue: BuilderIdImport,
Driver: driver,
}
} else {
artifact = &ExportArtifact{path: b.config.ExportPath}
}
2013-11-09 16:22:13 -05:00
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()
}
}