-
Notifications
You must be signed in to change notification settings - Fork 18
/
processTasks.go
289 lines (241 loc) · 9.34 KB
/
processTasks.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
package main
import (
"fmt"
"os"
"strings"
"sync"
"time"
"github.com/newrelic/newrelic-diagnostics-cli/output/color"
"github.com/newrelic/newrelic-diagnostics-cli/attach"
"github.com/newrelic/newrelic-diagnostics-cli/config"
log "github.com/newrelic/newrelic-diagnostics-cli/logger"
"github.com/newrelic/newrelic-diagnostics-cli/output"
"github.com/newrelic/newrelic-diagnostics-cli/registration"
"github.com/newrelic/newrelic-diagnostics-cli/suites"
"github.com/newrelic/newrelic-diagnostics-cli/tasks"
)
func processTasksToRun() {
log.Debugf("There are %d tasks in this queue\n", len(registration.Work.WorkQueue))
if config.Flags.Tasks != "" {
taskIdentifiers := processFlagsTasks(config.Flags.Tasks)
registration.AddTasksByIdentifiers(taskIdentifiers)
} else if config.Flags.Suites != "" {
matchedSuites, err := processFlagsSuites(config.Flags.Suites, os.Args)
if err != nil {
log.Infof("\nError:\n%s", err.Error())
os.Exit(1)
}
var suiteNameList []string
for _, suite := range matchedSuites {
suiteNameList = append(suiteNameList, suite.DisplayName)
}
log.Infof("%s %s\n", color.ColorString(color.White, "\nExecuting following diagnostic task suites:"), strings.Join(suiteNameList, ", "))
taskIdentifiers := suites.DefaultSuiteManager.FindTasksBySuites(matchedSuites)
registration.AddTasksByIdentifiers(taskIdentifiers)
} else if !config.Flags.Run { // only run all tasks if not running a script
registration.AddAllToQueue()
}
log.Debugf("There are %d tasks in this queue\n", len(registration.Work.WorkQueue))
registration.CompleteTaskRegistration()
}
func processTasks(options tasks.Options, overrides []override, wg *sync.WaitGroup) {
log.Debugf("work queue has %d items\n", len(registration.Work.WorkQueue))
taskCount := 0
for task := range registration.Work.WorkQueue {
taskCount++
if taskCount == 1 && !config.Flags.VeryQuiet {
// writes to the screen
output.WriteOutputHeader()
}
var taskOptions = make(map[string]string)
// Loop through incoming options to assign out to the named task Options to avoid carrying in the wrong options
for key, value := range options.Options {
taskOptions[key] = value
}
namedTaskOptions := tasks.Options{Options: taskOptions}
log.Debug("Running :", task.Identifier())
log.Debug("Incoming options are", options)
// Check for dependencies on the task and include results if dependent
dependentResults := make(map[string]tasks.Result)
for _, depIdent := range task.Dependencies() {
log.Debug("dependency for processing: ", depIdent)
dependentResults[depIdent] = registration.Work.Results[depIdent].Result
}
//Parse overrides to detect which task we are running
for _, value := range overrides {
// Initialize the taskOptions object
log.Debugf("override %s: %s", value.Identifier, value.value)
if strings.EqualFold(value.Identifier.String(), task.Identifier().String()) {
log.Debug("Adding override to task namedTaskOptions", value.key, ":", value.value)
namedTaskOptions.Options[value.key] = value.value
}
}
log.Debug("Starting", task.Identifier(), "with options", namedTaskOptions)
var result tasks.Result
// Check for an option key to map to Status or Payload and if so, bypass task execution
overrideEnabled := false
if _, ok := namedTaskOptions.Options["Status"]; ok {
log.Debug("Override Status passed in for ", task.Identifier(), "Value of ", namedTaskOptions.Options["Status"])
switch status := strings.ToLower(namedTaskOptions.Options["Status"]); status {
case "success":
result.Status = tasks.Success
case "warning":
result.Status = tasks.Warning
case "failure":
result.Status = tasks.Failure
case "info":
result.Status = tasks.Info
case "error":
result.Status = tasks.Error
case "none":
result.Status = tasks.None
default:
log.Info("Attempted to set status override to invalid status", namedTaskOptions.Options["Status"])
}
result.Summary += "Status set by override to " + namedTaskOptions.Options["Status"] + "\n"
overrideEnabled = true
}
if _, ok := namedTaskOptions.Options["Payload"]; ok {
log.Debug("Override Payload passed in for ", task.Identifier())
result.Payload = namedTaskOptions.Options["Payload"]
result.Summary += "Payload set by override\n"
overrideEnabled = true
}
if !overrideEnabled {
result = task.Execute(namedTaskOptions, dependentResults)
}
taskResult := registration.TaskResult{
Task: task,
Result: result,
WasOverride: overrideEnabled,
}
registration.Work.Results[task.Identifier().String()] = taskResult //This should be done in output.go but due to async causes issues
registration.Work.ResultsChannel <- taskResult
if len(result.FilesToCopy) > 0 {
log.Debug(" - writing result to file channel")
registration.Work.FilesChannel <- taskResult
}
} // end of loop to run tasks in
log.Debug("Closing task channel")
close(registration.Work.ResultsChannel)
close(registration.Work.FilesChannel)
log.Debug("Decrementing wait group in processTasks.")
wg.Done()
}
func processFlagsTasks(flagValue string) []string {
var validatedIdentifiers []string
identifiers := strings.Split(flagValue, ",")
for _, ident := range identifiers {
if len(ident) > 0 { //This removes the adding of a blank identifier
validatedIdentifiers = append(validatedIdentifiers, ident)
}
}
return validatedIdentifiers
}
func getLicenseKey(thisResult tasks.Result) ([]string, error) {
licenseKeyToSources, ok := thisResult.Payload.(map[string][]string)
if !ok {
return nil, fmt.Errorf("unable to retrieve license Key")
}
log.Debug("Valid License Key(s) provided")
validLicenseKeys := []string{}
for lk := range licenseKeyToSources {
validLicenseKeys = append(validLicenseKeys, lk)
}
return validLicenseKeys, nil
}
func processFlagsSuites(flagValue string, args []string) ([]suites.Suite, error) {
suiteIdentifiers := sanitizeAndParseFlagValue(flagValue)
sanitizedArgs := sanitizeOSArgs(args)
matchedSuites, unMatchedSuites := suites.DefaultSuiteManager.FindSuitesByIdentifiers(suiteIdentifiers)
suites.DefaultSuiteManager.AddSelectedSuites(matchedSuites)
//arguments passed that were intended to be suites but where not include due to misformat
unknownArgs := suites.DefaultSuiteManager.CaptureOutOfPlaceArgs(sanitizedArgs, suiteIdentifiers)
var errorMsg string
if len(unMatchedSuites) > 0 {
unMatchedSuitesList := " \"" + strings.Join(unMatchedSuites, "\"\n \"") + "\"\n"
errorMsg = fmt.Sprintf("\n- Could not find the following task suites:\n\n%s", unMatchedSuitesList)
}
if len(unknownArgs) > 0 {
unknownArgsList := " \"" + strings.Join(unknownArgs, "\"\n \"") + "\"\n"
errorMsg += fmt.Sprintf("\n- You may have attempted to pass these arguments as suites:\n\n%v", unknownArgsList)
}
if len(errorMsg) > 1 {
errorMsg += "\nPlease use the `--help suites` to check for available suites and proper formatting \n"
return matchedSuites, fmt.Errorf(errorMsg)
}
return matchedSuites, nil
}
func processUploads() {
log.Debug("processing uploads")
//get timestamp to use attachment
timestamp := time.Now().UTC().Format(time.RFC3339)
if config.Flags.YesToAll {
checkAttachmentFlags(timestamp)
return
}
if config.Flags.APIKey != "" || config.Flags.AutoAttach {
question := "We've created nrdiag-output.zip and nrdiag-output.json\n" +
"Do you want to upload these to your New Relic account?"
if promptUser(question) {
checkAttachmentFlags(timestamp)
}
}
}
func checkAttachmentFlags(timestamp string) {
var ValidLicenseKeys []string
attachDeps := new(attach.AttachDeps)
if config.Flags.APIKey != "" {
//hit DAS
apiKey := config.Flags.APIKey
attach.Upload("upload_api", apiKey, timestamp, attachDeps)
} else if config.Flags.AutoAttach { //check for validated license keys and upload with those keys
for _, taskResult := range registration.Work.Results {
if taskResult.Task.Identifier().String() == "Base/Config/ValidateLicenseKey" && taskResult.Result.Status == tasks.Success {
LicenseKeys, err := getLicenseKey(taskResult.Result)
if err != nil {
log.Debug("Could not retrieve a license key, automatic attachment will not be possible")
return
} else {
ValidLicenseKeys = LicenseKeys
}
} else if taskResult.Task.Identifier().String() == "Base/Config/ValidateLicenseKey" && taskResult.Result.Status != tasks.Success {
log.Info("No valid license keys specified, upload to New Relic Account cannot be completed")
return
}
}
for _, licenseKey := range ValidLicenseKeys {
log.Info("Uploading files by Account ID...")
attachDeps := new(attach.AttachDeps)
attach.Upload("upload_s3", licenseKey, timestamp, attachDeps)
}
}
}
func sanitizeOSArgs(osArgs []string) []string {
var sanitizedArgs []string
for _, osArg := range osArgs {
trimmedArg := strings.TrimSpace(osArg)
if trimmedArg == "" {
continue
}
if strings.Contains(trimmedArg, ",") {
splitArgs := strings.Split(trimmedArg, ",")
sanitizedArgs = append(sanitizedArgs, splitArgs...)
continue
}
sanitizedArgs = append(sanitizedArgs, trimmedArg)
}
return sanitizedArgs
}
func sanitizeAndParseFlagValue(flagValue string) []string {
var sanitizedArgs []string
parsedArgs := strings.Split(flagValue, ",")
for _, arg := range parsedArgs {
trimmedArg := strings.TrimSpace(arg)
if trimmedArg == "" {
continue
}
sanitizedArgs = append(sanitizedArgs, trimmedArg)
}
return sanitizedArgs
}