/
prow.go
335 lines (292 loc) · 10.1 KB
/
prow.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
/*
Copyright 2019 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// prow.go defines types and functions specific to prow logics
// All paths used in this package are gcs paths unless specified otherwise
package prow
import (
"bufio"
"context"
"encoding/json"
"log"
"os"
"path"
"sort"
"strconv"
"strings"
"knative.dev/pkg/test/gcs"
)
const (
// OrgName is the name of knative org
OrgName = "knative"
// BucketName is the gcs bucket for all knative builds
BucketName = "knative-prow"
// Latest is the filename storing latest build number
Latest = "latest-build.txt"
// BuildLog is the filename for build log
BuildLog = "build-log.txt"
// StartedJSON is the json file containing build started info
StartedJSON = "started.json"
// FinishedJSON is the json file containing build finished info
FinishedJSON = "finished.json"
// ArtifactsDir is the dir containing artifacts
ArtifactsDir = "artifacts"
// PresubmitJob means it runs on unmerged PRs.
PresubmitJob = "presubmit"
// PostsubmitJob means it runs on each new commit.
PostsubmitJob = "postsubmit"
// PeriodicJob means it runs on a time-basis, unrelated to git changes.
PeriodicJob = "periodic"
// BatchJob tests multiple unmerged PRs at the same time.
BatchJob = "batch"
)
// defined here so that it can be mocked for unit testing
var logFatalf = log.Fatalf
var ctx = context.Background()
// Job struct represents a job directory in gcs.
// gcs job StoragePath will be derived from Type if it's defined,
type Job struct {
Name string
Type string
Bucket string // optional
Repo string // optional
StoragePath string // optional
PullID int // only for Presubmit jobs
Builds []Build // optional
}
// Build points to a build stored under a particular gcs path.
type Build struct {
JobName string
StoragePath string
BuildID int
Bucket string // optional
StartTime *int64
FinishTime *int64
}
// Started holds the started.json values of the build.
type Started struct {
Timestamp int64 `json:"timestamp"` // epoch seconds
RepoVersion string `json:"repo-version"`
Node string `json:"node"`
Pull string `json:"pull"`
Repos map[string]string `json:"repos"` // {repo: branch_or_pull} map
}
// Finished holds the finished.json values of the build
type Finished struct {
// Timestamp is epoch seconds
Timestamp int64 `json:"timestamp"`
Passed bool `json:"passed"`
JobVersion string `json:"job-version"`
Metadata Metadata `json:"metadata"`
}
// Metadata contains metadata in finished.json
type Metadata map[string]interface{}
/* Local logics */
// GetLocalArtifactsDir gets the artifacts directory where prow looks for artifacts.
// By default, it will look at the env var ARTIFACTS.
func GetLocalArtifactsDir() string {
dir := os.Getenv("ARTIFACTS")
if dir == "" {
log.Printf("Env variable ARTIFACTS not set. Using %s instead.", ArtifactsDir)
dir = ArtifactsDir
}
return dir
}
/* GCS related logics */
// Initialize wraps gcs authentication, have to be invoked before any other functions
func Initialize(serviceAccount string) error {
return gcs.Authenticate(ctx, serviceAccount)
}
// NewJob creates new job struct
// pullID is only saved by Presubmit job for determining StoragePath
func NewJob(jobName, jobType, repoName string, pullID int) *Job {
job := Job{
Name: jobName,
Type: jobType,
Bucket: BucketName,
}
switch jobType {
case PeriodicJob, PostsubmitJob:
job.StoragePath = path.Join("logs", jobName)
case PresubmitJob:
job.PullID = pullID
job.StoragePath = path.Join("pr-logs", "pull", OrgName+"_"+repoName, strconv.Itoa(pullID), jobName)
case BatchJob:
job.StoragePath = path.Join("pr-logs", "pull", "batch", jobName)
default:
logFatalf("unknown job spec type: %v", jobType)
}
return &job
}
// PathExists checks if the storage path of a job exists in gcs or not
func (j *Job) PathExists() bool {
return gcs.Exists(ctx, BucketName, j.StoragePath)
}
// GetLatestBuildNumber gets the latest build number for job
func (j *Job) GetLatestBuildNumber() (int, error) {
logFilePath := path.Join(j.StoragePath, Latest)
contents, err := gcs.Read(ctx, BucketName, logFilePath)
if err != nil {
return 0, err
}
latestBuild, err := strconv.Atoi(strings.TrimSuffix(string(contents), "\n"))
if err != nil {
return 0, err
}
return latestBuild, nil
}
// NewBuild gets build struct based on job info
// No gcs operation is performed by this function
func (j *Job) NewBuild(buildID int) *Build {
build := Build{
Bucket: BucketName,
JobName: j.Name,
StoragePath: path.Join(j.StoragePath, strconv.Itoa(buildID)),
BuildID: buildID,
}
if startTime, err := build.GetStartTime(); err == nil {
build.StartTime = &startTime
}
if finishTime, err := build.GetFinishTime(); err == nil {
build.FinishTime = &finishTime
}
return &build
}
// GetFinishedBuilds gets all builds that have finished,
// by looking at existence of "finished.json" file
func (j *Job) GetFinishedBuilds() []Build {
var finishedBuilds []Build
builds := j.GetBuilds()
for _, build := range builds {
if build.IsFinished() {
finishedBuilds = append(finishedBuilds, build)
}
}
return finishedBuilds
}
// GetBuilds gets all builds from this job on gcs, precomputes start/finish time of builds
// by parsing "Started.json" and "Finished.json" on gcs, could be very expensive if there are
// large number of builds
func (j *Job) GetBuilds() []Build {
var builds []Build
for _, ID := range j.GetBuildIDs() {
builds = append(builds, *j.NewBuild(ID))
}
return builds
}
// GetBuildIDs gets all build IDs from this job on gcs, scans all direct child of gcs directory
// for job, keeps the ones that can be parsed as integer
func (j *Job) GetBuildIDs() []int {
var buildIDs []int
gcsBuildPaths := gcs.ListDirectChildren(ctx, j.Bucket, j.StoragePath)
for _, gcsBuildPath := range gcsBuildPaths {
if buildID, err := getBuildIDFromBuildPath(gcsBuildPath); err == nil {
buildIDs = append(buildIDs, buildID)
}
}
return buildIDs
}
// GetLatestBuilds get latest builds from gcs, sort by start time from newest to oldest,
// will return count number of builds
func (j *Job) GetLatestBuilds(count int) []Build {
// The timestamp of gcs directories are not usable,
// as they are all set to '0001-01-01 00:00:00 +0000 UTC',
// so use 'started.json' creation date for latest builds
builds := j.GetFinishedBuilds()
sort.Slice(builds, func(i, j int) bool {
if builds[i].StartTime == nil {
return false
}
if builds[j].StartTime == nil {
return true
}
return *builds[i].StartTime > *builds[j].StartTime
})
if len(builds) < count {
return builds
}
return builds[:count]
}
// IsStarted check if build has started by looking at "started.json" file
func (b *Build) IsStarted() bool {
return gcs.Exists(ctx, BucketName, path.Join(b.StoragePath, StartedJSON))
}
// IsFinished check if build has finished by looking at "finished.json" file
func (b *Build) IsFinished() bool {
return gcs.Exists(ctx, BucketName, path.Join(b.StoragePath, FinishedJSON))
}
// GetStartTime gets started timestamp of a build,
// returning -1 if the build didn't start or if it failed to get the timestamp
func (b *Build) GetStartTime() (int64, error) {
var started Started
if err := unmarshalJSONFile(path.Join(b.StoragePath, StartedJSON), &started); err != nil {
return -1, err
}
return started.Timestamp, nil
}
// GetFinishTime gets finished timestamp of a build,
// returning -1 if the build didn't finish or if it failed to get the timestamp
func (b *Build) GetFinishTime() (int64, error) {
var finished Finished
if err := unmarshalJSONFile(path.Join(b.StoragePath, FinishedJSON), &finished); err != nil {
return -1, err
}
return finished.Timestamp, nil
}
// GetArtifacts gets gcs path for all artifacts of current build
func (b *Build) GetArtifacts() []string {
return gcs.ListChildrenFiles(ctx, BucketName, b.GetArtifactsDir())
}
// GetArtifactsDir gets gcs path for artifacts of current build
func (b *Build) GetArtifactsDir() string {
return path.Join(b.StoragePath, ArtifactsDir)
}
// GetBuildLogPath gets "build-log.txt" path for current build
func (b *Build) GetBuildLogPath() string {
return path.Join(b.StoragePath, BuildLog)
}
// ReadFile reads given file of current build,
// relPath is the file path relative to build directory
func (b *Build) ReadFile(relPath string) ([]byte, error) {
return gcs.Read(ctx, BucketName, path.Join(b.StoragePath, relPath))
}
// ParseLog parses the build log and returns the lines where the checkLog func does not return an empty slice,
// checkLog function should take in the log statement and return a part from that statement that should be in the log output.
func (b *Build) ParseLog(checkLog func(s []string) *string) ([]string, error) {
var logs []string
f, err := gcs.NewReader(ctx, b.Bucket, b.GetBuildLogPath())
if err != nil {
return logs, err
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
if s := checkLog(strings.Fields(scanner.Text())); s != nil {
logs = append(logs, *s)
}
}
return logs, nil
}
// getBuildIDFromBuildPath digests gcs build path and return last portion of path
func getBuildIDFromBuildPath(buildPath string) (int, error) {
_, buildIDStr := path.Split(strings.TrimRight(buildPath, " /"))
return strconv.Atoi(buildIDStr)
}
// unmarshalJSONFile reads a file from gcs, parses it with xml and write to v.
// v must be an arbitrary struct, slice, or string.
func unmarshalJSONFile(storagePath string, v interface{}) error {
contents, err := gcs.Read(ctx, BucketName, storagePath)
if err != nil {
return err
}
return json.Unmarshal(contents, v)
}