-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime.go
254 lines (230 loc) · 7.05 KB
/
runtime.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
package main
import (
"log"
"strconv"
"strings"
"time"
"github.com/google/uuid"
"github.com/jrcichra/conditions"
pb "github.com/jrcichra/karmen/grpc"
)
func (k *karmen) dumbDownParamMap(m map[ParameterName]ParameterValue) map[string]string {
// This is annoying - Ultimate fix is have the strict types defined in gRPC?
newMap := make(map[string]string)
for key, val := range m {
newMap[string(key)] = string(val)
}
return newMap
}
func (k *karmen) smartenParamMap(m map[string]string) map[ParameterName]ParameterValue {
// This is annoying - Ultimate fix is have the strict types defined in gRPC?
newMap := make(map[ParameterName]ParameterValue)
for key, val := range m {
newMap[ParameterName(key)] = ParameterValue(val)
}
return newMap
}
func (k *karmen) dumbDownStateMap(m map[Variable]VariableValue) map[string]interface{} {
newMap := make(map[string]interface{})
for key, val := range m {
switch val {
case "true", "false":
b, _ := strconv.ParseBool(string(val))
newMap[string(key)] = b
default:
newMap[string(key)] = string(val)
}
}
return newMap
}
// m2 takes precendence over m1
func (k *karmen) combineParamMaps(m1, m2 map[ParameterName]ParameterValue) map[ParameterName]ParameterValue {
newMap := make(map[ParameterName]ParameterValue)
for k, v := range m1 {
newMap[k] = v
}
for k, v := range m2 {
newMap[k] = v
}
return newMap
}
func (k *karmen) evaluateConditions(cMap map[ConditionName]ConditionValue, uuid uuid.UUID) (bool, bool) {
b := true
fail := false
for key, val := range cMap {
if key != "if" {
k.eventPrint(uuid, "evaluateConditions() runtime error 1. Key should be 'if:'. Failing the job")
b = false
fail = true
break
}
p := conditions.NewParser(strings.NewReader(string(val)))
expr, err := p.Parse()
if err != nil {
k.eventPrint(uuid, "evaluateConditions() runtime error 2.")
k.eventPrint(uuid, err)
b = false
fail = true
break
}
b, err = conditions.Evaluate(expr, k.dumbDownStateMap(k.State.Events[UUID(uuid.String())]))
if err != nil {
k.eventPrint(uuid, "evaluateConditions() runtime error 3.")
k.eventPrint(uuid, err)
b = false
fail = true
break
}
k.eventPrint(uuid, "evaluateConditions() got a", b, "on condition", val)
// break on a false
if !b {
break
}
}
return b, fail
}
func (k *karmen) runBlock(block *Block, requesterName string, uuid uuid.UUID) bool {
var b bool
if block.Type == "serial" {
b = k.runSerialBlock(block, requesterName, uuid)
} else if block.Type == "parallel" {
b = k.runParallelBlock(block, requesterName, uuid)
} else {
k.eventPrint(uuid, "Unknown Block type: "+string(block.Type))
b = false
}
return b
}
// Execute each action one at a time
func (k *karmen) runSerialBlock(block *Block, requesterName string, uuid uuid.UUID) bool {
overallResult := true
for _, action := range block.Actions {
//Check the conditons
run, fail := k.evaluateConditions(action.Conditions, uuid)
if fail {
k.eventPrint(uuid, "Failing block because of configuration error")
break
}
if run {
result := k.runAction(uuid, action, requesterName)
if overallResult && !result {
overallResult = false
}
} else {
k.eventPrint(uuid, "Skipping action", action.ActionName, "because condition failed")
}
}
return overallResult
}
func (k *karmen) runParallelBlock(block *Block, requesterName string, uuid uuid.UUID) bool {
type Data struct {
Action *Action
Result bool
Skipped bool
}
c := make(chan Data)
for _, action := range block.Actions {
//Run each check in parallel
go func(a *Action, c chan Data) {
//Check the conditons
run, fail := k.evaluateConditions(a.Conditions, uuid)
if fail {
k.eventPrint(uuid, "Failing block because of configuration error")
c <- Data{Action: a, Result: false, Skipped: false}
}
if run {
res := k.runAction(uuid, a, requesterName)
c <- Data{Action: a, Result: res, Skipped: false}
} else {
k.eventPrint(uuid, "Skipping", block.Type, "because a condition failed")
c <- Data{Action: a, Result: true, Skipped: true}
}
}(action, c)
}
overallResult := true
// Gather up the results
for range block.Actions {
res := <-c
if !res.Result && !res.Skipped {
overallResult = false
}
}
return overallResult
}
func (k *karmen) runAction(uuid uuid.UUID, action *Action, requesterName string) bool {
// Build a grpc action - building in the actions from the static yaml and runtime actions
// runtime parameters take precedence over static parameters
a := &pb.Action{ActionName: string(action.ActionName), Timestamp: time.Now().Unix(), Parameters: k.dumbDownParamMap(k.combineParamMaps(action.Parameters, k.State.EventStates[UUID(uuid.String())]))}
// Form that into a request
request := &pb.ActionRequest{Action: a, RequesterName: requesterName}
// wait for us to have a dispatcher
for {
if _, ok := k.State.Hosts[action.HostName]; ok {
if k.State.Hosts[action.HostName].Dispatcher == nil {
log.Println("Waiting for dispatcher on host " + action.HostName + "...")
time.Sleep(1 * time.Second)
} else {
// We have a dispatcher, so we can break out of the loop
break
}
} else {
log.Println("Waiting for host " + action.HostName + "...")
time.Sleep(1 * time.Second)
}
}
// Send the request
k.eventPrint(uuid, "Dispatching action:", action.ActionName, "on", action.HostName)
err := k.State.Hosts[action.HostName].Dispatcher.Send(request)
if err != nil {
k.eventPrint(uuid, err)
}
// Set up the timeout goroutine
doneChan := make(chan *pb.ActionResponse)
// If there's a timeout, spawn a goroutine to enforce it
if action.Timeout > 0 {
go func() {
time.Sleep(action.Timeout)
doneChan <- nil
}()
}
// Spawn a goroutine to wait for the response
go func() {
response, err := k.State.Hosts[action.HostName].Dispatcher.Recv()
if err != nil {
k.eventPrint(uuid, err)
}
doneChan <- response
}()
// Wait for the action to complete or timeout
response := <-doneChan
//if we timed out we need to create a response for the system
if response == nil {
k.eventPrint(uuid, "Timeout on action:", action.ActionName, "on", action.HostName)
response = &pb.ActionResponse{Result: &pb.Result{Code: 502}}
}
// k.eventPrint(uuid, "Action response:")
// k.eventPrint(uuid, response)
// Parse the return code - may be expanded later
var passString string
pass := isPass(response.Result.Code)
if pass {
passString = "true"
} else {
passString = "false"
}
// TODO consider passing through response params to the event state
// Store the state of this action
k.State.Events[UUID(uuid.String())] = make(map[Variable]VariableValue)
for key, val := range response.Result.Parameters {
k.State.Events[UUID(uuid.String())][Variable(action.HostName)+"-"+Variable(key)] = VariableValue(val)
}
// Store the overall result
k.State.Events[UUID(uuid.String())][Variable(action.HostName)+"-"+Variable(action.ActionName)+"-pass"] = VariableValue(passString)
return pass
}
func (k *karmen) eventPrint(uuid uuid.UUID, s ...interface{}) {
var a []interface{}
a = append(a, uuid.String())
s = append(a, s...)
log.Println(s...)
}