forked from signal18/replication-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
190 lines (160 loc) · 5.78 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
// replication-manager - Replication Manager Monitoring and CLI for MariaDB and MySQL
// Authors: Guillaume Lefranc <[email protected]>
// Stephane Varoqui <[email protected]>
// This source code is licensed under the GNU General Public License, version 3.
// Redistribution/Reuse of this code is permitted under the GNU v3 license, as
// an additional term, ALL code must carry the original Author(s) credit in comment form.
// See LICENSE in this directory for the integral text.
package main
import (
"fmt"
"os"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tanji/replication-manager/config"
)
var (
cfgFile string
cfgGroup string
cfgGroupList []string
cfgGroupIndex int
conf config.Config
memprofile string
)
var confs = make(map[string]config.Config)
var (
// Version is the semantic version number, e.g. 1.0.1
Version string
// FullVersion is the semantic version number + git commit hash
FullVersion string
// Build is the build date of replication-manager
Build string
)
func init() {
log.SetFormatter(&log.TextFormatter{FullTimestamp: true})
cobra.OnInitialize(initConfig)
rootCmd.AddCommand(versionCmd)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "Configuration file (default is config.toml)")
rootCmd.PersistentFlags().StringVar(&cfgGroup, "config-group", "", "Configuration group (default is none)")
rootCmd.PersistentFlags().StringVar(&conf.User, "user", "", "User for MariaDB login, specified in the [user]:[password] format")
rootCmd.PersistentFlags().StringVar(&conf.Hosts, "hosts", "", "List of MariaDB hosts IP and port (optional), specified in the host:[port] format and separated by commas")
rootCmd.PersistentFlags().StringVar(&conf.RplUser, "rpluser", "", "Replication user in the [user]:[password] format")
rootCmd.Flags().StringVar(&conf.KeyPath, "keypath", "/etc/replication-manager/.replication-manager.key", "Encryption key file path")
rootCmd.PersistentFlags().BoolVar(&conf.Verbose, "verbose", false, "Print detailed execution info")
rootCmd.PersistentFlags().IntVar(&conf.LogLevel, "log-level", 0, "Log verbosity level")
rootCmd.PersistentFlags().StringVar(&memprofile, "memprofile", "/tmp/repmgr.mprof", "Write a memory profile to a file readable by pprof")
viper.BindPFlags(rootCmd.PersistentFlags())
if conf.Verbose == true && conf.LogLevel == 0 {
conf.LogLevel = 1
}
if conf.Verbose == false && conf.LogLevel > 0 {
conf.Verbose = true
}
}
func initConfig() {
// call after init if configuration file is provide
viper.SetConfigType("toml")
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
if _, err := os.Stat(cfgFile); os.IsNotExist(err) {
log.Fatal("No config file " + cfgFile)
}
} else {
viper.SetConfigName("config")
viper.AddConfigPath("/etc/replication-manager/")
viper.AddConfigPath(".")
if _, err := os.Stat("/etc/replication-manager/config.toml"); os.IsNotExist(err) {
log.Fatal("No config file etc/replication-manager/config.toml ")
}
}
viper.SetEnvPrefix("MRM")
err := viper.ReadInConfig()
if err == nil {
log.WithFields(log.Fields{
"file": viper.ConfigFileUsed(),
}).Debug("Using config file")
}
if _, ok := err.(viper.ConfigParseError); ok {
log.WithError(err).Fatal("Could not parse config file")
}
m := viper.AllKeys()
if cfgGroup == "" {
var clusterDicovery = map[string]string{}
var discoveries []string
for _, k := range m {
if strings.Contains(k, ".") {
mycluster := strings.Split(k, ".")[0]
if mycluster != "default" {
_, ok := clusterDicovery[mycluster]
if !ok {
clusterDicovery[mycluster] = mycluster
discoveries = append(discoveries, mycluster)
// log.Println(strings.Split(k, ".")[0])
}
}
}
}
cfgGroup = strings.Join(discoveries, ",")
log.WithField("clusters", cfgGroup).Debug("New clusters discovered")
}
cfgGroupIndex = 0
cf1 := viper.Sub("Default")
if cf1 == nil {
log.Fatal("config.toml has no [Default] configuration group and config group has not been specified")
}
cf1.Unmarshal(&conf)
if cfgGroup != "" {
cfgGroupList = strings.Split(cfgGroup, ",")
for _, gl := range cfgGroupList {
if gl != "" {
clusterconf := conf
cf2 := viper.Sub("Default")
cf2.Unmarshal(&clusterconf)
cfgGroup = gl
log.WithField("group", gl).Debug("Reading configuration group")
cf2 = viper.Sub(gl)
if cf2 == nil {
log.WithField("group", gl).Fatal("Could not parse configuration group")
}
cf2.Unmarshal(&clusterconf)
confs[cfgGroup] = clusterconf
cfgGroupIndex++
}
}
cfgGroupIndex--
log.WithField("cluster", cfgGroupList[cfgGroupIndex]).Debug("Default Cluster set")
cfgGroup = cfgGroupList[cfgGroupIndex]
} else {
cfgGroupList = append(cfgGroupList, "Default")
log.WithField("cluster", cfgGroupList[cfgGroupIndex]).Debug("Default Cluster set")
confs["Default"] = conf
cfgGroup = "Default"
}
}
func main() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(-1)
}
}
var rootCmd = &cobra.Command{
Use: "replication-manager",
Short: "Replication Manager tool for MariaDB and MySQL",
Long: `replication-manager allows users to monitor interactively MariaDB 10.x and MySQL GTID replication health
and trigger slave to master promotion (aka switchover), or elect a new master in case of failure (aka failover).`,
Run: func(cmd *cobra.Command, args []string) {
cmd.Usage()
},
}
var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the replication manager version number",
Long: `All software has versions. This is ours`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Replication Manager " + Version + " for MariaDB 10.x and MySQL 5.7 Series")
fmt.Println("Full Version: ", FullVersion)
fmt.Println("Build Time: ", Build)
},
}