/
volumes.go
559 lines (501 loc) · 13 KB
/
volumes.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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
package cli
import (
"context"
"fmt"
"os"
"strings"
"time"
"github.com/codegangsta/cli"
"github.com/libopenstorage/openstorage/api"
clusterclient "github.com/libopenstorage/openstorage/api/client/cluster"
volumeclient "github.com/libopenstorage/openstorage/api/client/volume"
"github.com/libopenstorage/openstorage/cluster"
"github.com/libopenstorage/openstorage/pkg/options"
"github.com/libopenstorage/openstorage/volume"
)
// VolumeSzUnits number representing size units.
type VolumeSzUnits uint64
const (
_ = iota
// KiB 1024 bytes
KiB VolumeSzUnits = 1 << (10 * iota)
// MiB 1024 KiB
MiB
// GiB 1024 MiB
GiB
// TiB 1024 GiB
TiB
// PiB 1024 PiB
PiB
)
type volDriver struct {
volDriver volume.VolumeDriver
name string
}
func processLabels(s string) (map[string]string, error) {
m := make(map[string]string)
labels := strings.Split(s, ",")
for _, v := range labels {
label := strings.Split(v, "=")
if len(label) != 2 {
return nil, fmt.Errorf("Malformed label: %s", v)
}
if _, ok := m[label[0]]; ok {
return nil, fmt.Errorf("Duplicate label: %s", v)
}
m[label[0]] = label[1]
}
return m, nil
}
func (v *volDriver) volumeOptions(cliContext *cli.Context) {
// Currently we choose the default version
clnt, err := volumeclient.NewDriverClient("", v.name, volume.APIVersion, "")
if err != nil {
fmt.Printf("Failed to initialize client library: %v\n", err)
os.Exit(1)
}
v.volDriver = volumeclient.VolumeDriver(clnt)
}
func (v *volDriver) volumeCreate(cliContext *cli.Context) {
var err error
var labels map[string]string
locator := &api.VolumeLocator{}
var id string
fn := "create"
if len(cliContext.Args()) != 1 {
missingParameter(cliContext, fn, "name", "Invalid number of arguments")
return
}
v.volumeOptions(cliContext)
if l := cliContext.String("label"); l != "" {
if labels, err = processLabels(l); err != nil {
cmdError(cliContext, fn, err)
return
}
}
locator = &api.VolumeLocator{
Name: cliContext.Args()[0],
VolumeLabels: labels,
}
fsType, err := api.FSTypeSimpleValueOf(cliContext.String("fs"))
if err != nil {
cmdError(cliContext, fn, err)
return
}
cosType, err := api.CosTypeSimpleValueOf(cliContext.String("cos"))
if err != nil {
cmdError(cliContext, fn, err)
return
}
spec := &api.VolumeSpec{
Size: uint64(VolumeSzUnits(cliContext.Int("s")) * MiB),
Format: fsType,
BlockSize: int64(cliContext.Int("b") * 1024),
HaLevel: int64(cliContext.Int("r")),
Cos: cosType,
SnapshotInterval: uint32(cliContext.Int("si")),
}
source := &api.Source{
Seed: cliContext.String("seed"),
}
if id, err = v.volDriver.Create(context.Background(), locator, source, spec); err != nil {
cmdError(cliContext, fn, err)
return
}
fmtOutput(cliContext, &Format{UUID: []string{string(id)}})
}
func (v *volDriver) volumeMount(cliContext *cli.Context) {
v.volumeOptions(cliContext)
fn := "mount"
if len(cliContext.Args()) < 1 {
missingParameter(cliContext, fn, "volumeID", "Invalid number of arguments")
return
}
volumeID := cliContext.Args()[0]
path := cliContext.String("path")
if path == "" {
missingParameter(cliContext, fn, "path", "Target mount path")
return
}
err := v.volDriver.Mount(context.TODO(), string(volumeID), path, nil)
if err != nil {
cmdError(cliContext, fn, err)
return
}
fmtOutput(cliContext, &Format{UUID: []string{volumeID}})
}
func (v *volDriver) volumeUnmount(cliContext *cli.Context) {
v.volumeOptions(cliContext)
fn := "unmount"
if len(cliContext.Args()) < 1 {
missingParameter(cliContext, fn, "volumeID", "Invalid number of arguments")
return
}
volumeID := cliContext.Args()[0]
path := cliContext.String("path")
opts := make(map[string]string)
opts[options.OptionsDeleteAfterUnmount] = "true"
err := v.volDriver.Unmount(context.TODO(), string(volumeID), path, opts)
if err != nil {
cmdError(cliContext, fn, err)
return
}
fmtOutput(cliContext, &Format{UUID: []string{volumeID}})
}
func (v *volDriver) volumeAttach(cliContext *cli.Context) {
fn := "attach"
if len(cliContext.Args()) < 1 {
missingParameter(cliContext, fn, "volumeID", "Invalid number of arguments")
return
}
v.volumeOptions(cliContext)
volumeID := cliContext.Args()[0]
devicePath, err := v.volDriver.Attach(context.TODO(), (volumeID), nil)
if err != nil {
cmdError(cliContext, fn, err)
return
}
fmtOutput(cliContext, &Format{Result: devicePath})
}
func (v *volDriver) volumeDetach(cliContext *cli.Context) {
fn := "detach"
if len(cliContext.Args()) < 1 {
missingParameter(cliContext, fn, "volumeID", "Invalid number of arguments")
return
}
volumeID := cliContext.Args()[0]
v.volumeOptions(cliContext)
err := v.volDriver.Detach(context.TODO(), string(volumeID), nil)
if err != nil {
cmdError(cliContext, fn, err)
return
}
fmtOutput(cliContext, &Format{UUID: []string{cliContext.Args()[0]}})
}
func (v *volDriver) volumeInspect(cliContext *cli.Context) {
v.volumeOptions(cliContext)
fn := "inspect"
if len(cliContext.Args()) < 1 {
missingParameter(cliContext, fn, "volumeID", "Invalid number of arguments")
return
}
d := make([]string, len(cliContext.Args()))
for i, v := range cliContext.Args() {
d[i] = string(v)
}
volumes, err := v.volDriver.Inspect(d)
if err != nil {
cmdError(cliContext, fn, err)
return
}
cmdOutputVolumes(volumes, cliContext.GlobalBool("raw"))
}
func (v *volDriver) volumeStats(cliContext *cli.Context) {
v.volumeOptions(cliContext)
fn := "stats"
if len(cliContext.Args()) != 1 {
missingParameter(cliContext, fn, "volumeID", "Invalid number of arguments")
return
}
stats, err := v.volDriver.Stats(string(cliContext.Args()[0]), true)
if err != nil {
cmdError(cliContext, fn, err)
return
}
cmdOutputProto(stats, cliContext.GlobalBool("raw"))
}
func (v *volDriver) volumeEnumerate(cliContext *cli.Context) {
locator := &api.VolumeLocator{}
var err error
fn := "enumerate"
locator.Name = cliContext.String("name")
if l := cliContext.String("label"); l != "" {
locator.VolumeLabels, err = processLabels(l)
if err != nil {
cmdError(cliContext, fn, err)
return
}
}
v.volumeOptions(cliContext)
volumes, err := v.volDriver.Enumerate(locator, nil)
if err != nil {
cmdError(cliContext, fn, err)
return
}
cmdOutputVolumes(volumes, cliContext.GlobalBool("raw"))
}
func (v *volDriver) volumeDelete(cliContext *cli.Context) {
fn := "delete"
if len(cliContext.Args()) < 1 {
missingParameter(cliContext, fn, "volumeID", "Invalid number of arguments")
return
}
volumeID := cliContext.Args()[0]
v.volumeOptions(cliContext)
err := v.volDriver.Delete(context.Background(), volumeID)
if err != nil {
cmdError(cliContext, fn, err)
return
}
fmtOutput(cliContext, &Format{UUID: []string{cliContext.Args()[0]}})
}
func (v *volDriver) snapCreate(cliContext *cli.Context) {
var err error
var labels map[string]string
fn := "snapCreate"
if len(cliContext.Args()) != 1 {
missingParameter(cliContext, fn, "volumeID", "Invalid number of arguments")
return
}
volumeID := cliContext.Args()[0]
v.volumeOptions(cliContext)
if l := cliContext.String("label"); l != "" {
if labels, err = processLabels(l); err != nil {
cmdError(cliContext, fn, err)
return
}
}
locator := &api.VolumeLocator{
Name: cliContext.String("name"),
VolumeLabels: labels,
}
readonly := cliContext.Bool("readonly")
noRetry := cliContext.Bool("noretry")
id, err := v.volDriver.Snapshot(volumeID, readonly, locator, noRetry)
if err != nil {
cmdError(cliContext, fn, err)
return
}
fmtOutput(cliContext, &Format{UUID: []string{string(id)}})
}
func (v *volDriver) snapEnumerate(cliContext *cli.Context) {
locator := &api.VolumeLocator{}
var err error
fn := "snap enumerate"
locator.Name = cliContext.String("name")
if l := cliContext.String("label"); l != "" {
locator.VolumeLabels, err = processLabels(l)
if err != nil {
cmdError(cliContext, fn, err)
return
}
}
v.volumeOptions(cliContext)
snaps, err := v.volDriver.Enumerate(locator, nil)
if err != nil {
cmdError(cliContext, fn, err)
return
}
if snaps == nil {
cmdError(cliContext, fn, err)
return
}
cmdOutputVolumes(snaps, cliContext.GlobalBool("raw"))
}
func (v *volDriver) volumeAlerts(cliContext *cli.Context) {
v.volumeOptions(cliContext)
clnt, err := clusterclient.NewClusterClient("", cluster.APIVersion)
if err != nil {
fmt.Printf("Failed to initialize client library: %v\n", err)
return
}
manager := clusterclient.ClusterManager(clnt)
alerts, err := manager.EnumerateAlerts(time.Time{}, time.Time{}, api.ResourceType_RESOURCE_TYPE_VOLUME)
if err != nil {
fmt.Printf("Unable to enumerate alerts: %v\n", err)
return
}
cmdOutputProto(alerts, cliContext.GlobalBool("raw"))
}
// baseVolumeCommand exports commands common to block and file volume drivers.
func baseVolumeCommand(v *volDriver) []cli.Command {
commands := []cli.Command{
{
Name: "create",
Aliases: []string{"c"},
Usage: "create a new volume",
Action: v.volumeCreate,
Flags: []cli.Flag{
cli.StringFlag{
Name: "label,l",
Usage: "Comma separated name=value pairs, e.g name=sqlvolume,type=production",
Value: "",
},
cli.IntFlag{
Name: "size,s",
Usage: "specify size in MB",
Value: 1000,
},
cli.StringFlag{
Name: "fs",
Usage: "filesystem to be laid out: none|xfs|ext4",
Value: "ext4",
},
cli.StringFlag{
Name: "seed",
Usage: "optional data that the volume should be seeded with",
},
cli.IntFlag{
Name: "block_size,b",
Usage: "block size in Kbytes",
Value: 32,
},
cli.IntFlag{
Name: "repl,r",
Usage: "replication factor [1..2]",
Value: 1,
},
cli.StringFlag{
Name: "cos",
Usage: "Class of Service: [high|medium|low]",
Value: "low",
},
cli.IntFlag{
Name: "snap_interval,si",
Usage: "snapshot interval in minutes, 0 disables snaps",
Value: 0,
},
},
},
{
Name: "mount",
Aliases: []string{"m"},
Usage: "Mount specified volume",
Action: v.volumeMount,
Flags: []cli.Flag{
cli.StringFlag{
Name: "path",
Usage: "destination path at which this volume must be mounted on",
},
},
},
{
Name: "unmount",
Aliases: []string{"u"},
Usage: "Unmount specified volume",
Action: v.volumeUnmount,
Flags: []cli.Flag{
cli.StringFlag{
Name: "path",
Usage: "destination path at which this volume must be mounted on",
},
},
},
{
Name: "delete",
Aliases: []string{"rm"},
Usage: "Detach specified volume",
Action: v.volumeDelete,
},
{
Name: "enumerate",
Aliases: []string{"e"},
Usage: "Enumerate volumes",
Action: v.volumeEnumerate,
Flags: []cli.Flag{
cli.StringFlag{
Name: "name",
Usage: "volume name used during creation if any",
},
cli.StringFlag{
Name: "label,l",
Usage: "Comma separated name=value pairs, e.g name=sqlvolume,type=production",
},
},
},
{
Name: "inspect",
Aliases: []string{"i"},
Usage: "Inspect volume",
Action: v.volumeInspect,
},
{
Name: "alerts",
Usage: "Enumerate volume alerts",
Action: v.volumeAlerts,
},
{
Name: "stats",
Usage: "volume stats",
Action: v.volumeStats,
},
{
Name: "snap",
Aliases: []string{"sc"},
Usage: "create snap",
Action: v.snapCreate,
Flags: []cli.Flag{
cli.StringFlag{
Name: "name",
Usage: "user friendly name",
},
cli.StringFlag{
Name: "label,l",
Usage: "Comma separated name=value pairs, e.g name=sqlvolume,type=production",
},
cli.BoolFlag{
Name: "readonly",
Usage: "true if snapshot is readonly",
},
},
},
{
Name: "snapEnumerate",
Aliases: []string{"se"},
Usage: "Enumerate snap",
Action: v.snapEnumerate,
Flags: []cli.Flag{
cli.StringFlag{
Name: "name, n",
Usage: "snap name used during creation",
},
cli.StringFlag{
Name: "label,l",
Usage: "Comma separated name=value pairs, e.g name=sqlvolume,type=production",
},
},
},
}
return commands
}
// BlockVolumeCommands exports CLI comamnds for a Block VolumeDriver.
func BlockVolumeCommands(name string) []cli.Command {
v := &volDriver{name: name}
blockCommands := []cli.Command{
{
Name: "attach",
Aliases: []string{"a"},
Usage: "Attach volume to specified path",
Action: v.volumeAttach,
Flags: []cli.Flag{
cli.StringFlag{
Name: "path,p",
Usage: "Path on local filesystem",
},
},
},
{
Name: "detach",
Aliases: []string{"d"},
Usage: "Detach specified volume",
Action: v.volumeDetach,
},
}
baseCommands := baseVolumeCommand(v)
return append(baseCommands, blockCommands...)
}
// FileVolumeCommands exports CLI comamnds for File VolumeDriver
func FileVolumeCommands(name string) []cli.Command {
v := &volDriver{name: name}
return baseVolumeCommand(v)
}
func cmdOutputVolumes(volumes []*api.Volume, j bool) {
fmt.Print("[")
for i, volume := range volumes {
fmt.Print(cmdMarshalProto(volume, j))
if i != len(volumes)-1 {
fmt.Print(",")
}
}
fmt.Println("]")
}