-
Notifications
You must be signed in to change notification settings - Fork 29
/
docker.go
540 lines (481 loc) · 16.6 KB
/
docker.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
package main
// docker volume plugin for providing dotmesh volumes to docker via e.g.
// docker run -v name:/path --volume-driver=dm
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"path/filepath"
"time"
)
const PLUGINS_DIR = "/run/docker/plugins"
const DM_SOCKET = PLUGINS_DIR + "/dm.sock"
type ResponseImplements struct {
// A response to the Plugin.Activate request
Implements []string
}
type RequestCreate struct {
// A request to create a volume for Docker
Name string
Opts map[string]string
}
type RequestMount struct {
// A request to mount a volume for Docker
Name string
}
type RequestGet struct {
// A request to get a volume for Docker
Name string
}
type RequestRemove struct {
// A request to remove a volume for Docker
Name string
}
type ResponseSimple struct {
// A response which only indicates if there was an error or not
Err string
}
type ResponseMount struct {
// A response to the VolumeDriver.Mount request
Mountpoint string
Err string
}
type ResponseListVolume struct {
// Used in the JSON representation of ResponseList
Name string
Mountpoint string
Status map[string]string // TODO actually start using the status to report on things in dm
}
type ResponseList struct {
// A response which enumerates volumes for VolumeDriver.List
Volumes []ResponseListVolume
Err string
}
type ResponseGet struct {
// A response which enumerates volumes for VolumeDriver.Get
Volume ResponseListVolume
Err string
}
// create a symlink from /dotmesh/:name[@:branch] into /dmfs/:filesystemId
func newContainerMountSymlink(name VolumeName, filesystemId string, subvolume string) (string, error) {
log.Printf("[newContainerMountSymlink] name=%+v, fsId=%s.%s", name, filesystemId, subvolume)
if _, err := os.Stat(CONTAINER_MOUNT_PREFIX); err != nil {
if os.IsNotExist(err) {
if err := os.MkdirAll(CONTAINER_MOUNT_PREFIX, 0700); err != nil {
log.Printf("[newContainerMountSymlink] error creating prefix %s: %+v", CONTAINER_MOUNT_PREFIX, err)
return "", err
}
} else {
log.Printf("[newContainerMountSymlink] error statting prefix %s: %+v", CONTAINER_MOUNT_PREFIX, err)
return "", err
}
}
parent := containerMntParent(name)
if _, err := os.Stat(parent); err != nil {
if os.IsNotExist(err) {
if err := os.MkdirAll(parent, 0700); err != nil {
log.Printf("[newContainerMountSymlink] error creating parent %s: %+v", parent, err)
return "", err
}
} else {
log.Printf("[newContainerMountSymlink] error statting parent %s: %+v", parent, err)
return "", err
}
}
// Raw ZFS mountpoint
mountpoint := containerMnt(name)
// Only create symlink if it doesn't already exist. Otherwise just hand it back
// (the target of it may have been updated elsewhere).
if stat, err := os.Stat(mountpoint); err != nil {
if os.IsNotExist(err) {
err = os.Symlink(mnt(filesystemId), mountpoint)
if err != nil {
log.Printf("[newContainerMountSymlink] error symlinking mountpoint %s: %+v", mountpoint, err)
return "", err
}
} else {
log.Printf("[newContainerMountSymlink] error statting mountpoint %s: %+v", mountpoint, err)
return "", err
}
} else {
// FIXME: Check it really is a symlink. Various bugs lead to a raw directory being here, which then silently breaks things.
log.Printf("[newContainerMountSymlink] mountpoint %s found: %+v", mountpoint, stat)
}
// ...and we return either that raw mountpoint, or a subvolume within
result := containerMntSubvolume(name, subvolume)
// Do we need to create the subvolume directory?
if _, err := os.Stat(result); err != nil {
if os.IsNotExist(err) {
if err := os.MkdirAll(result, 0777); err != nil {
log.Printf("[newContainerMountSymlink] error creating subdot %s: %+v", result, err)
return "", err
}
} else {
log.Printf("[newContainerMountSymlink] error statting subdot %s: %+v", result, err)
return "", err
}
}
log.Printf("[newContainerMountSymlink] returning %s", result)
return result, nil
}
func (state *InMemoryState) mustCleanupSocket() {
if _, err := os.Stat(PLUGINS_DIR); err != nil {
if err := os.MkdirAll(PLUGINS_DIR, 0700); err != nil {
log.Fatalf("Could not make plugin directory %s: %v", PLUGINS_DIR, err)
}
}
if _, err := os.Stat(DM_SOCKET); err == nil {
if err = os.Remove(DM_SOCKET); err != nil {
log.Fatalf("Could not clean up existing socket at %s: %v", DM_SOCKET, err)
}
}
}
// Annotate a context with admin-level authorization.
func AdminContext(ctx context.Context) context.Context {
ctx = context.WithValue(ctx, "authenticated-user-id", ADMIN_USER_UUID)
return ctx
}
func (state *InMemoryState) runPlugin() {
log.Printf("[runPlugin] Starting dm plugin with socket: %s", DM_SOCKET)
// docker acts like the admin user, for now.
ctx := AdminContext(context.Background())
state.mustCleanupSocket()
http.HandleFunc("/Plugin.Activate", func(w http.ResponseWriter, r *http.Request) {
log.Print("<= /Plugin.Activate")
responseJSON, _ := json.Marshal(&ResponseImplements{
Implements: []string{"VolumeDriver"},
})
log.Printf("=> %s", string(responseJSON))
w.Write(responseJSON)
})
http.HandleFunc("/VolumeDriver.Create", func(w http.ResponseWriter, r *http.Request) {
log.Print("<= /VolumeDriver.Create")
requestJSON, err := ioutil.ReadAll(r.Body)
if err != nil {
writeResponseErr(err, w)
return
}
request := new(RequestCreate)
err = json.Unmarshal(requestJSON, request)
if err != nil {
writeResponseErr(err, w)
return
}
namespace, localName, _, err := parseNamespacedVolumeWithSubvolumes(request.Name)
if err != nil {
writeResponseErr(err, w)
return
}
name := VolumeName{namespace, localName}
// for now, just name the volumes as requested by the user. later,
// adding ids and per-fs metadata may be useful.
if _, err := state.procureFilesystem(ctx, name); err != nil {
writeResponseErr(err, w)
return
}
// TODO acquire containerRuntimeLock and update our state and etcd with
// the fact that a container will soon be running on this volume...
writeResponseOK(w)
// asynchronously notify dotmesh that the containers running on a
// volume may have changed
go func() { state.fetchRelatedContainersChan <- true }()
})
http.HandleFunc("/VolumeDriver.Remove", func(w http.ResponseWriter, r *http.Request) {
/*
We do not actually want to remove the dm volume when Docker
references to them are removed.
This is a no-op.
*/
writeResponseOK(w)
// asynchronously notify dotmesh that the containers running on a
// volume may have changed
go func() { state.fetchRelatedContainersChan <- true }()
})
http.HandleFunc("/VolumeDriver.Path", func(w http.ResponseWriter, r *http.Request) {
// TODO: Only return the path if it's actually active on the local host.
log.Print("<= /VolumeDriver.Path")
requestJSON, err := ioutil.ReadAll(r.Body)
if err != nil {
writeResponseErr(err, w)
return
}
request := new(RequestMount)
if err := json.Unmarshal(requestJSON, request); err != nil {
writeResponseErr(err, w)
return
}
namespace, localName, subvolume, err := parseNamespacedVolumeWithSubvolumes(request.Name)
if err != nil {
writeResponseErr(err, w)
return
}
name := VolumeName{namespace, localName}
mountPoint := containerMntSubvolume(name, subvolume)
log.Printf("Mountpoint for %s: %s", name, mountPoint)
responseJSON, _ := json.Marshal(&ResponseMount{
Mountpoint: mountPoint,
Err: "",
})
log.Printf("=> %s", string(responseJSON))
w.Write(responseJSON)
// asynchronously notify dotmesh that the containers running on a
// volume may have changed
go func() { state.fetchRelatedContainersChan <- true }()
})
http.HandleFunc("/VolumeDriver.Mount", func(w http.ResponseWriter, r *http.Request) {
// TODO acquire containerRuntimeLock and update our state and etcd with
// the fact that a container will soon be running on this volume...
log.Print("<= /VolumeDriver.Mount")
requestJSON, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Fatalf("Unable to read response body %s", err)
}
request := new(RequestMount)
if err := json.Unmarshal(requestJSON, request); err != nil {
writeResponseErr(err, w)
return
}
namespace, localName, subvolume, err := parseNamespacedVolumeWithSubvolumes(request.Name)
if err != nil {
writeResponseErr(err, w)
return
}
name := VolumeName{namespace, localName}
filesystemId, err := state.procureFilesystem(ctx, name)
if err != nil {
writeResponseErr(err, w)
return
}
mountpoint, err := newContainerMountSymlink(name, filesystemId, subvolume)
if err != nil {
writeResponseErr(err, w)
return
}
// Allow things that don't want containers to start during their
// operations to delay the start of a container. Commented out because
// it causes a deadlock.
/*
state.containersLock.Lock()
defer state.containersLock.Unlock()
*/
log.Printf("Mountpoint for %s: %s", name, mountpoint)
responseJSON, _ := json.Marshal(&ResponseMount{
Mountpoint: mountpoint,
Err: "",
})
log.Printf("=> %s", string(responseJSON))
w.Write(responseJSON)
// asynchronously notify dotmesh that the containers running on a
// volume may have changed
go func() { state.fetchRelatedContainersChan <- true }()
go func() {
// Do this again a second later, to cope with Docker's lack of
// immediate consistency
time.Sleep(time.Second)
state.fetchRelatedContainersChan <- true
}()
})
http.HandleFunc("/VolumeDriver.Unmount", func(w http.ResponseWriter, r *http.Request) {
// TODO acquire containerRuntimeLock and update our state and etcd with
// the fact that one less container is now running on this volume...
writeResponseOK(w)
// asynchronously notify dotmesh that the containers running on a
// volume may have changed
go func() { state.fetchRelatedContainersChan <- true }()
go func() {
// Do this again a second later, to cope with Docker's lack of
// immediate consistency
time.Sleep(time.Second)
state.fetchRelatedContainersChan <- true
}()
})
http.HandleFunc("/VolumeDriver.List", func(w http.ResponseWriter, r *http.Request) {
log.Print("<= /VolumeDriver.List")
var response = ResponseList{
Err: "",
}
for _, fs := range (*state).registry.Filesystems() {
log.Printf("Mountpoint for %s: %s", fs, containerMnt(fs))
response.Volumes = append(response.Volumes, ResponseListVolume{
Name: fs.StringWithoutAdmin(),
Mountpoint: containerMnt(fs),
})
}
responseJSON, _ := json.Marshal(response)
log.Printf("=> %s", string(responseJSON))
w.Write(responseJSON)
// asynchronously notify dotmesh that the containers running on a
// volume may have changed
go func() { state.fetchRelatedContainersChan <- true }()
})
http.HandleFunc("/VolumeDriver.Get", func(w http.ResponseWriter, r *http.Request) {
log.Print("<= /VolumeDriver.Get")
requestJSON, err := ioutil.ReadAll(r.Body)
if err != nil {
writeResponseErr(err, w)
return
}
request := new(RequestMount)
if err := json.Unmarshal(requestJSON, request); err != nil {
writeResponseErr(err, w)
return
}
namespace, localName, subvolume, err := parseNamespacedVolumeWithSubvolumes(request.Name)
if err != nil {
writeResponseErr(err, w)
return
}
name := VolumeName{namespace, localName}
var response = ResponseGet{
Err: "",
}
// Technically, fetching the TopLevelFilesystem object from the
// registry isn't necessary, but maybe one day we'll get additional
// Status information from that call that we want to use here, so
// leaving it in for now rather than just hand-constructing the
// response from the name.
fs, err := (*state).registry.GetByName(name)
if err != nil {
response.Err = fmt.Sprintf("Error getting volume: %v", err)
}
mountpoint := containerMntSubvolume(fs.MasterBranch.Name, subvolume)
log.Printf("Mountpoint for %s (%+v): %s", request.Name, fs, mountpoint)
response.Volume = ResponseListVolume{
Name: request.Name,
Mountpoint: mountpoint,
}
responseJSON, _ := json.Marshal(response)
log.Printf("=> %s", string(responseJSON))
w.Write(responseJSON)
// asynchronously notify dotmesh that the containers running on a
// volume may have changed
go func() { state.fetchRelatedContainersChan <- true }()
})
listener, err := net.Listen("unix", DM_SOCKET)
if err != nil {
log.Fatalf("[runPlugin] Could not listen on %s: %v", DM_SOCKET, err)
}
http.Serve(listener, nil)
}
func (state *InMemoryState) runErrorPlugin() {
// A variant of the normal plugin which just returns immediately with
// errors. For bootstrapping.
log.Printf("[bootstrap] Starting dm temporary bootstrap plugin on %s", DM_SOCKET)
state.mustCleanupSocket()
http.HandleFunc("/Plugin.Activate", func(w http.ResponseWriter, r *http.Request) {
log.Print("[bootstrap] /Plugin.Activate")
responseJSON, _ := json.Marshal(&ResponseImplements{
Implements: []string{"VolumeDriver"},
})
w.Write(responseJSON)
})
http.HandleFunc("/VolumeDriver.Create", func(w http.ResponseWriter, r *http.Request) {
log.Print("[bootstrap] /VolumeDriver.Create")
writeResponseErr(fmt.Errorf("I'm sorry Dave, I can't do that. I'm still starting up."), w)
})
http.HandleFunc("/VolumeDriver.Remove", func(w http.ResponseWriter, r *http.Request) {
log.Print("[bootstrap] /VolumeDriver.Remove")
writeResponseOK(w)
})
http.HandleFunc("/VolumeDriver.Path", func(w http.ResponseWriter, r *http.Request) {
log.Print("[bootstrap] /VolumeDriver.Path")
requestJSON, err := ioutil.ReadAll(r.Body)
if err != nil {
writeResponseErr(err, w)
return
}
request := new(RequestMount)
if err := json.Unmarshal(requestJSON, request); err != nil {
writeResponseErr(err, w)
return
}
namespace, localName, subvolume, err := parseNamespacedVolumeWithSubvolumes(request.Name)
if err != nil {
writeResponseErr(err, w)
return
}
name := VolumeName{namespace, localName}
mountpoint := containerMntSubvolume(name, subvolume)
log.Printf("Mountpoint for %s: %s", name, mountpoint)
responseJSON, _ := json.Marshal(&ResponseMount{
Mountpoint: mountpoint,
Err: "",
})
log.Printf("=> %s", string(responseJSON))
w.Write(responseJSON)
})
http.HandleFunc("/VolumeDriver.Mount", func(w http.ResponseWriter, r *http.Request) {
log.Print("[bootstrap] /VolumeDriver.Mount")
writeResponseErr(fmt.Errorf("dotmesh still starting or dotmesh-etcd unable to achieve quorum"), w)
})
http.HandleFunc("/VolumeDriver.Unmount", func(w http.ResponseWriter, r *http.Request) {
log.Print("[bootstrap] /VolumeDriver.Unmount")
writeResponseErr(fmt.Errorf("dotmesh still starting or dotmesh-etcd unable to achieve quorum"), w)
})
http.HandleFunc("/VolumeDriver.List", func(w http.ResponseWriter, r *http.Request) {
log.Print("[bootstrap] /VolumeDriver.List")
var response = ResponseList{
Err: "dotmesh still starting or dotmesh-etcd unable to achieve quorum",
}
responseJSON, _ := json.Marshal(response)
w.Write(responseJSON)
})
listener, err := net.Listen("unix", DM_SOCKET)
if err != nil {
log.Fatalf("[bootstrap] Could not listen on %s: %v", DM_SOCKET, err)
}
http.Serve(listener, nil)
}
func writeResponseOK(w http.ResponseWriter) {
// A shortcut to writing a ResponseOK to w
responseJSON, _ := json.Marshal(&ResponseSimple{Err: ""})
w.Write(responseJSON)
}
func writeResponseErr(err error, w http.ResponseWriter) {
// A shortcut to responding with an error, and then log the error
errString := fmt.Sprintln(err)
log.Printf("Error: %v", err)
responseJSON, _ := json.Marshal(&ResponseSimple{Err: errString})
w.Write(responseJSON)
}
func (state *InMemoryState) cleanupDockerFilesystemState() error {
err := filepath.Walk(CONTAINER_MOUNT_PREFIX, func(symlinkPath string, info os.FileInfo, err error) error {
if !info.IsDir() {
target, err := os.Readlink(symlinkPath)
log.Printf("[cleanupDockerFilesystemState] Found %s -> %s", symlinkPath, target)
if err != nil {
if os.IsNotExist(err) {
// It's already gone, nothing to clean up.
} else {
// Some other error happened, that's not good.
return err
}
} else {
fsid, err := unmnt(target)
log.Printf("[cleanupDockerFilesystemState] Found %s -> %s extracted fsid %s", symlinkPath, target, fsid)
if err != nil {
return err
}
deleted, err := isFilesystemDeletedInEtcd(fsid)
if err != nil {
return err
}
if deleted {
log.Printf("[cleanupDockerFilesystemState] %s -> %s -> %s - deleting", symlinkPath, target, fsid)
if err := os.Remove(symlinkPath); err != nil {
return err
}
} else {
// Do nothing; the symlink has been taken over by another filesystem, and points to a new non-deleted fs.
}
}
}
return nil
})
return err
}