forked from Percona-Lab/pt-mysql-config-diff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
370 lines (308 loc) · 7.73 KB
/
main.go
File metadata and controls
370 lines (308 loc) · 7.73 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package main
import (
"bytes"
"database/sql"
"encoding/json"
"errors"
"fmt"
"log"
"os"
"strconv"
"strings"
_ "github.com/go-sql-driver/mysql"
flag "github.com/spf13/pflag"
ini "gopkg.in/ini.v1"
)
type options struct {
CNFs []string
DSNs dsnFlags
OutputFmt string
Help bool
compareBase string // First CNF or first MySQL used as comparisson base
}
type dsnFlag struct {
Host string
Port int
User string
Password string
Database string
Table string
protocol string
}
type dsnFlags []dsnFlag
func (d dsnFlags) String() string {
parts := []string{}
//if d.Host != "" {
// parts = append(parts, "h="+d.Host)
//}
//if d.Port != 0 {
// parts = append(parts, fmt.Sprintf("P=%d", d.Port))
//}
//if d.User != "" {
// parts = append(parts, "u="+d.User)
//}
//if d.Password != "" {
// parts = append(parts, "p="+d.Password)
//}
//if d.Database != "" {
// parts = append(parts, "D="+d.Database)
//}
//if d.Table != "" {
// parts = append(parts, "t="+d.Table)
//}
return strings.Join(parts, ",")
}
func (d dsnFlags) Set(value string) error {
parts := strings.Split(value, ",")
var dsn dsnFlag
for _, part := range parts {
if len(part) < 3 {
continue
}
key := string(part[0])
value := string(part[2:])
switch key {
case "D":
dsn.Database = value
case "h":
dsn.Host = value
case "p":
dsn.Password = value
case "P":
port, err := strconv.ParseInt(value, 10, 64)
if err != nil {
dsn.Port = int(port)
}
case "t":
dsn.Table = value
case "u":
dsn.User = value
}
}
if dsn.Host == "localhost" {
dsn.protocol = "unix"
} else {
dsn.protocol = "tcp"
}
d = append(d, dsn)
return nil
}
func (d dsnFlags) Type() string {
return "dsn"
}
func main() {
opts, err := processParams(os.Args[1:])
if err != nil {
os.Exit(1)
}
// Make a func to connect to the db, so it can be mocked on tests
dbConnector := func(dsn string) (*sql.DB, error) {
db, err := sql.Open("mysql", dsn)
if err != nil {
return nil, err
}
return db, nil
}
configs, err := getConfigs(opts, dbConnector)
if err != nil {
log.Printf("Cannot get configs: %s", err.Error())
os.Exit(1)
}
diffs := compare(configs)
formattedOutput, err := getFormattedOutput(opts.OutputFmt, diffs)
if err != nil {
log.Printf("Cannot get output formatter: %s", err.Error())
os.Exit(1)
}
fmt.Print(formattedOutput)
}
func getFormattedOutput(format string, diff map[string][]interface{}) (string, error) {
prettyStyle := false
switch format {
case "prettyJson":
prettyStyle = true
fallthrough
case "json":
output, err := json.Marshal(diff)
if prettyStyle {
output, err = json.MarshalIndent(diff, "", "\t")
}
if err != nil {
return "", err
}
return string(output), nil
case "plain":
var buffer bytes.Buffer
for key, val := range diff {
buffer.WriteString(fmt.Sprintf("%35s: %40s : %40s\n", key, val[0], val[1]))
}
return buffer.String(), nil
default:
return "", errors.New("The specified output format doesn't exist")
}
}
func newCNFReader(filename string) (configReader, error) {
cfg, err := ini.LoadSources(ini.LoadOptions{AllowBooleanKeys: true}, filename)
if err != nil {
return nil, err
}
if cfg == nil {
return nil, fmt.Errorf("Invalid file: %s", filename)
}
cnf := &config{configType: "cnf", entries: make(map[string]interface{})}
for _, key := range cfg.Section("mysqld").Keys() {
cnf.entries[key.Name()] = key.Value()
}
return cnf, nil
}
func newMySQLReader(db *sql.DB) (configReader, error) {
// Since the MySQL driver uses a lazy connection, check if we really can
// connect to the db
if err := db.Ping(); err != nil {
return nil, err
}
rows, err := db.Query("SHOW VARIABLES")
if err != nil {
return nil, err
}
ini := &config{configType: "mysql", entries: make(map[string]interface{})}
for rows.Next() {
var key string
var val interface{}
err := rows.Scan(&key, &val)
if err != nil {
continue
}
ini.entries[key] = val
}
return ini, nil
}
/*
We need to compare cfg1 vs cfg2 and cfg2 vs cfg1.
Configs can be:
cfg1 | cfg2
-----------+----------
key1 = A | key1 = A
key2 = B | key2 = C
key3 = D |
| key4 = E
So we need 2 inner loops: first through cfg1 keys and then through
cfg2 keys to be able to compare the keys that exist in cfg2 but are
missing in cfg1.
MySQL SHOW VARIABLES will return ALL variables but we must skip variables
in MySQL config that are missing in the cnf.
In the example above, if cfg2 is "cnf" type, key4 must be included in
the diff but, if cfg2 type is "mysql", it must be excluded from the diff.
*/
func compare(configs []configReader) map[string][]interface{} {
diffs := make(map[string][]interface{})
if len(configs) < 2 {
return nil
}
for i := 1; i < len(configs); i++ {
for key, value1 := range configs[0].Entries() {
value2, ok := configs[i].Get(key)
if !ok && (configs[0].Type() != "mysql" || configs[0].Type() == configs[1].Type()) {
addDiff(diffs, key, value1, "<Missing>")
continue
}
value1 = Normalize(value1)
value2 = Normalize(value2)
if fmt.Sprintf("%s", value1) != fmt.Sprintf("%s", value2) {
addDiff(diffs, key, value1, value2)
continue
}
}
for key, value1 := range configs[i].Entries() {
_, ok := configs[0].Get(key)
if !ok && (configs[i].Type() != "mysql" || configs[0].Type() == configs[i].Type()) {
addDiff(diffs, key, "<Missing>", value1)
}
}
}
return diffs
}
func normalizeValue(str interface{}) interface{} {
normalizers := normalizers{
&sizesNormalizer{},
&numbersNormalizer{},
&setsNormalizer{},
}
for _, n := range normalizers {
str = n.Normalize(str)
}
return str
}
func addDiff(diffs map[string][]interface{}, key string, value1, value2 interface{}) {
if _, ok := diffs[key]; !ok {
diffs[key] = append(diffs[key], value1)
}
diffs[key] = append(diffs[key], value2)
}
func processParams(arguments []string) (*options, error) {
opts := &options{}
fs := flag.NewFlagSet("default", flag.ContinueOnError)
fs.StringArrayVarP(&opts.CNFs, "cnf", "c", nil, "cnf file name")
fs.VarP(opts.DSNs, "dsn", "d", "full db dsn. Example: user:pass@tcp(127.1:3306)")
fs.StringVarP(&opts.OutputFmt, "output", "o", "plain", "Output formatting. Could be json, prettyJson or plain.")
err := fs.Parse(arguments)
if err != nil {
return nil, err
}
fs.SortFlags = false
fs.Visit(func(f *flag.Flag) {
if opts.compareBase != "" {
return
}
switch f.Name {
case "cnf":
opts.compareBase = "cnf"
case "dsn":
opts.compareBase = "dsn"
}
})
return opts, nil
}
func getConfigs(opts *options, dbConnector func(string) (*sql.DB, error)) ([]configReader, error) {
var configs []configReader
cnfs, err := getCNFs(opts.CNFs)
if err != nil {
return nil, err
}
mysqls, err := getMySQLs(opts.DSNs, dbConnector)
if err != nil {
return nil, err
}
if opts.compareBase == "mysql" {
configs = append(mysqls, cnfs...)
} else {
configs = append(cnfs, mysqls...)
}
return configs, nil
}
func getCNFs(filenames []string) ([]configReader, error) {
var configs []configReader
for _, filename := range filenames {
cfg, err := newCNFReader(filename)
if err != nil {
return nil, fmt.Errorf("Cannot read %s: %s", filename, err.Error())
}
configs = append(configs, cfg)
}
return configs, nil
}
func getMySQLs(dsns dsnFlags, dbConnector func(string) (*sql.DB, error)) ([]configReader, error) {
var configs []configReader
for _, dsn := range dsns {
db, err := dbConnector(dsn))
if err != nil {
return nil, fmt.Errorf("Cannot connect to the db %s", err.Error())
}
cfg, err := newMySQLReader(db)
if err != nil {
return nil, fmt.Errorf("Cannot read the config variables: %s", err.Error())
}
configs = append(configs, cfg)
}
return configs, nil
}