mirrored from https://chromium.googlesource.com/infra/luci/luci-go
/
info_swarming.go
216 lines (194 loc) · 5.52 KB
/
info_swarming.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
// Copyright 2020 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 job
import (
"reflect"
"time"
"github.com/golang/protobuf/ptypes"
"google.golang.org/protobuf/proto"
"go.chromium.org/luci/common/errors"
swarmingpb "go.chromium.org/luci/swarming/proto/api"
)
type swInfo struct {
*Swarming
userPayload *swarmingpb.CASTree
casUserPayload *swarmingpb.CASReference
}
var _ Info = swInfo{}
func (s swInfo) SwarmingHostname() string {
return s.GetHostname()
}
func (s swInfo) TaskName() string {
return s.GetTask().GetName()
}
func (s swInfo) CurrentIsolated() (*isolated, error) {
isolated := &isolated{}
var err error
if isolated.CASTree, err = s.currentIsoInfo(); err != nil {
return nil, err
}
if isolated.CASReference, err = s.currentCasInfo(); err != nil {
return nil, err
}
return isolated, nil
}
func (s swInfo) currentIsoInfo() (*swarmingpb.CASTree, error) {
isolatedOptions := map[string]*swarmingpb.CASTree{}
if up := s.userPayload; up != nil && up.Digest != "" {
isolatedOptions[up.Digest] = up
}
if sw := s.Swarming; sw != nil {
for _, slc := range sw.GetTask().GetTaskSlices() {
input := slc.GetProperties().GetCasInputs()
if input != nil {
isolatedOptions[input.Digest] = input
}
}
}
if len(isolatedOptions) > 1 {
return nil, errors.Reason(
"Definition contains multiple isolate inputs: %v", isolatedOptions).Err()
}
for _, v := range isolatedOptions {
return proto.Clone(v).(*swarmingpb.CASTree), nil
}
return nil, nil
}
func (s swInfo) currentCasInfo() (*swarmingpb.CASReference, error) {
casOptions := map[string]*swarmingpb.CASReference{}
if p := s.casUserPayload; p.GetDigest().GetHash() != "" {
casOptions[p.Digest.Hash] = p
}
if sw := s.Swarming; sw != nil {
for _, slc := range sw.GetTask().GetTaskSlices() {
input := slc.GetProperties().GetCasInputRoot()
if input != nil {
casOptions[input.Digest.GetHash()] = input
}
}
}
if len(casOptions) > 1 {
return nil, errors.Reason(
"Definition contains multiple RBE-CAS inputs: %v", casOptions).Err()
}
for _, v := range casOptions {
return proto.Clone(v).(*swarmingpb.CASReference), nil
}
return nil, nil
}
func (s swInfo) Dimensions() (ExpiringDimensions, error) {
ldims := logicalDimensions{}
var totalExpiration time.Duration
for _, slc := range s.GetTask().GetTaskSlices() {
exp, err := ptypes.Duration(slc.Expiration)
if err != nil {
return nil, errors.Annotate(err, "malformed expiration").Err()
}
totalExpiration += exp
for _, dim := range slc.GetProperties().GetDimensions() {
for _, val := range dim.Values {
ldims.updateDuration(dim.Key, val, totalExpiration)
}
}
}
return ldims.toExpiringDimensions(), nil
}
func (s swInfo) CIPDPkgs() (ret CIPDPkgs, err error) {
slices := s.GetTask().GetTaskSlices()
if len(slices) >= 1 {
if pkgs := slices[0].GetProperties().GetCipdInputs(); len(pkgs) > 0 {
ret = CIPDPkgs{}
ret.fromList(pkgs)
}
}
if len(slices) > 1 {
for idx, slc := range slices[1:] {
pkgDict := CIPDPkgs{}
pkgDict.fromList(slc.GetProperties().GetCipdInputs())
if !ret.equal(pkgDict) {
return nil, errors.Reason(
"slice %d has cipd pkgs which differ from slice 0: %v vs %v",
idx+1, pkgDict, ret).Err()
}
}
}
return
}
func (s swInfo) Env() (ret map[string]string, err error) {
slices := s.GetTask().GetTaskSlices()
extractEnv := func(slc *swarmingpb.TaskSlice) (slcEnv map[string]string) {
if env := slices[0].GetProperties().GetEnv(); len(env) > 0 {
slcEnv = make(map[string]string, len(env))
for _, pair := range env {
slcEnv[pair.Key] = pair.Value
}
}
return
}
if len(slices) >= 1 {
ret = extractEnv(slices[0])
for idx, slc := range slices[1:] {
if slcEnv := extractEnv(slc); !reflect.DeepEqual(ret, slcEnv) {
return nil, errors.Reason(
"slice %d has env which differs from slice 0: %v vs %v",
idx+1, slcEnv, ret).Err()
}
}
}
return
}
func (s swInfo) Priority() int32 {
return s.GetTask().GetPriority()
}
func (s swInfo) PrefixPathEnv() (ret []string, err error) {
slices := s.GetTask().GetTaskSlices()
if len(slices) >= 1 {
for _, keyVals := range slices[0].GetProperties().GetEnvPaths() {
if keyVals.Key == "PATH" {
ret = make([]string, len(keyVals.Values))
copy(ret, keyVals.Values)
break
}
}
}
if len(slices) > 1 {
for idx, slc := range slices[1:] {
foundIt := false
for _, keyVal := range slc.GetProperties().GetEnvPaths() {
if keyVal.Key == "PATH" {
foundIt = true
if !reflect.DeepEqual(ret, keyVal.Values) {
return nil, errors.Reason(
"slice %d has $PATH env prefixes which differ from slice 0: %v vs %v",
idx+1, keyVal.Values, ret).Err()
}
break
}
}
if !foundIt && len(ret) > 0 {
return nil, errors.Reason(
"slice %d has $PATH env prefixes which differ from slice 0: %v vs %v",
idx+1, []string{}, ret).Err()
}
}
}
return
}
func (s swInfo) Tags() (ret []string) {
if tags := s.GetTask().GetTags(); len(tags) > 0 {
ret = make([]string, len(tags))
copy(ret, tags)
}
return
}