-
Notifications
You must be signed in to change notification settings - Fork 0
/
workflow.go
181 lines (147 loc) · 3.67 KB
/
workflow.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
package sequence
import (
"fmt"
"github.com/sirupsen/logrus"
)
type Workflow struct {
Jobs []Job `yaml:"jobs"`
Hooks []Hook `yaml:"hooks"`
hookMap map[string]*Hook
}
func (w *Workflow) getJobs() []Job {
return w.Jobs
}
func (w *Workflow) getHooks() []Hook {
return w.Hooks
}
func NewWorkflow(filepath string) (*Workflow, error) {
d, err := readFile(filepath)
if err != nil {
return nil, err
}
w, err := parseYaml(d)
if err != nil {
return nil, err
}
w.buildHookMap()
err = w.validate()
if err != nil {
return nil, err
}
return w, nil
}
func (w *Workflow) Execute() error {
executedJobs := make(map[string]bool) // Track executed jobs
for _, job := range w.getJobs() {
if job.getSkip() {
logrus.WithFields(logrus.Fields{
"type": "job",
"name": job.getName(),
"action": "skip",
}).Info("property skip is set to true")
continue
}
if !job.dependenciesSatisfied(executedJobs) {
logrus.WithFields(logrus.Fields{
"type": "job",
"name": job.getName(),
"action": "skip",
"deps": job.getDependencies(),
}).Info("job dependencies not satisfied")
continue
}
err := w.executeJob(&job)
if err != nil {
executedJobs[job.Name] = false
if job.getExitOnError() {
logrus.WithFields(logrus.Fields{
"type": "job",
"name": job.getName(),
"action": "exit",
}).Info("property exit_on_error is set to true")
return fmt.Errorf("exiting due to error in job '%s': %v", job.getName(), err)
}
continue
}
executedJobs[job.Name] = true
}
return nil
}
func (w *Workflow) executeJob(job *Job) error {
logrus.WithFields(logrus.Fields{
"type": "job",
"name": job.getName(),
"action": "execute",
}).Info("executing command")
err := job.execute()
if err != nil {
logrus.WithFields(logrus.Fields{
"type": "job",
"name": job.getName(),
"action": "error",
"error": err,
}).Error("job execution failed")
if hook, exists := w.hookMap[job.getErrorHook()]; exists {
logrus.WithFields(logrus.Fields{
"type": "hook",
"name": hook.getName(),
"job": job.getName(),
"action": "execute",
}).Info("executing hook: on_error")
hook.execute()
}
return err
} else if hook, exists := w.hookMap[job.getSuccessHook()]; exists {
logrus.WithFields(logrus.Fields{
"type": "hook",
"name": hook.getName(),
"job": job.getName(),
"action": "execute",
}).Info("executing hook: on_success")
hook.execute()
}
return nil
}
func (w *Workflow) buildHookMap() {
w.hookMap = make(map[string]*Hook)
for i := range w.getHooks() {
hook := &w.Hooks[i]
w.hookMap[hook.Name] = hook
}
}
func (w *Workflow) validate() error {
if !w.validateJobUniqueNames() {
return fmt.Errorf("workflow contains duplicate job names")
}
if !w.validateJobHooksExists() {
return fmt.Errorf("workflow contains job hooks that do not exist")
}
return nil
}
func (w *Workflow) validateJobUniqueNames() bool {
names := make(map[string]struct{})
for _, job := range w.Jobs {
if _, exists := names[job.Name]; exists {
return false
}
names[job.Name] = struct{}{}
}
return true
}
func (w *Workflow) validateJobHooksExists() bool {
for _, job := range w.Jobs {
if job.getErrorHook() != "" && !w.hookExists(job.getErrorHook()) {
logrus.WithField("job", job.Name).Error("Job contains on_error hook that does not exist")
return false
}
if job.getSuccessHook() != "" && !w.hookExists(job.getSuccessHook()) {
logrus.WithField("job", job.Name).Error("Job contains on_success hook that does not exist")
return false
}
}
return true
}
func (w *Workflow) hookExists(hookName string) bool {
_, exists := w.hookMap[hookName]
return exists
}