forked from golang/build
/
fetchlogs.go
296 lines (256 loc) · 7.17 KB
/
fetchlogs.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
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Fetchlogs downloads build failure logs from the Go dashboard so
// they can be accessed and searched from the local file system.
//
// It organizes these logs into two directories created in the
// directory specified by the -dir flag (which typically defaults to
// ~/.cache/fetchlogs). The log/ directory contains all log files
// named the same way they are named by the dashboard (which happens
// to be the SHA-1 of their contents). The rev/ directory contains
// symlinks back to these logs named
//
// rev/<ISO 8601 commit date>-<git revision>/<builder>
//
// Fetchlogs will reuse existing log files and revision symlinks, so
// it only has to download logs that are new since the last time it
// was run.
//
// This makes failures easily searchable with standard tools. For
// example, to list the revisions and builders with a particular
// failure, use:
//
// grep -lR <regexp> rev | sort
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"sync"
"time"
"golang.org/x/build/types"
)
var defaultDir = filepath.Join(xdgCacheDir(), "fetchlogs")
var (
flagN = flag.Int("n", 300, "limit to most recent `N` commits")
flagPar = flag.Int("j", 5, "number of concurrent download `jobs`")
flagDir = flag.String("dir", defaultDir, "`directory` to save logs to")
)
func main() {
log.SetPrefix("fetchlogs: ")
log.SetFlags(0)
flag.Parse()
if flag.NArg() != 0 {
flag.Usage()
os.Exit(2)
}
// If the top-level directory is the default XDG cache
// directory, make sure it exists.
if *flagDir == defaultDir {
if err := xdgCreateDir(*flagDir); err != nil {
log.Fatal(err)
}
}
// Create directory structure.
if err := os.Chdir(*flagDir); err != nil {
log.Fatal(err)
}
ensureDir("log")
ensureDir("rev")
// Set up fetchers.
fetcher := newFetcher(*flagPar)
wg := sync.WaitGroup{}
// Fetch dashboard pages.
haveCommits := 0
for page := 0; haveCommits < *flagN; page++ {
url := fmt.Sprintf("https://build.golang.org/?mode=json&page=%d", page)
index, err := fetcher.get(url)
if err != nil {
log.Fatal(err)
}
var status types.BuildStatus
if err = json.NewDecoder(index).Decode(&status); err != nil {
log.Fatal("error unmarshalling result: ", err)
}
index.Close()
for _, rev := range status.Revisions {
haveCommits++
if haveCommits > *flagN {
break
}
if rev.Repo != "go" {
continue
}
// Create a revision directory. This way we
// have a record of commits with no failures.
date, err := parseRevDate(rev.Date)
if err != nil {
log.Fatal("malformed revision date: ", err)
}
revDir := revToDir(rev.Revision, date)
ensureDir(revDir)
// Save revision metadata.
buf := bytes.Buffer{}
enc := json.NewEncoder(&buf)
if err = enc.Encode(rev); err != nil {
log.Fatal(err)
}
if err = writeFileAtomic(filepath.Join(revDir, ".rev.json"), &buf); err != nil {
log.Fatal("error saving revision metadata: ", err)
}
// Save builders list so Results list can be
// interpreted.
if err = enc.Encode(status.Builders); err != nil {
log.Fatal(err)
}
if err = writeFileAtomic(filepath.Join(revDir, ".builders.json"), &buf); err != nil {
log.Fatal("error saving builders metadata: ", err)
}
// Fetch revision logs.
for i, res := range rev.Results {
if res == "" || res == "ok" {
continue
}
wg.Add(1)
go func(rev, builder, logURL string) {
defer wg.Done()
logPath := filepath.Join("log", filepath.Base(logURL))
err := fetcher.getFile(logURL, logPath)
if err != nil {
log.Fatal("error fetching log: ", err)
}
if err := linkLog(revDir, builder, logPath); err != nil {
log.Fatal("error linking log: ", err)
}
}(revDir, status.Builders[i], res)
}
}
}
wg.Wait()
}
// A fetcher downloads files over HTTP concurrently. It allows
// limiting the number of concurrent downloads and correctly handles
// multiple (possibly concurrent) fetches from the same URL to the
// same file.
type fetcher struct {
tokens chan struct{}
pending struct {
sync.Mutex
m map[string]*pendingFetch
}
}
type pendingFetch struct {
wchan chan struct{} // closed when fetch completes
// err is the error, if any, that occurred during this fetch.
// It will be set before wchan is closed.
err error
}
func newFetcher(jobs int) *fetcher {
f := new(fetcher)
f.tokens = make(chan struct{}, *flagPar)
for i := 0; i < jobs; i++ {
f.tokens <- struct{}{}
}
f.pending.m = make(map[string]*pendingFetch)
return f
}
// get performs an HTTP GET for URL and returns the body, while
// obeying the job limit on fetcher.
func (f *fetcher) get(url string) (io.ReadCloser, error) {
<-f.tokens
fmt.Println("fetching", url)
resp, err := http.Get(url)
f.tokens <- struct{}{}
if err != nil {
return nil, err
}
return resp.Body, nil
}
// getFile performs an HTTP GET for URL and writes it to filename. If
// the destination file already exists, this returns immediately. If
// another goroutine is currently fetching filename, this blocks until
// the fetch is done and then returns.
func (f *fetcher) getFile(url string, filename string) error {
// Do we already have it?
if _, err := os.Stat(filename); err == nil {
return nil
} else if !os.IsNotExist(err) {
return err
}
// Check if another fetcher is working on it.
f.pending.Lock()
if p, ok := f.pending.m[filename]; ok {
f.pending.Unlock()
<-p.wchan
return p.err
}
p := &pendingFetch{wchan: make(chan struct{})}
f.pending.m[filename] = p
f.pending.Unlock()
r, err := f.get(url)
if err == nil {
err = writeFileAtomic(filename, r)
r.Close()
}
p.err = err
close(p.wchan)
return p.err
}
// ensureDir creates directory name if it does not exist.
func ensureDir(name string) {
err := os.Mkdir(name, 0777)
if err != nil && !os.IsExist(err) {
log.Fatal("error creating directory ", name, ": ", err)
}
}
// writeFileAtomic atomically creates a file called filename and
// copies the data from r to the file.
func writeFileAtomic(filename string, r io.Reader) error {
tmpPath := filename + ".tmp"
if f, err := os.Create(tmpPath); err != nil {
return err
} else {
_, err := io.Copy(f, r)
if err == nil {
err = f.Sync()
}
err2 := f.Close()
if err == nil {
err = err2
}
if err != nil {
os.Remove(tmpPath)
return err
}
}
if err := os.Rename(tmpPath, filename); err != nil {
os.Remove(tmpPath)
return err
}
return nil
}
// linkLog creates a symlink for finding logPath based on its git
// revision and builder.
func linkLog(revDir, builder, logPath string) error {
// Create symlink.
err := os.Symlink("../../"+logPath, filepath.Join(revDir, builder))
if err != nil && !os.IsExist(err) {
return err
}
return nil
}
// parseRevDate parses a revision date in RFC3339.
func parseRevDate(date string) (time.Time, error) {
return time.Parse(time.RFC3339, date)
}
// revToDir returns the path of the revision directory for revision.
func revToDir(revision string, date time.Time) string {
return filepath.Join("rev", date.Format("2006-01-02T15:04:05")+"-"+revision[:7])
}