This repository has been archived by the owner on Oct 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
etcd.go
137 lines (131 loc) · 4.69 KB
/
etcd.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
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
log "github.com/sirupsen/logrus"
etcd "github.com/coreos/etcd/client"
"golang.org/x/net/context"
)
// backupETCD walks an etcd tree, applying
// a reap function per leaf node visited
func backupETCD() bool {
if brf.Endpoint == "" {
return false
}
cfg := etcd.Config{
Endpoints: []string{"http://" + brf.Endpoint},
Transport: etcd.DefaultTransport,
HeaderTimeoutPerRequest: time.Duration(brf.Timeout) * time.Second,
}
c, _ := etcd.New(cfg)
kapi = etcd.NewKeysAPI(c)
// use the etcd API to visit each node and store
// the values in the local filesystem:
visitETCD("/", reapsimple)
if lookupst(brf.StorageTarget) > 0 { // non-TTY, actual storage
// create an archive file of the node's values:
res := arch()
// transfer to remote, if applicable:
toremote(res)
}
return true
}
// visitETCD visits a path in the etcd tree
// and applies the reap function fn on the node
// at the path if it is a leaf node
func visitETCD(path string, fn reap) {
log.WithFields(log.Fields{"func": "visitETCD"}).Debug(fmt.Sprintf("On node %s", path))
copts := etcd.GetOptions{
Recursive: true,
Sort: false,
Quorum: true,
}
if resp, err := kapi.Get(context.Background(), path, &copts); err != nil {
log.WithFields(log.Fields{"func": "visitETCD"}).Error(fmt.Sprintf("%s", err))
} else {
if resp.Node.Dir { // there are children
log.WithFields(log.Fields{"func": "visitETCD"}).Debug(fmt.Sprintf("%s has %d children", path, len(resp.Node.Nodes)))
for _, node := range resp.Node.Nodes {
log.WithFields(log.Fields{"func": "visitETCD"}).Debug(fmt.Sprintf("Next visiting child %s", node.Key))
visitETCD(node.Key, fn)
}
} else { // we're on a leaf node
fn(resp.Node.Key, string(resp.Node.Value))
}
}
}
func restoreETCD() bool {
if lookupst(brf.StorageTarget) > 0 { // non-TTY, actual storage
// transfer from remote, if applicable:
a := fromremote()
// unarchive:
s := unarch(a)
defer func() {
_ = os.RemoveAll(s)
}()
cfg := etcd.Config{
Endpoints: []string{"http://" + brf.Endpoint},
Transport: etcd.DefaultTransport,
HeaderTimeoutPerRequest: time.Duration(brf.Timeout) * time.Second,
}
c, _ := etcd.New(cfg)
kapi = etcd.NewKeysAPI(c)
// walk the snapshot directory and use the etcd API to
// restore keys from the local filesystem - note that
// only non-existing keys will be created:
if err := filepath.Walk(s, visitETCDReverse); err != nil {
log.WithFields(log.Fields{"func": "restoreETCD"}).Error(fmt.Sprintf("%s", err))
return false
}
} else { // can't restore from TTY
return false
}
return true
}
func visitETCDReverse(path string, f os.FileInfo, err error) error {
if f.Name() == BURRYMETA_FILE || f.Name() == snapshotid {
return nil
} else {
cwd, _ := os.Getwd()
base, _ := filepath.Abs(filepath.Join(cwd, snapshotid))
key, _ := filepath.Rel(base, path)
// append the root "/" to make it a key and unescape ":"
key = "/" + strings.Replace(key, "BURRY_ESC_COLON", ":", -1)
if f.IsDir() {
cfile, _ := filepath.Abs(filepath.Join(path, CONTENT_FILE))
if _, eerr := os.Stat(cfile); eerr == nil { // there is a content file at this path
log.WithFields(log.Fields{"func": "visitETCDReverse"}).Debug(fmt.Sprintf("Attempting to insert %s as leaf key", key))
if c, cerr := readc(cfile); cerr != nil {
log.WithFields(log.Fields{"func": "visitETCDReverse"}).Error(fmt.Sprintf("%s", cerr))
if !forget {
return cerr
}
} else {
if _, kerr := kapi.Set(context.Background(), key, string(c), &etcd.SetOptions{Dir: false, PrevExist: etcd.PrevNoExist}); kerr == nil {
log.WithFields(log.Fields{"func": "visitETCDReverse"}).Info(fmt.Sprintf("Restored %s", key))
log.WithFields(log.Fields{"func": "visitETCDReverse"}).Debug(fmt.Sprintf("Value: %s", c))
numrestored = numrestored + 1
} else {
if !forget {
log.WithFields(log.Fields{"func": "visitETCDReverse"}).Error(fmt.Sprintf("%s", kerr))
return kerr
} else {
log.WithFields(log.Fields{"func": "visitETCDReverse"}).Info(fmt.Sprintf("Ignoring existing %s", kerr))
}
}
}
} else {
log.WithFields(log.Fields{"func": "visitETCDReverse"}).Debug(fmt.Sprintf("Attempting to insert %s as a non-leaf key", key))
if _, kerr := kapi.Set(context.Background(), key, "", &etcd.SetOptions{Dir: true, PrevExist: etcd.PrevNoExist}); kerr == nil {
log.WithFields(log.Fields{"func": "visitETCDReverse"}).Info(fmt.Sprintf("Restored %s", key))
numrestored = numrestored + 1
}
}
}
log.WithFields(log.Fields{"func": "visitETCDReverse"}).Debug(fmt.Sprintf("Visited %s", key))
}
return nil
}