-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.go
More file actions
240 lines (205 loc) · 6.34 KB
/
main.go
File metadata and controls
240 lines (205 loc) · 6.34 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
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
package main
import (
"fmt"
"os"
"os/signal"
"strconv"
"sync"
"syscall"
"time"
"github.com/fsnotify/fsnotify"
"github.com/snorwin/haproxy-reload-wrapper/pkg/exec"
"github.com/snorwin/haproxy-reload-wrapper/pkg/log"
"github.com/snorwin/haproxy-reload-wrapper/pkg/utils"
)
var (
executable string
processes []Process
l sync.RWMutex
terminated bool
shutdownWait *time.Duration
)
type Process struct {
cmd *exec.Cmd
terminateTime *time.Time
}
func main() {
// fetch the absolut path of the haproxy executable
var err error
executable, err = utils.LookupExecutablePathAbs("haproxy")
if err != nil {
log.Emergency(err.Error())
os.Exit(1)
}
shutdownWait = utils.ShutdownWait()
log.Notice(fmt.Sprintf("shutdown wait duration : %s", shutdownWait))
// execute haproxy with the flags provided as a child process
runInstance()
watchPath := utils.LookupWatchPath()
if watchPath == "" {
watchPath = utils.LookupHAProxyConfigFile()
}
// create a fsnotify.Watcher for config changes
fswatch, err := fsnotify.NewWatcher()
if err != nil {
log.Notice(fmt.Sprintf("fsnotify watcher create failed : %v", err))
os.Exit(1)
}
if utils.DisableReload() {
log.Notice("reload disabled : no watches added")
} else {
if err := fswatch.Add(watchPath); err != nil {
log.Notice(fmt.Sprintf("watch failed : %v", err))
os.Exit(1)
}
log.Notice(fmt.Sprintf("watch : %s", watchPath))
}
// initialize a signal handler for SIGINT, SIGTERM and SIGUSR1 (for OpenShift)
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGUSR1)
// endless for loop which handles signals, file system events as well as termination of the child process
for {
select {
case event := <-fswatch.Events:
// only care about events which may modify the contents of the directory
if !event.Has(fsnotify.Write) && !event.Has(fsnotify.Remove) && !event.Has(fsnotify.Create) {
continue
}
log.Notice(fmt.Sprintf("fs event for %s : %v", watchPath, event.Op))
// re-add watch if path was removed - config maps are updated by removing/adding a symlink
if event.Has(fsnotify.Remove) {
if err := fswatch.Add(watchPath); err != nil {
log.Alert(fmt.Sprintf("watch failed : %v", err))
} else {
log.Notice(fmt.Sprintf("watch : %s", watchPath))
}
}
// create a new haproxy process which will take over listeners
// from the previous ones after it was successfully started
runInstance()
// remove old haproxy processes if SHUTDOWN_WAIT has been specified
cleanupInstances()
case err := <-fswatch.Errors:
// handle errors of fsnotify.Watcher
log.Alert(err.Error())
case sig := <-sigs:
// handle SIGINT, SIGTERM, SIGUSR1 and propagate it to child process
log.Notice(fmt.Sprintf("received singal : %d", sig))
if len(processes) == 0 {
// received termination suddenly before child process was even started
os.Exit(0)
}
// set termination flag before propagating the signal in order to prevent race conditions
terminated = true
// propagate signal to child processes
for i := range processes {
if processes[i].cmd.Process != nil {
if err := processes[i].cmd.Process.Signal(sig); err != nil {
log.Warning(fmt.Sprintf("propagating signal %d to process %d failed : %s", sig, processes[i].cmd.Process.Pid, err.Error()))
}
}
}
}
}
}
func runInstance() {
log.Notice("starting validation")
// validate the config by using the "-c" flag
argsValidate := append(os.Args[1:], "-c")
cmdValidate := exec.Command(executable, argsValidate...)
cmdValidate.Stdout = os.Stdout
cmdValidate.Stderr = os.Stderr
cmdValidate.Env = utils.LoadEnvFile()
if err := cmdValidate.Run(); err != nil {
log.Warning("validate failed : " + err.Error())
// exit if the config is invalid and no other process is running
if len(processes) == 0 {
os.Exit(1)
}
return
}
// launch the actual haproxy including the previous pids to terminate
args := os.Args[1:]
if len(processes) > 0 {
args = append(args, []string{"-x", utils.LookupHAProxySocketPath(), "-sf"}...)
args = append(args, pids()...)
}
log.Notice("validate successful : starting process...")
cmd := exec.Command(executable, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = utils.LoadEnvFile()
if err := cmd.AsyncRun(); err != nil {
log.Warning("process starting failed : " + err.Error())
}
go func(cmd *exec.Cmd) {
<-cmd.Terminated
log.Notice(fmt.Sprintf("process %d terminated : %s", cmd.Process.Pid, cmd.Status()))
// exit if termination signal was received and the last process terminated abnormally
if terminated && cmd.ProcessState.ExitCode() != 0 {
os.Exit(cmd.ProcessState.ExitCode())
}
// remove the process from tracking
l.Lock()
for i := range processes {
if processes[i].cmd.Process.Pid == cmd.Process.Pid {
processes = append(processes[:i], processes[i+1:]...)
break
}
}
l.Unlock()
// exit if there are no more processes running
if len(processes) == 0 {
if cmd.ProcessState != nil && cmd.ProcessState.ExitCode() != 0 {
os.Exit(cmd.ProcessState.ExitCode())
} else {
os.Exit(0)
}
}
}(cmd)
log.Notice(fmt.Sprintf("process started : pid %d status %s", cmd.Process.Pid, cmd.Status()))
l.Lock()
defer l.Unlock()
// set older processes to terminating
for i := range processes {
if processes[i].terminateTime == nil {
t := time.Now()
processes[i].terminateTime = &t
}
}
// add the new process
processes = append(processes, Process{cmd: cmd})
}
func cleanupInstances() {
if shutdownWait == nil {
return
}
l.Lock()
defer l.Unlock()
// kill overstayed processes
tmp := processes[:0]
for i := range processes {
if processes[i].terminateTime != nil && processes[i].terminateTime.Before(time.Now().Add(-*shutdownWait)) {
proc, err := os.FindProcess(processes[i].cmd.Process.Pid)
if err != nil {
continue
}
err = proc.Signal(syscall.SIGTERM)
// if there is an error the process does not exist and can be removed
if err == nil {
log.Notice(fmt.Sprintf("process killed : pid %d", processes[i].cmd.Process.Pid))
}
} else {
tmp = append(tmp, processes[i])
}
}
processes = tmp
}
// pids returns the PID list
func pids() []string {
out := make([]string, 0, len(processes))
for _, c := range processes {
out = append(out, strconv.Itoa(c.cmd.Process.Pid))
}
return out
}