-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
executable file
·125 lines (96 loc) · 2.83 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
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
#!/usr/bin/env node
var request = require('request');
var program = require('commander');
var fs = require('fs');
var throttled = require('throttled-promise');
var pkg = require('./package');
function checkFile(file) {
if (!file) {
console.error('Error missing --file option');
process.exit(1);
}
}
program
.version(pkg.version)
.option('-f, --file <file>', 'backup file')
.option('-e, --etcd <etcd>', 'etcd url eg: https://0.0.0.0:4001')
.option('-c, --concurrency <concurrency>', 'max parallel requests')
program
.command('restore')
.description('restore keys from backup file')
.action(function(options) {
var file = options.parent.file || undefined;
var etcd = options.parent.etcd || 'http://0.0.0.0:4001';
var concurrency = options.parent.concurrency || 5;
checkFile(file);
var promises = [];
var configs = fs.readFileSync(file).toString();
configs = JSON.parse(configs);
configs.forEach(function(config) {
var options = {
rejectUnauthorized: false,
uri: etcd + '/v2/keys' + config.key,
method: 'put',
form: { value: config.value }
}
promises.push(new throttled(function(resolve, reject) {
console.log('Updating: ' + config.key);
request(options, function(err, response, body) {
if (err) {
return reject(err);
}
return resolve(body);
});
}));
});
throttled.all(promises, concurrency)
.then(function(results) {
console.log('All configs restored')
})
.catch(function(err) {
console.log(err);
process.exit(1);
});
});
program
.command('dump')
.description('dump keys to backup file')
.action(function(options) {
var file = options.parent.file || undefined;
var etcd = options.parent.etcd || 'http://0.0.0.0:4001';
var options = {
rejectUnauthorized: false,
uri: etcd + '/v2/keys/?recursive=true',
method: 'get',
json:true
}
request(options, function(err, res, body) {
if (err) {
console.error(err);
process.exit(1);
}
var configs = [];
var extractFromNodes = function(nodes) {
nodes.forEach(function(node) {
if (node.hasOwnProperty('dir') && node.dir === true) {
extractFromNodes(node.nodes);
} else if (node.hasOwnProperty('value')) {
configs.push({
key: node.key,
value: node.value
});
}
});
}
extractFromNodes(body.node.nodes);
configs = JSON.stringify(configs, null, 2);
fs.writeFileSync(file, configs, null, function(err) {
if (err) {
console.error(err);
process.exit(1);
}
});
});
});
program.parse(process.argv);
if (!program.args.length) program.help();