-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
198 lines (179 loc) · 5.55 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
package main
import (
"os"
"github.com/michaelgaida/consul-mirror/configuration"
"github.com/michaelgaida/consul-mirror/consul"
"github.com/michaelgaida/consul-mirror/storage"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
// Create a new instance of the logger. You can have any number of instances.
// var log = logrus.New()
func main() {
log.SetFormatter(&log.TextFormatter{FullTimestamp: true})
log.SetOutput(os.Stdout)
log.SetLevel(log.InfoLevel)
app := cli.NewApp()
app.Name = "consul-mirror"
app.Author = "Michael Gaida"
app.Copyright = "Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/"
app.Email = "michael.gaida@protonmail.com"
app.Description = "Mirror your consul cluster for fallback in case outages or to copy it into another environment"
app.Version = "0.1.0"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "loglevel",
Usage: "Sets the log level [debug, info, warning, error, fatal, panic]",
},
cli.StringFlag{
Name: "token",
Usage: "ACL Token to be used to interact with consul",
},
cli.StringFlag{
Name: "dbpassword",
Usage: "Database password to be used to connect",
},
}
app.Commands = []cli.Command{
{
Name: "validate",
Usage: "Performs a basic sanity test on consul-mirror configuration files",
UsageText: `consul-mirror validate [options] FILE
Performs a basic sanity test on consul-mirror configuration files.
The validate command will attempt to parse the contents just as the
"consul-mirror" command would, and catch any errors. This is useful
to do a test of the configuration only, without actually starting
consul-mirror.
Returns 0 if the configuration is valid, or 1 if there are problems.`,
Action: func(c *cli.Context) {
if c.Args().Present() {
if err := commandValidate(c.Args().First()); err != nil {
log.Error("Validation not successful: ", err.Error())
os.Exit(1)
}
log.Info("Validation successful, you can go ahead and use your config")
os.Exit(0)
}
cli.ShowCommandHelp(c, "validate")
},
},
{
Name: "import",
Usage: "import from consul",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "ignoredc",
Usage: "ignore the dc information in DB",
},
cli.BoolFlag{
Name: "dcprefix",
Usage: "prepend dc to key, if given with setprefix this one will be prepend first -> [setprefix + dcprefix + key]",
},
cli.StringFlag{
Name: "prefix",
Usage: "only import keys with given prefix",
},
cli.StringFlag{
Name: "setprefix",
Usage: "prepend given prefix to key, if given with dcprefix this one will be prepend last -> [setprefix + dcprefix + key]",
},
},
Action: func(c *cli.Context) {
storage, consul, err := initConsul(c)
defer storage.Close()
if err != nil {
log.Error("Failed importing data: ", err.Error())
}
commandImport(storage, consul, c.BoolT("ignoredc"), c.BoolT("dcprefix"), c.String("prefix"), c.String("setprefix"))
},
},
{
Name: "export",
Usage: "export from consul",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "ignoredc",
Usage: "ignore the original dc",
},
cli.StringFlag{
Name: "dc",
Usage: "only export from given dc",
},
cli.BoolFlag{
Name: "incremental",
Usage: "Only create a new entry when the value (for the DC) changed",
},
cli.StringFlag{
Name: "prefix",
Usage: "key prefix for keys to be exported",
},
},
Action: func(c *cli.Context) {
storage, consul, err := initConsul(c)
defer storage.Close()
if err != nil {
log.Error("Failed exporting data: ", err.Error())
}
commandExport(storage, consul, c.BoolT("ignoredc"), c.BoolT("incremental"), c.String("dc"), c.String("prefix"))
},
},
}
app.Run(os.Args)
}
func initConsul(cli *cli.Context) (*storage.Mssql, *consul.Consul, error) {
config := configuration.GetConfig("config.json")
config.OverwriteConfig(cli)
logLevel, err := log.ParseLevel(config.LogLevel)
if err != nil {
log.Panic("Cant parse log level: ", err.Error())
return nil, nil, err
}
log.SetLevel(logLevel)
log.Debug(config.PrintDebug())
// s := storage.Mssql{}
conn, err := storage.OpenConnection(config)
if err != nil {
log.Error("Error initializing storage")
return nil, nil, err
}
consul := consul.GetConsul(config)
return conn, consul, nil
}
func commandExport(storage *storage.Mssql, consul *consul.Consul, ignoreDC, incremental bool, dc, prefix string) {
dcs, err := consul.GetDCs(dc)
if err != nil {
log.Error("commandExport: Error getting the DCs from Consul: ", err.Error())
os.Exit(1)
}
kvs, err := consul.GetKVs(prefix, dcs)
if err != nil {
log.Error("commandExport: Error getting the KVs from Consul: ", err.Error())
os.Exit(1)
}
err = storage.WriteKVs(kvs, ignoreDC, incremental)
if err != nil {
log.Error("commandExport: Error writing the KVs to storage: ", err.Error())
os.Exit(1)
}
}
func commandImport(storage *storage.Mssql, consul *consul.Consul, ignoreDC, dcprefix bool, prefix, setprefix string) {
// log.Fatal("IMPORT!!!!!!!")
kvs, err := storage.GetKVs(prefix)
if err != nil {
log.Error("commandImport: Error getting data from the storage: ", err.Error())
os.Exit(1)
}
err = consul.WriteKVs(kvs, ignoreDC, dcprefix, setprefix)
if err != nil {
log.Error("commandImport: Error writing KVs to consul: ", err.Error())
os.Exit(1)
}
}
func commandValidate(file string) error {
testConfiguration := configuration.GetConfig(file)
err := testConfiguration.ValidateConfiguration()
if err != nil {
return err
}
return nil
}