/
action.go
118 lines (101 loc) · 2.89 KB
/
action.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
package action
import (
"time"
)
type (
// ActionID represents the id of an action available on an environment
ActionID string
//Action represents an action available on an environment
Action struct {
// The action id
id ActionID
// The action id on which this action depends
dependsOn ActionID
// The name of the action
name string
// The action steps
steps []step
}
)
const (
// NilActionID identifies no action, used to indicate that an action depends on nothing
NilActionID ActionID = "NIL"
// ValidateActionID identifies the action of validating an environment model.
ValidateActionID = "VALIDATE"
// CheckActionID identifies the action of returning the validation results of an environment model.
CheckActionID = "CHECK"
// FailOnErrorActionID identifies the action of failing if the environment model has validation errors.
FailOnErrorActionID = "FAIL"
// DumpActionID identifies the action of dumping the effective environment model.
DumpActionID = "DUMP"
// ApplyActionID identifies the action of applying a descriptor to reality
ApplyActionID = "APPLY"
)
// String returns the string representation of the action id
func (a ActionID) String() string {
return string(a)
}
func allActions() []Action {
r := make([]Action, 0)
r = append(r, applyAction)
r = append(r, checkAction)
r = append(r, dumpAction)
r = append(r, validateAction)
return r
}
// run runs an action for the given action manager and contexts
func (a Action) run(am *actionManager) (*ExecutionReport, Result, error) {
r := &ExecutionReport{}
if a.dependsOn != NilActionID {
// Obtain the dependent action
d, e := am.get(a.dependsOn)
if e != nil {
return r, nil, e
}
// Run the dependent action and aggregate its report (dropping the intermediate result)
rep, _, e := d.run(am)
if e != nil {
return r, nil, e
}
r.aggregate(*rep)
// If the report contains an error return it
if rep.Error != nil {
return r, nil, rep.Error
}
}
am.lC.Log().Printf(LogRunningAction, a.name)
// Run the final action and return its result
rep, res := a.launch(CreateRuntimeContext(am.lC, am.cM, am.aM))
r.aggregate(rep)
return r, res, nil
}
// launch runs the action on the given contenxt
func (a Action) launch(rC *runtimeContext) (ExecutionReport, Result) {
r := ExecutionReport{}
cleanups := []Cleanup{}
var finalRes Result
for _, f := range a.steps {
sCs, res := f(rC)
for _, sr := range sCs.Status {
i := int64(sr.ExecutionTime / time.Millisecond)
if i == 0 {
sr.ExecutionTime, _ = time.ParseDuration("1ms")
}
r.Steps.Status = append(r.Steps.Status, sr)
r.Steps.TotalExecutionTime = r.Steps.TotalExecutionTime + sr.ExecutionTime
if sr.cleanUp != nil {
cleanups = append(cleanups, sr.cleanUp)
}
e := sr.error
if e != nil {
cleanLaunched(cleanups, rC.lC)
r.Error = e
return r, nil
}
}
if res != nil {
finalRes = res
}
}
return r, finalRes
}