-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathhaproxy.go
46 lines (39 loc) · 863 Bytes
/
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
package main
import (
"log"
"os"
"os/exec"
"strconv"
"text/template"
)
var tpl *template.Template = nil
var pid int = -1
func createConfigFile(backends []Backend, templateFile, outputFile string) error {
cfgFile, _ := os.Create(outputFile)
defer cfgFile.Close()
if tpl == nil {
var err error = nil
tpl, err = template.ParseFiles(templateFile)
if err != nil {
return err
}
}
return tpl.Execute(cfgFile, backends)
}
func reloadHAproxy(command, configFile string) error {
var cmd *exec.Cmd = nil
if pid == -1 {
log.Println("Start HAproxy")
cmd = exec.Command(command, "-f", configFile)
go cmd.Wait()
} else {
log.Println("Restart HAproxy")
cmd = exec.Command(command, "-f", configFile, "-sf", strconv.Itoa(pid))
}
err := cmd.Start()
if err == nil {
pid = cmd.Process.Pid
log.Println("New pid: ", pid)
}
return err
}