forked from docker-flow/docker-flow-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathha_proxy.go
More file actions
105 lines (95 loc) · 2.71 KB
/
Copy pathha_proxy.go
File metadata and controls
105 lines (95 loc) · 2.71 KB
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
package main
import (
"fmt"
"os"
"os/exec"
"strings"
)
type Proxy interface {
RunCmd(extraArgs []string) error
CreateConfigFromTemplates(templatesPath string, configsPath string) error
ReadConfig(configsPath string) (string, error)
Reload() error
}
var proxy Proxy = HaProxy{}
type HaProxy struct{}
func (m HaProxy) RunCmd(extraArgs []string) error {
args := []string{
"-f",
"/cfg/haproxy.cfg",
"-D",
"-p",
"/var/run/haproxy.pid",
}
args = append(args, extraArgs...)
cmd := exec.Command("haproxy", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmdRunHa(cmd); err != nil {
configData, _ := readConfigsFile("/cfg/haproxy.cfg")
return fmt.Errorf("Command %s\n%s\n%s", strings.Join(cmd.Args, " "), err.Error(), string(configData))
}
return nil
}
func (m HaProxy) CreateConfigFromTemplates(templatesPath string, configsPath string) error {
configsContent, err := m.getConfigs(templatesPath)
if err != nil {
return err
}
configPath := fmt.Sprintf("%s/haproxy.cfg", configsPath)
return writeFile(configPath, []byte(configsContent), 0664)
}
func (m HaProxy) ReadConfig(configsPath string) (string, error) {
configPath := fmt.Sprintf("%s/haproxy.cfg", configsPath)
out, err := readFile(configPath)
if err != nil {
return "", err
}
return string(out[:]), nil
}
func (m HaProxy) Reload() error {
logPrintf("Reloading the proxy")
pidPath := "/var/run/haproxy.pid"
pid, err := readPidFile(pidPath)
if err != nil {
return fmt.Errorf("Could not read the %s file\n%s", pidPath, err.Error())
}
cmdArgs := []string{"-sf", string(pid)}
return HaProxy{}.RunCmd(cmdArgs)
}
func (m HaProxy) getConfigs(templatesPath string) (string, error) {
content := []string{}
configsFiles := []string{"haproxy.tmpl"}
configs, err := readConfigsDir(templatesPath)
if err != nil {
return "", fmt.Errorf("Could not read the directory %s\n%s", templatesPath, err.Error())
}
for _, fi := range configs {
if strings.HasSuffix(fi.Name(), "-fe.cfg") {
configsFiles = append(configsFiles, fi.Name())
}
}
for _, fi := range configs {
if strings.HasSuffix(fi.Name(), "-be.cfg") {
configsFiles = append(configsFiles, fi.Name())
}
}
for _, file := range configsFiles {
templateBytes, err := readConfigsFile(fmt.Sprintf("%s/%s", templatesPath, file))
if err != nil {
return "", fmt.Errorf("Could not read the file %s\n%s", file, err.Error())
}
content = append(content, string(templateBytes))
}
if len(configsFiles) == 1 {
content = append(content, `frontend dummy-fe
bind *:80
bind *:443
option http-server-close
acl url_dummy path_beg /dummy
use_backend dummy-be if url_dummy
backend dummy-be
server dummy 1.1.1.1:1111 check`)
}
return strings.Join(content, "\n\n"), nil
}