-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathregistrator.go
144 lines (120 loc) · 3.69 KB
/
registrator.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
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
package main
import (
"errors"
"fmt"
"log/syslog"
"os"
"time"
"github.com/x-cray/marathon-registrator/bridge"
"github.com/x-cray/marathon-registrator/types"
log "github.com/Sirupsen/logrus"
logrusSyslog "github.com/Sirupsen/logrus/hooks/syslog"
prefixed "github.com/x-cray/logrus-prefixed-formatter"
"gopkg.in/alecthomas/kingpin.v2"
)
const (
reconnectInterval = 5 * time.Second
)
var (
version string
app = kingpin.New("registrator", "Automatically registers/deregisters Marathon tasks as services in Consul.")
consul = app.Flag("consul", "Address and port of Consul agent").Short('c').Default("http://127.0.0.1:8500").URL()
marathon = app.Flag("marathon", "URL of Marathon instance. Multiple instances may be specified in case of HA setup: http://addr1:8080,addr2:8080,addr3:8080").Short('m').Default("http://127.0.0.1:8080").String()
resyncInterval = app.Flag("resync-interval", "Time interval to resync Marathon services to determine dangling instances. Valid time units are \"ns\", \"us\" (or \"µs\"), \"ms\", \"s\", \"m\", \"h\"").Short('i').Default("5m").Duration()
enableDryRun = app.Flag("dry-run", "Do not perform actual service registration/deregistration. Just log intents").Short('d').Bool()
logLevel = app.Flag("log-level", "Set the logging level - valid values are \"debug\", \"info\", \"warn\", \"error\", and \"fatal\"").Short('l').Default("info").Enum("debug", "info", "warn", "error", "fatal")
enableSyslog = app.Flag("syslog", "Send the log output to syslog").Short('s').Bool()
forceColors = app.Flag("force-colors", "Force colored log output").Short('r').Bool()
)
func validateParams(app *kingpin.Application) error {
if *resyncInterval <= 0 {
return errors.New("--resync-interval must be greater than 0")
}
return nil
}
func printVersion(*kingpin.ParseContext) error {
fmt.Fprintln(os.Stderr, version)
os.Exit(0)
return nil
}
func assert(err error) {
if err != nil {
log.Fatal(err)
}
}
func trySync(b *bridge.Bridge) bool {
if err := b.Sync(); err != nil {
log.Errorf("Failed to sync services: %v", err)
return false
}
return true
}
func main() {
config, err := getConfig()
assert(err)
log.Infof("Starting Marathon service registrator v%s", version)
b, err := bridge.New(config)
assert(err)
log.Info("Performing initial sync")
for {
if trySync(b) {
break
}
log.Infof("Retrying initial sync in %v", reconnectInterval)
time.Sleep(reconnectInterval)
}
quit := make(chan bool)
// Start the resync timer.
ticker := time.NewTicker(config.ResyncInterval)
go func() {
for {
select {
case <-ticker.C:
trySync(b)
case <-quit:
ticker.Stop()
return
}
}
}()
// Run the main event application loop.
for {
err = b.ProcessSchedulerEvents()
if err == nil {
break
}
log.Info("Retrying event stream connection in %v", reconnectInterval)
time.Sleep(reconnectInterval)
}
log.Warn("Scheduler event loop closed")
close(quit)
}
func getConfig() (*types.Config, error) {
app.Validate(validateParams)
kingpin.HelpFlag.Short('h')
app.Flag("version", "Print application version and exit").PreAction(printVersion).Short('v').Bool()
kingpin.MustParse(app.Parse(os.Args[1:]))
c := &types.Config{
Consul: *consul,
Marathon: *marathon,
ResyncInterval: *resyncInterval,
DryRun: *enableDryRun,
}
// Setup the logging.
level, err := log.ParseLevel(*logLevel)
if err != nil {
return nil, err
}
log.SetFormatter(&prefixed.TextFormatter{
ForceColors: *forceColors,
})
log.SetLevel(level)
if *enableSyslog {
hook, err := logrusSyslog.NewSyslogHook("", "", syslog.LOG_DEBUG, app.Name)
if err != nil {
return nil, err
}
log.AddHook(hook)
}
return c, nil
}