Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions cli/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,17 @@ func (e *ExecCommand) runExec(args []string) error {
}

wg.Wait()

execInfo, err := apiClient.ContainerExecInspect(ctx, createResp.ID)
if err != nil {
return err
}

code := execInfo.ExitCode
if code != 0 {
return ExitError{Code: int(code)}
}

return nil
}

Expand Down
23 changes: 0 additions & 23 deletions client/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,6 @@ func (client *APIClient) ContainerAttach(ctx context.Context, name string, stdin
return client.hijack(ctx, "/containers/"+name+"/attach", q, nil, header)
}

// ContainerCreateExec creates exec process.
func (client *APIClient) ContainerCreateExec(ctx context.Context, name string, config *types.ExecCreateConfig) (*types.ExecCreateResp, error) {
response, err := client.post(ctx, "/containers/"+name+"/exec", url.Values{}, config, nil)
if err != nil {
return nil, err
}

body := &types.ExecCreateResp{}
decodeBody(body, response.Body)
ensureCloseReader(response)

return body, nil
}

// ContainerStartExec starts exec process.
func (client *APIClient) ContainerStartExec(ctx context.Context, execid string, config *types.ExecStartConfig) (net.Conn, *bufio.Reader, error) {
header := map[string][]string{
"Content-Type": {"text/plain"},
}

return client.hijack(ctx, "/exec/"+execid+"/start", url.Values{}, config, header)
}

// ContainerUpgrade upgrade a container with new image and args.
func (client *APIClient) ContainerUpgrade(ctx context.Context, name string, config types.ContainerConfig, hostConfig *types.HostConfig) error {
// TODO
Expand Down
47 changes: 47 additions & 0 deletions client/container_exec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package client

import (
"bufio"
"context"
"net"
"net/url"

"github.com/alibaba/pouch/apis/types"
)

// ContainerCreateExec creates exec process.
func (client *APIClient) ContainerCreateExec(ctx context.Context, name string, config *types.ExecCreateConfig) (*types.ExecCreateResp, error) {
response, err := client.post(ctx, "/containers/"+name+"/exec", url.Values{}, config, nil)
if err != nil {
return nil, err
}

body := &types.ExecCreateResp{}
err = decodeBody(body, response.Body)
ensureCloseReader(response)

return body, err
}

// ContainerStartExec starts exec process.
func (client *APIClient) ContainerStartExec(ctx context.Context, execid string, config *types.ExecStartConfig) (net.Conn, *bufio.Reader, error) {
header := map[string][]string{
"Content-Type": {"text/plain"},
}

return client.hijack(ctx, "/exec/"+execid+"/start", url.Values{}, config, header)
}

// ContainerExecInspect get exec info with a specified exec id.
func (client *APIClient) ContainerExecInspect(ctx context.Context, execid string) (*types.ContainerExecInspect, error) {
resp, err := client.get(ctx, "/exec/"+execid+"/json", nil, nil)
if err != nil {
return nil, err
}

body := &types.ContainerExecInspect{}
err = decodeBody(body, resp.Body)
ensureCloseReader(resp)

return body, err
}
1 change: 1 addition & 0 deletions client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type ContainerAPIClient interface {
ContainerAttach(ctx context.Context, name string, stdin bool) (net.Conn, *bufio.Reader, error)
ContainerCreateExec(ctx context.Context, name string, config *types.ExecCreateConfig) (*types.ExecCreateResp, error)
ContainerStartExec(ctx context.Context, execid string, config *types.ExecStartConfig) (net.Conn, *bufio.Reader, error)
ContainerExecInspect(ctx context.Context, execid string) (*types.ContainerExecInspect, error)
ContainerGet(ctx context.Context, name string) (*types.ContainerJSON, error)
ContainerRename(ctx context.Context, id string, name string) error
ContainerRestart(ctx context.Context, name string, timeout string) error
Expand Down
10 changes: 10 additions & 0 deletions test/cli_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,13 @@ func (suite *PouchExecSuite) TestExecUlimit(c *check.C) {
out := command.PouchRun("exec", name, "sh", "-c", "ulimit -p").Stdout()
c.Assert(out, check.Equals, "256\n")
}

// TestExecExitCode test exit code after exec process exit.
func (suite *PouchExecSuite) TestExecExitCode(c *check.C) {
name := "TestExecExitCode"
command.PouchRun("run", "-d", "--name", name, busyboxImage, "top").Assert(c, icmd.Success)
defer DelContainerForceMultyTime(c, name)

command.PouchRun("exec", name, "sh", "-c", "exit 101").Assert(c, icmd.Expected{ExitCode: 101})
command.PouchRun("exec", name, "sh", "-c", "exit 0").Assert(c, icmd.Success)
}