forked from AliyunContainerService/pouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
container.go
133 lines (103 loc) · 3.26 KB
/
container.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package client
import (
"bufio"
"net"
"net/url"
"github.com/alibaba/pouch/apis/types"
)
// ContainerCreate creates a new container based in the given configuration.
func (client *APIClient) ContainerCreate(config *types.ContainerConfig, hostConfig *types.HostConfig, containerName string) (*types.ContainerCreateResp, error) {
createConfig := types.ContainerConfigWrapper{
ContainerConfig: config,
HostConfig: hostConfig,
}
q := url.Values{}
if containerName != "" {
q.Set("name", containerName)
}
resp, err := client.post("/containers/create", q, createConfig)
if err != nil {
return nil, err
}
container := &types.ContainerCreateResp{}
err = decodeBody(container, resp.Body)
ensureCloseReader(resp)
return container, err
}
// ContainerStart starts a created container.
func (client *APIClient) ContainerStart(name, detachKeys string) error {
q := url.Values{}
if detachKeys != "" {
q.Set("detachKeys", detachKeys)
}
resp, err := client.post("/containers/"+name+"/start", q, nil)
ensureCloseReader(resp)
return err
}
// ContainerStop stops a container
func (client *APIClient) ContainerStop(name string) error {
resp, err := client.post("/containers/"+name+"/stop", nil, nil)
ensureCloseReader(resp)
return err
}
// ContainerList returns the list of containers.
func (client *APIClient) ContainerList() ([]*types.Container, error) {
resp, err := client.get("/containers/json", nil)
if err != nil {
return nil, err
}
containers := []*types.Container{}
err = decodeBody(&containers, resp.Body)
ensureCloseReader(resp)
return containers, err
}
// ContainerAttach attach a container
func (client *APIClient) ContainerAttach(name string, stdin bool) (net.Conn, *bufio.Reader, error) {
q := url.Values{}
if stdin {
q.Set("stdin", "1")
} else {
q.Set("stdin", "0")
}
header := map[string][]string{
"Content-Type": {"text/plain"},
}
return client.hijack("/containers/"+name+"/attach", q, nil, header)
}
// ContainerCreateExec creates exec process.
func (client *APIClient) ContainerCreateExec(name string, config *types.ExecCreateConfig) (*types.ExecCreateResponse, error) {
response, err := client.post("/containers/"+name+"/exec", url.Values{}, config)
if err != nil {
return nil, err
}
body := &types.ExecCreateResponse{}
decodeBody(body, response.Body)
ensureCloseReader(response)
return body, nil
}
// ContainerStartExec starts exec process.
func (client *APIClient) ContainerStartExec(execid string, config *types.ExecStartConfig) (net.Conn, *bufio.Reader, error) {
header := map[string][]string{
"Content-Type": {"text/plain"},
}
return client.hijack("/exec/"+execid+"/start", url.Values{}, config, header)
}
// ContainerGet returns the detailed information of container.
func (client *APIClient) ContainerGet(name string) (*types.ContainerJSON, error) {
resp, err := client.get("/containers/"+name+"/json", nil)
if err != nil {
return nil, err
}
container := types.ContainerJSON{}
err = decodeBody(&container, resp.Body)
ensureCloseReader(resp)
return &container, err
}
// ContainerRename renames a container.
func (client *APIClient) ContainerRename(id string, name string) error {
q := url.Values{}
q.Add("name", name)
resp, err := client.post("/containers/"+id+"/rename", q, nil)
ensureCloseReader(resp)
return err
}