Skip to content

Commit

Permalink
Merge pull request #2250 from knightXun/envs
Browse files Browse the repository at this point in the history
feature: add envs for pouch exec
  • Loading branch information
allencloud authored Sep 19, 2018
2 parents 2208220 + 8cb32d2 commit c2553d6
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 2 deletions.
5 changes: 5 additions & 0 deletions apis/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3193,6 +3193,11 @@ definitions:
minItems: 1
items:
type: "string"
Env:
type: "array"
description: "envs for exec command in container"
items:
type: "string"
ContainerProcessList:
description: OK Response to ContainerTop operation
type: "object"
Expand Down
19 changes: 19 additions & 0 deletions apis/types/exec_create_config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions cli/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type ExecCommand struct {
Terminal bool
Detach bool
User string
Envs []string
}

// Init initializes ExecCommand command.
Expand All @@ -51,6 +52,7 @@ func (e *ExecCommand) addFlags() {
flagSet.BoolVarP(&e.Terminal, "tty", "t", false, "Allocate a tty device")
flagSet.BoolVarP(&e.Interactive, "interactive", "i", false, "Open container's STDIN")
flagSet.StringVarP(&e.User, "user", "u", "", "Username or UID (format: <name|uid>[:<group|gid>])")
flagSet.StringArrayVarP(&e.Envs, "env", "e", []string{}, "Set environment variables")
}

// runExec is the entry of ExecCommand command.
Expand All @@ -72,6 +74,7 @@ func (e *ExecCommand) runExec(args []string) error {
AttachStdin: !e.Detach && e.Interactive,
Privileged: false,
User: e.User,
Env: e.Envs,
}

createResp, err := apiClient.ContainerCreateExec(ctx, id, createExecConfig)
Expand Down
11 changes: 9 additions & 2 deletions daemon/mgr/container_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/alibaba/pouch/pkg/user"

"github.com/docker/docker/pkg/stdcopy"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
)

Expand All @@ -27,11 +27,18 @@ func (mgr *ContainerManager) CreateExec(ctx context.Context, name string, config
return "", fmt.Errorf("container %s is not running", c.ID)
}

envs, err := mergeEnvSlice(c.Config.Env, config.Env)

if err != nil {
return "", err
}

execid := randomid.Generate()
execConfig := &ContainerExecConfig{
ExecID: execid,
ExecCreateConfig: *config,
ContainerID: c.ID,
Env: envs,
}

mgr.ExecProcesses.Put(execid, execConfig)
Expand Down Expand Up @@ -109,7 +116,7 @@ func (mgr *ContainerManager) StartExec(ctx context.Context, execid string, attac
Args: execConfig.Cmd,
Terminal: execConfig.Tty,
Cwd: cwd,
Env: c.Config.Env,
Env: execConfig.Env,
User: specs.User{
UID: uid,
GID: gid,
Expand Down
3 changes: 3 additions & 0 deletions daemon/mgr/container_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ type ContainerExecConfig struct {

// WaitForClean means exec process can be removed.
WaitForClean bool

// Environment variables
Env []string
}

// AttachConfig wraps some infos of attaching.
Expand Down
21 changes: 21 additions & 0 deletions test/api_container_exec_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,27 @@ func (suite *APIContainerExecSuite) TestContainerCreateExecOk(c *check.C) {
CheckRespStatus(c, resp, 201)
}

// TestContainerCreateExecWithEnvs tests execing containers with Envs.
func (suite *APIContainerExecSuite) TestContainerCreateExecWithEnvs(c *check.C) {
cname := "TestContainerCreateExecWithEnvs"

CreateBusyboxContainerOk(c, cname)
defer DelContainerForceMultyTime(c, cname)

StartContainerOk(c, cname)

obj := map[string]interface{}{
"Cmd": []string{"Env"},
"Detach": true,
"Env": []string{"Test=OK"},
}

body := request.WithJSONBody(obj)
resp, err := request.Post("/containers/"+cname+"/exec", body)
c.Assert(err, check.IsNil)
CheckRespStatus(c, resp, 201)
}

// TestContainerCreateExecNoCmd tests execing containers is OK.
func (suite *APIContainerExecSuite) TestContainerCreateExecNoCmd(c *check.C) {
cname := "TestContainerCreateExecNoCmd"
Expand Down
15 changes: 15 additions & 0 deletions test/cli_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ func (suite *PouchExecSuite) TestExecMultiCommands(c *check.C) {
}
}

// TestExecWithEnvs is to verify Exec with Envs.
func (suite *PouchExecSuite) TestExecWithEnvs(c *check.C) {
name := "exec-normal3"
res := command.PouchRun("run", "-d", "--name", name, busyboxImage, "sleep", "100000")
defer DelContainerForceMultyTime(c, name)

res.Assert(c, icmd.Success)

res = command.PouchRun("exec", "-e \"Test=OK\"", name, "env")

if out := res.Combined(); !strings.Contains(out, "Test=OK") {
c.Fatalf("unexpected output %s expected %s\n", out, name)
}
}

// TestExecEcho tests exec prints the output.
func (suite *PouchExecSuite) TestExecEcho(c *check.C) {
name := "TestExecEcho"
Expand Down

0 comments on commit c2553d6

Please sign in to comment.