-
Notifications
You must be signed in to change notification settings - Fork 386
/
bundle.go
309 lines (264 loc) · 8.72 KB
/
bundle.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
package conslogging
import (
"archive/tar"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"net/url"
"os"
"regexp"
"strings"
"sync"
"time"
"github.com/earthly/earthly/cleanup"
"github.com/pkg/errors"
)
const fullLog = "_full"
type targetLogger struct {
writer *strings.Builder
prefix string
result string
status string
started time.Time
}
// BundleBuilder builds log bundles for local storage or upload to a logging server
type BundleBuilder struct {
entrypoint string
started time.Time
cleanup *cleanup.Collection
mu sync.Mutex
logsForTarget map[string]*targetLogger
}
// Write implements io.Writer as a passthrough to the underlying strings.Builder for convenience.
func (tl *targetLogger) Write(p []byte) (n int, err error) {
return tl.writer.Write(p)
}
// NewBundleBuilder makes a new BundleBuilder, that will write logs to the targeted root directory,
// and specify the entrypoint in the resulting manifest.
func NewBundleBuilder(entrypoint string, cleanup *cleanup.Collection) *BundleBuilder {
return &BundleBuilder{
entrypoint: entrypoint,
logsForTarget: map[string]*targetLogger{},
started: time.Now(),
cleanup: cleanup,
}
}
// PrefixResult sets the prefix(aka target) result as it should appear in the manifest for that specific target.
func (bb *BundleBuilder) PrefixResult(prefix, result string) {
bb.mu.Lock()
defer bb.mu.Unlock()
if builder, ok := bb.logsForTarget[prefix]; ok {
builder.result = result
}
}
// PrefixStatus sets the prefix(aka target) result as it should appear in the manifest for that specific target.
func (bb *BundleBuilder) PrefixStatus(prefix, status string) {
bb.mu.Lock()
defer bb.mu.Unlock()
if builder, ok := bb.logsForTarget[prefix]; ok {
builder.status = status
}
}
// PrefixWriter gets an io.Writer for a given prefix(aka target). If its a prefix we have not seen before,
// then generate a new writer to accommodate it.
func (bb *BundleBuilder) PrefixWriter(prefix string) io.Writer {
bb.mu.Lock()
defer bb.mu.Unlock()
if builder, ok := bb.logsForTarget[prefix]; ok {
return builder
}
writer := &targetLogger{
writer: &strings.Builder{},
status: StatusWaiting,
result: ResultCancelled,
started: time.Now(),
prefix: prefix,
}
bb.logsForTarget[prefix] = writer
return writer
}
// WriteToDisk aggregates all the data in the numerous prefix writers, and generates an Earthly log bundle.
// These bundles include a manifest generated from the aggregation of the prefixes (targets).
func (bb *BundleBuilder) WriteToDisk() (string, error) {
// Build file and io.Writer for saving log data
file, err := os.CreateTemp("", "earthly-log*.tar.gz")
if err != nil {
return "", errors.Wrapf(err, "could not create tarball")
}
defer file.Close()
bb.cleanup.Add(func() error {
return os.Remove(file.Name())
})
gzipWriter := gzip.NewWriter(file)
defer gzipWriter.Close()
tarWriter := tar.NewWriter(gzipWriter)
defer tarWriter.Close()
// Make a copy so that we keep the lock for as little time as possible.
bb.mu.Lock()
logsForTarget := make(map[string]*targetLogger, len(bb.logsForTarget))
for k, v := range bb.logsForTarget {
logsForTarget[k] = v
}
bb.mu.Unlock()
// Convert targets to manifest representations, get tar headers for data
targetData := make([]TargetManifest, 0)
for _, lines := range logsForTarget {
mt, err := lines.toManifestTarget()
if err != nil {
// Something was wrong with this targets logs (0 length, or blacklisted name...). Ignore it.
continue
}
targetData = append(targetData, mt)
trimmed := strings.TrimSpace(lines.prefix)
escaped := url.QueryEscape(trimmed)
err = tarWriter.WriteHeader(&tar.Header{
Name: fmt.Sprintf("target/%s", escaped),
Size: int64(lines.writer.Len()),
Mode: 0600,
ChangeTime: time.Now(),
})
if err != nil {
return "", errors.Wrapf(err, "could not write target header")
}
_, err = tarWriter.Write([]byte(lines.writer.String()))
if err != nil {
return "", errors.Wrapf(err, "could not write target data")
}
}
// build manifest and permissions
mani := bb.buildManifest(targetData)
manifestJSON, _ := json.Marshal(mani)
err = tarWriter.WriteHeader(&tar.Header{
Name: "manifest",
Size: int64(len(manifestJSON)),
Mode: 0600,
ChangeTime: time.Now(),
})
if err != nil {
return "", errors.Wrapf(err, "could not write manifest header")
}
_, err = tarWriter.Write(manifestJSON)
if err != nil {
return "", errors.Wrapf(err, "could not write manifest")
}
perm := bb.buildPermissions()
permissionsJSON, _ := json.Marshal(perm)
err = tarWriter.WriteHeader(&tar.Header{
Name: "permissions",
Size: int64(len(permissionsJSON)),
Mode: 0600,
ChangeTime: time.Now(),
})
if err != nil {
return "", errors.Wrapf(err, "could not write permissions header")
}
_, err = tarWriter.Write(permissionsJSON)
if err != nil {
return "", errors.Wrapf(err, "could not write permissions")
}
return file.Name(), nil
}
func (bb *BundleBuilder) buildManifest(targetManifests []TargetManifest) *Manifest {
manifest := &Manifest{
Version: 1,
Duration: int(time.Since(bb.started).Milliseconds()),
Status: StatusComplete,
Result: ResultSuccess,
CreatedAt: time.Now().In(time.UTC),
Entrypoint: bb.entrypoint,
Targets: targetManifests,
}
for _, tm := range targetManifests {
if tm.Name == fullLog {
// Full Log reserved name should not determine whole build status.
// Really, we could go back through after determining whole build status to set _full result & status to the
// values for the whole build; but it doesn't (yet) affect or mean anything to us. So leave it as is.
continue
}
if tm.Result != ResultSuccess {
manifest.Result = tm.Result
}
if tm.Status != StatusComplete {
manifest.Status = tm.Status
}
}
return manifest
}
func (bb *BundleBuilder) buildPermissions() *Permissions {
return &Permissions{
Version: 1,
Users: []string{"*"},
Orgs: []string{"*"},
}
}
func (tl *targetLogger) toManifestTarget() (TargetManifest, error) {
if tl.writer.Len() <= 0 {
// Do not write empty logs, if the prefix didn't write anything
return TargetManifest{}, errors.New("0 length target")
}
if tl.prefix == "ongoing" || tl.prefix == "buildkitd" {
// The ongoing & buildkitd init messages end up in here too. Since they are not updates from a vertex, we will
// never mark them as complete. Additionally, its not useful to have in the output. Ignore it here.
return TargetManifest{}, fmt.Errorf("blacklisted target name %s", tl.prefix)
}
command, summary := tl.getCommandAndSummary()
manifestTarget := TargetManifest{
Name: tl.prefix,
Status: tl.status,
Result: tl.result,
Duration: int(time.Since(tl.started).Milliseconds()),
Size: tl.writer.Len(),
Command: command,
Summary: summary,
}
return manifestTarget, nil
}
// Nobody expects ANSI in the command/summary.
// So, even if we don't inject color we should strip it since a tool inside could have done an ANSI too. *SIGH*
const ansi = "[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))"
var re = regexp.MustCompile(ansi)
func (tl *targetLogger) getCommandAndSummary() (string, string) {
rawText := tl.writer.String()
text := re.ReplaceAllString(rawText, "")
prettyPrefix := prettyPrefix(DefaultPadding, tl.prefix)
// regex to find command lines in the output.
regexStr := fmt.Sprintf(`(?m)^%s \| (\*cached\* |\*local\* | )*--> `, regexp.QuoteMeta(prettyPrefix))
r := regexp.MustCompile(regexStr)
matches := r.FindAllStringIndex(text, -1)
if len(matches) == 0 {
return "", ""
}
// Take the last match, and use the first line up to 120 characters, or first newlines... whichever comes first.
lastMatch := matches[len(matches)-1]
remainder := text[lastMatch[1]:] // The rest of the log from end of the last match
command := truncateString(remainder, 120) // The line up to a newline or 120 chars
// regex to get the last line, (ab)use groups to get the line without the prefix. Truncate it like command.
regexStr2 := fmt.Sprintf(`%s \| (.*)\n?$`, regexp.QuoteMeta(prettyPrefix))
r2 := regexp.MustCompile(regexStr2)
matches2 := r2.FindAllStringSubmatch(remainder, -1)
if len(matches2) == 0 {
return command, ""
}
summary := truncateString(matches2[len(matches2)-1][1], 120)
return command, summary
}
func truncateString(str string, length int) string {
// This weird truncation is needed to support multi-byte characters, which the slice notation does not account for.
if length <= 0 {
return ""
}
truncated := ""
count := 0
for _, char := range str {
if char == '\n' {
break
}
truncated += string(char)
count++
if count >= length {
break
}
}
return truncated
}