-
Notifications
You must be signed in to change notification settings - Fork 9
/
storedrun.go
208 lines (168 loc) · 4.64 KB
/
storedrun.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package storedrun
import (
"context"
"encoding/json"
"errors"
"os"
"sync"
"time"
"github.com/spf13/pflag"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/timestamppb"
"namespacelabs.dev/foundation/internal/cli/version"
"namespacelabs.dev/foundation/internal/codegen/protos"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/schema/storage"
"namespacelabs.dev/foundation/std/tasks"
)
var (
OutputPath string
ParentID string
AllocatedIDPath string
mu sync.Mutex
attachments []proto.Message
)
type Run struct {
Started time.Time
mu sync.Mutex
merged *storage.Command
}
type StoredRunID struct {
RunId string `json:"run_id,omitempty"`
}
func SetupFlags(flags *pflag.FlagSet) {
flags.StringVar(&OutputPath, "stored_run_output_path", "", "If set, outputs a serialized run to the specified path.")
flags.StringVar(&ParentID, "stored_run_parent_id", "", "If set, tags this section with the specified push.")
flags.StringVar(&AllocatedIDPath, "stored_run_id_path", "", "If set, uses the contents as a previously created run id.")
_ = flags.MarkHidden("stored_run_output_path")
_ = flags.MarkHidden("stored_run_parent_id")
_ = flags.MarkHidden("stored_run_id_path")
}
func New() *Run {
if OutputPath == "" {
return nil
}
r := &Run{Started: time.Now(), merged: &storage.Command{}}
if tasks.ActionStorer != nil {
panic("tasks.ActionStorer already set?")
}
tasks.ActionStorer = func(ra *tasks.RunningAction) {
cmd := tasks.Retain(ra)
r.mu.Lock()
r.merged.ActionLog = append(r.merged.ActionLog, cmd.ActionLog...)
r.merged.AttachedLog = append(r.merged.AttachedLog, cmd.AttachedLog...)
r.mu.Unlock()
}
return r
}
func (s *Run) Output(ctx context.Context, execErr error) error {
s.mu.Lock()
cmd := s.merged
s.merged = &storage.Command{}
s.mu.Unlock()
Attach(cmd)
st := nsErrorToStatus(execErr)
run := &storage.UndifferentiatedRun{
ParentRunId: ParentID,
Status: st.Proto(),
Created: timestamppb.New(s.Started),
Completed: timestamppb.Now(),
}
if AllocatedIDPath != "" {
p, err := LoadStoredID(AllocatedIDPath)
if err != nil {
return err
}
run.RunId = p.RunId
}
attachments := consumeAttachments()
if v, err := version.Current(); err == nil {
attachments = append(attachments, v)
}
for _, attachment := range attachments {
serialized, err := anypb.New(attachment)
if err != nil {
return fnerrors.InternalError("failed to serialize attachment: %w", err)
}
run.Attachment = append(run.Attachment, serialized)
}
if err := protos.WriteFile(OutputPath, run); err != nil {
return err
}
return nil
}
func nsErrorToStatus(err error) *status.Status {
st, _ := status.FromError(err)
// Find the deepest ActionError to provide the action trace for the root cause.
var actionErr *fnerrors.ActionError
for {
errors.As(err, &actionErr)
cause := errors.Unwrap(err)
if cause == nil {
break
}
err = cause
}
// Extract nearest stack.
var stackTracer fnerrors.StackTracer
if actionErr != nil {
errors.As(actionErr, &stackTracer)
trace := actionErr.Trace()
att := &storage.ActionTrace{}
for _, a := range trace {
ev := tasks.EventDataFromProto("", a)
st := tasks.MakeStoreProto(&ev, nil)
att.Task = append(att.Task, st)
}
if newSt, err := st.WithDetails(att); err == nil {
st = newSt
}
}
if stackTracer != nil {
trace := stackTracer.StackTrace()
att := &storage.StackTrace{}
for _, f := range trace {
st := &storage.StackTrace_Frame{
Filename: f.File(),
Line: int32(f.Line()),
Symbol: f.Name(),
}
att.Frame = append(att.Frame, st)
}
if newSt, err := st.WithDetails(att); err == nil {
st = newSt
}
}
return st
}
func consumeAttachments() []proto.Message {
mu.Lock()
defer mu.Unlock()
x := attachments
attachments = nil
return x
}
func Attach(m proto.Message) {
mu.Lock()
defer mu.Unlock()
attachments = append(attachments, m)
}
func LoadStoredID(path string) (StoredRunID, error) {
contents, err := os.ReadFile(path)
if err != nil {
return StoredRunID{}, fnerrors.InternalError("failed to load run id: %w", err)
}
var r StoredRunID
if err := json.Unmarshal(contents, &r); err != nil {
return StoredRunID{}, fnerrors.InternalError("failed to load run id: %w", err)
}
if r.RunId == "" {
return StoredRunID{}, fnerrors.InternalError("invalid run id")
}
return r, nil
}