Skip to content

Commit 0a1b946

Browse files
committed
completes work on mhausenblas#1
1 parent 8f7a86d commit 0a1b946

File tree

2 files changed

+74
-45
lines changed

2 files changed

+74
-45
lines changed

consul.go

Lines changed: 63 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
consul "github.com/hashicorp/consul/api"
88
"os"
99
"path/filepath"
10-
// "strings"
10+
"strings"
1111
)
1212

1313
// backupCONSUL walks an etcd tree, applying
@@ -44,17 +44,19 @@ func visitCONSUL(path string, fn reap) {
4444
if children, _, err := ckv.Keys(path, ",", &qopts); err != nil {
4545
log.WithFields(log.Fields{"func": "visitCONSUL"}).Error(fmt.Sprintf("%s", err))
4646
} else {
47-
if len(children) > 1 { // there are children
47+
if len(children) > 1 || path == "/" { // there are children
4848
log.WithFields(log.Fields{"func": "visitCONSUL"}).Debug(fmt.Sprintf("%s has %d children", path, len(children)))
4949
for _, node := range children {
5050
log.WithFields(log.Fields{"func": "visitCONSUL"}).Debug(fmt.Sprintf("Next visiting child %s", node))
5151
visitCONSUL(node, fn)
5252
}
5353
} 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+
}
5860
}
5961
}
6062
}
@@ -70,15 +72,15 @@ func restoreCONSUL() bool {
7072
_ = os.RemoveAll(s)
7173
}()
7274
cfg := consul.Config{
73-
Address: "http://" + brf.Endpoint,
75+
Address: brf.Endpoint,
7476
}
7577
cclient, _ := consul.NewClient(&cfg)
7678
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
7880
// restore keys from the local filesystem - note that
7981
// 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))
8284
return false
8385
}
8486
} else { // can't restore from TTY
@@ -88,37 +90,56 @@ func restoreCONSUL() bool {
8890
}
8991

9092
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+
}
123144
return nil
124145
}

main.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,12 @@ func init() {
9999
}
100100

101101
based = strconv.FormatInt(time.Now().Unix(), 10)
102-
if snapshotid == "" { // for backup ops
102+
if bop == BURRY_OPERATION_BACKUP {
103103
snapshotid = based
104104
} else { // for restore ops
105-
based = snapshotid
105+
if snapshotid != "" {
106+
based = snapshotid
107+
}
106108
}
107109
numrestored = 0
108110
}
@@ -127,7 +129,11 @@ func processop() bool {
127129
log.WithFields(log.Fields{"func": "processop"}).Error(fmt.Sprintf("Infra service %s unknown or not yet supported", brf.InfraService))
128130
}
129131
case BURRY_OPERATION_RESTORE:
130-
if snapshotid == based {
132+
if brf.StorageTarget == STORAGE_TARGET_TTY {
133+
log.WithFields(log.Fields{"func": "processop"}).Error(fmt.Sprintf("I can't restore from TTY, pick a different storage target with -t/--target"))
134+
return false
135+
}
136+
if snapshotid == "" {
131137
log.WithFields(log.Fields{"func": "processop"}).Error(fmt.Sprintf("You MUST supply a snapshot ID with -s/--snapshot"))
132138
return false
133139
}
@@ -136,6 +142,8 @@ func processop() bool {
136142
success = restoreZK()
137143
case INFRA_SERVICE_ETCD:
138144
success = restoreETCD()
145+
case INFRA_SERVICE_CONSUL:
146+
success = restoreCONSUL()
139147
default:
140148
log.WithFields(log.Fields{"func": "processop"}).Error(fmt.Sprintf("Infra service %s unknown or not yet supported", brf.InfraService))
141149
}

0 commit comments

Comments
 (0)