-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvolumes.go
More file actions
273 lines (243 loc) · 8.99 KB
/
volumes.go
File metadata and controls
273 lines (243 loc) · 8.99 KB
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
package parser
import (
"github.com/drycc/workflow-cli/internal/commands"
"github.com/drycc/workflow-cli/internal/completion"
"github.com/drycc/workflow-cli/internal/template"
"github.com/drycc/workflow-cli/pkg/i18n"
"github.com/spf13/cobra"
)
// NewVolumesCommand creates the volumes command
func NewVolumesCommand(cmdr *commands.DryccCmd) *cobra.Command {
cmd := &cobra.Command{
Use: "volumes",
Short: i18n.T("Manage volumes for your applications"),
RunE: func(cmd *cobra.Command, args []string) error {
results, _ := commands.ResponseLimit(limit)
return cmdr.VolumesList(app, results)
},
}
cmd.PersistentFlags().StringVarP(&app, "app", "a", "", i18n.T("The uniquely identifiable name for the application"))
cmd.Flags().IntVarP(&limit, "limit", "l", 0, i18n.T("The maximum number of results to display"))
appCompletion := completion.AppCompletion{ArgsLen: -1, ConfigFile: &cmdr.ConfigFile}
cmd.RegisterFlagCompletionFunc("app", appCompletion.CompletionFunc)
cmd.AddCommand(volumesListCommand(cmdr))
cmd.AddCommand(volumesAddCommand(cmdr))
cmd.AddCommand(volumesExpandCommand(cmdr))
cmd.AddCommand(volumesInfoCommand(cmdr))
cmd.AddCommand(volumesRemoveCommand(cmdr))
cmd.AddCommand(volumesClientCommand(cmdr))
cmd.AddCommand(volumesMountCommand(cmdr))
cmd.AddCommand(volumesUnmountCommand(cmdr))
return cmd
}
func volumesAddCommand(cmdr *commands.DryccCmd) *cobra.Command {
var flags struct {
vtype string
name string
size string
nfsServer string
nfsPath string
ossServer string
ossBucket string
ossAccessKey string
ossSecretKey string
}
cmd := &cobra.Command{
Use: "add <name> <size>",
Example: template.CustomExample(
"drycc volumes add myvolume 1G",
map[string]string{
"<name>": i18n.T("The volume name"),
"<size>": i18n.T("The volume size, such as '500G'"),
},
),
Short: i18n.T("Create a volume for the application"),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
flags.name = args[0]
flags.size = args[1]
parameters := make(map[string]interface{})
if flags.vtype == "nfs" {
parameters["nfs"] = map[string]interface{}{
"server": flags.nfsServer,
"path": flags.nfsPath,
}
} else if flags.vtype == "oss" {
parameters["oss"] = map[string]interface{}{
"server": flags.ossServer,
"bucket": flags.ossBucket,
"access_key": flags.ossAccessKey,
"secret_key": flags.ossSecretKey,
}
}
return cmdr.VolumesCreate(app, flags.name, flags.vtype, flags.size, parameters)
},
}
cmd.Flags().StringVarP(&flags.vtype, "type", "t", "csi", i18n.T("The volume type, such as csi, nfs, oss"))
cmd.Flags().StringVar(&flags.nfsServer, "nfs-server", "", i18n.T("The hostname or ip address of the nfs server"))
cmd.Flags().StringVar(&flags.nfsPath, "nfs-path", "", i18n.T("Path that is exported by the nfs server"))
cmd.Flags().StringVar(&flags.ossServer, "oss-server", "", i18n.T("Endpoint url for object storage service"))
cmd.Flags().StringVar(&flags.ossBucket, "oss-bucket", "", i18n.T("Bucket name in object storage"))
cmd.Flags().StringVar(&flags.ossAccessKey, "oss-access-key", "", i18n.T("Access key id for authentication"))
cmd.Flags().StringVar(&flags.ossSecretKey, "oss-secret-key", "", i18n.T("secret access key for authentication"))
cmd.Flags().SortFlags = false
cmd.MarkFlagsRequiredTogether("nfs-server", "nfs-path")
cmd.MarkFlagsRequiredTogether("oss-server", "oss-bucket", "oss-access-key", "oss-secret-key")
volumeTypeCompletion := completion.VolumeTypeCompletion{ArgsLen: -1, ConfigFile: &cmdr.ConfigFile}
cmd.RegisterFlagCompletionFunc("type", volumeTypeCompletion.CompletionFunc)
return cmd
}
func volumesExpandCommand(cmdr *commands.DryccCmd) *cobra.Command {
var flags struct {
name string
size string
}
volumeCompletion := completion.VolumeCompletion{AppID: &app, ArgsLen: 0, ConfigFile: &cmdr.ConfigFile}
cmd := &cobra.Command{
Use: "expand <name> <size>",
Example: template.CustomExample(
"drycc volumes expand myvolume 2G",
map[string]string{
"<name>": i18n.T("The volume name"),
"<size>": i18n.T("The volume size, such as '500G'"),
},
),
Short: i18n.T("Expand a volume capacity for the application"),
Args: cobra.ExactArgs(2),
ValidArgsFunction: volumeCompletion.CompletionFunc,
RunE: func(cmd *cobra.Command, args []string) error {
flags.name = args[0]
flags.size = args[1]
return cmdr.VolumesExpand(app, flags.name, flags.size)
},
}
return cmd
}
func volumesListCommand(cmdr *commands.DryccCmd) *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: i18n.T("List volumes in the application"),
RunE: func(cmd *cobra.Command, args []string) error {
results, _ := commands.ResponseLimit(limit)
return cmdr.VolumesList(app, results)
},
}
cmd.Flags().IntVarP(&limit, "limit", "l", 0, i18n.T("The maximum number of results to display"))
return cmd
}
func volumesInfoCommand(cmdr *commands.DryccCmd) *cobra.Command {
var flags struct {
name string
}
volumeCompletion := completion.VolumeCompletion{AppID: &app, ArgsLen: 0, ConfigFile: &cmdr.ConfigFile}
cmd := &cobra.Command{
Use: "info <name>",
Example: template.CustomExample(
"drycc volumes info myvolume",
map[string]string{
"<name>": i18n.T("The volume name to be info"),
},
),
Short: i18n.T("Print information about a volume"),
Args: cobra.ExactArgs(1),
ValidArgsFunction: volumeCompletion.CompletionFunc,
RunE: func(cmd *cobra.Command, args []string) error {
flags.name = args[0]
return cmdr.VolumesInfo(app, flags.name)
},
}
return cmd
}
func volumesRemoveCommand(cmdr *commands.DryccCmd) *cobra.Command {
var flags struct {
name string
}
volumeCompletion := completion.VolumeCompletion{AppID: &app, ArgsLen: 0, ConfigFile: &cmdr.ConfigFile}
cmd := &cobra.Command{
Use: "remove <name>",
Example: template.CustomExample(
"drycc volumes remove myvolume",
map[string]string{
"<name>": i18n.T("The volume name to be removed"),
},
),
Short: i18n.T("Delete a volume from the application"),
Args: cobra.ExactArgs(1),
ValidArgsFunction: volumeCompletion.CompletionFunc,
RunE: func(cmd *cobra.Command, args []string) error {
flags.name = args[0]
return cmdr.VolumesDelete(app, flags.name)
},
}
return cmd
}
func volumesClientCommand(cmdr *commands.DryccCmd) *cobra.Command {
volumesCmdCompletion := completion.VolumesCmdCompletion{ArgsLen: 0, ConfigFile: &cmdr.ConfigFile}
cmd := &cobra.Command{
Use: "client <cmd> <args>...",
Example: template.CustomExample(
"drycc volumes client ls vol://myvolume/",
map[string]string{
"<cmd>": i18n.T(`ls list volume files
cp copy volume files
rm remove volume files`),
"<args>": i18n.T(`arguments for running commands, when cmd is 'cp', args should be '[source] [dest]'.
volume path format 'vol://volumename/', '/' is equivalent to the mount path.`),
},
),
Short: i18n.T("The client used to manage volume files"),
Args: cobra.MinimumNArgs(2),
ValidArgsFunction: volumesCmdCompletion.CompletionFunc,
RunE: func(cmd *cobra.Command, args []string) error {
cmdType := args[0]
cmdArgs := args[1:]
return cmdr.VolumesClient(app, cmdType, cmdArgs...)
},
}
return cmd
}
func volumesMountCommand(cmdr *commands.DryccCmd) *cobra.Command {
volumesMountCompletion := completion.VolumesMountCompletion{AppID: &app, ArgsLen: 0, ConfigFile: &cmdr.ConfigFile}
cmd := &cobra.Command{
Use: "mount <name> <ptype>=<path>...",
Example: template.CustomExample(
"drycc volumes mount myvolume web=/data",
map[string]string{
"<name>": i18n.T("The volume name"),
"<ptype>": i18n.T("The process name as defined in your Procfile"),
"<path>": i18n.T("The filesystem path"),
},
),
Short: i18n.T("Mount a volume to process of the application"),
Args: cobra.MinimumNArgs(2),
ValidArgsFunction: volumesMountCompletion.CompletionFunc,
RunE: func(cmd *cobra.Command, args []string) error {
name := args[0]
mountSpecs := args[1:]
return cmdr.VolumesMount(app, name, mountSpecs)
},
}
return cmd
}
func volumesUnmountCommand(cmdr *commands.DryccCmd) *cobra.Command {
volumesUnmountCompletion := completion.VolumesUnmountCompletion{AppID: &app, ArgsLen: 0, ConfigFile: &cmdr.ConfigFile}
cmd := &cobra.Command{
Use: "unmount <name> <ptype>...",
Example: template.CustomExample(
"drycc volumes unmount myvolume web worker",
map[string]string{
"<name>": i18n.T("The volume name"),
"<ptype>": i18n.T("The process name as defined in your Procfile"),
},
),
Short: i18n.T("Unmount a volume from process of the application"),
Args: cobra.MinimumNArgs(2),
ValidArgsFunction: volumesUnmountCompletion.CompletionFunc,
RunE: func(cmd *cobra.Command, args []string) error {
name := args[0]
ptypes := args[1:]
return cmdr.VolumesUnmount(app, name, ptypes)
},
}
return cmd
}