-
Notifications
You must be signed in to change notification settings - Fork 0
/
dockerpc.go
284 lines (235 loc) · 5.88 KB
/
dockerpc.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
281
282
283
284
package dockerpc
import (
"bytes"
"crypto/tls"
"encoding/binary"
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"net/http/httputil"
"net/rpc"
"net/rpc/jsonrpc"
"net/url"
"os"
docker "github.com/fsouza/go-dockerclient"
)
//
// dockerpc Client data
//
type Client struct {
ID string // internal ID of docker container
name string // name to use
dockerImage string // docker image to use
stdErrBuf bytes.Buffer // buffer for storing stderr logs
endpoint string
output io.Writer
dockerClient *docker.Client
rpcClient *rpc.Client
clientConn net.Conn
DockerHostConfig *docker.HostConfig // host config parameters when starting docker
DockerConfig *docker.Config // config parameters when starting docker
}
// Create a new dockerpc Client client
func NewClient(name string, dockerImage string, endpoint string) *Client {
ret := &Client{
name: name,
dockerImage: dockerImage,
endpoint: endpoint,
}
return ret
}
// Close will remove the container, and close any client resources
func (d *Client) Close() error {
if d.dockerClient != nil {
opts := docker.RemoveContainerOptions{ID: d.ID, Force: true}
d.dockerClient.RemoveContainer(opts)
}
if d.rpcClient != nil {
err := d.rpcClient.Close()
d.rpcClient = nil
if err != nil {
return err
}
}
if d.rpcClient != nil {
err := d.rpcClient.Close()
d.rpcClient = nil
if err != nil {
return err
}
}
return nil
}
// AttachStreamingContainer will attach to a container.
func (d *Client) AttachStreamingContainer(opts docker.AttachToContainerOptions) error {
uri := "/containers/" + opts.Container + "/attach?" + queryString(opts)
u, err := url.Parse(d.endpoint + uri)
if err != nil {
return err
}
var rawConn net.Conn
if d.dockerClient.TLSConfig != nil {
rawConn, err = tls.Dial("tcp", u.Host, d.dockerClient.TLSConfig)
} else {
rawConn, err = net.Dial("tcp", u.Host)
}
if err != nil {
return err
}
var buf bytes.Buffer
req, err := http.NewRequest("POST", uri, &buf)
if err != nil {
return err
}
req.Header.Set("Content-Type", "plain/text")
req.Header.Set("Connection", "Upgrade")
req.Header.Set("Upgrade", "tcp")
clientconn := httputil.NewClientConn(rawConn, nil)
resp, err := clientconn.Do(req)
if err != nil {
log.Println("Error response from socket", resp)
return err
}
d.clientConn, _ = clientconn.Hijack()
return nil
}
// Call calls a method on the RPC server running on the client.
//
// `args` provide the arguments to the RPC call, results are stored in `reply`.
//
// Returns an error if it fails.
//
// At the end of each call, you can get the stderr log from the plugin via
// .StdError()
//
func (d *Client) Call(method string, args interface{}, reply interface{}) error {
d.stdErrBuf.Reset()
return d.rpcClient.Call(method, args, reply)
}
func (d *Client) StdError() string {
return string(d.stdErrBuf.Bytes())
}
//
// Start a docker container, and create a connection to /attach to it and send
// and receive RPC commands.
//
func (d *Client) Start() (err error) {
path := os.Getenv("DOCKER_CERT_PATH")
if path != "" {
ca := fmt.Sprintf("%s/ca.pem", path)
cert := fmt.Sprintf("%s/cert.pem", path)
key := fmt.Sprintf("%s/key.pem", path)
d.dockerClient, err = docker.NewTLSClient(d.endpoint, cert, key, ca)
} else {
d.dockerClient, err = docker.NewClient(d.endpoint)
}
if err != nil {
return err
}
defaultConfig := &docker.Config{
OpenStdin: true,
Image: d.dockerImage,
}
defaultHostConfig := &docker.HostConfig{}
opts := docker.CreateContainerOptions{
Config: defaultConfig,
HostConfig: defaultHostConfig,
}
if d.name != "" {
opts.Name = d.name
}
c, err := d.dockerClient.CreateContainer(opts)
if err != nil {
return err
}
d.ID = c.ID
err = d.dockerClient.StartContainer(c.ID, defaultHostConfig)
if err != nil {
return err
}
attachOpts := docker.AttachToContainerOptions{
Container: d.ID,
Stdout: true,
Stdin: true,
Stderr: true,
Stream: true,
}
err = d.AttachStreamingContainer(attachOpts)
if err != nil {
return err
}
pipes := &dockerPipes{
d.clientConn,
&d.stdErrBuf,
0,
0,
}
d.rpcClient = rpc.NewClientWithCodec(jsonrpc.NewClientCodec(pipes))
return nil
}
const (
STDIN = 0
STDOUT = 1
STDERR = 2
)
// todo close everything
type dockerPipes struct {
conn io.ReadWriteCloser
stdErrBuf *bytes.Buffer
bytesRemaining uint32
pipeName byte
}
func (pipe *dockerPipes) Read(b []byte) (int, error) {
// try to read n bytes from the connection
// this is the Docker header as described here:
// https://docs.docker.com/reference/api/docker_remote_api_v1.20/#attach-to-a-container
if pipe.bytesRemaining == 0 {
var p []byte = make([]byte, 1024)
c, err := pipe.conn.Read(p)
if err != nil {
return 0, err
}
if c != 8 {
return 0, errors.New("Expected 8 byte header from Docker")
}
var size uint32
sizeReader := bytes.NewReader(p[4:])
binary.Read(sizeReader, binary.BigEndian, &size)
pipe.pipeName = p[0]
pipe.bytesRemaining = size
}
pipeName := pipe.pipeName
c, err := pipe.conn.Read(b)
if err != nil {
return 0, err
}
// handle if the supplied byte array is smaller than the total size.
// if we haven't read the full # of bytes, then on the next Read()
// don't try to re-read the Docker header, simply continue reading the buffer.
bufSize := uint32(len(b))
if bufSize < pipe.bytesRemaining {
pipe.bytesRemaining = pipe.bytesRemaining - bufSize
} else {
pipe.bytesRemaining = 0
}
switch pipeName {
case STDIN:
return 0, nil
case STDOUT:
return c, nil
case STDERR:
// standard error - write it to buf.
pipe.stdErrBuf.Write(b[0:c])
return 0, nil
}
return 0, errors.New(fmt.Sprintf("Unsupported pipe: %d ", pipeName))
}
func (pipe *dockerPipes) Write(b []byte) (int, error) {
return pipe.conn.Write(b)
}
func (pipe *dockerPipes) Close() error {
return pipe.conn.Close()
}