packer-cn/builder/docker/builder.go

115 lines
2.6 KiB
Go
Raw Normal View History

2013-11-08 19:55:02 -05:00
package docker
import (
2015-05-29 12:29:59 -04:00
"log"
2017-04-04 16:39:01 -04:00
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/helper/multistep"
2017-04-04 16:39:01 -04:00
"github.com/hashicorp/packer/packer"
2013-11-08 19:55:02 -05:00
)
const (
BuilderId = "packer.docker"
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{Ctx: &b.config.ctx, Ui: ui}
if err := driver.Verify(); err != nil {
return nil, err
}
2015-05-29 12:29:59 -04:00
version, err := driver.Version()
if err != nil {
return nil, err
}
log.Printf("[DEBUG] Docker version: %s", version.String())
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{},
&communicator.StepConnect{
Config: &b.config.Comm,
Host: commHost,
SSHConfig: sshConfig(&b.config.Comm),
CustomConnect: map[string]multistep.Step{
"docker": &StepConnectDocker{},
},
},
&common.StepProvision{},
}
if b.config.Discard {
log.Print("[DEBUG] Container will be discarded")
} else if b.config.Commit {
log.Print("[DEBUG] Container will be committed")
steps = append(steps, new(StepCommit))
} else if b.config.ExportPath != "" {
log.Printf("[DEBUG] Container will be exported to %s", b.config.ExportPath)
steps = append(steps, new(StepExport))
} else {
2015-08-19 16:12:16 -04:00
return nil, errArtifactNotUsed
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!
b.runner = common.NewRunner(steps, b.config.PackerConfig, ui)
2013-11-08 19:55:02 -05:00
b.runner.Run(state)
// If there was an error, return that
if rawErr, ok := state.GetOk("error"); ok {
return nil, rawErr.(error)
}
// If it was cancelled, then just return
if _, ok := state.GetOk(multistep.StateCancelled); ok {
return nil, nil
}
2013-11-09 16:22:13 -05:00
// No errors, must've worked
var artifact packer.Artifact
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()
}
}