forked from NetApp/netappdvp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker_driver.go
344 lines (271 loc) · 9.24 KB
/
docker_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
// Copyright 2016 NetApp, Inc. All Rights Reserved.
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"sync"
log "github.com/Sirupsen/logrus"
"github.com/docker/go-plugins-helpers/volume"
"github.com/netapp/netappdvp/storage_drivers"
"github.com/netapp/netappdvp/utils"
)
type ndvpDriver struct {
m *sync.Mutex
root string
config storage_drivers.CommonStorageDriverConfig
sd storage_drivers.StorageDriver
}
// TODO: The volume prefix should be part of the driver config and used only internally in the driver APIs.
func (d *ndvpDriver) volumePrefix() string {
defaultPrefix := d.sd.DefaultStoragePrefix()
prefixToUse := defaultPrefix
storagePrefixRaw := d.config.StoragePrefixRaw // this is a raw version of the json value, we will get quotes in it
if len(storagePrefixRaw) >= 2 {
s := string(storagePrefixRaw)
if s == "\"\"" || s == "" {
prefixToUse = ""
//log.Debugf("storagePrefix is specified as \"\", using no prefix")
} else {
// trim quotes from start and end of string
prefixToUse = s[1 : len(s)-1]
//log.Debugf("storagePrefix is specified, using prefix: %v", prefixToUse)
}
} else {
prefixToUse = defaultPrefix
//log.Debugf("storagePrefix is unspecified, using default prefix: %v", prefixToUse)
}
return prefixToUse
}
func (d *ndvpDriver) volumeName(name string) string {
prefixToUse := d.volumePrefix()
if strings.HasPrefix(name, prefixToUse) {
return name
}
return prefixToUse + name
}
func (d *ndvpDriver) snapshotPrefix() string {
defaultPrefix := d.sd.DefaultSnapshotPrefix()
prefixToUse := defaultPrefix
snapshotPrefixRaw := d.config.SnapshotPrefixRaw // this is a raw version of the json value, we will get quotes in it
if len(snapshotPrefixRaw) >= 2 {
s := string(snapshotPrefixRaw)
if s == "\"\"" || s == "" {
prefixToUse = ""
//log.Debugf("snapshotPrefix is specified as \"\", using no prefix")
} else {
// trim quotes from start and end of string
prefixToUse = s[1 : len(s)-1]
//log.Debugf("snapshotPrefix is specified, using prefix: %v", prefixToUse)
}
} else {
prefixToUse = defaultPrefix
//log.Debugf("snapshotPrefix is unspecified, using default prefix: %v", prefixToUse)
}
return prefixToUse
}
func (d *ndvpDriver) mountpoint(name string) string {
return filepath.Join(d.root, name)
}
func newNetAppDockerVolumePlugin(root string, config storage_drivers.CommonStorageDriverConfig) (*ndvpDriver, error) {
// if root (volumeDir) doesn't exist, make it
dir, err := os.Lstat(root)
if os.IsNotExist(err) {
if err := os.MkdirAll(root, 0755); err != nil {
return nil, err
}
}
// if root (volumeDir) isn't a directory, error
if dir != nil && !dir.IsDir() {
return nil, fmt.Errorf("Volume directory '%v' exists and it's not a directory", root)
}
d := &ndvpDriver{
root: root,
config: config,
m: &sync.Mutex{},
sd: storage_drivers.Drivers[config.StorageDriverName],
}
return d, nil
}
// Create is part of the core Docker API and is called to create a docker volume
func (d ndvpDriver) Create(r volume.Request) volume.Response {
d.m.Lock()
defer d.m.Unlock()
log.Debugf("Create(%v)", r)
opts := r.Options
target := d.volumeName(r.Name)
var createErr error
// If 'from' is specified, create a snapshot and a clone rather than a new empty volume
if from, ok := opts["from"]; ok {
source := d.volumeName(from)
// If 'fromSnapshot' is specified, we use the existing snapshot instead
snapshot := opts["fromSnapshot"]
createErr = d.sd.CreateClone(target, source, snapshot, d.snapshotPrefix())
} else {
createErr = d.sd.Create(target, opts)
}
if createErr != nil {
return volume.Response{Err: fmt.Sprintf("Error creating volume: %v", createErr)}
}
return volume.Response{}
}
// Remove is part of the core Docker API and is called to remove a docker volume
func (d ndvpDriver) Remove(r volume.Request) volume.Response {
d.m.Lock()
defer d.m.Unlock()
log.Debugf("Remove(%v)", r)
target := d.volumeName(r.Name)
// allow user to completely disable volume deletion
if d.config.DisableDelete {
log.Infof("Skipping removal of %s because of user preference to disable volume deletion", target)
return volume.Response{}
}
// use the StorageDriver to destroy the storage objects
destroyErr := d.sd.Destroy(target)
if destroyErr != nil {
return volume.Response{Err: fmt.Sprintf("Problem removing docker volume: %v error: %v", target, destroyErr)}
}
// Best effort removal of the mountpoint
m := d.mountpoint(target)
os.Remove(m)
return volume.Response{}
}
func (d ndvpDriver) getPath(r volume.Request) (string, error) {
// Currently, this returns the mountpoint based on whether the path exists.
// Should it:
// a) Also return what the mountpoint would be if it were mounted, even if it isn't?
// b) Verify that the volume is actually mounted before returning it?
// c) Stay as-is?
target := d.volumeName(r.Name)
m := d.mountpoint(target)
log.Debugf("Getting path for volume '%s' as '%s'", target, m)
fi, err := os.Lstat(m)
if os.IsNotExist(err) {
return "", err
}
if fi == nil {
return "", fmt.Errorf("Could not stat %v", m)
}
return m, nil
}
// Path is part of the core Docker API and is called to return the filesystem path to a docker volume
func (d ndvpDriver) Path(r volume.Request) volume.Response {
d.m.Lock()
defer d.m.Unlock()
log.Debugf("Path(%v)", r)
mountpoint, err := d.getPath(r)
if err != nil {
return volume.Response{Err: err.Error()}
}
return volume.Response{
Mountpoint: mountpoint,
}
}
// Get is part of the core Docker API and is called to return details about a docker volume
func (d ndvpDriver) Get(r volume.Request) volume.Response {
d.m.Lock()
defer d.m.Unlock()
log.Debugf("Get(%v)", r)
// Gather the target volume name as the storage sees it
target := d.volumeName(r.Name)
// Ask the storage driver whether the specified volume exists
err := d.sd.Get(target)
if err != nil {
return volume.Response{Err: err.Error()}
}
// Get the mountpoint, if this volume is mounted
mountpoint, err := d.getPath(r)
// Ask the storage driver for the list of snapshots associated with the volume
snaps, err := d.sd.SnapshotList(target)
// If we don't get any snapshots, that's fine. We'll return an empty list.
status := map[string]interface{}{
"Snapshots": snaps,
}
vol := &volume.Volume{
Name: r.Name,
Mountpoint: mountpoint,
Status: status, // introduced in Docker 1.12, earlier versions ignore
}
return volume.Response{
Volume: vol,
}
}
// Mount is part of the core Docker API and is called to mount a docker volume
func (d ndvpDriver) Mount(r volume.MountRequest) volume.Response {
d.m.Lock()
defer d.m.Unlock()
log.Debugf("Mount(%v)", r)
target := d.volumeName(r.Name)
m := d.mountpoint(target)
log.Debugf("Mounting volume %s on %s", target, m)
fi, err := os.Lstat(m)
if os.IsNotExist(err) {
if err := os.MkdirAll(m, 0755); err != nil {
return volume.Response{Err: err.Error()}
}
} else if err != nil {
return volume.Response{Err: err.Error()}
}
if fi != nil && !fi.IsDir() {
return volume.Response{Err: fmt.Sprintf("%v already exists and it's not a directory", m)}
}
// check if already mounted before we do anything...
dfOuput, dfOuputErr := utils.GetDFOutput()
if dfOuputErr != nil {
return volume.Response{Err: fmt.Sprintf("Error checking if %v is already mounted: %v", m, dfOuputErr)}
}
for _, e := range dfOuput {
if e.Target == m {
log.Debugf("%v already mounted, returning existing mount", m)
return volume.Response{Mountpoint: m}
}
}
// use the StorageDriver to attach the storage objects, place any extra options in this map
attachOptions := make(map[string]string)
attachErr := d.sd.Attach(target, m, attachOptions)
if attachErr != nil {
log.Error(attachErr)
return volume.Response{Err: fmt.Sprintf("Problem attaching docker volume: %v mountpoint: %v error: %v", target, m, attachErr)}
}
return volume.Response{Mountpoint: m}
}
// Unmount is part of the core Docker API and is called to unmount a docker volume
func (d ndvpDriver) Unmount(r volume.UnmountRequest) volume.Response {
d.m.Lock()
defer d.m.Unlock()
log.Debugf("Unmount(%v)", r)
target := d.volumeName(r.Name)
m := d.mountpoint(target)
// use the StorageDriver to unmount the storage objects
detachErr := d.sd.Detach(target, m)
if detachErr != nil {
return volume.Response{Err: fmt.Sprintf("Problem unmounting docker volume: %v error: %v", target, detachErr)}
}
// Best effort removal of the mountpoint
os.Remove(m)
return volume.Response{}
}
// List is part of the core Docker API and is called to list all known docker volumes for this plugin
func (d ndvpDriver) List(r volume.Request) volume.Response {
d.m.Lock()
defer d.m.Unlock()
log.Debugf("List(%v)", r)
var volumes []*volume.Volume
vols, err := d.sd.List(d.volumePrefix())
if err != nil {
return volume.Response{Err: fmt.Sprintf("Unable to retrieve volume list, error: %v", err)}
}
for _, vol := range vols {
// What is the impact of leaving the mountpoints out of this response?
v := &volume.Volume{Name: vol}
volumes = append(volumes, v)
}
return volume.Response{Volumes: volumes}
}
func (d ndvpDriver) Capabilities(r volume.Request) volume.Response {
d.m.Lock()
defer d.m.Unlock()
log.Debugf("Capabilities(%v)", r)
return volume.Response{Capabilities: volume.Capability{Scope: "global"}}
}