-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
93 lines (81 loc) · 2.25 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
package main
import (
"gopkg.in/BlueDragonX/go-log.v1"
"gopkg.in/BlueDragonX/go-settings.v1"
"os"
"os/signal"
"syscall"
)
var logger *log.Logger = log.NewOrExit()
// Do basic configuration and return the config object.
func configure() *settings.Settings {
options := ParseOptionsOrExit(os.Args)
config := settings.LoadOrExit(options.Config)
// set config values from cli options
config.Set("exec", options.Exec)
if len(options.Etcd) > 0 {
config.Set("etcd.uris", options.Etcd)
}
if options.LogTarget != "" {
config.Set("logging.target", options.LogTarget)
}
if options.LogLevel != "" {
config.Set("logging.level", options.LogLevel)
}
// configure the logger
if logTarget, err := config.String("logging.target"); err == nil {
if logTargetObj, err := log.NewTarget(logTarget); err == nil {
logger.SetTarget(logTargetObj)
} else {
Fatalf("%s\n", err)
}
}
if logLevel, err := config.String("logging.level"); err == nil {
logger.SetLevel(log.NewLevel(logLevel))
}
// normalize etcd configuration
etcdURI := config.StringDflt("etcd.uri", "")
etcdURIs := config.StringArrayDflt("etcd.uris", []string{})
if len(etcdURIs) == 0 && etcdURI != "" {
rawURIs := make([]interface{}, 1)
rawURIs[0] = etcdURI
config.Set("etcd.uris", rawURIs)
}
// propogate default prefix to watchers
if prefix := config.StringDflt("etcd.prefix", ""); prefix != "" {
for _, watcher := range config.ObjectMapDflt("watchers", map[string]*settings.Settings{}) {
if watcherPrefix, err := watcher.String("prefix"); err == nil {
watcher.Set("prefix", JoinPath(prefix, watcherPrefix))
} else {
watcher.Set("prefix", prefix)
}
}
}
return config
}
// Run the app.
func main() {
config := configure()
logger.Info("starting sentinel")
sentinel := ConfigSentinel(config)
stop := make(chan bool)
defer close(stop)
go func() {
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
sig := <-signals
stop <- true
logger.Infof("got signal %s, stopping", sig)
}()
if sentinel.Client.Wait(stop) {
exec := config.StringArrayDflt("exec", []string{})
if !sentinel.Execute(exec) {
os.Exit(1)
}
if len(exec) > 0 {
logger.Info("execution complete")
} else {
sentinel.Run(stop)
}
}
}