forked from docker-archive/classicswarm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdockerserver.go
443 lines (392 loc) · 10.8 KB
/
dockerserver.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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
package backends
import (
"encoding/json"
"fmt"
"github.com/docker/libswarm"
"github.com/docker/libswarm/utils"
"github.com/dotcloud/docker/api"
"github.com/dotcloud/docker/pkg/version"
dockerContainerConfig "github.com/dotcloud/docker/runconfig"
dockerutils "github.com/dotcloud/docker/utils"
"github.com/gorilla/mux"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"time"
)
func DockerServer() libswarm.Sender {
backend := libswarm.NewServer()
backend.OnVerb(libswarm.Spawn, libswarm.Handler(func(ctx *libswarm.Message) error {
instance := utils.Task(func(in libswarm.Receiver, out libswarm.Sender) {
url := "tcp://localhost:4243"
if len(ctx.Args) > 0 {
url = ctx.Args[0]
}
err := listenAndServe(url, out)
if err != nil {
fmt.Printf("listenAndServe: %v", err)
}
})
_, err := ctx.Ret.Send(&libswarm.Message{Verb: libswarm.Ack, Ret: instance})
return err
}))
return backend
}
type HttpApiFunc func(out libswarm.Sender, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error
func listenAndServe(urlStr string, out libswarm.Sender) error {
fmt.Println("Starting Docker server...")
r, err := createRouter(out)
if err != nil {
return err
}
parsedUrl, err := url.Parse(urlStr)
if err != nil {
return err
}
var hostAndPath string
// For Unix sockets we need to capture the path as well as the host
if parsedUrl.Scheme == "unix" {
hostAndPath = "/" + parsedUrl.Host + parsedUrl.Path
} else {
hostAndPath = parsedUrl.Host
}
l, err := net.Listen(parsedUrl.Scheme, hostAndPath)
if err != nil {
return err
}
httpSrv := http.Server{Addr: hostAndPath, Handler: r}
return httpSrv.Serve(l)
}
func ping(out libswarm.Sender, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
_, err := w.Write([]byte{'O', 'K'})
return err
}
type containerJson struct {
Config *dockerContainerConfig.Config
HostConfig *dockerContainerConfig.HostConfig
ID string
Created string
Driver string
ExecDriver string
NetworkSettings struct {
Bridge string
Gateway string
IPAddress string
IPPrefixLen int
Ports map[string][]map[string]string
}
State struct {
Running bool
StartedAt string
FinishedAt string
ExitCode int
Paused bool
Pid int
}
Name string
Args []string
ProcessLbael string
ResolvConfPath string
HostnamePath string
HostsPath string
MountLabel string
Volumes map[string]string
VolumesRW map[string]bool
}
func getContainerJson(out libswarm.Sender, containerID string) (containerJson, error) {
o := libswarm.AsClient(out)
_, containerOut, err := o.Attach(containerID)
if err != nil {
return containerJson{}, err
}
container := libswarm.AsClient(containerOut)
responseJson, err := container.Get()
if err != nil {
return containerJson{}, err
}
var response containerJson
if err = json.Unmarshal([]byte(responseJson), &response); err != nil {
return containerJson{}, err
}
return response, nil
}
func getContainerInfo(out libswarm.Sender, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
container, err := getContainerJson(out, vars["name"])
if err != nil {
return err
}
return writeJSON(w, http.StatusOK, container)
}
func getContainersJSON(out libswarm.Sender, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
if err := r.ParseForm(); err != nil {
return err
}
o := libswarm.AsClient(out)
names, err := o.Ls()
if err != nil {
return err
}
var responses []interface{}
for _, name := range names {
response, err := getContainerJson(out, name)
if err != nil {
return err
}
created, err := time.Parse(time.RFC3339, response.Created)
if err != nil {
return err
}
var state string
if response.State.Running {
state = "Up"
} else {
state = fmt.Sprintf("Exited (%d)", response.State.ExitCode)
}
type port struct {
IP string
PrivatePort int
PublicPort int
Type string
}
var ports []port
for p, mappings := range response.NetworkSettings.Ports {
var portnum int
var proto string
_, err := fmt.Sscanf(p, "%d/%s", &portnum, &proto)
if err != nil {
return err
}
if len(mappings) > 0 {
for _, mapping := range mappings {
hostPort, err := strconv.Atoi(mapping["HostPort"])
if err != nil {
return err
}
newport := port{
IP: mapping["HostIp"],
PrivatePort: portnum,
PublicPort: hostPort,
Type: proto,
}
ports = append(ports, newport)
}
} else {
newport := port{
PublicPort: portnum,
Type: proto,
}
ports = append(ports, newport)
}
}
responses = append(responses, map[string]interface{}{
"Id": response.ID,
"Command": strings.Join(response.Config.Cmd, " "),
"Created": created.Unix(),
"Image": response.Config.Image,
"Names": []string{response.Name},
"Ports": ports,
"Status": state,
})
}
return writeJSON(w, http.StatusOK, responses)
}
func postContainersCreate(out libswarm.Sender, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
if err := r.ParseForm(); err != nil {
return nil
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
name := r.Form.Get("name")
if name != "" {
var reqJson map[string]interface{}
if err = json.Unmarshal(body, &reqJson); err != nil {
return err
}
reqJson["name"] = name
body, err = json.Marshal(reqJson)
if err != nil {
return err
}
}
container, err := libswarm.AsClient(out).Spawn(string(body))
if err != nil {
return err
}
responseJson, err := container.Get()
if err != nil {
return err
}
var response struct{ Id string }
if err = json.Unmarshal([]byte(responseJson), &response); err != nil {
return err
}
return writeJSON(w, http.StatusCreated, response)
}
func postContainersStart(out libswarm.Sender, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
if vars == nil {
return fmt.Errorf("Missing parameter")
}
// TODO: r.Body
name := vars["name"]
_, containerOut, err := libswarm.AsClient(out).Attach(name)
container := libswarm.AsClient(containerOut)
if err != nil {
return err
}
if err := container.Start(); err != nil {
return err
}
w.WriteHeader(http.StatusNoContent)
return nil
}
func postContainersStop(out libswarm.Sender, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
if vars == nil {
return fmt.Errorf("Missing parameter")
}
name := vars["name"]
_, containerOut, err := libswarm.AsClient(out).Attach(name)
container := libswarm.AsClient(containerOut)
if err != nil {
return err
}
if err := container.Stop(); err != nil {
return err
}
w.WriteHeader(http.StatusNoContent)
return nil
}
func hijackServer(w http.ResponseWriter) (io.ReadCloser, io.Writer, error) {
conn, _, err := w.(http.Hijacker).Hijack()
if err != nil {
return nil, nil, err
}
// Flush the options to make sure the client sets the raw mode
conn.Write([]byte{})
return conn, conn, nil
}
func postContainersAttach(out libswarm.Sender, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
if err := r.ParseForm(); err != nil {
return err
}
if vars == nil {
return fmt.Errorf("Missing parameter")
}
inStream, outStream, err := hijackServer(w)
if err != nil {
return err
}
defer func() {
if tcpc, ok := inStream.(*net.TCPConn); ok {
tcpc.CloseWrite()
} else {
inStream.Close()
}
}()
defer func() {
if tcpc, ok := outStream.(*net.TCPConn); ok {
tcpc.CloseWrite()
} else if closer, ok := outStream.(io.Closer); ok {
closer.Close()
}
}()
fmt.Fprintf(outStream, "HTTP/1.1 200 OK\r\nContent-Type: application/vnd.docker.raw-stream\r\n\r\n")
// TODO: if a TTY, then no multiplexing is done
errStream := dockerutils.NewStdWriter(outStream, dockerutils.Stderr)
outStream = dockerutils.NewStdWriter(outStream, dockerutils.Stdout)
_, containerOut, err := libswarm.AsClient(out).Attach(vars["name"])
if err != nil {
return err
}
container := libswarm.AsClient(containerOut)
containerR, _, err := container.Attach("")
var tasks sync.WaitGroup
go func() {
defer tasks.Done()
err := utils.DecodeStream(outStream, containerR, "stdout")
if err != nil {
fmt.Printf("decodestream: %v\n", err)
}
}()
tasks.Add(1)
go func() {
defer tasks.Done()
err := utils.DecodeStream(errStream, containerR, "stderr")
if err != nil {
fmt.Printf("decodestream: %v\n", err)
}
}()
tasks.Add(1)
tasks.Wait()
return nil
}
func postContainersWait(out libswarm.Sender, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
if vars == nil {
return fmt.Errorf("Missing parameter")
}
// TODO: this should wait for container to end out output correct
// exit status
return writeJSON(w, http.StatusOK, map[string]interface{}{
"StatusCode": "0",
})
}
func createRouter(out libswarm.Sender) (*mux.Router, error) {
r := mux.NewRouter()
m := map[string]map[string]HttpApiFunc{
"GET": {
"/_ping": ping,
"/containers/json": getContainersJSON,
"/containers/{name}/json": getContainerInfo,
},
"POST": {
"/containers/create": postContainersCreate,
"/containers/{name:.*}/attach": postContainersAttach,
"/containers/{name:.*}/start": postContainersStart,
"/containers/{name:.*}/stop": postContainersStop,
"/containers/{name:.*}/wait": postContainersWait,
},
"DELETE": {},
"OPTIONS": {},
}
for method, routes := range m {
for route, fct := range routes {
localRoute := route
localFct := fct
localMethod := method
f := makeHttpHandler(out, localMethod, localRoute, localFct, version.Version("0.11.0"))
// add the new route
if localRoute == "" {
r.Methods(localMethod).HandlerFunc(f)
} else {
r.Path("/v{version:[0-9.]+}" + localRoute).Methods(localMethod).HandlerFunc(f)
r.Path(localRoute).Methods(localMethod).HandlerFunc(f)
}
}
}
return r, nil
}
func makeHttpHandler(out libswarm.Sender, localMethod string, localRoute string, handlerFunc HttpApiFunc, dockerVersion version.Version) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// log the request
fmt.Printf("Calling %s %s\n", localMethod, localRoute)
version := version.Version(mux.Vars(r)["version"])
if version == "" {
version = api.APIVERSION
}
if err := handlerFunc(out, version, w, r, mux.Vars(r)); err != nil {
fmt.Printf("Error: %s", err)
}
}
}
func writeJSON(w http.ResponseWriter, code int, v interface{}) error {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
enc := json.NewEncoder(w)
return enc.Encode(v)
}