From b3672bf927f2104ed168ee5dcff4bc9728e84c06 Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Sun, 4 Aug 2019 15:38:58 +0200 Subject: [PATCH] docker driver: use toolbox exec Older version of docker doesn't support the exec api Env and WorkingDir options. Support these versions by doing the same we already do with the k8s driver: use the `toolbox exec` command that will set the provided Env, change the cwd to the WorkingDir and the exec the wanted command. --- internal/services/executor/driver/docker.go | 28 ++++++++++++++----- .../services/executor/driver/docker_test.go | 4 +++ internal/services/executor/driver/k8s_test.go | 4 +++ 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/internal/services/executor/driver/docker.go b/internal/services/executor/driver/docker.go index b3a6ea7..98b5ecf 100644 --- a/internal/services/executor/driver/docker.go +++ b/internal/services/executor/driver/docker.go @@ -22,6 +22,7 @@ import ( "io" "io/ioutil" "os" + "path/filepath" "runtime" "sort" "strconv" @@ -182,10 +183,11 @@ func (d *DockerDriver) NewPod(ctx context.Context, podConfig *PodConfig, out io. } pod := &DockerPod{ - id: podConfig.ID, - client: d.client, - executorID: d.executorID, - containers: []*DockerContainer{}, + id: podConfig.ID, + client: d.client, + executorID: d.executorID, + containers: []*DockerContainer{}, + initVolumeDir: podConfig.InitVolumeDir, } count := 0 @@ -333,6 +335,7 @@ func (d *DockerDriver) GetPods(ctx context.Context, all bool) ([]Pod, error) { client: d.client, executorID: d.executorID, containers: []*DockerContainer{}, + // TODO(sgotti) initvolumeDir isn't set } podsMap[podID] = pod } @@ -397,6 +400,8 @@ type DockerPod struct { labels map[string]string containers []*DockerContainer executorID string + + initVolumeDir string } type DockerContainer struct { @@ -475,11 +480,20 @@ func (s *Stdin) Close() error { func (dp *DockerPod) Exec(ctx context.Context, execConfig *ExecConfig) (ContainerExec, error) { endCh := make(chan error) + // old docker versions doesn't support providing Env (before api 1.25) and + // WorkingDir (before api 1.35) in exec command. + // Use a toolbox command that will set them up and then exec the real command. + envj, err := json.Marshal(execConfig.Env) + if err != nil { + return nil, err + } + + cmd := []string{filepath.Join(dp.initVolumeDir, "agola-toolbox"), "exec", "-e", string(envj), "-w", execConfig.WorkingDir, "--"} + cmd = append(cmd, execConfig.Cmd...) + dockerExecConfig := dockertypes.ExecConfig{ - Cmd: execConfig.Cmd, - Env: makeEnvSlice(execConfig.Env), + Cmd: cmd, Tty: execConfig.Tty, - WorkingDir: execConfig.WorkingDir, AttachStdin: execConfig.AttachStdin, AttachStdout: execConfig.Stdout != nil, AttachStderr: execConfig.Stderr != nil, diff --git a/internal/services/executor/driver/docker_test.go b/internal/services/executor/driver/docker_test.go index b35e54c..f7e1071 100644 --- a/internal/services/executor/driver/docker_test.go +++ b/internal/services/executor/driver/docker_test.go @@ -96,6 +96,10 @@ func TestDockerPod(t *testing.T) { ctx := context.Background() + if err := d.Setup(ctx); err != nil { + t.Fatalf("unexpected err: %v", err) + } + t.Run("create a pod with one container", func(t *testing.T) { pod, err := d.NewPod(ctx, &PodConfig{ ID: uuid.NewV4().String(), diff --git a/internal/services/executor/driver/k8s_test.go b/internal/services/executor/driver/k8s_test.go index 4b099c9..b99d37b 100644 --- a/internal/services/executor/driver/k8s_test.go +++ b/internal/services/executor/driver/k8s_test.go @@ -48,6 +48,10 @@ func TestK8sPod(t *testing.T) { ctx := context.Background() + if err := d.Setup(ctx); err != nil { + t.Fatalf("unexpected err: %v", err) + } + t.Run("create a pod with one container", func(t *testing.T) { pod, err := d.NewPod(ctx, &PodConfig{ ID: uuid.NewV4().String(),