/
main.go
255 lines (240 loc) · 6.2 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"text/template"
"github.com/codegangsta/cli"
"github.com/kelseyhightower/envconfig"
"github.com/sentinel-tools/sconf-parser"
"github.com/therealbill/libredis/client"
"github.com/therealbill/libredis/structures"
)
type LaunchConfig struct {
SentinelConfigFile string
}
var (
config LaunchConfig
enc *json.Encoder
pod *parser.PodConfig
app *cli.App
errlog *log.Logger
)
func init() {
// set up error logger
errlog = log.New(os.Stderr, "[ERROR]", log.Lshortfile|log.LstdFlags)
// set standard logger to stdout
log.SetOutput(os.Stdout)
err := envconfig.Process("sentinel-manager", &config)
if err != nil {
log.Fatal(err)
}
}
func main() {
app = cli.NewApp()
app.Name = "sentinel-manager"
app.Usage = "Interact with a Sentinel using configuration data"
app.Version = "0.1"
app.EnableBashCompletion = true
author := cli.Author{Name: "Bill Anderson", Email: "therealbill@me.com"}
app.Authors = append(app.Authors, author)
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "address, a",
Value: "localhost",
Usage: "Address of the sentinel to talk to",
},
cli.IntFlag{
Name: "port, p",
Value: 26379,
Usage: "Port of the sentinel to talk to",
},
cli.BoolFlag{
Name: "walkpod,w",
Usage: "Walk each pod for additional sentinels to operate on.",
},
}
app.Commands = []cli.Command{
{
Name: "addpod",
Usage: "Add a pod to the specified sentinel",
Action: AddPod,
Flags: []cli.Flag{
cli.StringFlag{
Name: "name, n",
Usage: "Name of the pod",
},
cli.StringFlag{
Name: "address, a",
Usage: "hostname/IP of master",
},
cli.StringFlag{
Name: "password, t",
Usage: "The password to use (will be used to set amster-auth as well)",
Value: "USE-A-REAL-PASSWORD",
},
cli.StringFlag{
Name: "reconfigure-script,r",
Usage: "Script to use on failover event",
},
cli.StringFlag{
Name: "notification-script,o",
Usage: "Script to use on failover event",
},
cli.IntFlag{
Name: "port, p",
Usage: "Port the master is running on",
Value: 6379,
},
cli.IntFlag{
Name: "quorum, q",
Usage: "Quorum for event triggers",
Value: 2,
},
},
},
{
Name: "set",
Usage: "Set a directive's value on *every* pod in Sentinel",
Action: SetOnAllPods,
Flags: []cli.Flag{
cli.StringFlag{
Name: "directive, d",
Usage: "Directive to set",
},
cli.StringFlag{
Name: "value, v",
Usage: "Value to set",
},
},
},
{
Name: "reset",
Usage: "Reset every pod in Sentinel",
Action: resetAllPods,
},
{
Name: "removepod",
Usage: "Remove pod from the specified sentinel",
Action: RemovePod,
Flags: []cli.Flag{
cli.StringFlag{
Name: "name, n",
Usage: "Name of the pod",
},
cli.StringFlag{
Name: "archive, a",
Usage: "Archive configuration locally before deleting",
},
},
},
}
app.Run(os.Args)
}
func getSentinel(c *cli.Context) (sentinel *client.Redis) {
saddr := fmt.Sprintf("%s:%d", c.GlobalString("address"), c.GlobalInt("port"))
sentinel, err := client.DialAddress(saddr)
bailOnError(err)
return
}
func AddPod(c *cli.Context) {
name := c.String("name")
password := c.String("password")
onreconfig := c.String("reconfigure-script")
notification := c.String("notification-script")
host := c.String("address")
quorum := c.Int("quorum")
port := c.Int("port")
sentinel := getSentinel(c)
ok, err := sentinel.SentinelMonitor(name, host, port, quorum)
if !ok {
errlog.Fatalf("on AddPod: err='%v'", err)
}
err = sentinel.SentinelSetString(name, "auth-pass", password)
if err != nil {
errlog.Fatalf("Error on setting auth-pass: '%v'", err)
}
if onreconfig > "" {
err = sentinel.SentinelSetString(name, "client-reconfig-script", onreconfig)
if err != nil {
errlog.Fatalf("Error on setting client-reconfig-script '%v'", err)
}
}
if notification > "" {
err = sentinel.SentinelSetString(name, "notification-script", notification)
if err != nil {
errlog.Fatalf("Error on setting notification-script '%v'", err)
}
}
}
func resetAllPods(c *cli.Context) {
log.Printf("TBI")
}
func SetOnAllPods(c *cli.Context) {
pods, err := getPodList(c)
bailOnError(err)
directive := c.Args()[0]
value := c.Args()[1]
sentinel := getSentinel(c)
log.Printf("Setting %s=%s on all pods...", directive, value)
for _, pod := range pods {
log.Printf("Operating on %s", pod.Name)
err := sentinel.SentinelSetString(pod.Name, directive, value)
if err != nil {
errlog.Printf("Unable to set %s to %s on pod '%s'. Error: '%v", directive, value, pod.Name, err)
}
}
}
func SetSentinelPod(c *cli.Context) {
sentinel_directive := c.String("directive")
value := c.String("value")
sentinels, err := pod.GetSentinels()
bailOnError(err)
for _, s := range sentinels {
//log.Printf("Updating Sentinel %s", s)
sentinel, err := client.DialAddress(s)
if err != nil {
log.Printf("Unable to connect to %s! You will need to manually adjust the directive's value for this sentinel.", s)
continue
}
sentinel.SentinelSetString(pod.Name, sentinel_directive, value)
}
}
func RemovePod(c *cli.Context) {
sentinel := getSentinel(c)
pod, err := sentinel.SentinelMaster(c.String("name"))
if c.Bool("archive") {
podname := c.String("name")
t := template.Must(template.New("podinfo").Parse(PodInfoTemplate))
filename := fmt.Sprintf("archive-%s.txt", podname)
arcfile, err := os.Create(filename)
if err != nil {
log.Fatalf("Unable to open %s for writing archive, bailing", filename)
}
err = t.Execute(arcfile, pod)
if err != nil {
log.Fatal("executing template:", err)
}
}
ok, err := sentinel.SentinelRemove(c.String("name"))
bailOnError(err)
if ok {
log.Printf("Pod %s was removed from sentinel", pod.Name)
} else {
log.Printf("Pod not on sentinel")
}
}
func bailOnError(err error) {
if err != nil {
errlog.SetFlags(log.Lshortfile | log.LstdFlags)
errlog.Output(2, err.Error())
errlog.SetFlags(log.LstdFlags)
log.Fatalf("aborting due to error")
}
}
func getPodList(c *cli.Context) (pods []structures.MasterInfo, err error) {
sentinel := getSentinel(c)
pods, err = sentinel.SentinelMasters()
return
}