-
Notifications
You must be signed in to change notification settings - Fork 74
/
supervisor.go
655 lines (540 loc) · 17.8 KB
/
supervisor.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
package engine
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/google/uuid"
"github.com/logrusorgru/aurora"
"github.com/otiai10/copy"
"github.com/testground/testground/pkg/api"
"github.com/testground/testground/pkg/config"
"github.com/testground/testground/pkg/logging"
"github.com/testground/testground/pkg/rpc"
"github.com/testground/testground/pkg/runner"
"github.com/testground/testground/pkg/task"
"golang.org/x/sync/errgroup"
)
type RunInput struct {
*api.RunRequest
Sources *api.UnpackedSources
}
type BuildInput struct {
*api.BuildRequest
Sources *api.UnpackedSources
}
func (e *Engine) addSignal(id string, ch chan int) {
e.signalsLk.Lock()
e.signals[id] = ch
e.signalsLk.Unlock()
}
func (e *Engine) deleteSignal(id string) {
e.signalsLk.Lock()
delete(e.signals, id)
e.signalsLk.Unlock()
}
func (e *Engine) worker(n int) {
logging.S().Infow("supervisor worker started", "worker_id", n)
taskTimeout := 10 * time.Minute
if e.EnvConfig().Daemon.Scheduler.TaskTimeoutMin != 0 {
taskTimeout = time.Duration(e.EnvConfig().Daemon.Scheduler.TaskTimeoutMin) * time.Minute
}
for {
tsk, err := e.queue.Pop()
if err == task.ErrQueueEmpty {
time.Sleep(time.Second)
continue
}
if err != nil {
logging.S().Errorw("error while popping task from the queue", "err", err)
continue
}
func() {
ctx, cancel := context.WithTimeout(context.Background(), taskTimeout)
defer cancel()
ch := make(chan int)
e.addSignal(tsk.ID, ch)
go func() {
select {
case <-ch:
e.deleteSignal(tsk.ID)
cancel()
case <-ctx.Done():
return
}
}()
tsk.States = append(tsk.States, task.DatedState{
State: task.StateProcessing,
Created: time.Now().UTC(),
})
err = e.store.PersistProcessing(tsk)
if err != nil {
logging.S().Errorw("could not persist task", "err", err)
}
logging.S().Infow("worker processing task", "worker_id", n, "task_id", tsk.ID)
err = e.postStatusToGithub(tsk)
if err != nil {
logging.S().Errorw("could not post status to github", "err", err)
}
// Create a packing directory under the work dir.
file := filepath.Join(e.EnvConfig().Dirs().Daemon(), tsk.ID+".out")
f, err := os.OpenFile(file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
logging.S().Errorw("could not create stop log", "err", err)
return
}
defer f.Close()
ow := rpc.NewFileOutputWriter(f)
var result interface{}
var errTask error
switch tsk.Type {
case task.TypeRun:
var res *api.RunOutput
res, errTask = e.doRun(ctx, tsk.ID, tsk.Input.(*RunInput), ow)
if errTask != nil {
errTask = &TaskExecutionError{TaskType: string(tsk.Type), WrappedErr: errTask}
logging.S().Errorw("doRun returned err", "err", errTask)
}
if res != nil {
result = res.Result
tsk.Composition = res.Composition
}
case task.TypeBuild:
var res []*api.BuildOutput
res, errTask = e.doBuild(ctx, tsk.Input.(*BuildInput), ow)
if errTask != nil {
errTask = &TaskExecutionError{TaskType: string(tsk.Type), WrappedErr: errTask}
logging.S().Errorw("doBuild returned err", "err", errTask)
}
if res != nil {
var artifactPaths []string
for _, ap := range res {
artifactPaths = append(artifactPaths, ap.ArtifactPath)
}
result = artifactPaths
}
default:
logging.S().Errorw("unknown task type", "type", tsk.Type)
return
}
newState := task.DatedState{
Created: time.Now().UTC(),
State: task.StateComplete,
}
if errTask != nil {
tsk.Error = errTask.Error()
var e *TaskExecutionError
if errors.As(errTask, &e) || errors.Is(errTask, context.Canceled) {
newState.State = task.StateCanceled
logging.S().Errorw("task cancelled due to error", "err", errTask)
} else {
logging.S().Infow("Task encountered error, but was not canceled.")
}
}
tsk.States = append(tsk.States, newState)
tsk.Result = result
err = e.store.PersistProcessing(tsk)
if err != nil {
logging.S().Errorw("could not persist task", "err", err)
return
}
err = e.store.ArchiveTask(tsk)
if err != nil {
logging.S().Errorw("could not archive task", "err", err)
return
}
err = e.postStatusToSlack(tsk)
if err != nil {
logging.S().Errorw("could not send status to slack", "err", err)
}
err = e.postStatusToGithub(tsk)
if err != nil {
logging.S().Errorw("could not post status to github", "err", err)
}
e.deleteSignal(tsk.ID)
logging.S().Infow("worker completed task", "worker_id", n, "task_id", tsk.ID)
}()
}
}
func (e *Engine) postStatusToGithub(tsk *task.Task) error {
if e.envcfg.Daemon.GithubRepoStatusToken == "" {
return nil
}
if !tsk.CreatedByCI() {
return nil
}
ownerrepo := strings.Split(tsk.CreatedBy.Repo, "/")
owner := ownerrepo[0]
repo := ownerrepo[1]
hash := tsk.CreatedBy.Commit
result, ok := tsk.Result.(*runner.Result)
if !ok {
return errors.New("can't post to github: task result is not from k8s")
}
var msg, state string
switch tsk.State().State {
case task.StateProcessing:
msg = "TaaS is running your plan"
state = "pending"
case task.StateComplete:
switch result.Outcome {
case task.OutcomeSuccess:
msg = "Testplan run succeeded!"
state = "success"
case task.OutcomeCanceled:
msg = "Testplan run was canceled!"
state = "failure"
case task.OutcomeFailure:
msg = "Testplan run failed!"
state = "failure"
case task.OutcomeUnknown:
return errors.New("can't post update to github: task outcome is unknown")
}
default:
return errors.New("can't post update to github: task state is not processing or completed")
}
cl := &http.Client{Timeout: time.Second * 10}
url := fmt.Sprintf("https://api.github.com/repos/%s/%s/statuses/%s", owner, repo, hash)
plan := tsk.Plan + "/" + tsk.Case
payload := fmt.Sprintf(`{"state":"%s","target_url":"https://ci.testground.ipfs.team/tasks","description":"%s","context":"taas/%s"}`, state, msg, plan)
body := strings.NewReader(payload)
req, err := http.NewRequest("POST", url, body)
if err != nil {
return err
}
req.Header.Add("Authorization", "Basic "+e.envcfg.Daemon.GithubRepoStatusToken)
req.Header.Set("Accept", "application/vnd.github.v3+json")
res, err := cl.Do(req)
if err != nil {
return err
}
res.Body.Close()
return nil
}
func (e *Engine) postStatusToSlack(tsk *task.Task) error {
if e.envcfg.Daemon.SlackWebhookURL == "" {
return nil
}
result, ok := tsk.Result.(*runner.Result)
if !ok {
return nil
}
payload := fmt.Sprintf(`{"text":"<https://ci.testground.ipfs.team/tasks#taskID_%s|%s> *%s* run completed"}`, tsk.ID, tsk.ID, tsk.Name())
switch result.Outcome {
case task.OutcomeSuccess:
payload = fmt.Sprintf(`{"text":"✅ <https://ci.testground.ipfs.team/tasks#taskID_%s|%s> *%s* run succeeded (%s) %s"}`, tsk.ID, tsk.ID, tsk.Name(), result, tsk.Took())
case task.OutcomeCanceled:
payload = fmt.Sprintf(`{"text":"⚪ <https://ci.testground.ipfs.team/tasks#taskID_%s|%s> *%s* run canceled %s ; %s"}`, tsk.ID, tsk.ID, tsk.Name(), tsk.Took(), tsk.Error)
case task.OutcomeFailure:
payload = fmt.Sprintf(`{"text":"❌ <https://ci.testground.ipfs.team/tasks#taskID_%s|%s> *%s* run failed (%s) %s ; %s"}`, tsk.ID, tsk.ID, tsk.Name(), result, tsk.Took(), tsk.Error)
}
cl := &http.Client{Timeout: time.Second * 10}
body := strings.NewReader(payload)
res, err := cl.Post(
e.envcfg.Daemon.SlackWebhookURL,
"application/json; charset=UTF-8",
body,
)
if err != nil {
return err
}
res.Body.Close()
return nil
}
func (e *Engine) doBuild(ctx context.Context, input *BuildInput, ow *rpc.OutputWriter) ([]*api.BuildOutput, error) {
sources := input.Sources
comp, err := input.Composition.PrepareForBuild(&input.Manifest)
if err != nil {
return nil, err
}
if err := comp.ValidateForBuild(); err != nil {
return nil, fmt.Errorf("invalid composition: %w", err)
}
var (
plan = clean(comp.Global.Plan)
)
// Validate builders we use
usedBuilders := comp.ListBuilders()
for _, b := range usedBuilders {
_, ok := e.builders[b]
if !ok {
return nil, fmt.Errorf("unrecognized builder: %s", b)
}
}
// Call healthcheck on the builders
for _, b := range usedBuilders {
bm := e.builders[b]
// Call the healthcheck routine if the runner supports it, with fix=true.
if hc, ok := bm.(api.Healthchecker); ok {
ow.Info("performing healthcheck on builder")
if rep, err := hc.Healthcheck(ctx, e, ow, true); err != nil {
return nil, fmt.Errorf("healthcheck and fix errored: %w", err)
} else if !rep.FixesSucceeded() {
return nil, fmt.Errorf("healthcheck fixes failed; aborting:\n%s", rep)
} else if !rep.ChecksSucceeded() {
ow.Warnf(aurora.Bold(aurora.Yellow("some healthchecks failed, but continuing")).String())
} else {
ow.Infof(aurora.Bold(aurora.Green("healthcheck: ok")).String())
}
}
}
// Builders are ready, let's go.
var (
// no need to synchronise access, as each goroutine will write its
// response in its index.
ress = make([]*api.BuildOutput, len(comp.Groups))
cancel context.CancelFunc
)
// obtain an explicitly cancellable context so we can stop build jobs if
// something fails.
ctx, cancel = context.WithCancel(ctx)
defer cancel()
// traverse groups, indexing them by the unique build key and remembering their position.
uniq := make(map[string][]int, len(comp.Groups))
for idx, g := range comp.Groups {
// NOTE: why do we even need this and don't rely on docker layer caching?
k := g.BuildKey()
uniq[k] = append(uniq[k], idx)
}
// prepare sources
var finalSources []*api.UnpackedSources
if uniqcnt := len(uniq); uniqcnt == 1 {
finalSources = []*api.UnpackedSources{sources}
} else {
finalSources = make([]*api.UnpackedSources, uniqcnt)
for i := 0; i < uniqcnt; i++ {
dst := fmt.Sprintf("%s-%d", strings.TrimSuffix(sources.BaseDir, "/"), i)
if err := copy.Copy(sources.BaseDir, dst); err != nil {
return nil, fmt.Errorf("failed to create unique source directories for multiple build jobs: %w", err)
}
src := &api.UnpackedSources{
BaseDir: dst,
PlanDir: filepath.Join(dst, filepath.Base(sources.PlanDir)),
}
if sources.SDKDir != "" {
src.SDKDir = filepath.Join(dst, filepath.Base(sources.SDKDir))
}
if sources.ExtraDir != "" {
src.ExtraDir = filepath.Join(dst, filepath.Base(sources.ExtraDir))
}
finalSources[i] = src
}
}
// Trigger a build job for each unique build, and wait until all of them are
// done, mapping the build artifacts back to the original group positions in
// the response.
errGroup, errGroupCtx := errgroup.WithContext(ctx)
concurrentBuilds := comp.Global.ConcurrentBuilds
if concurrentBuilds == 0 {
concurrentBuilds = -1
}
errGroup.SetLimit(concurrentBuilds)
var cnt int
for key, idxs := range uniq {
idxs := idxs
key := key // capture
src := finalSources[cnt]
cnt++
errGroup.Go(func() (err error) {
// Every Group in `idxs`` have the same build key. They are identitical when it comes to build,
// so it's safe to use the first one to build them all.
grp := comp.Groups[idxs[0]]
// Pluck all IDs from the groups this build artifact is for.
grpids := make([]string, 0, len(idxs))
for _, idx := range idxs {
grpids = append(grpids, comp.Groups[idx].ID)
}
// get the builder
builder := grp.Builder
bm := e.builders[builder]
ow.Infow("performing build for groups", "plan", plan, "groups", grpids, "builder", builder)
deps := make(map[string]api.DependencyTarget, len(grp.Build.Dependencies))
for _, dep := range grp.Build.Dependencies {
deps[dep.Module] = api.DependencyTarget{
Target: dep.Target,
Version: dep.Version,
}
}
// This var compiles all configurations to coalesce.
//
// Precedence (highest to lowest):
//
// 1. CLI --run-param, --build-param flags.
// 2. .env.toml.
// 3. Builder defaults (applied by the builder itself, nothing to do here).
//
var cfg config.CoalescedConfig
cfg = cfg.Append(e.envcfg.Builders[builder]) // env config for the builder
groupCfg := cfg.Append(grp.BuildConfig) // add the group config
// Coalesce all configurations and deserialize into the config type
// mandated by the builder.
obj, err := groupCfg.CoalesceIntoType(bm.ConfigType())
if err != nil {
return fmt.Errorf("error while coalescing configuration values: %w", err)
}
in := &api.BuildInput{
BuildID: uuid.New().String()[24:],
EnvConfig: *e.envcfg,
TestPlan: plan,
Selectors: grp.Build.Selectors,
Dependencies: deps,
BuildConfig: obj,
UnpackedSources: src,
}
res, err := bm.Build(errGroupCtx, in, ow)
if err != nil {
ow.Infow("build failed", "plan", plan, "groups", grpids, "builder", builder, "error", err)
return err
}
res.BuilderID = bm.ID()
// no need for a mutex as the indices we access do not intersect
// across goroutines.
for _, idx := range uniq[key] {
ress[idx] = res
}
ow.Infow("build succeeded", "plan", plan, "groups", grpids, "builder", builder, "artifact", res.ArtifactPath)
return nil
})
}
// Wait until all goroutines are done. If any failed, return the error.
if err := errGroup.Wait(); err != nil {
return nil, err
}
return ress, nil
}
func (e *Engine) doRun(ctx context.Context, id string, input *RunInput, ow *rpc.OutputWriter) (*api.RunOutput, error) {
if len(input.BuildGroups) > 0 {
bcomp, err := input.Composition.PickGroups(input.BuildGroups...)
if err != nil {
return nil, err
}
bout, err := e.doBuild(ctx, &BuildInput{
BuildRequest: &api.BuildRequest{
Composition: bcomp,
Manifest: input.Manifest,
},
Sources: input.Sources,
}, ow)
if err != nil {
return nil, err
}
// Populate the returned build IDs. This is returned so the
// client can store the composition with artifacts if they chose to.
for i, groupIdx := range input.BuildGroups {
g := input.Composition.Groups[groupIdx]
g.Run.Artifact = bout[i].ArtifactPath
}
}
comp, err := input.Composition.PrepareForRun(&input.Manifest)
if err != nil {
return nil, err
}
if err := comp.ValidateForRun(); err != nil {
return nil, err
}
compositionUsedForRun := comp
var (
plan = comp.Global.Plan
tcase = comp.Global.Case
trunner = comp.Global.Runner
)
// Get the runner.
run := e.runners[trunner]
// Call the healthcheck routine if the runner supports it, with fix=true.
if hc, ok := run.(api.Healthchecker); ok {
ow.Info("performing healthcheck on runner")
if rep, err := hc.Healthcheck(ctx, e, ow, true); err != nil {
return nil, fmt.Errorf("healthcheck and fix errored: %w", err)
} else if !rep.FixesSucceeded() {
return nil, fmt.Errorf("healthcheck fixes failed; aborting:\n%s", rep)
} else if !rep.ChecksSucceeded() {
ow.Warnf(aurora.Bold(aurora.Yellow("some healthchecks failed, but continuing")).String())
} else {
ow.Infof(aurora.Bold(aurora.Green("healthcheck: ok")).String())
}
}
// This var compiles all configurations to coalesce.
//
// Precedence (highest to lowest):
//
// 1. CLI --run-param, --build-param flags.
// 2. .env.toml.
// 3. Builder defaults (applied by the builder itself, nothing to do here).
//
var cfg config.CoalescedConfig
// 2. Get the env config for the runner.
cfg = cfg.Append(e.envcfg.Runners[trunner])
var flag = e.envcfg.Runners[trunner][config.RunnerDisabledFlag]
if flag == true {
return nil, runner.ErrRunnerDisabled
}
// 1. Get overrides from the composition.
cfg = cfg.Append(comp.Global.RunConfig)
// Coalesce all configurations and deserialize into the config type
// mandated by the runner.
obj, err := cfg.CoalesceIntoType(run.ConfigType())
if err != nil {
return nil, fmt.Errorf("error while coalescing configuration values: %w", err)
}
if (len(input.RunIds) > 1) {
// TODO: remove when we can build multiple runs
return nil, fmt.Errorf("cannot specify multiple run ids for now")
}
runId := input.RunIds[0]
framedComp, err := comp.FrameForRuns(runId);
if err != nil {
return nil, fmt.Errorf("error while framing composition for run: %s: %w", runId, err)
}
compRun := framedComp.Runs[0]
in := api.RunInput{
RunID: id,
EnvConfig: *e.envcfg,
RunnerConfig: obj,
TestPlan: clean(plan),
TestCase: clean(tcase),
TotalInstances: int(compRun.TotalInstances),
Groups: make([]*api.RunGroup, 0, len(compRun.Groups)),
DisableMetrics: comp.Global.DisableMetrics,
}
for _, grp := range compRun.Groups {
buildgroup, err := framedComp.GetGroup(grp.EffectiveGroupId())
if err != nil {
return nil, err
}
g := &api.RunGroup{
ID: grp.ID,
Instances: int(grp.CalculatedInstanceCount()),
ArtifactPath: buildgroup.Run.Artifact,
Parameters: grp.TestParams,
Resources: grp.Resources,
Profiles: grp.Profiles,
}
in.Groups = append(in.Groups, g)
}
ow.Infow("starting run", "run_id", id, "plan", in.TestPlan, "case", in.TestCase, "runner", trunner, "instances", in.TotalInstances)
out, err := run.Run(ctx, &in, ow)
if err == nil {
message := "run finished with outcome unknown"
if out.Result != nil {
message = fmt.Sprintf("run finished with %v", out.Result)
}
ow.Infow(message, "run_id", id, "plan", plan, "case", tcase, "runner", trunner, "instances", in.TotalInstances)
} else if errors.Is(err, context.Canceled) {
ow.Infow("run canceled", "run_id", id, "plan", plan, "case", tcase, "runner", trunner, "instances", in.TotalInstances)
} else {
ow.Warnw("run finished in error", "run_id", id, "plan", plan, "case", tcase, "runner", trunner, "instances", in.TotalInstances, "error", err)
}
if out != nil { // TODO: Make sure all runners return a value, and get rid of nil check
out.Composition = *compositionUsedForRun
}
return out, err
}
func clean(name string) string {
forbiddenChar := "/"
name = strings.Replace(name, forbiddenChar, "-", -1)
return name
}