forked from QubitProducts/bamboo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_handler.go
123 lines (101 loc) · 2.89 KB
/
event_handler.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
package event_bus
import (
"github.com/QubitProducts/bamboo/Godeps/_workspace/src/github.com/samuel/go-zookeeper/zk"
"github.com/QubitProducts/bamboo/configuration"
"github.com/QubitProducts/bamboo/services/haproxy"
"github.com/QubitProducts/bamboo/services/template"
"io/ioutil"
"log"
"os/exec"
)
type MarathonEvent struct {
// EventType can be
// api_post_event, status_update_event, subscribe_event
EventType string
Timestamp string
}
type ZookeeperEvent struct {
Source string
EventType string
}
type ServiceEvent struct {
EventType string
}
type Handlers struct {
Conf *configuration.Configuration
Zookeeper *zk.Conn
}
func (h *Handlers) MarathonEventHandler(event MarathonEvent) {
log.Printf("%s => %s\n", event.EventType, event.Timestamp)
queueUpdate(h)
h.Conf.StatsD.Increment(1.0, "callback.marathon", 1)
}
func (h *Handlers) ServiceEventHandler(event ServiceEvent) {
log.Println("Domain mapping: Stated changed")
queueUpdate(h)
h.Conf.StatsD.Increment(1.0, "reload.domain", 1)
}
var updateChan = make(chan *Handlers, 1)
func init() {
go func() {
log.Println("Starting update loop")
for {
h := <-updateChan
handleHAPUpdate(h.Conf, h.Zookeeper)
}
}()
}
var queueUpdateSem = make(chan int, 1)
func queueUpdate(h *Handlers) {
queueUpdateSem <- 1
select {
case _ = <-updateChan:
log.Println("Found pending update request. Don't start another one.")
default:
log.Println("Queuing an haproxy update.")
}
updateChan <- h
<-queueUpdateSem
}
func handleHAPUpdate(conf *configuration.Configuration, conn *zk.Conn) bool {
currentContent, _ := ioutil.ReadFile(conf.HAProxy.OutputPath)
templateContent, err := ioutil.ReadFile(conf.HAProxy.TemplatePath)
if err != nil {
log.Panicf("Cannot read template file: %s", err)
}
templateData, err := haproxy.GetTemplateData(conf, conn)
if err != nil {
log.Printf("Not updating haproxy because we failed to retrieve template data: \n %s\n", err)
return false
}
newContent, err := template.RenderTemplate(conf.HAProxy.TemplatePath, string(templateContent), templateData)
if err != nil {
log.Fatalf("Template syntax error: \n %s", err)
}
if currentContent == nil || string(currentContent) != newContent {
err := ioutil.WriteFile(conf.HAProxy.OutputPath, []byte(newContent), 0666)
if err != nil {
log.Fatalf("Failed to write template on path: %s", err)
}
err = execCommand(conf.HAProxy.ReloadCommand)
if err != nil {
log.Fatalf("HAProxy: update failed\n")
} else {
conf.StatsD.Increment(1.0, "reload.marathon", 1)
log.Println("HAProxy: Configuration updated")
}
return true
} else {
log.Println("HAProxy: Same content, no need to reload")
return false
}
}
func execCommand(cmd string) error {
log.Printf("Exec cmd: %s \n", cmd)
output, err := exec.Command("sh", "-c", cmd).CombinedOutput()
if err != nil {
log.Println(err.Error())
log.Println("Output:\n" + string(output[:]))
}
return err
}