forked from openebs-archive/maya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtask_functions.go
313 lines (280 loc) · 8.9 KB
/
runtask_functions.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
/*
Copyright 2018 The OpenEBS Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package templatefuncs
import (
"encoding/json"
"strings"
"text/template"
cmd "github.com/openebs/maya/pkg/task/v1alpha1"
"github.com/openebs/maya/pkg/util"
)
// delete returns a new instance of delete based runtask command
//
// Examples:
// ---------
// {{- delete jiva volume | run -}}
// {{- delete cstor volume | run -}}
func delete(ml ...cmd.RunCommandMiddleware) *cmd.RunCommand {
return cmd.RunCommandMiddlewareList(ml).Update(cmd.Command().DeleteAction())
}
// get returns a new instance of get based runtask command
//
// Examples:
// ---------
// {{- get jiva volume | run -}}
// {{- get cstor volume | run -}}
// {{- get http | url $url | run -}}
func get(ml ...cmd.RunCommandMiddleware) *cmd.RunCommand {
return cmd.RunCommandMiddlewareList(ml).Update(cmd.Command().GetAction())
}
// list returns a new instance of list based runtask command
//
// Examples:
// ---------
// {{- list jiva volume | run -}}
// {{- list cstor volume | run -}}
func list(ml ...cmd.RunCommandMiddleware) *cmd.RunCommand {
return cmd.RunCommandMiddlewareList(ml).Update(cmd.Command().ListAction())
}
// create returns a new instance of create based runtask command
//
// Examples:
// ---------
// {{- create jiva volume | run -}}
// {{- create cstor volume | run -}}
func create(ml ...cmd.RunCommandMiddleware) *cmd.RunCommand {
return cmd.RunCommandMiddlewareList(ml).Update(cmd.Command().CreateAction())
}
// post returns a new instance of post based runtask command
//
// Examples:
// ---------
// {{- post http | url $url -}}
func post(ml ...cmd.RunCommandMiddleware) *cmd.RunCommand {
return cmd.RunCommandMiddlewareList(ml).Update(cmd.Command().PostAction())
}
// put returns a new instance of put based runtask command
//
// Examples:
// ---------
// {{- put http | url $url -}}
func put(ml ...cmd.RunCommandMiddleware) *cmd.RunCommand {
return cmd.RunCommandMiddlewareList(ml).Update(cmd.Command().PutAction())
}
// patch returns a new instance of patch based runtask command
//
// Examples:
// ---------
// {{- patch jiva volume | run -}}
// {{- patch cstor volume | run -}}
func patch(ml ...cmd.RunCommandMiddleware) *cmd.RunCommand {
return cmd.RunCommandMiddlewareList(ml).Update(cmd.Command().PatchAction())
}
// update returns a new instance of update based runtask command
//
// Examples:
// ---------
// {{- update jiva volume | run -}}
// {{- update cstor volume | run -}}
func update(ml ...cmd.RunCommandMiddleware) *cmd.RunCommand {
return cmd.RunCommandMiddlewareList(ml).Update(cmd.Command().UpdateAction())
}
// http returns a new instance of http based runtask command
//
// Examples:
// ---------
// {{- post http | url $url | run -}}
// {{- get http | url $url | run -}}
func http() cmd.RunCommandMiddleware {
return cmd.HttpCategory()
}
// jiva returns a new instance of jiva based runtask command
//
// Examples:
// ---------
// {{- get jiva volume | run -}}
// {{- list jiva volume | run -}}
func jiva() cmd.RunCommandMiddleware {
return cmd.JivaCategory()
}
// cstor returns a new instance of cstor based runtask command
//
// Examples:
// ---------
// {{- get cstor volume | run -}}
// {{- list cstor volume | run -}}
func cstor() cmd.RunCommandMiddleware {
return cmd.CstorCategory()
}
// volume returns a new instance of volume based runtask command
//
// Examples:
// ---------
// {{- get cstor volume | run -}}
// {{- list cstor volume | run -}}
// {{- update jiva volume | run -}}
// {{- create jiva volume | run -}}
func volume() cmd.RunCommandMiddleware {
return cmd.VolumeCategory()
}
// snapshot returns a new instance of snapshot based runtask command
//
// Examples:
// {{- create cstor snapshot | run -}}
func snapshot() cmd.RunCommandMiddleware {
return cmd.SnapshotCategory()
}
// slect returns the values of the specified paths post runtask command
// execution
//
// Examples:
// ---------
// {{- select "all" | get cstor volume | run -}}
// {{- select "name" | list cstor volume | run -}}
// {{- select "name" "namespace" | update jiva volume | run -}}
// {{- select ".metadata.name" | create jiva volume | run -}}
func slect(paths ...string) cmd.RunCommandMiddleware {
if len(paths) == 0 {
paths = append(paths, "all")
}
return cmd.Select(paths)
}
// toJsonObj marshals and returns the json representation of value interface
//
// {{- "{'name':'openebs'}" | toJsonObj -}}
func toJsonObj(value interface{}) (b []byte) {
b, _ = json.Marshal(value)
return
}
// withOption sets the provided <key,value> pair as an input data to run command
//
// {{- delete jiva volume | withOption "url" $url | withOption "name" "myvol" | run -}}
func withOption(key string, value interface{}, given *cmd.RunCommand) (updated *cmd.RunCommand) {
return cmd.WithData(given, key, value)
}
// run executes the runtask command
//
// Examples:
// ---------
// {{- get jiva volume | run -}}
func run(given *cmd.RunCommand) cmd.RunCommandResult {
return given.Run()
}
// runAlways will always execute the provided command
//
// Examples:
// ---------
// {{- $cond := runAlways .TaskResult -}}
// {{- $store := storeAt .TaskResult -}}
// {{- $runner := storeRunnerCond $store $cond -}}
func runAlways() cmd.RunCondition {
return cmd.RunAlways()
}
// storeAt sets the storage to save the command's execution result(s)
//
// Examples:
// ---------
// {{- $store := storeAt .TaskResult -}}
// {{- $runner := storeRunner $store -}}
func storeAt(kv map[string]interface{}) cmd.BucketStorageCondition {
return cmd.KVStore(kv)
}
// storeRunner provides a utility that helps executing a run command as well as
// saving the execution result(s)
//
// Examples:
// ---------
// {{- $store := storeAt .TaskResult -}}
// {{- $runner := storeRunner $store -}}
func storeRunner(store cmd.BucketStorageCondition) cmd.Interface {
return cmd.StoreCommand(store)
}
// storeRunnerCond provides a utility that helps executing a run command as well
// as saving the execution result(s)
//
// Examples:
// ---------
// {{- $cond := runAlways .TaskResult -}}
// {{- $store := storeAt .TaskResult -}}
// {{- $runner := storeRunnerCond $store $cond -}}
func storeRunnerCond(store cmd.BucketStorage, cond cmd.RunCondition) cmd.Interface {
return cmd.StoreCommandCondition(store, cond)
}
// runas executes the runtask command by mapping it against a provided id
//
// Examples:
// ---------
// {{- $store := storeAt .TaskResult -}}
// {{- $runner := storeRunner $store -}}
// {{- get jiva volume | runas "getJivaVol" $runner -}}
// {{- get cstor volume | runas "getCstorVol" $runner -}}
func runas(id string, runner cmd.Interface, given *cmd.RunCommand) cmd.RunCommandResult {
runner.Map(id, given)
return runner.Run()
}
// runlog executes the runtask command and stores the result as well as other
// information at the provided paths
//
// Examples:
// ---------
// {{- get jiva volume | runlog "getj.result" "getj.extras" .Values -}}
//
// NOTES:
// -----------
// '.Values' is of type map[string]interface{} & is provided to the
// template before template execution
//
// Once above template gets executed '.Values' will have .Values.getj.result
// and .Values.getj.extras set with result and extras due to runtask command
// execution
func runlog(resultpath, debugpath string, store map[string]interface{}, given *cmd.RunCommand) (res cmd.RunCommandResult) {
res = run(given)
resultpath = strings.TrimPrefix(resultpath, ".")
debugpath = strings.TrimPrefix(debugpath, ".")
// result store
resultlocator := strings.Split(resultpath, ".")
util.SetNestedField(store, res.Result(), resultlocator...)
// debug store
debuglocator := strings.Split(debugpath, ".")
util.SetNestedField(store, res.Debug(), debuglocator...)
return
}
// runCommandFuncs returns the set of runtask command based template functions
func runCommandFuncs() template.FuncMap {
return template.FuncMap{
"delete": delete,
"get": get,
"lst": list,
"create": create,
"post": post,
"patch": patch,
"update": update,
"put": put,
"jiva": jiva,
"cstor": cstor,
"volume": volume,
"http": http,
"withOption": withOption,
"withoption": withOption,
"run": run,
"runlog": runlog,
"select": slect,
"snapshot": snapshot,
"storeAt": storeAt,
"storeRunner": storeRunner,
"storeRunnerCond": storeRunnerCond,
"runas": runas,
"runAlways": runAlways,
"toJsonObj": toJsonObj,
}
}