forked from openshift/origin
/
haproxy.go
92 lines (82 loc) · 2.56 KB
/
haproxy.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
package haproxy
import (
"fmt"
"github.com/golang/glog"
"io/ioutil"
"os"
"os/exec"
"github.com/openshift/origin/pkg/router"
)
const (
ConfigTemplate = "/var/lib/haproxy/conf/haproxy_template.conf"
ConfigFile = "/var/lib/haproxy/conf/haproxy.config"
HostMapFile = "/var/lib/haproxy/conf/host_be.map"
HostMapSniFile = "/var/lib/haproxy/conf/host_be_sni.map"
HostMapResslFile = "/var/lib/haproxy/conf/host_be_ressl.map"
HostMapWsFile = "/var/lib/haproxy/conf/host_be_ws.map"
)
type HaproxyRouter struct {
*router.Routes
}
func NewRouter() *HaproxyRouter {
r := &HaproxyRouter{&router.Routes{}}
r.ReadRoutes()
return r
}
func (hr *HaproxyRouter) writeServer(f *os.File, id string, s *router.Endpoint) {
f.WriteString(fmt.Sprintf(" server %s %s:%s check inter 5000ms\n", id, s.IP, s.Port))
}
func (hr *HaproxyRouter) WriteConfig() {
//ReadRoutes()
hf, herr := os.Create(HostMapFile)
if herr != nil {
glog.Fatalf("Error creating host map file - %s", herr.Error())
}
dat, terr := ioutil.ReadFile(ConfigTemplate)
if terr != nil {
glog.Fatalf("Error reading from template configuration - %s", terr.Error())
}
f, err := os.Create(ConfigFile)
if err != nil {
glog.Fatalf("Error opening file haproxy.conf - %s", err.Error())
}
f.WriteString(string(dat))
for frontendname, frontend := range hr.GlobalRoutes {
if len(frontend.HostAliases) == 0 || len(frontend.EndpointTable) == 0 {
continue
}
for host := range frontend.HostAliases {
if frontend.HostAliases[host] != "" {
hf.WriteString(fmt.Sprintf("%s %s\n", frontend.HostAliases[host], frontendname))
}
}
f.WriteString(fmt.Sprintf("backend be_%s\n mode http\n balance leastconn\n timeout check 5000ms\n", frontendname))
for seid, se := range frontend.EndpointTable {
hr.writeServer(f, seid, &se)
}
f.WriteString("\n")
}
f.Close()
}
func execCmd(cmd *exec.Cmd) (string, bool) {
out, err := cmd.CombinedOutput()
var returnStr string
if err != nil {
fmt.Sprintf(returnStr, "Error executing command.\n%s", err.Error())
} else {
returnStr = string(out)
}
return returnStr, err == nil
}
func (hr *HaproxyRouter) ReloadRouter() bool {
oldPid, oerr := ioutil.ReadFile("/var/lib/haproxy/run/haproxy.pid")
cmd := exec.Command("/usr/local/sbin/haproxy", "-f", ConfigFile, "-p", "/var/lib/haproxy/run/haproxy.pid")
if oerr == nil {
cmd = exec.Command("/usr/local/sbin/haproxy", "-f", ConfigFile, "-p", "/var/lib/haproxy/run/haproxy.pid", "-sf", string(oldPid))
}
out, err := execCmd(cmd)
if err == false {
glog.Errorf("Error reloading haproxy router - %s", out)
}
return err
}