This repository was archived by the owner on Aug 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 605
Expand file tree
/
Copy pathdocker.go
More file actions
343 lines (287 loc) · 8.24 KB
/
Copy pathdocker.go
File metadata and controls
343 lines (287 loc) · 8.24 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Package docker provides a layer on top of Docker's API via Kite handlers.
package docker
import (
"errors"
"fmt"
"io"
"strconv"
"strings"
"sync"
"time"
"unicode/utf8"
dockerclient "github.com/fsouza/go-dockerclient"
"github.com/koding/kite"
"github.com/rogpeppe/go-charset/charset"
_ "github.com/rogpeppe/go-charset/data"
)
// Docker defines the main configuration. One instance is running as one Docker
// client, so multiple instances of a Docker struct can connect to multiple
// Docker Servers.
type Docker struct {
client *dockerclient.Client
log kite.Logger
}
// New connects to a Docker Deamon specified with the given URL. It can be a
// TCP address or a UNIX socket.
func New(url string, log kite.Logger) *Docker {
// the error is returned only when the passed URL is not parsable via
// url.Parse, so we can safely neglect it
client, _ := dockerclient.NewClient(url)
return &Docker{
client: client,
log: log,
}
}
// Create creates a new container
func (d *Docker) Create(r *kite.Request) (interface{}, error) {
var params struct {
// Custom Container name
Name string
// Image name
Image string
}
if err := r.Args.One().Unmarshal(¶ms); err != nil {
return nil, err
}
if params.Image == "" {
return nil, errors.New("missing arg: image is empty")
}
// generate new name if name is missing
if params.Name == "" {
params.Name = r.Username + "-" + strconv.FormatInt(time.Now().UTC().UnixNano(), 10)
}
opts := dockerclient.CreateContainerOptions{
Name: params.Name,
Config: &dockerclient.Config{
Image: params.Image,
Tty: true,
// the following Attach fields need to be set so we can open a TTY
// instace via the Connect method.
AttachStdin: true,
AttachStdout: true,
AttachStderr: true,
},
}
container, err := d.client.CreateContainer(opts)
if err != nil {
return nil, err
}
return container.Name, nil
}
// Connect connects to an existing Container by spawning a new process and
// attaching to it.
func (d *Docker) Connect(r *kite.Request) (interface{}, error) {
var params struct {
// The ID of the container. This needs to be created and started before
// we can use Connect.
ID string
// Cmd contains the command which is executed and passed to the docker
// exec api. If empty "bash" is used.
Cmd string
SizeX, SizeY int
Remote Remote
}
if err := r.Args.One().Unmarshal(¶ms); err != nil {
return nil, err
}
if params.ID == "" {
return nil, errors.New("missing arg: container ID is empty")
}
cmd := []string{"bash"}
if params.Cmd != "" {
cmd = strings.Fields(params.Cmd)
}
createOpts := dockerclient.CreateExecOptions{
Container: params.ID,
Tty: true,
Cmd: cmd,
// we attach to anything, it's used in the same was as with `docker
// exec`
AttachStdout: true,
AttachStderr: true,
AttachStdin: true,
}
// now we create a new Exec instance. It will return us an exec ID which
// will be used to start the created exec instance
d.log.Info("Creating exec instance")
ex, err := d.client.CreateExec(createOpts)
if err != nil {
return nil, err
}
// these pipes are important as we are acting as a proxy between the
// Browser (client side) and Docker Deamon. For example, every input coming
// from the client side is being written into inWritePipe and this input is
// then read by docker exec via the inReadPipe.
inReadPipe, inWritePipe := io.Pipe()
outReadPipe, outWritePipe := io.Pipe()
opts := dockerclient.StartExecOptions{
Detach: false,
Tty: true,
OutputStream: outWritePipe,
ErrorStream: outWritePipe, // this is ok, that's how tty works
InputStream: inReadPipe,
}
// Control characters needs to be in ISO-8859 charset, so be sure that
// UTF-8 writes are translated to this charset, for more info:
// http://en.wikipedia.org/wiki/Control_character
controlSequence, err := charset.NewWriter("ISO-8859-1", inWritePipe)
if err != nil {
return nil, err
}
errCh := make(chan error)
closeCh := make(chan bool)
server := &Server{
Session: ex.ID,
remote: params.Remote,
out: outReadPipe,
in: inWritePipe,
controlSequence: controlSequence,
closeChan: closeCh,
client: d.client,
}
go func() {
d.log.Info("Starting exec instance '%s'", ex.ID)
err := d.client.StartExec(ex.ID, opts)
errCh <- err
// call the remote function that we ended the session
server.remote.SessionEnded.Call()
}()
go func() {
select {
case err := <-errCh:
if err != nil {
d.log.Error("startExec error: ", err)
}
case <-closeCh:
// once we close them the underlying hijack process in docker
// client package will end too, which will close the underlying
// connection once it's finished/returned.
inReadPipe.CloseWithError(errors.New("user closed the session"))
inWritePipe.CloseWithError(errors.New("user closed the session"))
outReadPipe.CloseWithError(errors.New("user closed the session"))
outWritePipe.CloseWithError(errors.New("user closed the session"))
}
}()
var once sync.Once
// Read the STDOUT from shell process and send to the connected client.
// https://github.com/koding/koding/commit/50cbd3609af93334150f7951dae49a23f71078f6
go func() {
buf := make([]byte, (1<<12)-utf8.UTFMax, 1<<12)
for {
n, err := server.out.Read(buf)
for n < cap(buf)-1 {
r, _ := utf8.DecodeLastRune(buf[:n])
if r != utf8.RuneError {
break
}
server.out.Read(buf[n : n+1])
n++
}
// we need to set it for the first time. Because "StartExec" is
// called in a goroutine and it's blocking there is now way we know
// when it's ready. Therefore we set the size only once when get an
// output. After that the client side is setting the TTY size with
// the Server.SetSize method, that is called everytime the client
// side sends a size command to us.
once.Do(func() {
// Y is height, X is width
err = d.client.ResizeExecTTY(ex.ID, int(params.SizeY), int(params.SizeX))
if err != nil {
fmt.Println("error resizing", err)
}
})
server.remote.Output.Call(string(filterInvalidUTF8(buf[:n])))
if err != nil {
break
}
}
d.log.Debug("Breaking out of for loop")
}()
d.log.Debug("Returning server")
return server, nil
}
// Stop stops a running container
func (d *Docker) Stop(r *kite.Request) (interface{}, error) {
var params struct {
// The ID of the container.
ID string
}
if err := r.Args.One().Unmarshal(¶ms); err != nil {
return nil, err
}
if params.ID == "" {
return nil, errors.New("missing arg: container is is empty")
}
if err := d.client.StopContainer(params.ID, 0); err != nil {
return nil, err
}
return true, nil
}
// Start starts a stopped container
func (d *Docker) Start(r *kite.Request) (interface{}, error) {
var params struct {
// The ID of the container.
ID string
}
if err := r.Args.One().Unmarshal(¶ms); err != nil {
return nil, err
}
if params.ID == "" {
return nil, errors.New("missing arg: container is is empty")
}
if err := d.client.StartContainer(params.ID, nil); err != nil {
return nil, err
}
return true, nil
}
// RemoveContainer removes a container
func (d *Docker) RemoveContainer(r *kite.Request) (interface{}, error) {
var params struct {
// The ID of the container.
ID string
// A flag that indicates whether Docker should remove the volumes
// associated to the container.
RemoveVolumes bool
// A flag that indicates whether Docker should remove the container
// even if it is currently running.
Force bool
}
if err := r.Args.One().Unmarshal(¶ms); err != nil {
return nil, err
}
if params.ID == "" {
return nil, errors.New("missing arg: container is is empty")
}
opts := dockerclient.RemoveContainerOptions{
ID: params.ID,
}
if err := d.client.RemoveContainer(opts); err != nil {
return nil, err
}
return true, nil
}
// List lists all available containers
func (d *Docker) List(r *kite.Request) (interface{}, error) {
filter := dockerclient.ListContainersOptions{
All: true,
}
return d.client.ListContainers(filter)
}
func filterInvalidUTF8(buf []byte) []byte {
i := 0
j := 0
for {
r, l := utf8.DecodeRune(buf[i:])
if l == 0 {
break
}
if r < 0xD800 {
if i != j {
copy(buf[j:], buf[i:i+l])
}
j += l
}
i += l
}
return buf[:j]
}