forked from AliyunContainerService/pouch
/
volume.go
389 lines (330 loc) · 10.8 KB
/
volume.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
package main
import (
"context"
"fmt"
"os"
"strings"
"github.com/alibaba/pouch/apis/filters"
"github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/cli/inspect"
"github.com/alibaba/pouch/pkg/log"
"github.com/spf13/cobra"
)
// volumeDescription is used to describe volume command in detail and auto generate command doc.
var volumeDescription = "Manager the volumes in pouchd. " +
"It contains the functions of create/remove/list/inspect volume, 'driver' is used to list drivers that pouch support. " +
"The default volume driver is local, it will make a directory to bind into container."
// VolumeCommand is used to implement 'volume' command.
type VolumeCommand struct {
baseCommand
}
// Init initializes VolumeCommand command.
func (v *VolumeCommand) Init(c *Cli) {
v.cli = c
v.cmd = &cobra.Command{
Use: "volume [command]",
Short: "Manage pouch volumes",
Long: volumeDescription,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return fmt.Errorf("command 'pouch volume %s' does not exist.\nPlease execute `pouch volume --help` for more help", args[0])
},
}
c.AddCommand(v, &VolumeCreateCommand{})
c.AddCommand(v, &VolumeRemoveCommand{})
c.AddCommand(v, &VolumeInspectCommand{})
c.AddCommand(v, &VolumeListCommand{})
}
// RunE is the entry of VolumeCommand command.
func (v *VolumeCommand) RunE(args []string) error {
return nil
}
// volumeCreateDescription is used to describe volume create command in detail and auto generate command doc.
var volumeCreateDescription = "Create a volume in pouchd. " +
"It must specify volume's name, size and driver. You can use 'volume driver' to get drivers that pouch support."
// VolumeCreateCommand is used to implement 'volume create' command.
type VolumeCreateCommand struct {
baseCommand
name string
driver string
options []string
labels []string
selectors []string
}
// Init initializes VolumeCreateCommand command.
func (v *VolumeCreateCommand) Init(c *Cli) {
v.cli = c
v.cmd = &cobra.Command{
Use: "create [OPTIONS]",
Short: "Create a volume",
Long: volumeCreateDescription,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return v.runVolumeCreate(args)
},
Example: volumeCreateExample(),
}
v.addFlags()
}
// addFlags adds flags for specific command.
func (v *VolumeCreateCommand) addFlags() {
flagSet := v.cmd.Flags()
flagSet.StringVarP(&v.name, "name", "n", "", "Specify name for volume")
flagSet.StringVarP(&v.driver, "driver", "d", "local", "Specify volume driver name (default 'local')")
flagSet.StringSliceVarP(&v.options, "option", "o", nil, "Set volume driver options")
flagSet.StringSliceVarP(&v.labels, "label", "l", nil, "Set labels for volume")
flagSet.StringSliceVarP(&v.selectors, "selector", "s", nil, "Set volume selectors")
}
// runVolumeCreate is the entry of VolumeCreateCommand command.
func (v *VolumeCreateCommand) runVolumeCreate(args []string) error {
log.With(nil).Debugf("create a volume: %s, options: %v, labels: %v, selectors: %v",
v.name, v.options, v.labels, v.selectors)
return v.volumeCreate()
}
func (v *VolumeCreateCommand) volumeCreate() error {
volumeReq := &types.VolumeCreateConfig{
Driver: v.driver,
Name: v.name,
DriverOpts: map[string]string{},
Labels: map[string]string{},
}
if err := parseVolume(volumeReq, v); err != nil {
return err
}
ctx := context.Background()
apiClient := v.cli.Client()
volume, err := apiClient.VolumeCreate(ctx, volumeReq)
if err != nil {
return err
}
v.cli.Print(volume)
return nil
}
func parseVolume(volumeCreateConfig *types.VolumeCreateConfig, v *VolumeCreateCommand) error {
// analyze labels.
for _, label := range v.labels {
l := strings.Split(label, "=")
if len(l) != 2 {
return fmt.Errorf("unknown label %s: label format must be key=value", label)
}
volumeCreateConfig.Labels[l[0]] = l[1]
}
// analyze options.
for _, option := range v.options {
opt := strings.Split(option, "=")
if len(opt) != 2 {
return fmt.Errorf("unknown option %s: option format must be key=value", option)
}
volumeCreateConfig.DriverOpts[opt[0]] = opt[1]
}
// analyze selectors.
for _, selector := range v.selectors {
s := strings.Split(selector, "=")
if len(s) != 2 {
return fmt.Errorf("unknown selector %s: selector format must be key=value", selector)
}
volumeCreateConfig.DriverOpts["selector."+s[0]] = s[1]
}
return nil
}
// volumeCreateExample shows examples in volume create command, and is used in auto-generated cli docs.
func volumeCreateExample() string {
return `$ pouch volume create -d local -n pouch-volume -o opt.size=100g
Mountpoint:
Name: pouch-volume
Scope:
CreatedAt:
Driver: local`
}
// volumeRmDescription is used to describe volume rm command in detail and auto generate command doc.
var volumeRmDescription = "Remove a volume in pouchd. " +
"It must specify volume's name, and the volume will be removed when it is existent and unused."
// VolumeRemoveCommand is used to implement 'volume rm' command.
type VolumeRemoveCommand struct {
baseCommand
}
// Init initializes VolumeRemoveCommand command.
func (v *VolumeRemoveCommand) Init(c *Cli) {
v.cli = c
v.cmd = &cobra.Command{
Use: "remove [OPTIONS] NAME",
Aliases: []string{"rm"},
Short: "Remove a volume",
Long: volumeRmDescription,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return v.runVolumeRm(args)
},
Example: volumeRmExample(),
}
v.addFlags()
}
// addFlags adds flags for specific command.
func (v *VolumeRemoveCommand) addFlags() {}
// runVolumeRm is the entry of VolumeRemoveCommand command.
func (v *VolumeRemoveCommand) runVolumeRm(args []string) error {
name := args[0]
log.With(nil).Debugf("remove a volume: %s", name)
ctx := context.Background()
apiClient := v.cli.Client()
err := apiClient.VolumeRemove(ctx, name)
if err == nil {
fmt.Printf("Removed: %s\n", name)
}
return err
}
// volumeRmExample shows examples in volume rm command, and is used in auto-generated cli docs.
func volumeRmExample() string {
return `$ pouch volume rm pouch-volume
Removed: pouch-volume`
}
// volumeInspectDescription is used to describe volume inspect command in detail and auto generate command doc.
var volumeInspectDescription = "Inspect one or more volumes in pouchd. " +
"It must specify volume's name."
// VolumeInspectCommand is used to implement 'volume inspect' command.
type VolumeInspectCommand struct {
baseCommand
format string
}
// Init initializes VolumeInspectCommand command.
func (v *VolumeInspectCommand) Init(c *Cli) {
v.cli = c
v.cmd = &cobra.Command{
Use: "inspect [OPTIONS] Volume [Volume...]",
Short: "Inspect one or more pouch volumes",
Long: volumeInspectDescription,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return v.runVolumeInspect(args)
},
Example: volumeInspectExample(),
}
v.addFlags()
}
// addFlags adds flags for specific command.
func (v *VolumeInspectCommand) addFlags() {
v.cmd.Flags().StringVarP(&v.format, "format", "f", "", "Format the output using the given go template")
}
// runVolumeInspect is the entry of VolumeInspectCommand command.
func (v *VolumeInspectCommand) runVolumeInspect(args []string) error {
ctx := context.Background()
apiClient := v.cli.Client()
getRefFunc := func(ref string) (interface{}, error) {
return apiClient.VolumeInspect(ctx, ref)
}
return inspect.Inspect(os.Stdout, args, v.format, getRefFunc)
}
// volumeInspectExample shows examples in volume inspect command, and is used in auto-generated cli docs.
func volumeInspectExample() string {
return `$ pouch volume inspect pouch-volume
{
"CreatedAt": "2018-4-2 14:33:45",
"Driver": "local",
"Labels": {
"backend": "local",
"hostname": "ubuntu"
},
"Mountpoint": "/mnt/local/pouch-volume",
"Name": "pouch-volume",
"Status": {
"sifter": "Default",
"size": "10g"
}
}`
}
// volumeListDescription is used to describe volume list command in detail and auto generate command doc.
var volumeListDescription = "List volumes in pouchd. " +
"It lists the volume's name"
// VolumeListCommand is used to implement 'volume rm' command.
type VolumeListCommand struct {
baseCommand
size bool
mountPoint bool
quiet bool
filter []string
}
// Init initializes VolumeListCommand command.
func (v *VolumeListCommand) Init(c *Cli) {
v.cli = c
v.cmd = &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "List volumes",
Long: volumeListDescription,
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
return v.runVolumeList(args)
},
Example: volumeListExample(),
}
v.addFlags()
}
// addFlags adds flags for specific command.
func (v *VolumeListCommand) addFlags() {
flagSet := v.cmd.Flags()
flagSet.BoolVar(&v.size, "size", false, "Display volume size")
flagSet.BoolVar(&v.mountPoint, "mountpoint", false, "Display volume mountpoint")
flagSet.BoolVarP(&v.quiet, "quiet", "q", false, "Only display volume names")
flagSet.StringSliceVarP(&v.filter, "filter", "f", []string{}, "Filter output based on conditions provided, filter support driver, name, label")
}
// runVolumeList is the entry of VolumeListCommand command.
func (v *VolumeListCommand) runVolumeList(args []string) error {
log.With(nil).Debugf("list the volumes")
ctx := context.Background()
apiClient := v.cli.Client()
volumeFilterArgs, err := filters.FromFilterOpts(v.filter)
if err != nil {
return err
}
volumeList, err := apiClient.VolumeList(ctx, volumeFilterArgs)
if err != nil {
return err
}
if (v.size || v.mountPoint) && v.quiet {
return fmt.Errorf("Conflicting options: --size (or --mountpoint) and -q")
}
display := v.cli.NewTableDisplay()
displayHead := []string{"VOLUME NAME"}
if !v.quiet {
displayHead = append([]string{"DRIVER"}, displayHead...)
if v.size {
displayHead = append(displayHead, "SIZE")
}
if v.mountPoint {
displayHead = append(displayHead, "MOUNT POINT")
}
}
display.AddRow(displayHead)
for _, volume := range volumeList.Volumes {
displayLine := []string{volume.Name}
if !v.quiet {
displayLine = append([]string{volume.Driver}, displayLine...)
if v.size {
if s, ok := volume.Status["size"]; ok {
displayLine = append(displayLine, s.(string))
} else {
displayLine = append(displayLine, "ulimit")
}
}
if v.mountPoint {
displayLine = append(displayLine, volume.Mountpoint)
}
}
display.AddRow(displayLine)
}
display.Flush()
return nil
}
// volumeListExample shows examples in volume list command, and is used in auto-generated cli docs.
func volumeListExample() string {
return `$ pouch volume list
DRIVER VOLUME NAME
local pouch-volume-1
local pouch-volume-2
local pouch-volume-3
$ pouch volume list --quiet
VOLUME NAME
pouch-volume-1
pouch-volume-2
pouch-volume-3`
}