-
Notifications
You must be signed in to change notification settings - Fork 3
/
consul.go
172 lines (148 loc) · 4.05 KB
/
consul.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"fmt"
"log"
"os"
"os/signal"
"strconv"
"syscall"
"time"
consulclient "github.com/hashicorp/consul/api"
uuid "github.com/satori/go.uuid"
)
var serviceIDs []string
var agent *consulclient.Agent
func setupConsul() {
var err error
// Check environment variables
if advertisedHost == "" {
advertisedHost = os.Getenv("ADVERTISED_HOST")
}
if advertisedPort == 0 && os.Getenv("ADVERTISED_PORT") != "" {
advertisedPort, err = strconv.Atoi(os.Getenv("ADVERTISED_PORT"))
if err != nil {
log.Printf("Could not parse ADVERTISED_PORT: %v", err.Error())
return
}
}
if consulHost == "" {
consulHost = os.Getenv("CONSUL_HOST")
}
if consulHost == "" {
consulHost = os.Getenv("CONSUL_PORT_8500_TCP_ADDR")
}
portString := ""
if consulPort == 0 {
if os.Getenv("CONSUL_PORT") != "" {
portString = os.Getenv("CONSUL_PORT")
}
if os.Getenv("CONSUL_PORT_8500_TCP_PORT") != "" {
portString = os.Getenv("CONSUL_PORT_8500_TCP_PORT")
}
if portString != "" {
consulPort, err = strconv.Atoi(portString)
if err != nil {
log.Printf("Could not parse port: %v", err.Error())
return
}
}
}
if !(advertisedHost != "" && advertisedPort != 0 && consulHost != "" && consulPort != 0) {
log.Printf("not connecting to Consul. Please set consulHost, consulPort, advertisedHost, advertisedPort in gruntfile.yml, or set the CONSUL_ADDR, CONSUL_PORT_8500_TCP_PORT, ADVERTISED_PORT, ADVERTISED_HOST environment variables")
return
}
consulConfig := consulclient.DefaultConfig()
consulConfig.Address = fmt.Sprintf("%v:%d", consulHost, consulPort)
consul, err := consulclient.NewClient(consulConfig)
if err != nil {
log.Fatal(err)
}
agent = consul.Agent()
// Register cleanup callback
// when the program exits with SIGTERM and Interrupt (SIGINT), cleanly leave consul
s := make(chan os.Signal, 1)
signal.Notify(s, os.Interrupt)
signal.Notify(s, syscall.SIGTERM)
go func() {
<-s
for _, id := range serviceIDs {
log.Printf("deregister %v", id)
err := agent.ServiceDeregister(id)
if err != nil {
log.Printf("Could not deregister %v\n", err.Error())
}
}
os.Exit(0)
}()
}
func registerConfigWithConsul(configD *ConfigD) {
var err error
// Make sure we have something to report...
if len(configD.Services) == 0 {
return
}
setupConsul()
if agent == nil {
log.Printf("not registering %v with consul, no connection exists", configD.Name)
return
}
// Register us as a service. Each endpoint is listed as a tag.
tags := make([]string, 0)
// tags = append(tags, name)
// for _, service := range configD.Services {
// tags = append(tags, service.EndPoint)
// }
name := configD.Name
if name == "" {
name = "grunt"
}
service := consulclient.AgentServiceRegistration{
ID: "grunt-" + uuid.NewV4().String(),
Name: name,
Tags: tags,
Port: advertisedPort,
Address: advertisedHost,
}
err = agent.ServiceRegister(&service)
if err != nil {
log.Printf("error registering %v", err.Error())
}
log.Printf("Registered %v / %v @ %v:%v with consul", service.Name, service.ID, service.Address, service.Port)
serviceIDs = append(serviceIDs, service.ID)
// Register our check
// We must check in with Consul every minute
check := consulclient.AgentCheckRegistration{
Name: service.ID,
ServiceID: service.ID,
}
check.TTL = "1m"
err = agent.CheckRegister(&check)
if err != nil {
log.Printf("error registering check %v", err.Error())
}
ttl := func() {
numberOfJobs := 0
for _, job := range jobs {
for _, service := range configD.Services {
if job.EndPoint == service.EndPoint {
numberOfJobs++
}
}
}
if numberOfJobs <= config.WarnLevel {
agent.PassTTL(check.Name, fmt.Sprintf("%d jobs", numberOfJobs))
} else if numberOfJobs <= config.CriticalLevel {
agent.WarnTTL(check.Name, fmt.Sprintf("%d jobs", numberOfJobs))
} else {
agent.FailTTL(check.Name, fmt.Sprintf("%d jobs", numberOfJobs))
}
}
go func() {
ttl()
// wake up every 30 seconds and check in
ticker := time.NewTicker(time.Second * 30)
for range ticker.C {
ttl()
}
}()
}