-
Notifications
You must be signed in to change notification settings - Fork 351
/
service.go
590 lines (512 loc) · 17.7 KB
/
service.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
package actions
//go:generate go run github.com/golang/mock/mockgen@v1.6.0 -package=mock -destination=mock/mock_actions.go github.com/treeverse/lakefs/pkg/actions Source,OutputWriter
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"sync"
"time"
"github.com/antonmedv/expr"
"github.com/hashicorp/go-multierror"
"github.com/treeverse/lakefs/pkg/auth"
"github.com/treeverse/lakefs/pkg/graveler"
"github.com/treeverse/lakefs/pkg/kv"
"github.com/treeverse/lakefs/pkg/logging"
"github.com/treeverse/lakefs/pkg/stats"
"google.golang.org/protobuf/types/known/timestamppb"
)
const (
PartitionKey = "actions"
reposPrefix = "repos"
runsPrefix = "runs"
tasksPrefix = "tasks"
branchPrefix = "branches"
commitPrefix = "commits"
)
var (
ErrNotFound = errors.New("not found")
ErrNilValue = errors.New("nil value")
ErrIfExprNotBool = errors.New("hook 'if' expression should evaluate to a boolean")
)
type Config struct {
Enabled bool
Lua struct {
NetHTTPEnabled bool
}
Env struct {
Enabled bool
Prefix string
}
}
// StoreService is an implementation of actions.Service that saves
// the run data to the blockstore and to the actions.Store (which is a
// fancy name for a DB - kv style or postgres directly)
type StoreService struct {
Store Store
idGen IDGenerator
Source Source
Writer OutputWriter
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
stats stats.Collector
cfg Config
endpoint *http.Server
serverAddress string
}
type Task struct {
RunID string
HookRunID string
Action *Action
HookID string
Hook Hook
If string
Err error
StartTime time.Time
EndTime time.Time
}
type RunResult struct {
RunID string `db:"run_id" json:"run_id"`
BranchID string `db:"branch_id" json:"branch_id"`
SourceRef string `db:"source_ref" json:"source_ref"`
EventType string `db:"event_type" json:"event_type"`
CommitID string `db:"commit_id" json:"commit_id,omitempty"`
StartTime time.Time `db:"start_time" json:"start_time"`
EndTime time.Time `db:"end_time" json:"end_time"`
Passed bool `db:"passed" json:"passed"`
}
type TaskResult struct {
RunID string `db:"run_id" json:"run_id"`
HookRunID string `db:"hook_run_id" json:"hook_run_id"`
HookID string `db:"hook_id" json:"hook_id"`
ActionName string `db:"action_name" json:"action_name"`
StartTime time.Time `db:"start_time" json:"start_time"`
EndTime time.Time `db:"end_time" json:"end_time"`
Passed bool `db:"passed" json:"passed"`
}
type RunManifest struct {
Run RunResult `json:"run"`
HooksRun []TaskResult `json:"hooks,omitempty"`
}
func (r *TaskResult) LogPath() string {
return FormatHookOutputPath(r.RunID, r.HookRunID)
}
func RunResultFromProto(pb *RunResultData) *RunResult {
return &RunResult{
RunID: pb.RunId,
BranchID: pb.BranchId,
SourceRef: pb.SourceRef,
EventType: pb.EventType,
CommitID: pb.CommitId,
StartTime: pb.StartTime.AsTime(),
EndTime: pb.EndTime.AsTime(),
Passed: pb.Passed,
}
}
func protoFromRunResult(m *RunResult) *RunResultData {
return &RunResultData{
RunId: m.RunID,
BranchId: m.BranchID,
CommitId: m.CommitID,
SourceRef: m.SourceRef,
EventType: m.EventType,
StartTime: timestamppb.New(m.StartTime),
EndTime: timestamppb.New(m.EndTime),
Passed: m.Passed,
}
}
func taskResultFromProto(pb *TaskResultData) *TaskResult {
return &TaskResult{
RunID: pb.RunId,
HookRunID: pb.HookRunId,
HookID: pb.HookId,
ActionName: pb.ActionName,
StartTime: pb.StartTime.AsTime(),
EndTime: pb.EndTime.AsTime(),
Passed: pb.Passed,
}
}
func protoFromTaskResult(m *TaskResult) *TaskResultData {
return &TaskResultData{
RunId: m.RunID,
HookRunId: m.HookRunID,
HookId: m.HookID,
ActionName: m.ActionName,
StartTime: timestamppb.New(m.StartTime),
EndTime: timestamppb.New(m.EndTime),
Passed: m.Passed,
}
}
//nolint:gochecknoinits
func init() {
kv.MustRegisterType("*", kv.FormatPath("repos", "*", "tasks"), (&TaskResultData{}).ProtoReflect().Type())
kv.MustRegisterType("*", kv.FormatPath("repos", "*", "runs"), (&RunResultData{}).ProtoReflect().Type())
kv.MustRegisterType("*", kv.FormatPath("repos", "*", "branches"), (&kv.SecondaryIndex{}).ProtoReflect().Type())
kv.MustRegisterType("*", kv.FormatPath("repos", "*", "commits"), (&kv.SecondaryIndex{}).ProtoReflect().Type())
}
func baseActionsPath(repoID string) string {
return kv.FormatPath(reposPrefix, repoID)
}
func TasksPath(repoID, runID string) string {
return kv.FormatPath(baseActionsPath(repoID), tasksPrefix, runID)
}
func RunPath(repoID, runID string) []byte {
return []byte(kv.FormatPath(baseActionsPath(repoID), runsPrefix, runID))
}
func byBranchPath(repoID, branchID string) string {
return kv.FormatPath(baseActionsPath(repoID), branchPrefix, branchID)
}
func byCommitPath(repoID, commitID string) string {
return kv.FormatPath(baseActionsPath(repoID), commitPrefix, commitID)
}
func RunByBranchPath(repoID, branchID, runID string) []byte {
return []byte(kv.FormatPath(byBranchPath(repoID, branchID), runID))
}
func RunByCommitPath(repoID, commitID, runID string) []byte {
return []byte(kv.FormatPath(byCommitPath(repoID, commitID), runID))
}
type RunResultIterator interface {
Next() bool
Value() *RunResult
Err() error
Close()
}
type TaskResultIterator interface {
Next() bool
Value() *TaskResult
Err() error
Close()
}
type Service interface {
Stop()
Run(ctx context.Context, record graveler.HookRecord) error
UpdateCommitID(ctx context.Context, repositoryID string, storageNamespace string, runID string, commitID string) error
GetRunResult(ctx context.Context, repositoryID string, runID string) (*RunResult, error)
GetTaskResult(ctx context.Context, repositoryID string, runID string, hookRunID string) (*TaskResult, error)
ListRunResults(ctx context.Context, repositoryID string, branchID, commitID string, after string) (RunResultIterator, error)
ListRunTaskResults(ctx context.Context, repositoryID string, runID string, after string) (TaskResultIterator, error)
graveler.HooksHandler
}
func NewService(ctx context.Context, store Store, source Source, writer OutputWriter, idGen IDGenerator, stats stats.Collector, cfg Config, serverAddress string) *StoreService {
ctx, cancel := context.WithCancel(ctx)
return &StoreService{
Store: store,
Source: source,
Writer: writer,
ctx: ctx,
idGen: idGen,
cancel: cancel,
wg: sync.WaitGroup{},
stats: stats,
cfg: cfg,
serverAddress: serverAddress,
}
}
func (s *StoreService) Stop() {
s.cancel()
s.wg.Wait()
}
func (s *StoreService) SetEndpoint(h *http.Server) {
s.endpoint = h
}
func (s *StoreService) asyncRun(ctx context.Context, record graveler.HookRecord) {
s.wg.Add(1)
go func() {
defer s.wg.Done()
// load the user from the original context
user, err := auth.GetUser(ctx)
if err != nil {
if !errors.Is(err, auth.ErrUserNotFound) {
logging.FromContext(s.ctx).WithError(err).WithField("record", record).
Info("Failed getting user from context")
} else {
ctx = s.ctx
}
} else {
ctx = auth.WithUser(s.ctx, user)
}
// passing the global (possibly wrapped) context for cancelling all runs when lakeFS shuts down
if err := s.Run(ctx, record); err != nil {
logging.FromContext(s.ctx).WithError(err).WithField("record", record).
Info("Async run of hook failed")
}
}()
}
// Run load and run actions based on the event information
func (s *StoreService) Run(ctx context.Context, record graveler.HookRecord) error {
if !s.cfg.Enabled {
logging.FromContext(ctx).WithField("record", record).Debug("Hooks are disabled, skipping hooks execution")
return nil
}
// load relevant actions
spec := MatchSpec{
EventType: record.EventType,
BranchID: record.BranchID,
}
logging.FromContext(ctx).WithFields(logging.Fields{"record": record, "spec": spec}).Debug("Filtering actions")
actions, err := s.loadMatchedActions(ctx, record, spec)
if err != nil || len(actions) == 0 {
return err
}
// allocate and run hooks
tasks, err := s.allocateTasks(record.RunID, actions)
if err != nil {
return err
}
runErr := s.runTasks(ctx, record, tasks)
// keep results before returning an error (if any)
err = s.saveRunInformation(ctx, record, tasks)
if err != nil {
return err
}
return runErr
}
func (s *StoreService) loadMatchedActions(ctx context.Context, record graveler.HookRecord, spec MatchSpec) ([]*Action, error) {
actions, err := LoadActions(ctx, s.Source, record)
if err != nil {
return nil, err
}
return MatchedActions(actions, spec)
}
func (s *StoreService) allocateTasks(runID string, actions []*Action) ([][]*Task, error) {
var tasks [][]*Task
for actionIdx, action := range actions {
var actionTasks []*Task
for hookIdx, hook := range action.Hooks {
h, err := NewHook(hook, action, s.cfg, s.endpoint, s.serverAddress, s.stats)
if err != nil {
return nil, err
}
task := &Task{
RunID: runID,
HookRunID: NewHookRunID(actionIdx, hookIdx),
Action: action,
HookID: hook.ID,
Hook: h,
If: hook.If,
}
// append new task or chain to the last one based on the current action
actionTasks = append(actionTasks, task)
}
if len(actionTasks) > 0 {
tasks = append(tasks, actionTasks)
}
}
return tasks, nil
}
func (s *StoreService) runTasks(ctx context.Context, record graveler.HookRecord, tasks [][]*Task) error {
var g multierror.Group
for _, actionTasks := range tasks {
actionTasks := actionTasks // pin
g.Go(func() error {
var actionErr error
for _, task := range actionTasks {
hookOutputWriter := &HookOutputWriter{
Writer: s.Writer,
StorageNamespace: record.StorageNamespace.String(),
RunID: task.RunID,
HookRunID: task.HookRunID,
ActionName: task.Action.Name,
HookID: task.HookID,
}
// evaluate if expression and keep error for later
var ifHook bool
ifHook, task.Err = runHookIfEval(task, actionErr)
if task.Err == nil && !ifHook {
continue
}
// execute hook if expression evaluated as true or if expression is empty
task.StartTime = time.Now().UTC()
var buf bytes.Buffer
if task.Err == nil {
task.Err = task.Hook.Run(ctx, record, &buf)
}
task.EndTime = time.Now().UTC()
s.stats.CollectEvent(stats.Event{Class: "actions_service", Name: string(record.EventType)})
if task.Err != nil {
_, _ = fmt.Fprintf(&buf, "Error: %s\n", task.Err)
// wrap error with more information
task.Err = fmt.Errorf("hook run id '%s' failed on action '%s' hook '%s': %w",
task.HookRunID, task.Action.Name, task.HookID, task.Err)
}
err := hookOutputWriter.OutputWrite(ctx, &buf, int64(buf.Len()))
if err != nil {
// non task error - we stop the tasks processing
return fmt.Errorf("failed to write action log. Run id '%s' action '%s' hook '%s': %w",
task.HookRunID, task.Action.Name, task.HookID, err)
}
// stop execution if needed - keep using lastErr for return value.
// we do not return here as we like to process all the steps
if task.Err != nil {
// capture the first error
if actionErr == nil {
actionErr = task.Err
}
}
}
return actionErr
})
}
return g.Wait().ErrorOrNil()
}
func runHookIfEval(task *Task, actionErr error) (bool, error) {
result := actionErr == nil // by default, we run based on current status - success: no error
if task.If == "" {
return result, nil
}
env := map[string]any{
"success": func() bool { return actionErr == nil },
"failure": func() bool { return actionErr != nil },
}
output, err := expr.Eval(task.If, env)
if err != nil {
return false, err
}
if b, ok := output.(bool); ok {
return b, nil
}
return false, ErrIfExprNotBool
}
func (s *StoreService) saveRunInformation(ctx context.Context, record graveler.HookRecord, tasks [][]*Task) error {
if len(tasks) == 0 {
return nil
}
manifest := buildRunManifestFromTasks(record, tasks)
err := s.saveRunManifestDB(ctx, record.RepositoryID, manifest)
if err != nil {
return fmt.Errorf("insert run information: %w", err)
}
return s.saveRunManifestObjectStore(ctx, manifest, record.StorageNamespace.String(), record.RunID)
}
func (s *StoreService) saveRunManifestObjectStore(ctx context.Context, manifest RunManifest, storageNamespace string, runID string) error {
manifestJSON, err := json.Marshal(manifest)
if err != nil {
return fmt.Errorf("marshal run manifest: %w", err)
}
runManifestPath := FormatRunManifestOutputPath(runID)
manifestReader := bytes.NewReader(manifestJSON)
manifestSize := int64(len(manifestJSON))
return s.Writer.OutputWrite(ctx, storageNamespace, runManifestPath, manifestReader, manifestSize)
}
func (s *StoreService) saveRunManifestDB(ctx context.Context, repositoryID graveler.RepositoryID, manifest RunManifest) error {
return s.Store.saveRunManifest(ctx, repositoryID, manifest)
}
func buildRunManifestFromTasks(record graveler.HookRecord, tasks [][]*Task) RunManifest {
manifest := RunManifest{
Run: RunResult{
RunID: record.RunID,
BranchID: record.BranchID.String(),
SourceRef: record.SourceRef.String(),
EventType: string(record.EventType),
Passed: true,
CommitID: record.CommitID.String(),
},
}
for _, actionTasks := range tasks {
for _, task := range actionTasks {
// record hook run information
taskStarted := !task.StartTime.IsZero()
manifest.HooksRun = append(manifest.HooksRun, TaskResult{
RunID: task.RunID,
HookRunID: task.HookRunID,
HookID: task.HookID,
ActionName: task.Action.Name,
StartTime: task.StartTime,
EndTime: task.EndTime,
Passed: taskStarted && task.Err == nil, // mark skipped tasks as failed
})
// keep min run start time using non-skipped tasks
if manifest.Run.StartTime.IsZero() || (taskStarted && task.StartTime.Before(manifest.Run.StartTime)) {
manifest.Run.StartTime = task.StartTime
}
// keep max run end time
if manifest.Run.EndTime.IsZero() || task.EndTime.After(manifest.Run.EndTime) {
manifest.Run.EndTime = task.EndTime
}
// did we fail
manifest.Run.Passed = manifest.Run.Passed && task.Err == nil
}
}
return manifest
}
// UpdateCommitID assume record is a post event, we use the PreRunID to update the commit_id and save the run manifest again
func (s *StoreService) UpdateCommitID(ctx context.Context, repositoryID string, storageNamespace string, runID string, commitID string) error {
manifest, err := s.Store.UpdateCommitID(ctx, repositoryID, runID, commitID)
if err != nil {
return fmt.Errorf("updating commit ID: %w", err)
}
if manifest == nil {
return nil
}
// update manifest
return s.saveRunManifestObjectStore(ctx, *manifest, storageNamespace, runID)
}
func (s *StoreService) GetRunResult(ctx context.Context, repositoryID string, runID string) (*RunResult, error) {
return s.Store.GetRunResult(ctx, repositoryID, runID)
}
func (s *StoreService) GetTaskResult(ctx context.Context, repositoryID string, runID string, hookRunID string) (*TaskResult, error) {
return s.Store.GetTaskResult(ctx, repositoryID, runID, hookRunID)
}
func (s *StoreService) ListRunResults(ctx context.Context, repositoryID string, branchID, commitID string, after string) (RunResultIterator, error) {
return s.Store.ListRunResults(ctx, repositoryID, branchID, commitID, after)
}
func (s *StoreService) ListRunTaskResults(ctx context.Context, repositoryID string, runID string, after string) (TaskResultIterator, error) {
return s.Store.ListRunTaskResults(ctx, repositoryID, runID, after)
}
func (s *StoreService) PreCommitHook(ctx context.Context, record graveler.HookRecord) error {
return s.Run(ctx, record)
}
func (s *StoreService) PostCommitHook(ctx context.Context, record graveler.HookRecord) error {
// update pre-commit with commit ID if needed
err := s.UpdateCommitID(ctx, record.RepositoryID.String(), record.StorageNamespace.String(), record.PreRunID, record.CommitID.String())
if err != nil {
return err
}
s.asyncRun(ctx, record)
return nil
}
func (s *StoreService) PreMergeHook(ctx context.Context, record graveler.HookRecord) error {
return s.Run(ctx, record)
}
func (s *StoreService) PostMergeHook(ctx context.Context, record graveler.HookRecord) error {
// update pre-merge with commit ID if needed
err := s.UpdateCommitID(ctx, record.RepositoryID.String(), record.StorageNamespace.String(), record.PreRunID, record.CommitID.String())
if err != nil {
return err
}
s.asyncRun(ctx, record)
return nil
}
func (s *StoreService) PreCreateTagHook(ctx context.Context, record graveler.HookRecord) error {
return s.Run(ctx, record)
}
func (s *StoreService) PostCreateTagHook(ctx context.Context, record graveler.HookRecord) {
s.asyncRun(ctx, record)
}
func (s *StoreService) PreDeleteTagHook(ctx context.Context, record graveler.HookRecord) error {
return s.Run(ctx, record)
}
func (s *StoreService) PostDeleteTagHook(ctx context.Context, record graveler.HookRecord) {
s.asyncRun(ctx, record)
}
func (s *StoreService) PreCreateBranchHook(ctx context.Context, record graveler.HookRecord) error {
return s.Run(ctx, record)
}
func (s *StoreService) PostCreateBranchHook(ctx context.Context, record graveler.HookRecord) {
s.asyncRun(ctx, record)
}
func (s *StoreService) PreDeleteBranchHook(ctx context.Context, record graveler.HookRecord) error {
return s.Run(ctx, record)
}
func (s *StoreService) PostDeleteBranchHook(ctx context.Context, record graveler.HookRecord) {
s.asyncRun(ctx, record)
}
func (s *StoreService) NewRunID() string {
return s.idGen.NewRunID()
}
func NewHookRunID(actionIdx, hookIdx int) string {
return fmt.Sprintf("%04d_%04d", actionIdx, hookIdx)
}