-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildinfo.go
145 lines (125 loc) · 4.75 KB
/
buildinfo.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
// Copyright 2017 The LUCI 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.
package swarming
import (
"context"
"github.com/TriggerMail/luci-go/common/errors"
"github.com/TriggerMail/luci-go/common/logging"
miloProto "github.com/TriggerMail/luci-go/common/proto/milo"
"github.com/TriggerMail/luci-go/grpc/grpcutil"
"github.com/TriggerMail/luci-go/logdog/common/types"
milo "github.com/TriggerMail/luci-go/milo/api/proto"
"github.com/TriggerMail/luci-go/milo/buildsource/rawpresentation"
)
// BuildInfoProvider provides build information.
//
// In a production system, this will be completely defaults. For testing, the
// various services and data sources may be substituted for testing stubs.
type BuildInfoProvider struct {
// swarmingServiceFunc returns a SwarmingService instance for the supplied
// parameters.
//
// If nil, a production fetcher will be generated.
swarmingServiceFunc func(c context.Context, host string) (SwarmingService, error)
}
func (p *BuildInfoProvider) newSwarmingService(c context.Context, host string) (SwarmingService, error) {
if p.swarmingServiceFunc == nil {
return NewProdService(c, host)
}
return p.swarmingServiceFunc(c, host)
}
// GetBuildInfo resolves a Milo protobuf Step for a given Swarming task.
func (p *BuildInfoProvider) GetBuildInfo(c context.Context, req *milo.BuildInfoRequest_Swarming,
projectHint string) (*milo.BuildInfoResponse, error) {
// Load the Swarming task (no log content).
sf, err := p.newSwarmingService(c, req.Host)
if err != nil {
logging.WithError(err).Errorf(c, "Failed to create Swarming fetcher.")
return nil, grpcutil.Internal
}
// Use default Swarming host.
host := sf.GetHost()
logging.Infof(c, "Loading build info for Swarming host %s, task %s.", host, req.Task)
fr, err := swarmingFetch(c, sf, req.Task, swarmingFetchParams{})
if err != nil {
if err == ErrNotMiloJob {
logging.Warningf(c, "User requested nonexistent task or does not have permissions.")
return nil, grpcutil.NotFound
}
logging.WithError(err).Errorf(c, "Failed to load Swarming task.")
return nil, grpcutil.Internal
}
// Determine the LogDog annotation stream path for this Swarming task.
//
// On failure, will return a gRPC error.
stream, err := resolveLogDogAnnotations(c, fr.res.Tags)
if err != nil {
logging.WithError(err).Warningf(c, "Failed to get annotation stream parameters.")
return nil, err
}
logging.Fields{
"host": stream.Host,
"project": stream.Project,
"path": stream.Path,
}.Infof(c, "Resolved LogDog annotation stream.")
step, err := rawpresentation.ReadAnnotations(c, stream)
if err != nil {
return nil, errors.Annotate(err, "failed to read annotations").Err()
}
// Add Swarming task parameters to the Milo step.
if err := addTaskToMiloStep(c, sf.GetHost(), fr.res, step); err != nil {
return nil, err
}
prefix, name := stream.Path.Split()
return &milo.BuildInfoResponse{
Project: string(stream.Project),
Step: step,
AnnotationStream: &miloProto.LogdogStream{
Server: stream.Host,
Prefix: string(prefix),
Name: string(name),
},
}, nil
}
// resolveLogDogAnnotations returns a configured AnnotationStream given the input
// parameters.
//
// This will return a gRPC error on failure.
func resolveLogDogAnnotations(c context.Context, tagsRaw []string) (*types.StreamAddr, error) {
addr, err := resolveLogDogStreamAddrFromTags(swarmingTags(tagsRaw))
if err != nil {
logging.WithError(err).Debugf(c, "Could not resolve stream address from tags.")
return nil, grpcutil.NotFound
}
return addr, nil
}
func resolveLogDogStreamAddrFromTags(tags map[string]string) (*types.StreamAddr, error) {
// If we don't have a LUCI project, abort.
luciProject, logLocation := tags["luci_project"], tags["log_location"]
switch {
case luciProject == "":
return nil, errors.New("no 'luci_project' tag")
case logLocation == "":
return nil, errors.New("no 'log_location' tag")
}
addr, err := types.ParseURL(logLocation)
if err != nil {
return nil, errors.Annotate(err, "could not parse LogDog stream from location").Err()
}
// The LogDog stream's project should match the LUCI project.
if string(addr.Project) != luciProject {
return nil, errors.Reason("stream project %q doesn't match LUCI project %q", addr.Project, luciProject).Err()
}
return addr, nil
}