forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
280 lines (248 loc) · 7.64 KB
/
client.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package dockerhelper
import (
"fmt"
"io"
"io/ioutil"
"time"
"golang.org/x/net/context"
"github.com/docker/docker/pkg/stdcopy"
"github.com/docker/engine-api/client"
"github.com/docker/engine-api/types"
"github.com/docker/engine-api/types/container"
"github.com/docker/engine-api/types/network"
)
const (
// defaultDockerOpTimeout is the default timeout of short running docker operations.
defaultDockerOpTimeout = 10 * time.Minute
)
// NewClient creates an instance of the client Interface, given a docker engine
// client
func NewClient(endpoint string, client *client.Client) Interface {
return &dockerClient{
endpoint: endpoint,
client: client,
}
}
type dockerClient struct {
endpoint string
client *client.Client
}
type operationTimeout struct {
err error
}
func (e operationTimeout) Error() string {
return fmt.Sprintf("operation timeout: %v", e.err)
}
func (c *dockerClient) Endpoint() string {
return c.endpoint
}
func (c *dockerClient) ServerVersion() (*types.Version, error) {
ctx, cancel := defaultContext()
defer cancel()
version, err := c.client.ServerVersion(ctx)
if ctxErr := contextError(ctx); ctxErr != nil {
return nil, ctxErr
}
return &version, err
}
func (c *dockerClient) Info() (*types.Info, error) {
ctx, cancel := defaultContext()
defer cancel()
info, err := c.client.Info(ctx)
if ctxErr := contextError(ctx); ctxErr != nil {
return nil, ctxErr
}
return &info, err
}
func (c *dockerClient) ContainerList(options types.ContainerListOptions) ([]types.Container, error) {
ctx, cancel := defaultContext()
defer cancel()
containers, err := c.client.ContainerList(ctx, options)
if ctxErr := contextError(ctx); ctxErr != nil {
return nil, ctxErr
}
return containers, err
}
func (c *dockerClient) ContainerCreate(config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, name string) (*types.ContainerCreateResponse, error) {
ctx, cancel := defaultContext()
defer cancel()
response, err := c.client.ContainerCreate(ctx, config, hostConfig, networkingConfig, name)
if ctxErr := contextError(ctx); ctxErr != nil {
return nil, ctxErr
}
return &response, err
}
func (c *dockerClient) ContainerInspect(container string) (*types.ContainerJSON, error) {
ctx, cancel := defaultContext()
defer cancel()
response, err := c.client.ContainerInspect(ctx, container)
if ctxErr := contextError(ctx); ctxErr != nil {
return nil, ctxErr
}
return &response, err
}
func (c *dockerClient) ContainerRemove(container string, options types.ContainerRemoveOptions) error {
ctx, cancel := defaultContext()
defer cancel()
err := c.client.ContainerRemove(ctx, container, options)
if ctxErr := contextError(ctx); ctxErr != nil {
return ctxErr
}
return err
}
func (c *dockerClient) ContainerLogs(container string, options types.ContainerLogsOptions, stdOut, stdErr io.Writer) error {
ctx, cancel := defaultContext()
defer cancel()
response, err := c.client.ContainerLogs(ctx, container, options)
if ctxErr := contextError(ctx); ctxErr != nil {
return ctxErr
}
if err != nil {
return err
}
defer response.Close()
return redirectResponseToOutputStream(stdOut, stdErr, response)
}
func (c *dockerClient) ContainerStart(container string) error {
ctx, cancel := defaultContext()
defer cancel()
err := c.client.ContainerStart(ctx, container)
if ctxErr := contextError(ctx); ctxErr != nil {
return ctxErr
}
return err
}
func (c *dockerClient) ContainerStop(container string, timeout int) error {
ctx, cancel := defaultContext()
defer cancel()
err := c.client.ContainerStop(ctx, container, timeout)
if ctxErr := contextError(ctx); ctxErr != nil {
return ctxErr
}
return err
}
func (c *dockerClient) ContainerWait(container string) (int, error) {
ctx, cancel := defaultContext()
defer cancel()
rc, err := c.client.ContainerWait(ctx, container)
if ctxErr := contextError(ctx); ctxErr != nil {
return 0, ctxErr
}
return rc, err
}
func (c *dockerClient) CopyToContainer(container string, dest string, src io.Reader, options types.CopyToContainerOptions) error {
return c.client.CopyToContainer(context.Background(), container, dest, src, options)
}
func (c *dockerClient) CopyFromContainer(container string, src string) (io.ReadCloser, error) {
response, _, err := c.client.CopyFromContainer(context.Background(), container, src)
return response, err
}
func (c *dockerClient) ContainerExecCreate(container string, config types.ExecConfig) (*types.ContainerExecCreateResponse, error) {
ctx, cancel := defaultContext()
defer cancel()
response, err := c.client.ContainerExecCreate(ctx, container, config)
if ctxErr := contextError(ctx); ctxErr != nil {
return nil, ctxErr
}
if err != nil {
return nil, err
}
return &response, err
}
func (c *dockerClient) ContainerExecAttach(execID string, stdIn io.Reader, stdOut, stdErr io.Writer) error {
ctx, cancel := defaultContext()
defer cancel()
response, err := c.client.ContainerExecAttach(ctx, execID, types.ExecConfig{
AttachStdin: stdIn != nil,
AttachStdout: true,
AttachStderr: true,
})
if ctxErr := contextError(ctx); ctxErr != nil {
return ctxErr
}
if err != nil {
return err
}
defer response.Close()
return holdHijackedConnection(stdIn, stdOut, stdErr, response)
}
func (c *dockerClient) ContainerExecInspect(execID string) (*types.ContainerExecInspect, error) {
ctx, cancel := defaultContext()
defer cancel()
response, err := c.client.ContainerExecInspect(ctx, execID)
if ctxErr := contextError(ctx); ctxErr != nil {
return nil, ctxErr
}
return &response, err
}
func (c *dockerClient) ImageInspectWithRaw(imageID string, getSize bool) (*types.ImageInspect, []byte, error) {
ctx, cancel := defaultContext()
defer cancel()
image, raw, err := c.client.ImageInspectWithRaw(ctx, imageID, getSize)
if ctxErr := contextError(ctx); ctxErr != nil {
return nil, nil, ctxErr
}
return &image, raw, err
}
func (c *dockerClient) ImagePull(ref string, options types.ImagePullOptions, writer io.Writer) error {
ctx := context.Background()
response, err := c.client.ImagePull(ctx, ref, options)
if err != nil {
return err
}
defer response.Close()
_, err = io.Copy(writer, response)
return err
}
func defaultContext() (context.Context, context.CancelFunc) {
return context.WithTimeout(context.Background(), defaultDockerOpTimeout)
}
func contextError(ctx context.Context) error {
if ctx.Err() == context.DeadlineExceeded {
return operationTimeout{err: ctx.Err()}
}
return ctx.Err()
}
// redirectResponseToOutputStream redirect the response stream to stdout and stderr.
func redirectResponseToOutputStream(outputStream, errorStream io.Writer, resp io.Reader) error {
if outputStream == nil {
outputStream = ioutil.Discard
}
if errorStream == nil {
errorStream = ioutil.Discard
}
_, err := stdcopy.StdCopy(outputStream, errorStream, resp)
return err
}
// holdHijackedConnection holds the HijackedResponse, redirects the inputStream to the connection, and redirects the response
// stream to stdout and stderr.
func holdHijackedConnection(inputStream io.Reader, outputStream, errorStream io.Writer, resp types.HijackedResponse) error {
receiveStdout := make(chan error)
if outputStream != nil || errorStream != nil {
go func() {
receiveStdout <- redirectResponseToOutputStream(outputStream, errorStream, resp.Reader)
}()
}
sendStdin := make(chan error, 1)
go func() {
defer resp.CloseWrite()
if inputStream != nil {
_, err := io.Copy(resp.Conn, inputStream)
sendStdin <- err
return
}
sendStdin <- nil
}()
select {
case err := <-receiveStdout:
return err
case sendErr := <-sendStdin:
if sendErr != nil {
return sendErr
}
if outputStream != nil || errorStream != nil {
return <-receiveStdout
}
}
return nil
}