-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay.go
108 lines (92 loc) · 2.18 KB
/
play.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
package play
import (
"fmt"
"log"
"os"
"strings"
"sync"
"github.com/olekukonko/tablewriter"
)
type Stage struct {
Steps []string `yaml:"steps"`
}
type Play struct {
Vars map[string]string `yaml:"vars"`
wg *sync.WaitGroup
Stages []Stage `yaml:"play"`
errorChan chan error
tasks []*task
}
func NewPlay(stages []Stage, vars map[string]string) *Play {
wg := new(sync.WaitGroup)
errorChan := make(chan error)
return &Play{
wg: wg,
Vars: vars,
Stages: stages,
errorChan: errorChan,
}
}
func (p *Play) printErrors() {
for er := range p.errorChan {
log.Print("Error during execution: ", er)
}
}
func (p *Play) Run(verbose bool) {
go p.printErrors()
for stageIdx, stage := range p.Stages {
p.wg.Add(len(stage.Steps))
for taskIdx, command := range stage.Steps {
t := newTask(fmt.Sprintf("%d_%d", stageIdx, taskIdx), command, p)
p.tasks = append(p.tasks, t)
go t.Run(verbose)
}
p.wg.Wait()
}
}
func (p *Play) getLogsDir() string {
return "/tmp/" + binaryName + "_log"
}
func (p *Play) DumpLogs() {
for _, t := range p.tasks {
err := t.DumpOutput(p.getLogsDir())
if err != nil {
log.Print(err)
}
}
}
func (p *Play) PrintResults() {
var data [][]string
for _, t := range p.tasks {
var status string
if t.Success {
status = "success"
} else {
status = "failed"
}
data = append(data, []string{
t.Name,
t.StartedAt.Format(TimeFormat),
t.EndedAt.Format(TimeFormat),
t.EndedAt.Sub(t.StartedAt).String(),
status,
strings.Replace(strings.Join(t.Cmd.Args, " "), "bash -c", "", -1),
p.getLogsDir() + "/" + t.Name + "/full.log",
})
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "Start", "End", "Duration", "Status", "Command", "Logs at"})
table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(true)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetHeaderLine(false)
table.SetBorder(false)
table.SetTablePadding("\t") // pad with tabs
table.SetNoWhiteSpace(true)
table.AppendBulk(data) // Add Bulk Data
table.Render()
}