/
container_exec.go
63 lines (51 loc) · 1.85 KB
/
container_exec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package client
import (
"bufio"
"context"
"net"
"net/url"
"strconv"
"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) {
if config.Detach {
_, err := client.post(ctx, "/exec/"+execID+"/start", url.Values{}, config, nil)
return nil, nil, err
}
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
}
// ContainerExecResize changes the size of the tty for an exec process running inside a container.
func (client *APIClient) ContainerExecResize(ctx context.Context, execID string, options types.ResizeOptions) error {
query := url.Values{}
query.Set("h", strconv.Itoa(int(options.Height)))
query.Set("w", strconv.Itoa(int(options.Width)))
resp, err := client.post(ctx, "/exec/"+execID+"/resize", query, nil, nil)
ensureCloseReader(resp)
return err
}