-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
152 lines (132 loc) · 3.75 KB
/
main.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
package main
import (
"flag"
"log"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/oklog/run"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type resource struct {
team string
pipeline string
name string
token string
}
var (
listenAddr string
concourseURL string
authUser string
authPassword string
refreshInterval time.Duration
webhookConcurrency int
flags *flag.FlagSet
debug bool
)
func init() {
//we go with our own flagset to get rid of crap added by glog to the default flagset
flags = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
flags.StringVar(&listenAddr, "listen-addr", ":8080", "Listen address of webhook ingester")
flags.StringVar(&concourseURL, "concourse-url", "", "External URL of the concourse api")
flags.StringVar(&authUser, "auth-user", "", "Basic auth concourse username")
flags.StringVar(&authPassword, "auth-password", "", "Basic auth concourse password")
flags.DurationVar(&refreshInterval, "refresh-interval", 5*time.Minute, "Resource refresh interval")
flags.IntVar(&webhookConcurrency, "webhook-concurrency", 20, "How many resources to notify in parallel")
flags.BoolVar(&debug, "dry-run", false, "Dry-run. Don't call webhooks")
}
func main() {
flags.Parse(os.Args[1:])
if concourseURL == "" || authUser == "" || authPassword == "" {
log.Fatal("Missing one or more of required flags: -concourse-url -auth-user -auth-password")
}
client, err := NewConcourseClient(concourseURL, authUser, authPassword)
if err != nil {
log.Fatalf("Failed to create Concourse client")
}
var group run.Group
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt, syscall.SIGTERM) // Push signals into channel
//setup signal Handler
cancelSignal := make(chan struct{})
group.Add(func() error {
select {
case sig := <-sigs:
log.Printf("Received %s signal, shutting down", sig)
case <-cancelSignal:
}
return nil
}, func(_ error) {
close(cancelSignal)
})
//setup resource cache
cancelCache := make(chan struct{})
group.Add(func() error {
defer logend(logstart("resource cache"))
tick := time.NewTicker(refreshInterval)
defer tick.Stop()
for {
if err := UpdateCache(*client); err != nil {
log.Printf("Failed to update cache: %s", err)
}
select {
case <-tick.C:
case <-cancelCache:
return nil
}
}
}, func(_ error) {
close(cancelCache)
})
//setup workqueue
requestQueue := NewRequestWorkqueue(webhookConcurrency)
cancelQueue := make(chan struct{})
group.Add(func() error {
defer logend(logstart("request workqueue"))
requestQueue.Run(cancelQueue)
return nil
}, func(_ error) {
close(cancelQueue)
})
//setup http server
ln, err := net.Listen("tcp", listenAddr)
if err != nil {
log.Fatalf("Failed to listen on %s: %s", listenAddr, err)
}
group.Add(func() error {
defer logend(logstart("http server"))
log.Printf("Listening for incoming webhooks on %s", ln.Addr())
mux := http.NewServeMux()
requestCounter := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "http_requests_total",
Help: "Total number of incoming HTTP requests",
},
[]string{"code", "method"},
)
prometheus.Register(requestCounter)
ghHandler := promhttp.InstrumentHandlerCounter(requestCounter, &GithubWebhookHandler{requestQueue})
mux.Handle("/github", ghHandler)
mux.Handle("/metrics", promhttp.Handler())
return http.Serve(ln, mux)
}, func(_ error) {
ln.Close()
})
group.Run()
}
func logstart(what string) string {
log.Println("Starting ", what)
return what
}
func logend(what string) {
log.Println("Stopped ", what)
}
func debugf(format string, args ...interface{}) {
if debug {
log.Printf(format, args...)
}
}