7
7
consul "github.com/hashicorp/consul/api"
8
8
"os"
9
9
"path/filepath"
10
- // "strings"
10
+ "strings"
11
11
)
12
12
13
13
// backupCONSUL walks an etcd tree, applying
@@ -44,17 +44,19 @@ func visitCONSUL(path string, fn reap) {
44
44
if children , _ , err := ckv .Keys (path , "," , & qopts ); err != nil {
45
45
log .WithFields (log.Fields {"func" : "visitCONSUL" }).Error (fmt .Sprintf ("%s" , err ))
46
46
} else {
47
- if len (children ) > 1 { // there are children
47
+ if len (children ) > 1 || path == "/" { // there are children
48
48
log .WithFields (log.Fields {"func" : "visitCONSUL" }).Debug (fmt .Sprintf ("%s has %d children" , path , len (children )))
49
49
for _ , node := range children {
50
50
log .WithFields (log.Fields {"func" : "visitCONSUL" }).Debug (fmt .Sprintf ("Next visiting child %s" , node ))
51
51
visitCONSUL (node , fn )
52
52
}
53
53
} else { // we're on a leaf node
54
- if node , _ , err := ckv .Get (path , & qopts ); err != nil {
55
- log .WithFields (log.Fields {"func" : "visitCONSUL" }).Error (fmt .Sprintf ("%s" , err ))
56
- } else {
57
- fn ("/" + node .Key , string (node .Value ))
54
+ if path != "/" {
55
+ if node , _ , err := ckv .Get (path , & qopts ); err != nil {
56
+ log .WithFields (log.Fields {"func" : "visitCONSUL" }).Error (fmt .Sprintf ("%s" , err ))
57
+ } else {
58
+ fn ("/" + node .Key , string (node .Value ))
59
+ }
58
60
}
59
61
}
60
62
}
@@ -70,15 +72,15 @@ func restoreCONSUL() bool {
70
72
_ = os .RemoveAll (s )
71
73
}()
72
74
cfg := consul.Config {
73
- Address : "http://" + brf .Endpoint ,
75
+ Address : brf .Endpoint ,
74
76
}
75
77
cclient , _ := consul .NewClient (& cfg )
76
78
ckv = cclient .KV ()
77
- // walk the snapshot directory and use the etcd API to
79
+ // walk the snapshot directory and use the Consul API to
78
80
// restore keys from the local filesystem - note that
79
81
// only non-existing keys will be created:
80
- if err := filepath .Walk (s , visitETCDReverse ); err != nil {
81
- log .WithFields (log.Fields {"func" : "restoreETCD " }).Error (fmt .Sprintf ("%s" , err ))
82
+ if err := filepath .Walk (s , visitCONSULReverse ); err != nil {
83
+ log .WithFields (log.Fields {"func" : "restoreCONSUL " }).Error (fmt .Sprintf ("%s" , err ))
82
84
return false
83
85
}
84
86
} else { // can't restore from TTY
@@ -88,37 +90,56 @@ func restoreCONSUL() bool {
88
90
}
89
91
90
92
func visitCONSULReverse (path string , f os.FileInfo , err error ) error {
91
- // if f.Name() == BURRYMETA_FILE || f.Name() == snapshotid {
92
- // return nil
93
- // } else {
94
- // cwd, _ := os.Getwd()
95
- // base, _ := filepath.Abs(filepath.Join(cwd, snapshotid))
96
- // key, _ := filepath.Rel(base, path)
97
- // // append the root "/" to make it a key and unescape ":"
98
- // key = "/" + strings.Replace(key, "BURRY_ESC_COLON", ":", -1)
99
- // if f.IsDir() {
100
- // cfile, _ := filepath.Abs(filepath.Join(path, CONTENT_FILE))
101
- // if _, eerr := os.Stat(cfile); eerr == nil { // there is a content file at this path
102
- // log.WithFields(log.Fields{"func": "visitETCDReverse"}).Debug(fmt.Sprintf("Attempting to insert %s as leaf key", key))
103
- // if c, cerr := readc(cfile); cerr != nil {
104
- // log.WithFields(log.Fields{"func": "visitETCDReverse"}).Error(fmt.Sprintf("%s", cerr))
105
- // return cerr
106
- // } else {
107
- // if _, kerr := kapi.Set(context.Background(), key, string(c), &etcd.SetOptions{Dir: false, PrevExist: etcd.PrevNoExist}); kerr == nil {
108
- // log.WithFields(log.Fields{"func": "visitETCDReverse"}).Info(fmt.Sprintf("Restored %s", key))
109
- // log.WithFields(log.Fields{"func": "visitETCDReverse"}).Debug(fmt.Sprintf("Value: %s", c))
110
- // numrestored = numrestored + 1
111
- // }
112
- // }
113
- // } else {
114
- // log.WithFields(log.Fields{"func": "visitETCDReverse"}).Debug(fmt.Sprintf("Attempting to insert %s as a non-leaf key", key))
115
- // if _, kerr := kapi.Set(context.Background(), key, "", &etcd.SetOptions{Dir: true, PrevExist: etcd.PrevNoExist}); kerr == nil {
116
- // log.WithFields(log.Fields{"func": "visitETCDReverse"}).Info(fmt.Sprintf("Restored %s", key))
117
- // numrestored = numrestored + 1
118
- // }
119
- // }
120
- // }
121
- // log.WithFields(log.Fields{"func": "visitETCDReverse"}).Debug(fmt.Sprintf("Visited %s", key))
122
- // }
93
+ if f .Name () == BURRYMETA_FILE || f .Name () == snapshotid {
94
+ return nil
95
+ } else {
96
+ cwd , _ := os .Getwd ()
97
+ base , _ := filepath .Abs (filepath .Join (cwd , snapshotid ))
98
+ key , _ := filepath .Rel (base , path )
99
+ qopts := consul.QueryOptions {
100
+ RequireConsistent : true ,
101
+ }
102
+ // unescape ":"
103
+ key = strings .Replace (key , "BURRY_ESC_COLON" , ":" , - 1 )
104
+ if f .IsDir () {
105
+ cfile , _ := filepath .Abs (filepath .Join (path , CONTENT_FILE ))
106
+ if _ , eerr := os .Stat (cfile ); eerr == nil { // there is a content file at this path
107
+ log .WithFields (log.Fields {"func" : "visitCONSULReverse" }).Debug (fmt .Sprintf ("Attempting to insert %s as leaf key" , key ))
108
+ if c , cerr := readc (cfile ); cerr != nil {
109
+ log .WithFields (log.Fields {"func" : "visitCONSULReverse" }).Error (fmt .Sprintf ("%s" , cerr ))
110
+ return cerr
111
+ } else {
112
+ if node , _ , eerr := ckv .Get (key , & qopts ); eerr != nil {
113
+ log .WithFields (log.Fields {"func" : "visitCONSULReverse" }).Error (fmt .Sprintf ("%s" , eerr ))
114
+ } else {
115
+ if node == nil { // key does not exist yet
116
+ p := & consul.KVPair {Key : key , Value : c }
117
+ if _ , kerr := ckv .Put (p , nil ); kerr != nil {
118
+ log .WithFields (log.Fields {"func" : "visitCONSULReverse" }).Error (fmt .Sprintf ("%s" , kerr ))
119
+ } else {
120
+ log .WithFields (log.Fields {"func" : "visitCONSULReverse" }).Info (fmt .Sprintf ("Restored %s" , key ))
121
+ log .WithFields (log.Fields {"func" : "visitCONSULReverse" }).Debug (fmt .Sprintf ("Value: %s" , c ))
122
+ numrestored = numrestored + 1
123
+ }
124
+ }
125
+ }
126
+ }
127
+ } else {
128
+ log .WithFields (log.Fields {"func" : "visitCONSULReverse" }).Debug (fmt .Sprintf ("Attempting to insert %s as a non-leaf key" , key ))
129
+ if node , _ , eerr := ckv .Get (key , & qopts ); eerr != nil {
130
+ log .WithFields (log.Fields {"func" : "visitCONSULReverse" }).Error (fmt .Sprintf ("%s" , eerr ))
131
+ } else {
132
+ if node == nil { // key does not exist yet
133
+ p := & consul.KVPair {Key : key , Value : nil }
134
+ if _ , kerr := ckv .Put (p , nil ); kerr == nil {
135
+ log .WithFields (log.Fields {"func" : "visitCONSULReverse" }).Info (fmt .Sprintf ("Restored %s" , key ))
136
+ numrestored = numrestored + 1
137
+ }
138
+ }
139
+ }
140
+ }
141
+ }
142
+ log .WithFields (log.Fields {"func" : "visitCONSULReverse" }).Debug (fmt .Sprintf ("Visited %s" , key ))
143
+ }
123
144
return nil
124
145
}
0 commit comments