-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure.go
56 lines (51 loc) · 1.08 KB
/
configure.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
package main
import (
"crypto/md5"
"io"
"log"
"os"
"time"
"path/filepath"
dockerClient "github.com/docker/docker/client"
"github.com/subgraph/inotify"
)
func pollConfig(dockerCli *dockerClient.Client) {
filePath := filepath.Join(ConfigDir, "/services/", ServiceName, "/haproxy.cfg")
fileHash := ""
for {
f, err := os.Open(filePath)
if err != nil {
log.Println(err)
}
h := md5.New()
if _, err := io.Copy(h, f); err != nil {
log.Println(err)
}
newHash := string(h.Sum(nil))
if fileHash != newHash {
configureService(dockerCli)
fileHash = newHash
}
f.Close()
time.Sleep(3 * time.Second)
}
}
func watchConfig(dockerCli *dockerClient.Client) {
filePath := filepath.Join(ConfigDir, "/services/", ServiceName, "/haproxy.cfg")
watcher, err := inotify.NewWatcher()
err = watcher.Watch(filePath)
if err != nil {
log.Fatal(err)
}
for {
select {
case ev := <-watcher.Event:
log.Println("event:", ev, ev.Mask)
if ev.Mask == inotify.IN_MODIFY {
configureService(dockerCli)
}
case err := <-watcher.Error:
log.Println("error:", err)
}
}
}