-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
include.go
251 lines (224 loc) · 5.4 KB
/
include.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
package runn
import (
"context"
"errors"
)
const includeRunnerKey = "include"
type includeRunner struct {
name string
path string
params map[string]any
runResults []*RunResult
}
type includeConfig struct {
path string
vars map[string]any
skipTest bool
force bool
step *step
}
type includedRunErr struct {
err error
}
func newIncludedRunErr(err error) *includedRunErr {
return &includedRunErr{err: err}
}
func (e *includedRunErr) Error() string {
return e.err.Error()
}
func (e *includedRunErr) Unwrap() error {
return e.err
}
func (e *includedRunErr) Is(target error) bool {
err := target
for {
_, ok := err.(*includedRunErr) //nolint:errorlint
if ok {
return true
}
if err = errors.Unwrap(err); err == nil {
break
}
}
return false
}
func newIncludeRunner() (*includeRunner, error) {
return &includeRunner{}, nil
}
func (rnr *includeRunner) Run(ctx context.Context, s *step) error {
o := s.parent
c := s.includeConfig
if o.thisT != nil {
o.thisT.Helper()
}
rnr.runResults = nil
var err error
ipath := rnr.path
if ipath == "" {
ipath = c.path
}
// ipath must not be variable expanded. Because it will be impossible to identify the step of the included runbook in case of run failure.
ipath, err = fp(ipath, o.root)
if err != nil {
return err
}
// Store before record
store := o.store.toMap()
store[storeRootKeyIncluded] = o.included
if !s.deferred {
store[storeRootKeyPrevious] = o.store.latest()
}
nodes, err := s.expandNodes()
if err != nil {
return err
}
if rnr.name != "" {
v, ok := nodes[rnr.name].(map[string]any)
if ok {
store[storeRootKeyNodes] = v
}
}
params := map[string]any{}
for k, v := range rnr.params {
switch ov := v.(type) {
case string:
var vv any
vv, err = o.expandBeforeRecord(ov, s)
if err != nil {
return err
}
evv, err := evaluateSchema(vv, o.root, store)
if err != nil {
return err
}
params[k] = evv
case map[string]any, []any:
vv, err := o.expandBeforeRecord(ov, s)
if err != nil {
return err
}
params[k] = vv
default:
params[k] = ov
}
}
if len(params) > 0 {
store[storeRootKeyParams] = params
}
pstore := map[string]any{
storeRootKeyParent: store,
}
oo, err := o.newNestedOperator(c.step, bookWithStore(ipath, pstore), SkipTest(c.skipTest))
if err != nil {
return err
}
// Override vars
for k, v := range c.vars {
switch ov := v.(type) {
case string:
var vv any
vv, err = o.expandBeforeRecord(ov, s)
if err != nil {
return err
}
evv, err := evaluateSchema(vv, oo.root, store)
if err != nil {
return err
}
oo.store.vars[k] = evv
case map[string]any, []any:
vv, err := o.expandBeforeRecord(ov, s)
if err != nil {
return err
}
oo.store.vars[k] = vv
default:
oo.store.vars[k] = ov
}
}
if err := rnr.run(ctx, oo, s); err != nil {
return err
}
return nil
}
func (rnr *includeRunner) run(ctx context.Context, oo *operator, s *step) error {
o := s.parent
ops := oo.toOperatorN()
sorted, err := sortWithNeeds(ops.ops)
if err != nil {
return err
}
// Filter already runned runbooks
var filtered []*operator
for _, ooo := range sorted {
if oo.bookPath == ooo.bookPath {
// The originally included oo is not filtered.
filtered = append(filtered, ooo)
} else if _, ok := ops.nm.TryGet(ooo.bookPath); !ok {
filtered = append(filtered, ooo)
}
}
// Do not use ops.runN because runN closes the runners.
// And one runbook should be run sequentially.
// ref: https://github.com/k1LoW/runn/blob/b81205550f0e15fec509a596fcee8619e345ae95/docs/designs/id.md
for _, ooo := range filtered {
ooo.parent = oo.parent
if err := ooo.run(ctx); err != nil {
rnr.runResults = append(rnr.runResults, ooo.runResult)
return newIncludedRunErr(err)
}
rnr.runResults = append(rnr.runResults, ooo.runResult)
}
o.record(s.idx, oo.store.toMapForIncludeRunner())
return nil
}
// newNestedOperator create nested operator.
func (o *operator) newNestedOperator(parent *step, opts ...Option) (*operator, error) {
popts := append([]Option{included(true)}, o.exportOptionsToBePropagated()...)
// Prefer child runbook opts
// For example, if a runner with the same name is defined in the child runbook to be included, it takes precedence.
opts = append(popts, opts...)
oo, err := New(opts...)
if err != nil {
return nil, err
}
// Nested operatorN do not inherit beforeFuncs/afterFuncs
oo.t = o.thisT
oo.thisT = o.thisT
oo.sw = o.sw
oo.capturers = o.capturers
oo.parent = parent
oo.store.parentVars = o.store.toMap()
oo.store.kv = o.store.kv
oo.store.runNIndex = o.store.runNIndex
oo.dbg = o.dbg
oo.nm = o.nm
oo.deferred = o.deferred
return oo, nil
}
// export exports options.
func (o *operator) exportOptionsToBePropagated() []Option {
var opts []Option
// Set parent runners for re-use
for k, r := range o.httpRunners {
opts = append(opts, reuseHTTPRunner(k, r))
}
for k, r := range o.dbRunners {
opts = append(opts, reuseDBRunner(k, r))
}
for k, r := range o.grpcRunners {
opts = append(opts, reuseGrpcRunner(k, r))
}
for k, r := range o.sshRunners {
opts = append(opts, reuseSSHRunner(k, r))
}
opts = append(opts, Debug(o.debug))
opts = append(opts, Profile(o.profile))
opts = append(opts, SkipTest(o.skipTest))
opts = append(opts, Force(o.force))
opts = append(opts, Trace(o.trace))
for k, f := range o.store.funcs {
opts = append(opts, Func(k, f))
}
return opts
}