/
reporter.go
463 lines (368 loc) · 9.43 KB
/
reporter.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
package goansible
import (
"fmt"
"io"
"os"
"strings"
"time"
"github.com/mgutz/ansi"
)
type Reporter interface {
StartTasks(r *Runner)
FinishTasks(r *Runner)
StartHandlers(r *Runner)
FinishHandlers(r *Runner)
FinishAll(r *Runner)
EndReporter(r *Runner, err error)
StartTask(task *Task, name, args string, vars Vars)
FinishTask(task *Task, res *Result)
//FinishAsyncTask(act *AsyncAction)
//Progress(str string)
//JSONProgress(data []byte) error
}
/*
type ProgressReporter interface {
Progress(string)
JSONProgress(data []byte) error
}
*/
type CLIReporter struct {
out io.Writer
Output bool
Debug bool
Start time.Time
}
type StructReporter struct {
Hostname string
ServerType string
Project string
Start time.Time
TasksFinishDur time.Duration
FinishDur time.Duration
TaskCount int
Out []TaskResult
}
var sCLIReporter *CLIReporter = &CLIReporter{out: os.Stdout}
var sJsonChunkReporter *JsonChunkReporter = &JsonChunkReporter{out: os.Stdout}
var sStructReporter *StructReporter = &StructReporter{}
var failMsg, changedMsg, okMsg func(string) string
func init() {
failMsg = ansi.ColorFunc("red+b")
changedMsg = ansi.ColorFunc("yellow")
okMsg = ansi.ColorFunc("green")
}
func (c *CLIReporter) StartTasks(r *Runner) {
c.Start = time.Now()
fmt.Fprintf(c.out, "== tasks @ %v\n", r.Start)
}
func (c *CLIReporter) FinishTasks(r *Runner) {
dur := time.Since(c.Start)
fmt.Fprintf(c.out, "%7.3f = All tasks to finish\n", dur.Seconds())
}
func (c *CLIReporter) StartHandlers(r *Runner) {
dur := time.Since(c.Start)
fmt.Fprintf(c.out, "%7.3f + Running any handlers\n", dur.Seconds())
}
func (c *CLIReporter) FinishHandlers(r *Runner) {
dur := time.Since(c.Start)
fmt.Fprintf(c.out, "%7.3f = All handlers to finish\n", dur.Seconds())
}
func (c *CLIReporter) FinishAll(r *Runner) {
dur := time.Since(c.Start)
fmt.Fprintf(c.out, "%7.3f = All finish\n", dur.Seconds())
}
func (c *CLIReporter) EndReporter(r *Runner, err error) {}
func (c *CLIReporter) StartTask(task *Task, name, args string, vars Vars) {
dur := time.Since(c.Start)
if task.Async() {
fmt.Fprintf(c.out, "%7.3f - %s &\n", dur.Seconds(), name)
} else {
fmt.Fprintf(c.out, "%7.3f - %s\n", dur.Seconds(), name)
}
if c.Output {
fmt.Fprintf(c.out, "%7.3f %s: %s\n", dur.Seconds(), task.Command(), inlineVars(vars))
}
}
func renderShellResult(res *Result) (string, bool) {
rcv, ok := res.Get("rc")
if !ok {
return "", false
}
stdoutv, ok := res.Get("stdout")
if !ok {
return "", false
}
stderrv, ok := res.Get("stderr")
if !ok {
return "", false
}
rc := rcv.Read().(int)
stdout := stdoutv.Read().(string)
stderr := stderrv.Read().(string)
if rc == 0 && len(stdout) == 0 && len(stderr) == 0 {
return "OK", true
} else if len(stderr) == 0 && len(stdout) < 60 {
stdout = strings.Replace(stdout, "\n", " ", -1)
return fmt.Sprintf(`rc: %d, stdout: "%s"`, rc, stdout), true
}
return "", false
}
func (c *CLIReporter) FinishTask(task *Task, res *Result) {
if res == nil {
return
}
dur := time.Since(c.Start)
indent := fmt.Sprintf("%7.3f ", dur.Seconds())
label := changedMsg("Changed")
if !res.Changed {
label = okMsg("OK")
}
if res.Failed {
label = failMsg("Failed")
}
if str, ok := renderShellResult(res); ok {
str = strings.TrimSpace(str)
if str != "" {
lines := strings.Split(str, "\n")
indented := strings.Join(lines, indent+"\n")
fmt.Fprintf(c.out, "%7.3f * %s:\n", dur.Seconds(), label)
fmt.Fprintf(c.out, "%7.3f %s\n", dur.Seconds(), indented)
}
return
}
if res.Data != nil {
fmt.Fprintf(c.out, "%7.3f * %s:\n", dur.Seconds(), label)
if c.Output {
fmt.Fprintf(c.out, "%s\n", indentedVars(Vars(res.Data), indent))
}
}
}
/*
func (c *CLIReporter) FinishAsyncTask(act *AsyncAction) {
dur := time.Since(c.Start)
if act.Error == nil {
fmt.Fprintf(c.out, "%7.3f * %s (async success)\n", dur.Seconds(), act.Task.Name())
} else {
fmt.Fprintf(c.out, "%7.3f * %s (async error:%s)\n", dur.Seconds(), act.Task.Name(), act.Error)
}
}
*/
/*
func (c *CLIReporter) Progress(str string) {
dur := time.Since(c.Start)
lines := strings.Split(str, "\n")
out := strings.Join(lines, fmt.Sprintf("\n%7.3f + ", dur.Seconds()))
fmt.Fprintf(c.out, "%7.3f + %s\n", dur.Seconds(), out)
}
func (c *CLIReporter) JSONProgress(data []byte) error {
cr := JsonChunkReconstitute{c}
return cr.Input(data)
}
type AdhocProgress struct {
out io.Writer
Start time.Time
}
func (a *AdhocProgress) Progress(str string) {
dur := time.Since(a.Start)
lines := strings.Split(str, "\n")
out := strings.Join(lines, fmt.Sprintf("\n%7.3f ", dur.Seconds()))
fmt.Fprintf(a.out, "%7.3f %s\n", dur.Seconds(), out)
}
func (a *AdhocProgress) JSONProgress(data []byte) error {
cr := JsonChunkReconstitute{a}
return cr.Input(data)
}
*/
type JsonChunkReporter struct {
out io.Writer
Start time.Time
}
func (c *JsonChunkReporter) send(args ...interface{}) {
b := ijson(args...)
fmt.Fprintf(c.out, "%d\n%s\n", len(b), string(b))
}
func (c *JsonChunkReporter) StartTasks(r *Runner) {
c.Start = r.Start
c.send("phase", "start", "time", r.Start.String())
}
func (c *JsonChunkReporter) FinishTasks(r *Runner) {
c.send("phase", "finish")
}
func (c *JsonChunkReporter) StartHandlers(r *Runner) {
c.send("phase", "start_handlers")
}
func (c *JsonChunkReporter) FinishHandlers(r *Runner) {
c.send("phase", "finish_handlers")
}
func (c *JsonChunkReporter) FinishAll(r *Runner) {}
func (c *JsonChunkReporter) EndReporter(r *Runner, err error) {}
func (c *JsonChunkReporter) StartTask(task *Task, name, args string, vars Vars) {
dur := time.Since(c.Start).Seconds()
typ := "sync"
if task.Async() {
typ = "async"
}
c.send(
"phase", "start_task",
"type", typ,
"name", name,
"command", task.Command(),
"args", args,
"vars", vars,
"delta", dur)
}
func (c *JsonChunkReporter) FinishTask(task *Task, res *Result) {
if res == nil {
return
}
dur := time.Since(c.Start).Seconds()
c.send(
"phase", "finish_task",
"delta", dur,
"result", res)
}
/*
func (c *JsonChunkReporter) FinishAsyncTask(act *AsyncAction) {
dur := time.Since(c.Start).Seconds()
if act.Error == nil {
c.send(
"phase", "finish_task",
"delta", dur,
"result", act.Result)
} else {
c.send(
"phase", "finish_task",
"delta", dur,
"error", act.Error)
}
}
*/
/*
func (c *JsonChunkReporter) Progress(str string) {
dur := time.Since(c.Start).Seconds()
c.send(
"phase", "progress",
"delta", dur,
"progress", str)
}
func (c *JsonChunkReporter) JSONProgress(data []byte) error {
dur := time.Since(c.Start).Seconds()
raw := json.RawMessage(data)
c.send(
"phase", "json_progress",
"delta", dur,
"progress", &raw)
return nil
}
*/
/*
type JsonChunkReconstitute struct {
report ProgressReporter
}
func (j *JsonChunkReconstitute) Input(data []byte) error {
m := make(map[string]interface{})
err := json.Unmarshal(data, &m)
if err != nil {
return err
}
return j.InputMap(m, 0)
}
func (j *JsonChunkReconstitute) InputMap(m map[string]interface{}, depth int) error {
phase, ok := m["phase"]
if !ok {
return fmt.Errorf("No phase specified")
}
var prefix string
if depth > 0 {
prefix = fmt.Sprintf("[%d] ", depth)
}
switch phase {
case "start":
time, ok := m["time"]
if !ok {
time = "(unknown)"
}
j.report.Progress(fmt.Sprintf("%sremote tasks @ %s", prefix, time))
case "start_task":
j.report.Progress(fmt.Sprintf("%s- %s", prefix, m["name"]))
mv := m["vars"].(map[string]interface{})
j.report.Progress(fmt.Sprintf("%s %s: %s", prefix, m["command"], inlineMap(mv)))
case "finish_task":
res := m["result"].(map[string]interface{})
data := res["data"].(map[string]interface{})
label := "result"
if res["changed"].(bool) == false {
label = "ok"
} else if res["failed"].(bool) == true {
label = "failed"
}
reported := false
if v, ok := data["_result"]; ok {
if str, ok := v.(string); ok {
if str != "" {
j.report.Progress(fmt.Sprintf("%s* %s:", prefix, label))
j.report.Progress(prefix + " " + str)
}
reported = true
}
}
if !reported {
if len(data) > 0 {
j.report.Progress(fmt.Sprintf("%s* %s:", prefix, label))
j.report.Progress(indentedMap(data, prefix+" "))
}
}
case "json_progress":
ds := m["progress"].(map[string]interface{})
j.InputMap(ds, depth+1)
}
return nil
}
*/
type TaskResult struct {
Name string
Cmd string
Args string
Vars Vars
Dur time.Duration
Result Result
}
const MaxTaskResult = 500
func (c *StructReporter) StartTasks(r *Runner) {
c.TaskCount = 0
c.Start = r.Start
c.Out = make([]TaskResult, 0, MaxTaskResult)
}
func (c *StructReporter) FinishTasks(r *Runner) {
c.TasksFinishDur = time.Since(c.Start)
}
func (c *StructReporter) StartHandlers(r *Runner) {
}
func (c *StructReporter) FinishHandlers(r *Runner) {
c.FinishDur = time.Since(c.Start)
}
func (c *StructReporter) StartTask(task *Task, name, args string, vars Vars) {
if len(c.Out) < MaxTaskResult {
c.Out = append(c.Out, TaskResult{
Name: name,
Cmd: task.Command(),
Args: args,
Vars: vars,
})
}
c.TaskCount = len(c.Out)
}
func (c *StructReporter) FinishTask(task *Task, res *Result) {
if res == nil {
return
}
i := c.TaskCount - 1
c.Out[i].Result = *res
c.Out[i].Dur = time.Since(c.Start)
}
func (c *StructReporter) FinishAll(r *Runner) {}
func (c *StructReporter) EndReporter(r *Runner, err error) {}
type Svinfo struct {
Project string `yaml:"project"`
ServerType string `yaml:"servertype"`
}