Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add taskcluster pulse support. #18

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ go 1.14

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pborman/uuid v1.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/streadway/amqp v0.0.0-20200108173154-1c71cc93ed71
github.com/stretchr/testify v1.1.4-0.20160524234229-8d64eb7173c7
github.com/taskcluster/pulse-go v1.0.0
github.com/taskcluster/taskcluster/clients/client-go/v23 v23.0.0
github.com/urfave/cli v1.17.1-0.20160608151511-fa949b48f384
go.mozilla.org/mozlog v0.0.0-20160610165107-cd74695caf44
launchpad.net/gocheck v0.0.0-20140225173054-000000000087 // indirect
)
32 changes: 32 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,18 +1,50 @@
github.com/cenkalti/backoff/v3 v3.0.0 h1:ske+9nBpD9qZsTBoF41nW5L+AIuFBKMeze18XQ3eG1c=
github.com/cenkalti/backoff/v3 v3.0.0/go.mod h1:cIeZDE3IrqwwJl6VUwCN6trj1oXrTS4rc0ij+ULvLYs=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
github.com/fatih/camelcase v1.0.0 h1:hxNvNX/xYBp0ovncs8WyWZrOrpBNub/JfaMvbURyft8=
github.com/fatih/camelcase v1.0.0/go.mod h1:yN2Sb0lFhZJUdVvtELVWefmrXpuZESvPmqwoZc+/fpc=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/google/uuid v1.0.0 h1:b4Gk+7WdP/d3HZH8EJsZpvV7EtDOgaZLtnaNGIu1adA=
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/pborman/uuid v1.2.0 h1:J7Q5mO4ysT1dv8hyrUGHb9+ooztCXu1D8MY8DZYsu3g=
github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/streadway/amqp v0.0.0-20190827072141-edfb9018d271/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
github.com/streadway/amqp v0.0.0-20200108173154-1c71cc93ed71 h1:2MR0pKUzlP3SGgj5NYJe/zRYDwOu9ku6YHy+Iw7l5DM=
github.com/streadway/amqp v0.0.0-20200108173154-1c71cc93ed71/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
github.com/stretchr/testify v1.1.4-0.20160524234229-8d64eb7173c7 h1:5KNW+VDCZxdFJasHPW1AehzhTIm69pHQq2psutjjXRk=
github.com/stretchr/testify v1.1.4-0.20160524234229-8d64eb7173c7/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/taskcluster/httpbackoff/v3 v3.0.0 h1:Zh2BCW2iA3fzBBuZo2E4MvwyPSB6aimyI4EreeK3TRM=
github.com/taskcluster/httpbackoff/v3 v3.0.0/go.mod h1:99ubellEC0fmRj7wnGkPftT2xfCY7NmbjT3gzn2ZPUM=
github.com/taskcluster/jsonschema2go v1.0.0 h1:ZEDj2NKh8Sceq36zyLhSV6ann/aNXKZIe9cAXq7CDdk=
github.com/taskcluster/jsonschema2go v1.0.0/go.mod h1:jhsT3XPj3iLNRx0efJVfFzZBZgxeYE7IHfZAai8wuKQ=
github.com/taskcluster/pulse-go v1.0.0 h1:ys4ZUNp5TYiV5LSMxge4YF/AtuBUNH9StAA/bkno+r0=
github.com/taskcluster/pulse-go v1.0.0/go.mod h1:uuaqnRQj9XqouabCEKjnrlJiC6UT9Gurx2oSe6s+irM=
github.com/taskcluster/slugid-go v1.1.0 h1:SWsUplliyamdYzOKVM4+lDohZKuL63fKreGkvIKJ9aI=
github.com/taskcluster/slugid-go v1.1.0/go.mod h1:5sOAcPHjqso1UkKxSl77CkKgOwha0D9X0msBKBj0AOg=
github.com/taskcluster/taskcluster-base-go v1.0.0/go.mod h1:ByyzyqqufsfZTrAHUw+0Grp8FwZAizZOKzVE1IpDXxQ=
github.com/taskcluster/taskcluster-lib-urls v12.0.0+incompatible h1:57WLzh7B04y6ahTOJ8wjvdkbwYqnyJkwLXQ1Tu4E/DU=
github.com/taskcluster/taskcluster-lib-urls v12.0.0+incompatible/go.mod h1:ALqTgi15AmJGEGubRKM0ydlLAFatlQPrQrmal9YZpQs=
github.com/taskcluster/taskcluster/clients/client-go/v23 v23.0.0 h1:QQDQUy8zHLdfMF+EQEkA+NRGfjdykhHxNcPMakr+sm8=
github.com/taskcluster/taskcluster/clients/client-go/v23 v23.0.0/go.mod h1:5NZbdHaaF2/yjkHtlcEKm4AXs3EH3M5Kih0wAEGRlMs=
github.com/tent/hawk-go v0.0.0-20161026210932-d341ea318957 h1:6Fre/uvwovW5YY4nfHZk66cAg9HjT9YdFSAJHUUgOyQ=
github.com/tent/hawk-go v0.0.0-20161026210932-d341ea318957/go.mod h1:dch7ywQEefE1ibFqBG1erFibrdUIwovcwQjksYuHuP4=
github.com/urfave/cli v1.17.1-0.20160608151511-fa949b48f384 h1:nYZrQfefkHIWNPwwIToQOnTeZ5nQt4nUHiS4wHQBWSA=
github.com/urfave/cli v1.17.1-0.20160608151511-fa949b48f384/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.1.0/go.mod h1:5yf86TLmAcydyeJq5YvxkGPE2fm/u4myDekKRoLuqhs=
go.mozilla.org/mozlog v0.0.0-20160610165107-cd74695caf44 h1:ZwI2d4V8jjame21x82EXAGuKKStymcxBsNt0hkN4/5E=
go.mozilla.org/mozlog v0.0.0-20160610165107-cd74695caf44/go.mod h1:jHoPAGnDrCy6kaI2tAze5Prf0Nr0w/oNkROt2lw3n3o=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190719005602-e377ae9d6386/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
launchpad.net/gocheck v0.0.0-20140225173054-000000000087/go.mod h1:hj7XX3B/0A+80Vse0e+BUHsHMTEhd0O4cpUHr/e/BUM=
30 changes: 30 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,24 @@ func main() {
Value: "hgmo",
EnvVar: "HGMO_PULSE_QUEUE",
},
cli.StringFlag{
Name: "cloudops-deploy-pulse-prefix",
Usage: "Pulse route to listen to.",
Value: "cloudops.deploy.v1",
EnvVar: "CLOUDOPS_DEPLOY_PULSE_PREFIX",
},
cli.StringFlag{
Name: "cloudops-deploy-pulse-queue",
Usage: "Pulse queue to listen to.",
Value: "deploy-proxy",
EnvVar: "CLOUDOPS_DEPLOY_PULSE_QUEUE",
},
cli.StringFlag{
Name: "taskcluster-root-url",
Usage: "Taskcluster root URL",
Value: "https://firefox-ci-tc.services.mozilla.com",
EnvVar: "TASKCLUSTER_ROOT_URL",
},
}

app.Action = func(c *cli.Context) error {
Expand Down Expand Up @@ -111,6 +129,18 @@ func main() {
c.String("pulse-host"),
)

taskclusterPulseHandler := proxyservice.NewTaskclusterPulseHandler(
jenkins,
&pulse,
c.String("cloudops-deploy-pulse-queue"),
c.String("cloudops-deploy-pulse-prefix"),
c.String("taskcluster-root-url"),
)

if err := taskclusterPulseHandler.Consume(); err != nil {
return cli.NewExitError(fmt.Sprintf("Could not listen to taskcluster pulse: %v", err), 1)
}

hgmoPulseHandler := proxyservice.NewHgmoPulseHandler(
jenkins,
&pulse,
Expand Down
22 changes: 22 additions & 0 deletions proxyservice/jenkins.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,25 @@ func TriggerHgJob(j Jenkins, repoPath string, repoUrl string, head string, data
params.Set("RawJSON", string(rawJSON))
return j.TriggerJob(path, params)
}

func TriggerTaskclusterJob(j Jenkins, route string, deploy DeployExtra, data *TaskCompletedMessage) error {
// FIXME: This should probably be split on .
if !regexp.MustCompile(`^[a-zA-Z0-9_\-]{2,255}$`).MatchString(route) {
return fmt.Errorf("Invalid data.Repository.Namespace: %s", route)
}

rawJSON, err := json.Marshal(data)
if err != nil {
return fmt.Errorf("Error marshaling data: %v", err)
}
path := path.Join("/job/taskcluster/job",
route, "job", route)
params := url.Values{}
params.Set("IMAGE_TASK_ID", deploy.ImageTaskId)
if deploy.Variant != "" {
params.Set("VARIANT", deploy.Variant)
}
params.Set("RawJSON", string(rawJSON))
return j.TriggerJob(path, params)
}

98 changes: 98 additions & 0 deletions proxyservice/taskcluster_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package proxyservice

import (
"encoding/json"
"fmt"
"log"
"strings"

"github.com/streadway/amqp"
"github.com/taskcluster/pulse-go/pulse"
"github.com/taskcluster/taskcluster/clients/client-go/v23/tcqueue"
"github.com/taskcluster/taskcluster/clients/client-go/v23/tcqueueevents"
)

type TaskCompletedMessage = tcqueueevents.TaskCompletedMessage

type routeTaskCompleted struct {
Route string
}

func (binding routeTaskCompleted) RoutingKey() string {
return fmt.Sprintf("route.%s", binding.Route)
}

func (binding routeTaskCompleted) ExchangeName() string {
return "exchange/taskcluster-queue/v1/task-completed"
}

func (binding routeTaskCompleted) NewPayloadObject() interface{} {
return new(tcqueueevents.TaskCompletedMessage)
}

type DeployExtra struct {
ImageTaskId string `json:"image-task-id"`
Variant string `json:"variant"`
}

func ParseExtra(extra *json.RawMessage) (DeployExtra, error) {
var raw struct {
Deploy DeployExtra `json:"cloudops-deploy"`
}
if err := json.Unmarshal(*extra, &raw); err != nil {
return DeployExtra{}, err
}
return raw.Deploy, nil
}

type TaskclusterPulseHandler struct {
Jenkins Jenkins
Pulse *pulse.Connection
QueueName string
PulseRoutePrefix string
TcQueue *tcqueue.Queue
}

func NewTaskclusterPulseHandler(jenkins Jenkins, pulse *pulse.Connection, queueName string, routePrefix string, taskclusterRootUrl string) *TaskclusterPulseHandler {
return &TaskclusterPulseHandler{
Jenkins: jenkins,
Pulse: pulse,
QueueName: queueName,
PulseRoutePrefix: routePrefix,
TcQueue: tcqueue.New(nil, taskclusterRootUrl),
}
}

func (handler *TaskclusterPulseHandler) handleMessage(message interface{}, delivery amqp.Delivery) {
routingKeyPrefix := "route." + handler.PulseRoutePrefix
switch t := message.(type) {
case *tcqueueevents.TaskCompletedMessage:
if strings.HasPrefix(delivery.RoutingKey, routingKeyPrefix) {
route := strings.TrimPrefix(delivery.RoutingKey, routingKeyPrefix)
task, err := handler.TcQueue.Task(t.Status.TaskID)
if err == nil {
log.Printf("Error triggering taskcluster job: %s", err)
}
deploy, err := ParseExtra(&task.Extra)
if err == nil {
log.Printf("Error triggering taskcluster job: %s", err)
}
if err := TriggerTaskclusterJob(handler.Jenkins, route, deploy, t); err != nil {
log.Printf("Error triggering taskcluster job: %s", err)
}
}
}
delivery.Ack(false) // acknowledge message *after* processing

}

func (handler *TaskclusterPulseHandler) Consume() error {
_, err := handler.Pulse.Consume(
handler.QueueName,
handler.handleMessage,
1, // prefetch 1 message at a time
false, // don't autoacknowledge messages
routeTaskCompleted{Route: handler.PulseRoutePrefix + ".#"},
)
return err
}
22 changes: 22 additions & 0 deletions vendor/github.com/cenkalti/backoff/v3/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions vendor/github.com/cenkalti/backoff/v3/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions vendor/github.com/cenkalti/backoff/v3/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions vendor/github.com/cenkalti/backoff/v3/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 66 additions & 0 deletions vendor/github.com/cenkalti/backoff/v3/backoff.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading