forked from seatgeek/resec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
195 lines (184 loc) · 5.8 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
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
package main
import (
"os"
"sort"
"strings"
"time"
gelf "github.com/seatgeek/logrus-gelf-formatter"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"github.com/nirahapp/resec/resec/reconciler"
)
var Version = "local-dev"
func main() {
app := cli.NewApp()
app.Name = "resec"
app.Usage = "Redis cluster manager"
app.Version = Version
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "announce-addr",
Usage: "IP:Port of Redis to be announced to Consul",
EnvVars: []string{"ANNOUNCE_ADDR"},
},
&cli.DurationFlag{
Name: "consul-deregister-service-after",
Usage: "Specifies that checks associated with a service should deregister after this time. If a check is in the critical state for more than this configured value, then its associated service (and all of its associated checks) will automatically be deregistered",
Value: 72 * time.Hour,
EnvVars: []string{"CONSUL_DEREGISTER_SERVICE_AFTER"},
},
&cli.StringFlag{
Name: "consul-lock-key",
Usage: "KV lock location, should be overridden if multiple instances running in the same consul DC",
Value: "resec/.lock",
EnvVars: []string{"CONSUL_LOCK_KEY"},
},
&cli.IntFlag{
Name: "consul-lock-monitor-retries",
Usage: "Number of retries of lock receives 500 Error from Consul",
Value: 3,
EnvVars: []string{"CONSUL_LOCK_MONITOR_RETRIES"},
},
&cli.DurationFlag{
Name: "consul-lock-monitor-retry-interval",
Usage: "Retry interval if lock receives 500 Error from Consul",
Value: time.Second,
EnvVars: []string{"CONSUL_LOCK_MONITOR_RETRY_INTERVAL"},
},
&cli.StringFlag{
Name: "consul-lock-session-name",
Usage: "Lock session Name to distinguish multiple resec masters on one host",
Value: "resec",
EnvVars: []string{"CONSUL_LOCK_SESSION_NAME"},
},
&cli.DurationFlag{
Name: "consul-lock-ttl",
Value: 15 * time.Second,
EnvVars: []string{"CONSUL_LOCK_TTL"},
},
&cli.StringFlag{
Name: "consul-service-name",
Usage: "Consul service name for tag based service discovery",
EnvVars: []string{"CONSUL_SERVICE_NAME"},
},
&cli.StringFlag{
Name: "consul-service-prefix",
Usage: "Name Prefix, will be followed by '-[master|slave]', ignored if CONSUL_SERVICE_NAME is used",
Value: "redis",
EnvVars: []string{"CONSUL_SERVICE_PREFIX"},
},
&cli.StringFlag{
Name: "consul-master-tags",
Usage: "Comma separated list of tags to be added to master instance. The first tag (index 0) is used to configure the role of the Redis/resec task, and must be different from index 0 in SLAVE_TAGS",
EnvVars: []string{"MASTER_TAGS"},
},
&cli.StringFlag{
Name: "consul-slave-tags",
Usage: "Comma separated list of tags to be added to slave instance. The first tag (index 0) is used to configure the role of the Redis/resec task, and must be different from index 0 in MASTER_TAGS",
EnvVars: []string{"SLAVE_TAGS"},
},
&cli.DurationFlag{
Name: "healthcheck-interval",
Value: 5 * time.Second,
EnvVars: []string{"HEALTHCHECK_INTERVAL"},
},
&cli.DurationFlag{
Name: "healthcheck-timeout",
Value: 2 * time.Second,
EnvVars: []string{"HEALTHCHECK_TIMEOUT"},
},
&cli.StringFlag{
Name: "log-level",
Value: "info",
Usage: "Log level (debug, info, warn/warning, error, fatal, panic)",
EnvVars: []string{"LOG_LEVEL"},
},
&cli.StringFlag{
Name: "log-format",
Value: "text",
Usage: "Log format (text, gelf, json)",
EnvVars: []string{"LOG_FORMAT"},
},
&cli.StringFlag{
Name: "redis-addr",
Value: "127.0.0.1:6379",
Usage: "IP + Port for the Redis server",
EnvVars: []string{"REDIS_ADDR"},
},
&cli.StringFlag{
Name: "redis-password",
Usage: "Password for the Redis server",
EnvVars: []string{"REDIS_PASSWORD"},
},
&cli.BoolFlag{
Name: "tls",
Usage: "Enable TLS with redis (needs a custom redis build with BUILD_TLS=yes flag)",
EnvVars: []string{"REDIS_TLS"},
},
&cli.PathFlag{
Name: "tls-cert-file",
Usage: "Specify the certificate for TLS/SSL",
EnvVars: []string{"REDIS_TLS_CERT_FILE"},
},
&cli.PathFlag{
Name: "tls-key-file",
Usage: "Specify the key for TLS/SSL",
EnvVars: []string{"REDIS_TLS_KEY_FILE"},
},
&cli.PathFlag{
Name: "tls-ca-cert-file",
Usage: "Specify the CA certificate for TLS/SSL",
EnvVars: []string{"REDIS_TLS_CA_CERT_FILE"},
},
&cli.BoolFlag{
Name: "tls-insecure-skip-verify",
Usage: "Is Isecure Skip Verify set as true?",
EnvVars: []string{"REDIS_INSECURE_SKIP_VERIFY"},
},
}
app.Before = func(c *cli.Context) error {
level, err := log.ParseLevel(c.String("log-level"))
if err != nil {
return err
}
log.SetLevel(level)
switch strings.ToLower(c.String("log-format")) {
case "text":
log.SetFormatter(&log.TextFormatter{FullTimestamp: true})
case "json":
log.SetFormatter(&log.JSONFormatter{
FieldMap: log.FieldMap{
log.FieldKeyTime: "@timestamp",
log.FieldKeyLevel: "@level",
log.FieldKeyMsg: "@message",
},
})
case "gelf":
log.SetFormatter(&gelf.GelfFormatter{})
default:
log.Fatalf("Invalid log format '%s', please use on of [text, json, gelf]", c.String("log-format"))
}
log.Infof("Starting ReSeC %s", Version)
if c.Bool("tls") {
if c.String("tls-cert-file") == "" ||
c.String("tls-key-file") == "" ||
c.String("tls-ca-cert-file") == "" {
log.Fatalf("all TLS related flags (tls-cert-file, tls-key-file, tls-ca-cert-file) must be provided when TLS is enabled")
}
}
return nil
}
app.Action = func(c *cli.Context) error {
r, err := reconciler.NewReconciler(c)
if err != nil {
return err
}
r.Run()
return nil
}
sort.Sort(cli.FlagsByName(app.Flags))
sort.Sort(cli.CommandsByName(app.Commands))
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}