Skip to content

Commit

Permalink
internal/docker: simplify docker executor logic
Browse files Browse the repository at this point in the history
  • Loading branch information
andrestc committed Jun 7, 2018
1 parent 9803f01 commit fdc0dce
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 31 deletions.
17 changes: 7 additions & 10 deletions internal/docker/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ package docker

import (
"fmt"
"path/filepath"
"strings"

"github.com/fsouza/go-dockerclient"
"github.com/tsuru/tsuru/exec"
Expand All @@ -26,19 +24,18 @@ func (d *Executor) Execute(opts exec.ExecuteOptions) error {

func (d *Executor) ExecuteAsUser(user string, opts exec.ExecuteOptions) error {
cmd := append([]string{opts.Cmd}, opts.Args...)
binary := filepath.Base(cmd[0])
if binary != "bash" && binary != "sh" {
cmd = append([]string{"/bin/sh", "-lc"}, strings.Join(cmd, " "))
}
if opts.Dir != "" {
cmd = append(cmd[:2], fmt.Sprintf("cd %s && %s", opts.Dir, strings.Join(cmd[2:], " ")))
cmd = append([]string{
"/bin/sh", "-lc",
fmt.Sprintf("cd %s && exec $0 \"$@\"", opts.Dir),
}, cmd...)
}
if len(opts.Envs) > 0 {
var exports []string
envCmd := []string{"env"}
for _, e := range opts.Envs {
exports = append(exports, fmt.Sprintf("export %q && ", e))
envCmd = append(envCmd, e)
}
cmd = append(cmd[:2], fmt.Sprintf("%s %s", strings.Join(exports, ""), strings.Join(cmd[2:], " ")))
cmd = append(envCmd, cmd...)
}
e, err := d.Client.api.CreateExec(docker.CreateExecOptions{
Container: d.ContainerID,
Expand Down
21 changes: 0 additions & 21 deletions internal/docker/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,6 @@ func (s *S) TestSidecarExecuteIntegration(c *check.C) {
Envs: []string{"MYENV=myval", "ANOTHERENV=anotherval"},
expectedOut: "myval\n",
},
{
Name: "env-non-bash",
Cmd: "echo",
Args: []string{"$MYENV"},
Envs: []string{"MYENV=myval", "ANOTHERENV=anotherval"},
expectedOut: "myval\n",
},
{
Name: "dir-env",
Cmd: "/bin/sh",
Expand All @@ -124,27 +117,13 @@ func (s *S) TestSidecarExecuteIntegration(c *check.C) {
Dir: "$MYDIR",
expectedOut: "/etc\n",
},
{
Name: "env-with-quotes-non-bash",
Cmd: "echo",
Args: []string{"$MYENV"},
Envs: []string{"MYENV={\"a\": \"b\", \"a2\": \"b2\"}"},
expectedOut: "{\"a\": \"b\", \"a2\": \"b2\"}\n",
},
{
Name: "env-with-quotes",
Cmd: "/bin/sh",
Args: []string{"-lc", "echo $MYENV"},
Envs: []string{"MYENV={\"a\": \"b\", \"a2\": \"b2\"}"},
expectedOut: "{\"a\": \"b\", \"a2\": \"b2\"}\n",
},
{
Name: "env-with-quotes-non-bash",
Cmd: "echo",
Args: []string{"$MYENV"},
Envs: []string{"MYENV={'a': 'b', 'a2': 'b2'}"},
expectedOut: "{'a': 'b', 'a2': 'b2'}\n",
},
{
Name: "env-with-quotes",
Cmd: "/bin/sh",
Expand Down

0 comments on commit fdc0dce

Please sign in to comment.