-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
303 lines (272 loc) · 6.6 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
292
293
294
295
296
297
298
299
300
301
302
303
package main
import (
"context"
"fmt"
"io/ioutil"
"os"
"strconv"
"time"
"github.com/rancher/kine/pkg/client"
kineep "github.com/rancher/kine/pkg/endpoint"
"go.etcd.io/etcd/clientv3"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
var (
mode string
endpoint string
etcd_direct string
dqlite_direct string
db string
)
func main() {
app := cli.NewApp()
app.Name = "migrator"
app.Description = "Tool to migrate etcd to dqlite"
app.UsageText = "Copy etcd data to kine\n" +
"migrator --mode [backup-etcd|restore-etcd|backup-dqlite|restore-dqlite] --endpoint [etcd or kine endpoint] --db-dir [dir to store entries]\n" +
"OR\n" +
"migrator --mode direct --etcd-direct [etcd endpoint] --dqlite-direct [kine endpoint]"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "endpoint",
// Value: "http://127.0.0.1:12379",
Value: "unix:///var/snap/microk8s/current/var/kubernetes/backend/kine.sock",
Destination: &endpoint,
},
cli.StringFlag{
Name: "etcd-direct",
Value: "http://127.0.0.1:12379",
Destination: &etcd_direct,
},
cli.StringFlag{
Name: "dqlite-direct",
Value: "unix:///var/snap/microk8s/current/var/kubernetes/backend/kine.sock",
Destination: &dqlite_direct,
},
cli.StringFlag{
Name: "mode",
Value: "backup",
//Value: "restore",
//Value: "direct",
Destination: &mode,
},
cli.StringFlag{
Name: "db-dir",
Value: "db",
Destination: &db,
},
cli.BoolFlag{Name: "debug"},
}
app.Action = run
if err := app.Run(os.Args); err != nil {
logrus.Fatal(err)
}
}
func check(e error) {
if e != nil {
panic(e)
}
}
func backup_etcd(ep string, dir string) error {
ctx := context.Background()
cli, err := clientv3.New(clientv3.Config{
Endpoints: []string{ep},
})
check(err)
defer cli.Close()
resp, err := cli.Get(ctx, "", clientv3.WithPrefix(), clientv3.WithSort(clientv3.SortByKey, clientv3.SortDescend))
check(err)
err = os.Mkdir(db, 0700)
check(err)
for i, ev := range resp.Kvs {
logrus.Debugf("%d) %s\n", i, ev.Key)
var keyfile = db + "/" + strconv.Itoa(i) + ".key"
var datafile = db + "/" + strconv.Itoa(i) + ".data"
keyf, err := os.Create(keyfile)
check(err)
defer keyf.Close()
dataf, err := os.Create(datafile)
check(err)
defer dataf.Close()
keyf.Write(ev.Key)
dataf.Write(ev.Value)
dataf.Close()
keyf.Close()
}
return nil
}
func put_key(c client.Client, ctx context.Context, key string, databytes []byte) {
err := c.Create(ctx, key, databytes)
if err != nil {
if err.Error() == "key exists" {
var attempts = 1
for attempts < 5 {
err = c.Put(ctx, key, databytes)
if err == nil {
break
}
attempts++
time.Sleep(50 * time.Millisecond)
}
check(err)
} else {
panic(err)
}
}
}
func restore_to_dqlite(ep string, dir string) error {
ctx := context.Background()
etcdcfg := kineep.ETCDConfig{
Endpoints: []string{ep},
LeaderElect: false,
}
c, err := client.New(etcdcfg)
check(err)
defer c.Close()
var i = 0
for true {
var keyfile = db + "/" + strconv.Itoa(i) + ".key"
var datafile = db + "/" + strconv.Itoa(i) + ".data"
_, err := os.Stat(keyfile)
if os.IsNotExist(err) {
fmt.Printf("Processed %d entries", i)
break
}
keybytes, err := ioutil.ReadFile(keyfile)
check(err)
key := string(keybytes)
databytes, err := ioutil.ReadFile(datafile)
check(err)
logrus.Debugf("%d) %s", i, key)
put_key(c, ctx, key, databytes)
i++
}
return nil
}
func backup_dqlite(ep string, dir string) error {
ctx := context.Background()
etcdcfg := kineep.ETCDConfig{
Endpoints: []string{ep},
LeaderElect: false,
}
c, err := client.New(etcdcfg)
check(err)
defer c.Close()
resp, err := c.List(ctx, "/", 0)
check(err)
err = os.Mkdir(db, 0700)
check(err)
for i, kv := range resp {
logrus.Debugf("%d) %s\n", i, kv.Key)
data, err := c.Get(ctx, string(kv.Key))
check(err)
var keyfile = db + "/" + strconv.Itoa(i) + ".key"
var datafile = db + "/" + strconv.Itoa(i) + ".data"
keyf, err := os.Create(keyfile)
check(err)
defer keyf.Close()
dataf, err := os.Create(datafile)
check(err)
defer dataf.Close()
keyf.Write(kv.Key)
dataf.Write(data.Data)
dataf.Close()
keyf.Close()
}
return nil
}
func restore_to_etcd(ep string, dir string) error {
ctx := context.Background()
cli, err := clientv3.New(clientv3.Config{
Endpoints: []string{ep},
})
check(err)
defer cli.Close()
var i = 0
for true {
var keyfile = db + "/" + strconv.Itoa(i) + ".key"
var datafile = db + "/" + strconv.Itoa(i) + ".data"
_, err := os.Stat(keyfile)
if os.IsNotExist(err) {
fmt.Printf("Processed %d entries", i)
break
}
keybytes, err := ioutil.ReadFile(keyfile)
check(err)
databytes, err := ioutil.ReadFile(datafile)
check(err)
_, err = cli.Put(ctx, string(keybytes), string(databytes))
check(err)
i++
}
return nil
}
func direct(etcd_direct string, dqlite_direct string) error {
ctx_dqlite := context.Background()
dqlite_cfg := kineep.ETCDConfig{
Endpoints: []string{dqlite_direct},
LeaderElect: false,
}
dqlite, err := client.New(dqlite_cfg)
check(err)
defer dqlite.Close()
ctx_etcd := context.Background()
etcd, err := clientv3.New(clientv3.Config{
Endpoints: []string{etcd_direct},
})
check(err)
defer etcd.Close()
resp, err := etcd.Get(ctx_etcd, "", clientv3.WithPrefix(), clientv3.WithSort(clientv3.SortByKey, clientv3.SortDescend))
check(err)
for i, ev := range resp.Kvs {
logrus.Debugf("%d) %s", i, ev.Key)
err = dqlite.Create(ctx_dqlite, string(ev.Key), ev.Value)
if err != nil {
if err.Error() == "key exists" {
err = dqlite.Put(ctx_dqlite, string(ev.Key), ev.Value)
check(err)
} else {
panic(err)
}
}
i++
}
return nil
}
func run(c *cli.Context) {
if c.Bool("debug") {
logrus.SetLevel(logrus.DebugLevel)
}
logrus.Infof("mode: %s, endpoint: %s, dir: %s", mode, endpoint, db)
if mode == "backup" || mode == "backup-etcd" {
err := backup_etcd(endpoint, db)
if err != nil {
logrus.Fatal(err)
}
} else if mode == "restore" || mode == "restore-to-dqlite" || mode == "restore-dqlite" {
err := restore_to_dqlite(endpoint, db)
if err != nil {
logrus.Fatal(err)
}
} else if mode == "backup-dqlite" {
err := backup_dqlite(endpoint, db)
if err != nil {
logrus.Fatal(err)
}
} else if mode == "restore-to-etcd" || mode == "restore-etcd" {
err := restore_to_etcd(endpoint, db)
if err != nil {
logrus.Fatal(err)
}
} else if mode == "direct" {
err := direct(etcd_direct, dqlite_direct)
if err != nil {
logrus.Fatal(err)
}
} else {
logrus.Fatal("Unknown mode")
return
}
return
}