-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
291 lines (250 loc) · 5.3 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
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
package main
import "fmt"
import "log"
import "time"
import "os"
import "os/signal"
import "syscall"
import "database/sql"
import "flag"
import _ "github.com/go-sql-driver/mysql"
import "gopkg.in/ini.v1"
var timeout = 3600
var quiet = false
var verbose = false
var canExit = false
var cnf_files []string
var log_e = log.New(os.Stderr, "ERROR ", 1)
var qLock = [...]string{"FLUSH LOGS;", "FLUSH TABLES WITH READ LOCK;", "SET GLOBAL read_only = ON;"}
var qUnlock = [...]string{"SET GLOBAL read_only = OFF;", "UNLOCK TABLES;"}
// https://stackoverflow.com/a/18411978
func VersionOrdinal(version string) string {
// ISO/IEC 14651:2011
const maxByte = 1<<8 - 1
vo := make([]byte, 0, len(version)+8)
j := -1
for i := 0; i < len(version); i++ {
b := version[i]
if '0' > b || b > '9' {
vo = append(vo, b)
j = -1
continue
}
if j == -1 {
vo = append(vo, 0x00)
j = len(vo) - 1
}
if vo[j] == 1 && vo[j+1] == '0' {
vo[j+1] = b
continue
}
if vo[j]+1 > maxByte {
panic("VersionOrdinal: invalid version")
}
vo = append(vo, b)
vo[j]++
}
return string(vo)
}
func read_cnf(cnf string) (string, string, string, string) {
if verbose {
fmt.Println("Read cnf-file: "+cnf)
}
username := ""
password := ""
hostname := ""
socket := ""
// No cnf? Return defaults
if _, err := os.Stat(cnf); os.IsNotExist(err) {
if !quiet {
log_e.Println(err.Error())
}
return username, password, hostname, socket
}
cfg, err := ini.Load(cnf)
if err != nil {
if !quiet {
log_e.Println(err.Error())
}
return username, password, hostname, socket
}
if cfg.Section("client").HasKey("host") {
hostname = cfg.Section("client").Key("host").String()
}
if cfg.Section("client").HasKey("user") {
username = cfg.Section("client").Key("user").String()
}
if cfg.Section("client").HasKey("password") {
password = cfg.Section("client").Key("password").String()
}
if cfg.Section("client").HasKey("socket") {
socket = cfg.Section("client").Key("socket").String()
}
return username, password, hostname, socket
}
func dbGetVersion(db *sql.DB) string {
var v string
rows, err := db.Query("SELECT VERSION() as v;")
if err != nil {
if !quiet {
log_e.Println(err.Error())
}
return v
}
defer rows.Close()
for rows.Next() {
err := rows.Scan(&v)
if err != nil {
if !quiet {
log_e.Fatal(err)
}
}
}
err = rows.Err()
if err != nil {
if !quiet {
log_e.Fatal(err)
}
}
return v
}
func dbLock(db *sql.DB) {
v := dbGetVersion(db)
my, need := VersionOrdinal(v), VersionOrdinal("5.5.0")
if my > need {
_, err := db.Query("FLUSH ENGINE LOGS;")
if err != nil {
if !quiet {
log_e.Println(err.Error())
}
}
}
for _,q := range qLock {
// Execute the query
if verbose {
fmt.Println("Queue: "+q)
}
_, err := db.Query(q)
if err != nil {
if !quiet {
log_e.Println(err.Error())
}
}
}
}
func dbUnlock(db *sql.DB) {
for _,q := range qUnlock {
// Execute the query
if verbose {
fmt.Println("Queue: "+q)
}
_, err := db.Query(q)
if err != nil {
if !quiet {
log_e.Println(err.Error())
}
}
}
}
func main() {
numbPtr := flag.Int("timeout", timeout, "wait timeout to unlock MySQL databases")
boolPtrV := flag.Bool("verbose", false, "be verbose")
boolPtrQ := flag.Bool("quiet", false, "be quiet")
flag.Parse()
timeout = *numbPtr
verbose = *boolPtrV
quiet = *boolPtrQ
cnf_files = append(cnf_files, "~/.my.cnf", "/etc/mysql/root.cnf", "/etc/mysql/debian.cnf")
if len(flag.Args()) > 0 {
for _,cnf := range flag.Args() {
cnf_files = append(cnf_files, cnf)
}
}
if verbose {
fmt.Printf("cnf-files: %v\n", cnf_files)
}
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
// goroutine for signals
go func() {
<-sigs
canExit = true
}()
if verbose {
fmt.Printf("awaiting signal or timeout in %d seconds\n", timeout)
}
dsn := ""
var db *sql.DB
var err error
for _,cnf := range cnf_files {
username, password, hostname, socket := read_cnf(cnf)
dsn = ""
if socket != "" {
if _, err := os.Stat(socket); !os.IsNotExist(err) {
dsn = username+":"+password+"@unix("+socket+")/"
}
}
if dsn == "" {
if hostname != "" {
dsn = username+":"+password+"@tcp("+hostname+")/"
}
}
if dsn == "" {
if !quiet {
log_e.Printf("cnf-file %s not parsed or don't have needed fields 'hostname'/'socket'!", cnf)
}
continue
}
if username == "" || password == "" {
if !quiet {
log_e.Printf("cnf-file %s not parsed or don't have needed fields 'username', 'password'!", cnf)
}
continue
}
db, err = sql.Open("mysql", dsn)
if err != nil {
if !quiet {
log_e.Println(err.Error())
}
dsn = ""
continue
}
defer db.Close()
// Open doesn't open a connection. Validate DSN data:
err = db.Ping()
if err != nil {
if !quiet {
log_e.Println(err.Error())
}
dsn = ""
continue
}
// We found working credentials
break
}
if dsn == "" {
if !quiet {
log_e.Println("No active auth credentials available!")
}
os.Exit(1)
}
dbLock(db)
// Dumb check for SIGNAL and exit
for i:= 0; i<timeout; i++ {
time.Sleep(time.Second)
err = db.Ping()
if err != nil {
if !quiet {
log_e.Println(err.Error())
}
canExit = true
}
if canExit {
break
}
}
dbUnlock(db)
if verbose {
fmt.Println("exiting")
}
}