-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
114 lines (101 loc) · 2.5 KB
/
main.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"os/signal"
"time"
"github.com/fsnotify/fsnotify"
"github.com/sirupsen/logrus"
"github.com/thetooth/vyos-failover/config"
"github.com/thetooth/vyos-failover/decision"
"github.com/thetooth/vyos-failover/statistics"
"github.com/thetooth/vyos-failover/util"
)
var (
debug, trace bool
path string
statPath string
)
func main() {
flag.BoolVar(&debug, "debug", false, "Show additional output")
flag.BoolVar(&trace, "trace", false, "Show A LOT of output")
flag.StringVar(&path, "config", "/run/vyos-failover.conf", "Path to protocols failover configuration")
flag.StringVar(&statPath, "socket", "/tmp/vyos-failover", "Path to statstics socket")
flag.Parse()
if trace {
logrus.SetLevel(logrus.TraceLevel)
logrus.SetReportCaller(true)
} else if debug {
logrus.SetLevel(logrus.DebugLevel)
}
// Attempt configuration file load
cfg, err := config.Load(path)
if err != nil {
logrus.Panic(err)
}
// Create config watcher
watcher, err := fsnotify.NewWatcher()
if err != nil {
logrus.Fatal(err)
}
defer watcher.Close()
err = watcher.Add(path)
if err != nil {
logrus.Fatal(err)
}
// Control signals
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
flap := time.NewTicker(cfg.FlapRate.Duration)
// Build runtime route table
routes := decision.BuildRoutes(cfg)
for {
select {
case <-c: // Clean up and quit
for routeName, route := range cfg.Route {
fmt.Println("[ EXIT_CLEANUP ] route: ", routeName)
arg := fmt.Sprintf("route del %v proto failover table %v", routeName, route.Table)
util.Exec("ip", arg)
}
return
case event, ok := <-watcher.Events:
if !ok {
break
}
logrus.Trace("fsnotify:", event)
if event.Has(fsnotify.Write) {
for _, route := range routes {
for _, nexthop := range route.Nexthops {
nexthop.Check.Stop()
}
}
cfg, err = config.Load(path)
if err != nil {
logrus.Error("Did not load changed config: ", err)
break
}
routes = decision.BuildRoutes(cfg)
logrus.Info("Loaded configuration changes")
}
case <-flap.C:
// Compute next routing table
err = decision.Evaluate(routes)
if err != nil {
logrus.Debug("Issue computing routes: ", err)
}
// Update and output statistics
stats := statistics.Build(routes)
b, err := json.Marshal(stats)
if err != nil {
logrus.Panic(err)
}
err = ioutil.WriteFile(statPath, b, 0644)
if err != nil {
logrus.Panic(err)
}
}
}
}