2013-11-09 01:17:46 -05:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-11-09 01:17:46 -05:00
|
|
|
"fmt"
|
2018-01-22 18:32:33 -05:00
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-11-09 01:17:46 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepRun struct {
|
|
|
|
containerId string
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepRun) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2013-11-09 01:17:46 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2019-12-17 17:34:58 -05:00
|
|
|
config, ok := state.Get("config").(*Config)
|
|
|
|
if !ok {
|
|
|
|
err := fmt.Errorf("error encountered obtaining docker config")
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-11-09 01:17:46 -05:00
|
|
|
|
2013-11-09 16:03:01 -05:00
|
|
|
runConfig := ContainerConfig{
|
2013-12-27 12:17:45 -05:00
|
|
|
Image: config.Image,
|
|
|
|
RunCommand: config.RunCommand,
|
2014-09-05 18:48:42 -04:00
|
|
|
Volumes: make(map[string]string),
|
2016-04-29 22:12:20 -04:00
|
|
|
Privileged: config.Privileged,
|
2013-11-09 02:43:41 -05:00
|
|
|
}
|
|
|
|
|
2014-09-05 18:48:42 -04:00
|
|
|
for host, container := range config.Volumes {
|
|
|
|
runConfig.Volumes[host] = container
|
|
|
|
}
|
2019-12-17 17:34:58 -05:00
|
|
|
|
|
|
|
tempDir := state.Get("temp_dir").(string)
|
2017-07-17 12:08:33 -04:00
|
|
|
runConfig.Volumes[tempDir] = config.ContainerDir
|
2014-09-05 18:48:42 -04:00
|
|
|
|
2019-12-17 17:34:58 -05:00
|
|
|
driver := state.Get("driver").(Driver)
|
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)
|
2019-12-13 14:57:01 -05:00
|
|
|
// instance_id is the generic term used so that users can have access to the
|
|
|
|
// instance id inside of the provisioners, used in step_provision.
|
|
|
|
state.Put("instance_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))
|
2019-03-27 17:51:50 -04:00
|
|
|
driver.KillContainer(s.containerId)
|
2013-11-09 16:03:01 -05:00
|
|
|
|
|
|
|
// Reset the container ID so that we're idempotent
|
|
|
|
s.containerId = ""
|
2013-11-09 01:17:46 -05:00
|
|
|
}
|