Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package buildbot | 5 package buildbot |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "bytes" | 8 "bytes" |
| 9 "compress/gzip" | 9 "compress/gzip" |
| 10 "compress/zlib" | 10 "compress/zlib" |
| 11 "encoding/base64" | 11 "encoding/base64" |
| 12 "encoding/json" | 12 "encoding/json" |
| 13 "fmt" | 13 "fmt" |
| 14 "net/http" | 14 "net/http" |
| 15 "strings" | 15 "strings" |
| 16 "time" | 16 "time" |
| 17 | 17 |
| 18 ds "github.com/luci/gae/service/datastore" | 18 ds "github.com/luci/gae/service/datastore" |
| 19 "github.com/luci/luci-go/common/clock" | 19 "github.com/luci/luci-go/common/clock" |
| 20 "github.com/luci/luci-go/common/iotools" | 20 "github.com/luci/luci-go/common/iotools" |
| 21 "github.com/luci/luci-go/common/logging" | 21 "github.com/luci/luci-go/common/logging" |
| 22 "github.com/luci/luci-go/milo/appengine/common" | |
| 22 "github.com/luci/luci-go/server/router" | 23 "github.com/luci/luci-go/server/router" |
| 23 | 24 |
| 24 "golang.org/x/net/context" | 25 "golang.org/x/net/context" |
| 25 | 26 |
| 26 "github.com/luci/luci-go/common/tsmon/field" | 27 "github.com/luci/luci-go/common/tsmon/field" |
| 27 "github.com/luci/luci-go/common/tsmon/metric" | 28 "github.com/luci/luci-go/common/tsmon/metric" |
| 28 ) | 29 ) |
| 29 | 30 |
| 30 var ( | 31 var ( |
| 31 // publicSubName is the name of the pubsub subscription that milo is exp ecting. | |
| 32 // TODO(hinoka): This should be read from luci-config. | |
| 33 publicSubName = "projects/luci-milo/subscriptions/buildbot-public" | |
| 34 internalSubName = "projects/luci-milo/subscriptions/buildbot-private" | |
| 35 | |
| 36 // Metrics | 32 // Metrics |
| 37 buildCounter = metric.NewCounter( | 33 buildCounter = metric.NewCounter( |
| 38 "luci/milo/buildbot_pubsub/builds", | 34 "luci/milo/buildbot_pubsub/builds", |
| 39 "The number of buildbot builds received by Milo from PubSub", | 35 "The number of buildbot builds received by Milo from PubSub", |
| 40 nil, | 36 nil, |
| 41 field.Bool("internal"), | 37 field.Bool("internal"), |
| 42 field.String("master"), | 38 field.String("master"), |
| 43 field.String("builder"), | 39 field.String("builder"), |
| 44 field.Bool("finished"), | 40 field.Bool("finished"), |
| 45 // Status can be one of 3 options. "New", "Replaced", "Rejected ". | 41 // Status can be one of 3 options. "New", "Replaced", "Rejected ". |
| (...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 300 msg := pubSubSubscription{} | 296 msg := pubSubSubscription{} |
| 301 now := int(clock.Now(c).Unix()) | 297 now := int(clock.Now(c).Unix()) |
| 302 defer r.Body.Close() | 298 defer r.Body.Close() |
| 303 dec := json.NewDecoder(r.Body) | 299 dec := json.NewDecoder(r.Body) |
| 304 if err := dec.Decode(&msg); err != nil { | 300 if err := dec.Decode(&msg); err != nil { |
| 305 logging.WithError(err).Errorf( | 301 logging.WithError(err).Errorf( |
| 306 c, "Could not decode message. %s", err) | 302 c, "Could not decode message. %s", err) |
| 307 return 200 // This is a hard failure, we don't want PubSub to re try. | 303 return 200 // This is a hard failure, we don't want PubSub to re try. |
| 308 } | 304 } |
| 309 internal := true | 305 internal := true |
| 306 // Get the name of the subscription on luci-config | |
| 307 settings, err := common.GetSettings(c) | |
| 308 if err != nil { | |
| 309 logging.WithError(err).Errorf(c, | |
| 310 "Cannot load settings to check subscription name.") | |
| 311 // This is a configuration error. Tell PubSub to retry until we fix our | |
| 312 // configs. | |
| 313 return 500 | |
|
nodir
2017/03/24 08:41:15
please use http.Status* contants
In unchanged code
hinoka
2017/03/28 17:47:20
Done.
| |
| 314 } | |
| 310 switch msg.Subscription { | 315 switch msg.Subscription { |
| 311 » // TODO(hinoka): Move these names to luci-config | 316 » case settings.Buildbot.PublicTopic: |
| 312 » case publicSubName, publicSubName + "-dev": | |
| 313 internal = false | 317 internal = false |
| 314 » case internalSubName, internalSubName + "-dev": | 318 » case settings.Buildbot.InternalTopic: |
| 315 // internal = true, but that's already set. | 319 // internal = true, but that's already set. |
| 316 default: | 320 default: |
| 317 logging.Errorf( | 321 logging.Errorf( |
| 318 c, "Subscription name %s does not match %s or %s", | 322 c, "Subscription name %s does not match %s or %s", |
| 319 » » » msg.Subscription, publicSubName, internalSubName) | 323 » » » msg.Subscription, settings.Buildbot.PublicTopic, |
| 320 » » return 200 | 324 » » » settings.Buildbot.InternalTopic) |
| 325 » » // This is a configuration error. Tell PubSub to retry until we fix our | |
| 326 » » // configs. | |
| 327 » » return 500 | |
| 321 } | 328 } |
| 322 logging.Infof( | 329 logging.Infof( |
| 323 c, "Message ID \"%s\" from subscription %s is %d bytes long", | 330 c, "Message ID \"%s\" from subscription %s is %d bytes long", |
| 324 msg.Message.MessageID, msg.Subscription, r.ContentLength) | 331 msg.Message.MessageID, msg.Subscription, r.ContentLength) |
| 325 bbMsg, err := msg.GetData() | 332 bbMsg, err := msg.GetData() |
| 326 if err != nil { | 333 if err != nil { |
| 327 logging.WithError(err).Errorf(c, "Could not base64 decode messag e %s", err) | 334 logging.WithError(err).Errorf(c, "Could not base64 decode messag e %s", err) |
| 328 return 200 | 335 return 200 |
| 329 } | 336 } |
| 330 builds, master, err := unmarshal(c, bbMsg) | 337 builds, master, err := unmarshal(c, bbMsg) |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 398 | 405 |
| 399 } | 406 } |
| 400 if master != nil { | 407 if master != nil { |
| 401 code := doMaster(c, master, internal) | 408 code := doMaster(c, master, internal) |
| 402 if code != 0 { | 409 if code != 0 { |
| 403 return code | 410 return code |
| 404 } | 411 } |
| 405 } | 412 } |
| 406 return 200 | 413 return 200 |
| 407 } | 414 } |
| OLD | NEW |