2013-11-09 02:43:41 -05:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2017-10-10 16:45:47 -04:00
|
|
|
"fmt"
|
2013-11-09 02:43:41 -05:00
|
|
|
"github.com/mitchellh/multistep"
|
2017-10-10 16:45:47 -04:00
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2013-11-09 02:43:41 -05:00
|
|
|
)
|
|
|
|
|
2015-06-15 01:09:38 -04:00
|
|
|
type StepConnectDocker struct{}
|
2013-11-09 02:43:41 -05:00
|
|
|
|
2015-06-15 01:09:38 -04:00
|
|
|
func (s *StepConnectDocker) Run(state multistep.StateBag) multistep.StepAction {
|
2015-07-16 22:07:39 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2013-11-09 02:43:41 -05:00
|
|
|
containerId := state.Get("container_id").(string)
|
2015-05-29 12:29:59 -04:00
|
|
|
driver := state.Get("driver").(Driver)
|
2013-11-09 02:43:41 -05:00
|
|
|
tempDir := state.Get("temp_dir").(string)
|
|
|
|
|
2015-05-29 12:29:59 -04:00
|
|
|
// Get the version so we can pass it to the communicator
|
|
|
|
version, err := driver.Version()
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2017-10-10 16:45:47 -04:00
|
|
|
containerUser, err := getContainerUser(containerId)
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-11-09 02:43:41 -05:00
|
|
|
// Create the communicator that talks to Docker via various
|
|
|
|
// os/exec tricks.
|
|
|
|
comm := &Communicator{
|
2017-10-10 16:45:47 -04:00
|
|
|
ContainerID: containerId,
|
|
|
|
HostDir: tempDir,
|
|
|
|
ContainerDir: config.ContainerDir,
|
|
|
|
Version: version,
|
|
|
|
Config: config,
|
|
|
|
ContainerUser: containerUser,
|
2013-11-09 02:43:41 -05:00
|
|
|
}
|
|
|
|
|
2015-06-15 01:09:38 -04:00
|
|
|
state.Put("communicator", comm)
|
|
|
|
return multistep.ActionContinue
|
2013-11-09 02:43:41 -05:00
|
|
|
}
|
|
|
|
|
2015-06-15 01:09:38 -04:00
|
|
|
func (s *StepConnectDocker) Cleanup(state multistep.StateBag) {}
|
2017-10-10 16:45:47 -04:00
|
|
|
|
|
|
|
func getContainerUser(containerId string) (string, error) {
|
|
|
|
inspectArgs := []string{"docker", "inspect", "--format", "{{.Config.User}}", containerId}
|
|
|
|
stdout, err := exec.Command(inspectArgs[0], inspectArgs[1:]...).Output()
|
|
|
|
if err != nil {
|
|
|
|
errStr := fmt.Sprintf("Failed to inspect the container: %s", err)
|
|
|
|
if ee, ok := err.(*exec.ExitError); ok {
|
|
|
|
errStr = fmt.Sprintf("%s, %s", errStr, ee.Stderr)
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf(errStr)
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(string(stdout)), nil
|
|
|
|
}
|