builder/docker: verify docker is available on path

This commit is contained in:
Mitchell Hashimoto 2013-11-09 21:26:05 -08:00
parent f18516a65a
commit 80ed0d1d0c
4 changed files with 24 additions and 3 deletions

View File

@ -25,6 +25,11 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
}
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
}
steps := []multistep.Step{
&StepTempDir{},
&StepPull{},
@ -40,9 +45,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
state.Put("ui", ui)
// Setup the driver that will talk to Docker
state.Put("driver", &DockerDriver{
Ui: ui,
})
state.Put("driver", driver)
// Run!
if b.config.PackerDebug {

View File

@ -20,6 +20,9 @@ type Driver interface {
// StopContainer forcibly stops a container.
StopContainer(id string) error
// Verify verifies that the driver can run
Verify() error
}
// ContainerConfig is the configuration used to start a container.

View File

@ -82,3 +82,11 @@ func (d *DockerDriver) StartContainer(config *ContainerConfig) (string, error) {
func (d *DockerDriver) StopContainer(id string) error {
return exec.Command("docker", "kill", id).Run()
}
func (d *DockerDriver) Verify() error {
if _, err := exec.LookPath("docker"); err != nil {
return err
}
return nil
}

View File

@ -12,6 +12,7 @@ type MockDriver struct {
StartID string
StartError error
StopError error
VerifyError error
ExportCalled bool
ExportID string
@ -21,6 +22,7 @@ type MockDriver struct {
StartConfig *ContainerConfig
StopCalled bool
StopID string
VerifyCalled bool
}
func (d *MockDriver) Export(id string, dst io.Writer) error {
@ -54,3 +56,8 @@ func (d *MockDriver) StopContainer(id string) error {
d.StopID = id
return d.StopError
}
func (d *MockDriver) Verify() error {
d.VerifyCalled = true
return d.VerifyError
}