2013-11-09 01:17:46 -05:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StepRun struct {
|
|
|
|
containerId string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepRun) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
config := state.Get("config").(*Config)
|
2013-11-09 16:03:01 -05:00
|
|
|
driver := state.Get("driver").(Driver)
|
2013-11-09 02:43:41 -05:00
|
|
|
tempDir := state.Get("temp_dir").(string)
|
2013-11-09 01:17:46 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
2013-11-09 16:03:01 -05:00
|
|
|
runConfig := ContainerConfig{
|
2013-12-27 12:17:45 -05:00
|
|
|
Image: config.Image,
|
|
|
|
RunCommand: config.RunCommand,
|
2013-11-09 16:03:01 -05:00
|
|
|
Volumes: map[string]string{
|
|
|
|
tempDir: "/packer-files",
|
|
|
|
},
|
2013-11-09 02:43:41 -05:00
|
|
|
}
|
|
|
|
|
2013-12-27 12:17:45 -05:00
|
|
|
ui.Say("Starting docker container...")
|
2013-11-09 16:03:01 -05:00
|
|
|
containerId, err := driver.StartContainer(&runConfig)
|
|
|
|
if err != nil {
|
2013-11-09 01:17:46 -05:00
|
|
|
err := fmt.Errorf("Error running container: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-11-09 16:03:01 -05:00
|
|
|
// Save the container ID
|
|
|
|
s.containerId = containerId
|
2013-11-09 02:43:41 -05:00
|
|
|
state.Put("container_id", s.containerId)
|
2013-11-09 16:03:01 -05:00
|
|
|
ui.Message(fmt.Sprintf("Container ID: %s", s.containerId))
|
2013-11-09 01:17:46 -05:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepRun) Cleanup(state multistep.StateBag) {
|
|
|
|
if s.containerId == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-11-09 22:17:27 -05:00
|
|
|
driver := state.Get("driver").(Driver)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
2013-11-09 02:43:41 -05:00
|
|
|
// Kill the container. We don't handle errors because errors usually
|
|
|
|
// just mean that the container doesn't exist anymore, which isn't a
|
|
|
|
// big deal.
|
2013-11-09 22:17:27 -05:00
|
|
|
ui.Say(fmt.Sprintf("Killing the container: %s", s.containerId))
|
2013-11-09 16:03:01 -05:00
|
|
|
driver.StopContainer(s.containerId)
|
|
|
|
|
|
|
|
// Reset the container ID so that we're idempotent
|
|
|
|
s.containerId = ""
|
2013-11-09 01:17:46 -05:00
|
|
|
}
|