-
Notifications
You must be signed in to change notification settings - Fork 19
/
config.go
152 lines (118 loc) · 2.56 KB
/
config.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 (
"io/ioutil"
"os"
"text/template"
"fmt"
"encoding/json"
"sync"
)
var LocalFilename string
var TemplateFile string
var ConfigFile string
func SetFileName(c string) error {
LocalFilename = c
return nil
}
func SetTemplateFileName(c string) error {
TemplateFile = c
return nil
}
func SetConfigFileName(c string) error {
ConfigFile = c
return nil
}
// updates the weight of a server of a specific backend with a new weight
func UpdateWeightInConfig(backend string, server string, weight int, config *Config) error {
config.Mutex.RLock()
defer config.Mutex.RUnlock()
for _, be := range config.Backends {
if be.Name == backend {
for _, srv := range be.BackendServers {
if srv.Name == server {
srv.Weight = weight
}
}
}
}
err := WriteConfigToDisk(config)
return err
}
func GetACLsFromConfig(frontend string, config *Config) [] *ACL {
config.Mutex.RLock()
defer config.Mutex.RUnlock()
var acls [] *ACL
for _, fe := range config.Frontends {
fmt.Printf(fe.Name)
if fe.Name == frontend {
acls = fe.ACLs
}
}
return acls
}
// Render a config object to a HAproxy config file
func RenderConfig(config *Config) error {
f, err := ioutil.ReadFile(TemplateFile)
if err != nil {
return err
}
fp, err := os.OpenFile(ConfigFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
return err
}
defer fp.Close()
config.Mutex.RLock()
defer config.Mutex.RUnlock()
// before rendering, commit config to disk
err = WriteConfigToDisk(config)
if err != nil {
return err
}
t := template.Must(template.New(TemplateFile).Parse(string(f)))
err = t.Execute(fp, &config)
if err != nil {
return err
}
return nil
}
func GetConfigFromDisk() (*Config, error) {
c := ConfigObj
s, err := ioutil.ReadFile(LocalFilename)
if err != nil {
return c,err
}
err = json.Unmarshal(s, &ConfigObj)
if err != nil {
fmt.Println("Error parsing JSON")
return c,err
}
ConfigObj.Mutex = new(sync.RWMutex)
return ConfigObj, err
}
func WriteConfigToDisk(config *Config) error {
b, err := json.Marshal(config)
if err != nil {
return err
}
err = ioutil.WriteFile(LocalFilename, b, 0666)
if err != nil {
return err
}
return nil
}
func RenderLocalProxyConfig(payload []byte, conf *Config) {
json.Unmarshal(payload, conf)
err := RenderConfig(ConfigObj)
if err != nil {
log.Error("Error rendering config file")
return
} else {
err = Reload()
if err != nil {
log.Error("Error reloading the HAproxy configuration")
return
} else {
log.Info("Successfully reloaded HAproxy configuration")
}
}
}