go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/buildbucket/cmd/bbagent/swarming.go (about) 1 // Copyright 2019 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package main 16 17 import ( 18 "context" 19 20 "google.golang.org/protobuf/proto" 21 22 bbpb "go.chromium.org/luci/buildbucket/proto" 23 "go.chromium.org/luci/common/errors" 24 "go.chromium.org/luci/common/system/environ" 25 "go.chromium.org/luci/lucictx" 26 "go.chromium.org/luci/swarming/client/swarming" 27 ) 28 29 // readBuildSecrets reads BuildSecrets message from swarming secret bytes. 30 func readBuildSecrets(ctx context.Context) (*bbpb.BuildSecrets, error) { 31 swarming := lucictx.GetSwarming(ctx) 32 if swarming == nil { 33 return nil, errors.Reason("no swarming secret bytes; is this a Swarming Task with secret bytes?").Err() 34 } 35 36 secrets := &bbpb.BuildSecrets{} 37 if err := proto.Unmarshal(swarming.SecretBytes, secrets); err != nil { 38 return nil, errors.Annotate(err, "failed to read BuildSecrets message from swarming secret bytes").Err() 39 } 40 return secrets, nil 41 } 42 43 // populateSwarmingInfoFromEnv populates part of missing fields under 44 // `build.infra.swarming` using values from `SWARMING_*` environment 45 // variables. 46 func populateSwarmingInfoFromEnv(build *bbpb.Build, env environ.Env) { 47 if build.Infra == nil { 48 build.Infra = &bbpb.BuildInfra{} 49 } 50 if build.Infra.Swarming == nil { 51 build.Infra.Swarming = &bbpb.BuildInfra_Swarming{} 52 } 53 54 swarm := build.Infra.Swarming 55 if v, ok := env.Lookup(swarming.ServerEnvVar); ok && swarm.Hostname == "" { 56 swarm.Hostname = v 57 } 58 if v, ok := env.Lookup(swarming.TaskIDEnvVar); ok && swarm.TaskId == "" { 59 swarm.TaskId = v 60 } 61 }