2013-11-08 19:55:02 -05:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2015-05-29 12:29:59 -04:00
|
|
|
"log"
|
|
|
|
|
2013-11-08 19:55:02 -05:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/common"
|
2015-06-15 01:09:38 -04:00
|
|
|
"github.com/mitchellh/packer/helper/communicator"
|
2013-11-08 19:55:02 -05:00
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
2015-08-18 17:38:32 -04: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) {
|
2015-05-27 16:03:47 -04:00
|
|
|
driver := &DockerDriver{Ctx: &b.config.ctx, Ui: ui}
|
2013-11-10 00:26:05 -05:00
|
|
|
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{
|
2013-11-09 02:43:41 -05:00
|
|
|
&StepTempDir{},
|
2013-11-09 01:00:57 -05:00
|
|
|
&StepPull{},
|
2013-11-09 01:17:46 -05:00
|
|
|
&StepRun{},
|
2015-06-15 01:09:38 -04:00
|
|
|
&communicator.StepConnect{
|
|
|
|
Config: &b.config.Comm,
|
|
|
|
Host: commHost,
|
|
|
|
SSHConfig: sshConfig(&b.config.Comm),
|
|
|
|
CustomConnect: map[string]multistep.Step{
|
|
|
|
"docker": &StepConnectDocker{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&common.StepProvision{},
|
2014-09-04 21:03:15 -04:00
|
|
|
}
|
|
|
|
|
2015-08-18 17:38:32 -04:00
|
|
|
if b.config.Discard {
|
|
|
|
log.Print("[DEBUG] Container will be discarded")
|
|
|
|
} else if b.config.Commit {
|
|
|
|
log.Print("[DEBUG] Container will be committed")
|
2014-09-04 21:03:15 -04:00
|
|
|
steps = append(steps, new(StepCommit))
|
2015-08-18 17:38:32 -04:00
|
|
|
} else if b.config.ExportPath != "" {
|
|
|
|
log.Printf("[DEBUG] Container will be exported to %s", b.config.ExportPath)
|
2014-09-04 21:03:15 -04:00
|
|
|
steps = append(steps, new(StepExport))
|
2015-08-18 17:38:32 -04:00
|
|
|
} 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
|
|
|
|
2013-11-09 15:12:23 -05:00
|
|
|
// Setup the driver that will talk to Docker
|
2013-11-10 00:26:05 -05:00
|
|
|
state.Put("driver", driver)
|
2013-11-09 15:12:23 -05:00
|
|
|
|
2013-11-08 19:55:02 -05:00
|
|
|
// Run!
|
2016-09-13 20:04:18 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2015-05-29 12:37:27 -04:00
|
|
|
// 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
|
2015-05-29 12:37:27 -04:00
|
|
|
var artifact packer.Artifact
|
2014-09-04 21:03:15 -04:00
|
|
|
if b.config.Commit {
|
2014-07-20 14:57:10 -04:00
|
|
|
artifact = &ImportArtifact{
|
|
|
|
IdValue: state.Get("image_id").(string),
|
2014-09-04 21:03:15 -04:00
|
|
|
BuilderIdValue: BuilderIdImport,
|
2014-07-20 14:57:10 -04:00
|
|
|
Driver: driver,
|
|
|
|
}
|
2014-09-04 21:03:15 -04:00
|
|
|
} else {
|
|
|
|
artifact = &ExportArtifact{path: b.config.ExportPath}
|
2014-07-20 14:57:10 -04:00
|
|
|
}
|
2015-05-29 12:37:27 -04:00
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|