/
instance_utils.go
77 lines (67 loc) · 2.35 KB
/
instance_utils.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
/*
Copyright 2015 Shlomi Noach, courtesy Booking.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package inst
import (
"regexp"
"strconv"
"strings"
)
var (
DowntimeLostInRecoveryMessage = "lost-in-recovery"
)
// MajorVersion returns a MySQL major version number (e.g. given "5.5.36" it returns "5.5")
func MajorVersion(version string) []string {
tokens := strings.Split(version, ".")
if len(tokens) < 2 {
return []string{"0", "0"}
}
return tokens[:2]
}
// IsSmallerMajorVersion tests two versions against another and returns true if
// the former is a smaller "major" varsion than the latter.
// e.g. 5.5.36 is NOT a smaller major version as comapred to 5.5.40, but IS as compared to 5.6.9
func IsSmallerMajorVersion(version string, otherVersion string) bool {
thisMajorVersion := MajorVersion(version)
otherMajorVersion := MajorVersion(otherVersion)
for i := 0; i < len(thisMajorVersion); i++ {
thisToken, _ := strconv.Atoi(thisMajorVersion[i])
otherToken, _ := strconv.Atoi(otherMajorVersion[i])
if thisToken < otherToken {
return true
}
if thisToken > otherToken {
return false
}
}
return false
}
// IsSmallerBinlogFormat tests two binlog formats and sees if one is "smaller" than the other.
// "smaller" binlog format means you can replicate from the smaller to the larger.
func IsSmallerBinlogFormat(binlogFormat string, otherBinlogFormat string) bool {
if binlogFormat == "STATEMENT" {
return (otherBinlogFormat == "ROW" || otherBinlogFormat == "MIXED")
}
if binlogFormat == "MIXED" {
return otherBinlogFormat == "ROW"
}
return false
}
// RegexpMatchPatterns returns true if s matches any of the provided regexpPatterns
func RegexpMatchPatterns(s string, regexpPatterns []string) bool {
for _, filter := range regexpPatterns {
if matched, err := regexp.MatchString(filter, s); err == nil && matched {
return true
}
}
return false
}