forked from AaronO/etcd-dump
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
56 lines (44 loc) · 1.22 KB
/
index.js
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
// Requires
var Q = require('q');
var _ = require('underscore');
var qClass = require('qpatch').qClass;
// Etcd client
var Etcd = qClass(require('node-etcd'), ['watcher']);
// Since etcd create the dir keys automatically
// transform the tree of keys
// to contain only a flat array of leaves
function normalize(obj) {
obj = obj.node || obj;
// Is a leaf
if(!_.has(obj, 'nodes')) {
// We don't want the modifiedIndex attr in our dumps/restores
return _.pick(obj, 'key', 'value');
}
return _.flatten(_.map(obj.nodes, normalize));
}
function Dumper(host,port) {
// ETCD client
this.store = new Etcd(host, port);
_.bindAll(this);
}
// Get a JS object of the DB
Dumper.prototype.dump = function() {
return this.store.get('', {
recursive: true
})
.then(normalize);
};
// Restore a list of keys
Dumper.prototype.restore = function(entries) {
var self = this;
return Q.all(_.map(entries, function(entry) {
return self.store.set(entry.key, entry.value);
}));
};
// Restore the database from input data
function createDumper(host,port) {
return new Dumper(host,port);
}
// Exports
module.exports = createDumper;
module.exports.normalize = normalize;