-
Notifications
You must be signed in to change notification settings - Fork 19
/
haproxy-rest.go
363 lines (265 loc) · 8.5 KB
/
haproxy-rest.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//todo: Removing of acls and spikelimits is not persisted yet. Adding is working
package main
import (
"github.com/gin-gonic/gin"
"flag"
"io/ioutil"
"github.com/jcelliott/lumber"
"os"
"strconv"
)
// override the standard Gin-Gonic middleware to add the CORS headers
func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Content-Type", "application/json")
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
}
}
// set some globally used vars
var (
ConfigObj *Config
logFile, _ = lumber.NewFileLogger("/tmp/haproxy-rest.log", lumber.INFO, lumber.ROTATE, 1000, 3, 100)
logConsole = lumber.NewConsoleLogger(lumber.INFO)
log = lumber.NewMultiLogger()
zkClient ZookeeperClient
)
func main() {
log.Prefix("Haproxy-rest")
log.AddLoggers(logFile, logConsole)
// implicit -h prints out help messages
port := flag.Int("port",10001, "Port/IP to use for the REST interface. Overrides $PORT0 env variable")
lbConfigFile := flag.String("lbConfigFile", "resources/haproxy_new.cfg", "Location of the target HAproxy config file")
lbTemplateFile := flag.String("lbTemplate", "resources/templates/haproxy_cfg.template", "Template file to build HAproxy load balancer config")
proxyTemplateFile := flag.String("proxyTemplate", "resources/templates/haproxy_localproxy_cfg.template", "Template file to build HAproxy local proxy config")
proxyConfigFile := flag.String("proxyConfigFile", "resources/templates/haproxy_localproxy_new.cfg", "Location of the target HAproxy localproxy config")
binary := flag.String("binary", "/usr/local/bin/haproxy", "Path to the HAproxy binary")
kafkaSwitch := flag.String("kafkaSwitch","off", "Switch whether to enable Kafka streaming")
kafkaHost := flag.String("kafkaHost", "localhost", "The hostname or ip address of the Kafka host")
kafkaPort := flag.Int("kafkaPort",9092, "The port of the Kafka host")
mode := flag.String("mode","loadbalancer", "Switch for \"loadbalancer\" or \"localproxy\" mode")
zooConString := flag.String("zooConString", "localhost", "A zookeeper ensemble connection string")
zooConKey := flag.String("zooConKey", "magnetic", "Zookeeper root key")
pidFile := flag.String("pidFile", "resources/haproxy-private.pid", "Location of the HAproxy PID file")
flag.Parse()
// set intial values based on the mode chosen
var perstConfFile = ""
var exampleFile = ""
if *mode == "loadbalancer" {
log.Info(" ==> Starting in Load Balancer mode <==")
perstConfFile = "resources/persistent_lb_config.json"
exampleFile = "examples/config_example.json"
SetTemplateFileName(*lbTemplateFile)
SetConfigFileName(*lbConfigFile)
} else if *mode == "localproxy" {
log.Info(" ==> Starting in Local Proxy mode <==")
perstConfFile = "resources/persistent_localproxy_config.json"
exampleFile = "examples/config_localproxy_example.json"
SetTemplateFileName(*proxyTemplateFile)
SetConfigFileName(*proxyConfigFile)
zkClient = ZookeeperClient{*zooConString}
log.Info("Connecting to Zookeeper ensemble on " + *zooConString)
zkConnection := zkClient.connect()
defer zkConnection.Close()
zkClient.watchLocalProxyConfig(zkConnection,"/" + *zooConKey + "/localproxy")
} else {
log.Error("No correct mode chosen. Please choose either \"loadbalancer\" or \"localproxy\"")
os.Exit(1)
}
// load a persistent config, if any...
SetFileName(perstConfFile)
ConfigObj, err := GetConfigFromDisk()
if err != nil {
log.Warn("Unable to load persistent config from disk")
log.Warn("Loading example config from " + exampleFile)
// set config temporarily to example config
SetFileName(exampleFile)
ConfigObj, err = GetConfigFromDisk()
SetFileName(perstConfFile)
} else {
log.Info("Loading persistent configuration from disk...")
}
if ConfigObj.PidFile != *pidFile {
ConfigObj.PidFile = *pidFile
}
//Create and empty pid file on the specified location, if not already there
if _, err := os.Stat(*pidFile); err == nil {
log.Info("Pid file exists, proceeding...")
} else {
emptyPid := []byte("")
ioutil.WriteFile(*pidFile, emptyPid, 0644)
}
SetPidFileName(*pidFile)
SetBinaryFileName(*binary)
err = RenderConfig(ConfigObj)
if err != nil {
log.Error("Error rendering config file: " + err.Error())
return
} else {
err = Reload()
if err != nil {
log.Error("Error reloading the HAproxy configuration")
return
}
}
if *kafkaSwitch == "on" {
// Setup Kafka producer
setUpProducer(*kafkaHost, *kafkaPort, *mode)
}
// Create log listener
// logSocket := "/var/run/vamp.log.sock"
// logListener(logSocket)
// defer os.Remove(logSocket)
log.Info("Starting REST server")
// initialize the web stack
r := gin.New()
// Global middlewares
r.Use(CORSMiddleware())
r.Use(gin.Logger())
r.Use(gin.Recovery())
v1 := r.Group("/v1")
{
/*
Backend Actions
*/
v1.POST("/backend/:name/server/:server/weight/:weight", func(c *gin.Context){
backend := c.Params.ByName("name")
server := c.Params.ByName("server")
weight,_ := strconv.Atoi(c.Params.ByName("weight"))
status, err := SetWeight(backend, server, weight)
// check on runtime errors
if err != nil {
c.String(500, err.Error())
} else {
switch status {
case "No such server.\n\n":
c.String(404, status)
case "No such backend.\n\n":
c.String(404, status)
default:
//update the config object with the new weight
err = UpdateWeightInConfig(backend, server, weight, ConfigObj)
c.String(200,"Ok")
}
}
})
/*
Frontend Actions
*/
v1.POST("frontend/:name/acl/:acl/:pattern",func(c *gin.Context){
backend := c.Params.ByName("name")
acl := c.Params.ByName("acl")
pattern := c.Params.ByName("pattern")
status,err := AddAcl(backend,acl,pattern)
// check on runtime errors
if err != nil {
c.String(500, err.Error())
} else {
switch status {
case "No such backend.\n\n":
c.String(404, status)
default:
//update the config object with the new acl
//err = UpdateWeightInConfig(backend, server, weight, ConfigObj)
c.String(200,"Ok")
}
}
})
v1.GET("/frontend/:name/acls",func(c *gin.Context){
frontend := c.Params.ByName("name")
status := GetACLsFromConfig(frontend,ConfigObj)
if err != nil {
c.String(500, err.Error())
} else {
c.JSON(200, status)
}
})
/*
Stats Actions
*/
// get standard stats output from haproxy
v1.GET("/stats", func(c *gin.Context) {
status, err := GetStats("all")
if err != nil {
c.String(500, err.Error())
} else {
c.JSON(200, status)
}
})
v1.GET("/stats/backend", func(c *gin.Context) {
status, err := GetStats("backend")
if err != nil {
c.String(500, err.Error())
} else {
c.JSON(200, status)
}
})
v1.GET("/stats/frontend", func(c *gin.Context) {
status, err := GetStats("frontend")
if err != nil {
c.String(500, err.Error())
} else {
c.JSON(200, status)
}
})
v1.GET("/stats/server", func(c *gin.Context) {
status, err := GetStats("server")
if err != nil {
c.String(500, err.Error())
} else {
c.JSON(200, status)
}
})
/*
Full Config Actions
*/
// get config file
v1.GET("/config", func(c *gin.Context){
c.JSON(200, ConfigObj)
})
// set config file
v1.POST("/config", func(c *gin.Context){
// clear the current config. This is dirty, should be able to clear the ConfigObj in
// one go.
ConfigObj.Frontends = [] *Frontend{}
ConfigObj.Backends = [] *Backend{}
ConfigObj.Services = [] *Service{}
c.Bind(&ConfigObj)
err = RenderConfig(ConfigObj)
if err != nil {
c.String(500, "Error rendering config file")
return
} else {
err = Reload()
if err != nil {
c.String(500, "Error reloading the HAproxy configuration")
return
} else {
c.String(200, "Ok")
}
}
})
/*
Info
*/
// get info on running process
v1.GET("/info", func(c *gin.Context) {
status, err := GetInfo()
if err != nil {
c.String(500, err.Error())
} else {
c.JSON(200, status)
}
})
/*
GUI
*/
}
// get the Mesos port to listen on
if *port == 10001 {
envPort := os.Getenv("PORT0")
if envPort != "" { *port, err = strconv.Atoi(envPort) }
}
// Listen and server on port
r.Run("0.0.0.0:" + strconv.Itoa(*port))
}