-
Notifications
You must be signed in to change notification settings - Fork 20
/
driver.go
431 lines (351 loc) · 11.2 KB
/
driver.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
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"sync"
"github.com/docker/docker/api/types/filters"
"golang.org/x/oauth2"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/docker/go-plugins-helpers/volume"
"github.com/linode/linodego"
log "github.com/sirupsen/logrus"
)
type linodeVolumeDriver struct {
instanceID int
region string
linodeLabel string
linodeToken string
mutex *sync.Mutex
linodeAPIPtr *linodego.Client
}
const (
fsTagPrefix = "docker-volume-filesystem-"
)
// Constructor
func newLinodeVolumeDriver(linodeLabel string, linodeToken string) linodeVolumeDriver {
driver := linodeVolumeDriver{
linodeToken: linodeToken,
linodeLabel: linodeLabel,
mutex: &sync.Mutex{},
}
if _, err := driver.linodeAPI(); err != nil {
log.Fatalf("Could not initialize Linode API: %s", err)
}
return driver
}
func (driver *linodeVolumeDriver) linodeAPI() (*linodego.Client, error) {
if driver.linodeToken == "" {
return nil, fmt.Errorf("Linode Token required. Set the token by calling \"docker plugin set <plugin-name> linode-token=<linode token>\"")
}
if driver.linodeAPIPtr != nil {
return driver.linodeAPIPtr, nil
}
driver.linodeAPIPtr = setupLinodeAPI(*linodeTokenParamPtr)
if driver.instanceID == 0 {
if err := driver.determineLinodeID(); err != nil {
driver.linodeAPIPtr = nil
return nil, err
}
}
return driver.linodeAPIPtr, nil
}
func setupLinodeAPI(token string) *linodego.Client {
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
oauth2Client := &http.Client{
Transport: &oauth2.Transport{
Source: tokenSource,
},
}
api := linodego.NewClient(oauth2Client)
ua := fmt.Sprintf("docker-volume-linode/%s linodego/%s", VERSION, linodego.Version)
api.SetUserAgent(ua)
return &api
}
func (driver *linodeVolumeDriver) determineLinodeID() error {
if driver.linodeLabel == "" {
var hostnameErr error
driver.linodeLabel, hostnameErr = os.Hostname()
if hostnameErr != nil {
return fmt.Errorf("Could not determine hostname: %s", hostnameErr)
}
}
jsonFilter, _ := json.Marshal(map[string]string{"label": driver.linodeLabel})
listOpts := linodego.NewListOptions(0, string(jsonFilter))
linodes, lErr := driver.linodeAPIPtr.ListInstances(context.Background(), listOpts)
if lErr != nil {
return fmt.Errorf("Could not determine Linode instance ID from Linode label %s due to error: %s", driver.linodeLabel, lErr)
} else if len(linodes) != 1 {
return fmt.Errorf("Could not determine Linode instance ID from Linode label %s", driver.linodeLabel)
}
driver.instanceID = linodes[0].ID
if driver.region == "" {
driver.region = linodes[0].Region
}
return nil
}
// Get implementation
func (driver *linodeVolumeDriver) Get(req *volume.GetRequest) (*volume.GetResponse, error) {
log.Infof("Get(%s)", req.Name)
linVol, err := driver.findVolumeByLabel(req.Name)
if err != nil {
return nil, err
}
if linVol == nil {
return nil, fmt.Errorf("got a NIL volume. Volume may not exist")
}
vol := linodeVolumeToDockerVolume(*linVol)
resp := &volume.GetResponse{Volume: vol}
log.Infof("Get(): {Name: %s; Mountpoint: %s;}", vol.Name, vol.Mountpoint)
return resp, nil
}
// List implementation
func (driver *linodeVolumeDriver) List() (*volume.ListResponse, error) {
log.Infof("List()")
var jsonFilter []byte
var err error
//
api, err := driver.linodeAPI()
if err != nil {
return nil, err
}
//
var volumes []*volume.Volume
// filters
if jsonFilter, err = json.Marshal(map[string]string{"region": driver.region}); err != nil {
return nil, err
}
listOpts := linodego.NewListOptions(0, string(jsonFilter))
log.Debug("linode api listOpts: ", listOpts)
linVols, err := api.ListVolumes(context.Background(), listOpts)
if err != nil {
return nil, err
}
log.Debugf("Got %d volume count from api", len(linVols))
for _, linVol := range linVols {
vol := linodeVolumeToDockerVolume(linVol)
log.Debugf("Volume: %+v", vol)
volumes = append(volumes, vol)
}
log.Infof("List() returning %d volumes", len(volumes))
return &volume.ListResponse{Volumes: volumes}, nil
}
// Create implementation
func (driver *linodeVolumeDriver) Create(req *volume.CreateRequest) error {
log.Infof("Create(%s)", req.Name)
api, err := driver.linodeAPI()
if err != nil {
return err
}
driver.mutex.Lock()
defer driver.mutex.Unlock()
var size int
if sizeOpt, ok := req.Options["size"]; ok {
s, err := strconv.Atoi(sizeOpt)
if err != nil {
return fmt.Errorf("Invalid size")
}
size = s
}
createOpts := linodego.VolumeCreateOptions{
Label: req.Name,
LinodeID: driver.instanceID,
Size: size,
}
if fsOpt, ok := req.Options["filesystem"]; ok {
createOpts.Tags = append(createOpts.Tags, fsTagPrefix+fsOpt)
}
if deleteOpt, ok := req.Options["delete-on-remove"]; ok {
b, err := strconv.ParseBool(deleteOpt)
if err != nil {
return fmt.Errorf("Invalid delete-on-remove argument")
}
if b {
createOpts.Tags = append(createOpts.Tags, "docker-volume-delete-on-remove")
}
}
if _, err := api.CreateVolume(context.Background(), createOpts); err != nil {
return fmt.Errorf("Create(%s) Failed: %s", req.Name, err)
}
return nil
}
// Remove implementation
func (driver *linodeVolumeDriver) Remove(req *volume.RemoveRequest) error {
driver.mutex.Lock()
defer driver.mutex.Unlock()
//
api, err := driver.linodeAPI()
if err != nil {
return err
}
//
linVol, err := driver.findVolumeByLabel(req.Name)
if err != nil {
return err
}
// Send detach request
if err := detachAndWait(api, linVol.ID); err != nil {
return err
}
// Optionally send Delete request
for _, t := range linVol.Tags {
if t == "docker-volume-delete-on-remove" {
if err := api.DeleteVolume(context.Background(), linVol.ID); err != nil {
return err
}
break
}
}
return nil
}
// Mount implementation
func (driver *linodeVolumeDriver) Mount(req *volume.MountRequest) (*volume.MountResponse, error) {
log.Infof("Called Mount %s", req.Name)
api, err := driver.linodeAPI()
if err != nil {
return nil, err
}
linVol, err := driver.findVolumeByLabel(req.Name)
if err != nil {
return nil, err
}
// if volume not attached, attach to this linode
if linVol.LinodeID == nil {
if err := attachAndWait(api, linVol.ID, driver.instanceID); err != nil {
return nil, fmt.Errorf("Error attaching volume(%s) to linode: %s", req.Name, err)
}
} else if *linVol.LinodeID != driver.instanceID { // If volume attached to another linode... send detach request
cli, err := client.NewEnvClient()
if err != nil {
panic(err)
}
filters := filters.Args{}
filters.Add("volume", req.Name)
listOpts := types.ContainerListOptions{
Filters: filters,
}
containers, err := cli.ContainerList(context.Background(), listOpts)
if err != nil {
return nil, fmt.Errorf("Error detecting containers using volume from remote linode: %s", err)
}
if len(containers) > 0 {
return nil, fmt.Errorf("Error detaching volume from remote linode: volume in use by %s", containers[0].ID)
}
if err := detachAndWait(api, linVol.ID); err != nil {
return nil, fmt.Errorf("Error detaching volume from remote linode: %s", err)
}
if err := attachAndWait(api, linVol.ID, driver.instanceID); err != nil {
return nil, fmt.Errorf("Error attaching volume(%s) to linode: %s", req.Name, err)
}
}
// else... linode already attached to current host
// wait for kernel to have block device available
if err := waitForDeviceFileExists(linVol.FilesystemPath, 300); err != nil {
return nil, err
}
// Format block device if no FS found
if GetFSType(linVol.FilesystemPath) == "" {
log.Infof("Formatting device:%s;", linVol.FilesystemPath)
filesystem := "ext4"
for _, tag := range linVol.Tags {
if strings.HasPrefix(tag, fsTagPrefix) {
filesystem = tag[len(fsTagPrefix):]
break
}
}
if err := Format(linVol.FilesystemPath, filesystem); err != nil {
return nil, err
}
}
// Create mount point using label (if not exists)
mp := labelToMountPoint(linVol.Label)
if _, err := os.Stat(mp); os.IsNotExist(err) {
log.Infof("Creating mountpoint directory: %s", mp)
if err = os.MkdirAll(mp, 0755); err != nil {
return nil, fmt.Errorf("Error creating mountpoint directory(%s): %s", mp, err)
}
}
if err := Mount(linVol.FilesystemPath, mp); err != nil {
return nil, fmt.Errorf("Error mounting volume(%s) to directory(%s): %s", linVol.FilesystemPath, mp, err)
}
log.Infof("Mount Call End: %s", req.Name)
return &volume.MountResponse{Mountpoint: mp}, nil
}
// Path implementation
func (driver *linodeVolumeDriver) Path(req *volume.PathRequest) (*volume.PathResponse, error) {
log.Infof("Path(%s)", req.Name)
linVol, err := driver.findVolumeByLabel(req.Name)
if err != nil {
return nil, err
}
mp := labelToMountPoint(linVol.Label)
log.Infof("Path(): %s", mp)
return &volume.PathResponse{Mountpoint: mp}, nil
}
// Unmount implementation
func (driver *linodeVolumeDriver) Unmount(req *volume.UnmountRequest) error {
log.Infof("Unmount(%s)", req.Name)
linVol, err := driver.findVolumeByLabel(req.Name)
if err != nil {
return err
}
if err := Umount(labelToMountPoint(linVol.Label)); err != nil {
return fmt.Errorf("Unable to Unmount(%s): %s", req.Name, err)
}
log.Infof("Unmount(): %s", req.Name)
return nil
}
// Capabilities implementation
func (driver *linodeVolumeDriver) Capabilities() *volume.CapabilitiesResponse {
log.Infof("Capabilities(): Scope: global")
return &volume.CapabilitiesResponse{Capabilities: volume.Capability{Scope: "global"}}
}
// findVolumeByLabel looks up linode volume by label
func (driver *linodeVolumeDriver) findVolumeByLabel(volumeLabel string) (*linodego.Volume, error) {
var jsonFilter []byte
var err error
var linVols []linodego.Volume
//
api, err := driver.linodeAPI()
if err != nil {
return nil, err
}
if jsonFilter, err = json.Marshal(map[string]string{"label": volumeLabel, "region": driver.region}); err != nil {
return nil, err
}
listOpts := linodego.NewListOptions(0, string(jsonFilter))
if linVols, err = api.ListVolumes(context.Background(), listOpts); err != nil {
return nil, err
}
if len(linVols) != 1 {
return nil, fmt.Errorf("Instance %d Volume with name %s not found", driver.instanceID, volumeLabel)
}
return &linVols[0], nil
}
func detachAndWait(api *linodego.Client, volumeID int) error {
// Send detach request
if err := api.DetachVolume(context.Background(), volumeID); err != nil {
return fmt.Errorf("Error detaching volumeID(%d): %s", volumeID, err)
}
// Wait for linode to have the volume detached
if err := waitForLinodeVolumeDetachment(*api, volumeID); err != nil {
return fmt.Errorf("Error waiting for detachment of volumeID(%d): %s", volumeID, err)
}
return nil
}
func attachAndWait(api *linodego.Client, volumeID int, linodeID int) error {
// attach
attachOpts := linodego.VolumeAttachOptions{LinodeID: linodeID}
if _, err := api.AttachVolume(context.Background(), volumeID, &attachOpts); err != nil {
return fmt.Errorf("Error attaching volume(%d) to linode(%d): %s", volumeID, linodeID, err)
}
if _, err := api.WaitForVolumeLinodeID(context.Background(), volumeID, &linodeID, 300); err != nil {
return fmt.Errorf("Error waiting for attachment of volume(%d) to linode(%d): %s", volumeID, linodeID, err)
}
return nil
}