/
executor.go
312 lines (270 loc) · 8.55 KB
/
executor.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
/*
* Copyright (c) 2021, 2023 Oracle and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at
* https://oss.oracle.com/licenses/upl.
*/
package cmd
import (
"encoding/json"
"errors"
"fmt"
"github.com/oracle/coherence-cli/pkg/config"
"github.com/oracle/coherence-cli/pkg/constants"
"github.com/oracle/coherence-cli/pkg/fetcher"
"github.com/oracle/coherence-cli/pkg/utils"
"github.com/spf13/cobra"
"strconv"
"strings"
"time"
)
var (
executorAttributeName string
executorAttributeValue string
executorValidAttributes = []string{"traceLogging"}
)
const (
provideExecutorName = "you must provide an executor name"
cannotFindExecutors = "unable to find any executors in this cluster"
stringTrue = "true"
stringFalse = "false"
)
// getExecutorsCmd represents the get executors command.
var getExecutorsCmd = &cobra.Command{
Use: "executors",
Short: "display executors for a cluster",
Long: `The 'get executors' command displays the executors for a cluster.`,
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
var (
dataFetcher fetcher.Fetcher
executorsResult []byte
)
connection, dataFetcher, err := GetConnectionAndDataFetcher()
if err != nil {
return err
}
for {
var executors config.Executors
executors, err = getExecutorDetails(dataFetcher, OutputFormat == constants.TABLE)
if err != nil {
return err
}
if isJSONPathOrJSON() {
// encode the struct so we get the updated fields
executorsResult, err = json.Marshal(executors)
if err != nil {
return err
}
if err = processJSONOutput(cmd, executorsResult); err != nil {
return err
}
} else {
printWatchHeader(cmd)
cmd.Println(FormatCurrentCluster(connection))
cmd.Print(FormatExecutors(executors.Executors, true))
}
// check to see if we should exit if we are not watching
if !isWatchEnabled() {
break
}
// we are watching services so sleep and then repeat until CTRL-C
time.Sleep(time.Duration(watchDelay) * time.Second)
}
return nil
},
}
// describeExecutorCmd represents the describe executor command.
var describeExecutorCmd = &cobra.Command{
Use: "executor executor-name",
Short: "describe an executor",
Long: `The 'describe executor' command shows information related to a specific executor.`,
ValidArgsFunction: completionExecutors,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
displayErrorAndExit(cmd, provideExecutorName)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
var (
err error
dataFetcher fetcher.Fetcher
connection string
executors = config.Executors{}
finalExecutors = config.Executors{}
executorData []byte
executor = args[0]
result string
)
// retrieve the current context or the value from "-c"
connection, dataFetcher, err = GetConnectionAndDataFetcher()
if err != nil {
return err
}
executors, err = getExecutorDetails(dataFetcher, false)
if err != nil {
return err
}
if len(executors.Executors) == 0 {
return errors.New(cannotFindExecutors)
}
// filter out any executors without the name
for _, value := range executors.Executors {
if value.Name == executor {
finalExecutors.Executors = append(finalExecutors.Executors, value)
}
}
if len(finalExecutors.Executors) == 0 {
return fmt.Errorf("unable to find executor with name %s", executor)
}
executorData, err = json.Marshal(finalExecutors)
if err != nil {
return err
}
if strings.Contains(OutputFormat, constants.JSONPATH) {
result, err := utils.GetJSONPathResults(executorData, OutputFormat)
if err != nil {
return err
}
cmd.Println(result)
} else if OutputFormat == constants.JSON {
cmd.Println(string(executorData))
} else {
cmd.Println(FormatCurrentCluster(connection))
cmd.Println("EXECUTOR DETAILS")
cmd.Println("----------------")
for _, executor := range finalExecutors.Executors {
executorData, err = json.Marshal(executor)
if err != nil {
return err
}
result, err = FormatJSONForDescribe(executorData, true, "Name", "Member Id")
if err != nil {
return err
}
cmd.Println(result)
}
}
return nil
},
}
// setExecutorCmd represents the set executor command.
var setExecutorCmd = &cobra.Command{
Use: "executor executor-name",
Short: "set an executor attribute",
Long: `The 'set executor' command sets an attribute for a specific executor across
all nodes. The following attribute names are allowed: traceLogging.`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
displayErrorAndExit(cmd, "you must provide an executor name")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
var (
err error
dataFetcher fetcher.Fetcher
executors = config.Executors{}
finalExecutors = config.Executors{}
executor = args[0]
actualValue interface{}
)
if !utils.SliceContains(executorValidAttributes, executorAttributeName) {
return fmt.Errorf("attribute name %s is invalid. Please choose one of\n%v",
executorAttributeName, executorValidAttributes)
}
if executorAttributeName == executorValidAttributes[0] {
if executorAttributeValue != stringTrue && executorAttributeValue != stringFalse {
return fmt.Errorf("value for %s should be true or false", executorAttributeName)
}
actualValue, _ = strconv.ParseBool(executorAttributeValue)
}
// retrieve the current context or the value from "-c"
_, dataFetcher, err = GetConnectionAndDataFetcher()
if err != nil {
return err
}
executors, err = getExecutorDetails(dataFetcher, false)
if err != nil {
return err
}
if len(executors.Executors) == 0 {
return errors.New("unable to find any executors in this cluster")
}
// filter out any executors without the name
for _, value := range executors.Executors {
if value.Name == executor {
finalExecutors.Executors = append(finalExecutors.Executors, value)
}
}
if len(finalExecutors.Executors) == 0 {
return fmt.Errorf("unable to find executor with name %s", executor)
}
// confirm the operation
if !confirmOperation(cmd, fmt.Sprintf("Are you sure you want to set the value of attribute %s to %s for %s? (y/n) ",
executorAttributeName, executorAttributeValue, executor)) {
return nil
}
_, err = dataFetcher.SetExecutorAttribute(executor, executorAttributeName, actualValue)
if err != nil {
return err
}
cmd.Println(OperationCompleted)
return nil
},
}
// getExecutorDetails returns the executor details for the cluster
// if summary is true then the data is summarised by name.
func getExecutorDetails(dataFetcher fetcher.Fetcher, summary bool) (config.Executors, error) {
var (
executorsResult []byte
executors = config.Executors{}
err error
emptyResult = config.Executors{}
)
executorsResult, err = dataFetcher.GetExecutorsJSON()
if err != nil {
return emptyResult, err
}
// return if no executors found
if len(executorsResult) == 0 {
return emptyResult, nil
}
err = json.Unmarshal(executorsResult, &executors)
if err != nil {
return emptyResult, utils.GetError("unable to decode executors details", err)
}
// only include a summary of the executors
if summary {
finalExecutors := config.Executors{}
for _, value := range executors.Executors {
// find the executor in the finalExecutors slice
index := -1
for i, v := range finalExecutors.Executors {
if v.Name == value.Name {
index = i
}
}
if index == -1 {
// not found, do append the current executor
value.MemberCount = 1
finalExecutors.Executors = append(finalExecutors.Executors, value)
} else {
// update the count
finalExecutors.Executors[index].MemberCount++
finalExecutors.Executors[index].TasksInProgressCount += value.TasksInProgressCount
finalExecutors.Executors[index].TasksCompletedCount += value.TasksCompletedCount
finalExecutors.Executors[index].TasksRejectedCount += value.TasksRejectedCount
}
}
return finalExecutors, nil
}
return executors, nil
}
func init() {
setExecutorCmd.Flags().BoolVarP(&automaticallyConfirm, "yes", "y", false, confirmOptionMessage)
setExecutorCmd.Flags().StringVarP(&executorAttributeName, "attribute", "a", "", "attribute name to set")
_ = setExecutorCmd.MarkFlagRequired("attribute")
setExecutorCmd.Flags().StringVarP(&executorAttributeValue, "value", "v", "", "attribute value to set")
_ = setExecutorCmd.MarkFlagRequired("value")
}