-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrds-cluster-route53-records.js
332 lines (279 loc) · 11.1 KB
/
rds-cluster-route53-records.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
"use strict";
// TODO - daemonize/set interval
// TODO - check existing record and if no change needed, don't update
// throw new Error(require('util').inspect(data.DBInstances[i].Endpoint, {showHidden: false, depth: null}));
var pidfile = '/tmp/rcrr.pid';
// write the PID file to avoid collision
writePID();
process
.on('exit', exitHandler.bind(null, {cleanup: true})) // do something when app is closing
.on('SIGINT', exitHandler.bind(null, {exit: true})) // catches ctrl+c event
.on('uncaughtException', exitHandler.bind(null, {exit: true})); //catches uncaught exceptions
processConfs(function(confs) {
var batch = {};
var cconfs = confs.slice(0); // clone confs
(function addClusterInfo() {
var conf = cconfs.splice(0, 1)[0];
console.log('\nProcessing Cluster Info for : ' + conf.rdscluster);
getDBClustersSummary(conf, function(dbInfo) {
try {
var nosyncwait = conf.nosyncwait || false,
cred = {r53zoneid: conf.r53zoneid, r53access: conf.r53access, r53secret: conf.r53secret, nosyncwait: nosyncwait},
cred_uniq = conf.r53zoneid + '-' + conf.r53access + '-' + conf.r53secret + '-' + conf.nosyncwait;
batch[cred_uniq] = batch[cred_uniq] || {cred: cred, records: []}; // initialize if different
// get all the read instances
dbInfo.read.forEach(function(info, i) {
if (typeof info.ip !== 'undefined' && typeof info.host !== 'undefined') {
batch[cred_uniq].records.push({host: conf.r53readpre + '-' + (i + 1) + '.' + conf.r53domain, ip: info.ip, ttl: conf.timetolive});
}
})
// get all the write instances
dbInfo.write.forEach(function(info, i) {
if (typeof info.ip !== 'undefined' && typeof info.host !== 'undefined') {
batch[cred_uniq].records.push({host: conf.r53writepre + '-' + (i + 1) + '.' + conf.r53domain, ip: info.ip, ttl: conf.timetolive});
}
})
// only process once we have everything
if (cconfs.length == 0) {
// write the r53 records
setRoute53Records(batch, function(r53, changeID) {
console.log('Done');
});
} else {
addClusterInfo();
}
} catch (e) {
console.error(e);
}
});
})();
})
function processConfs(cb) {
var fs = require('fs'),
program = require('commander'),
cliarg = process.argv[2],
confs = [],
conf, files;
if (cliarg !== undefined) {
if (fs.existsSync(cliarg)) {
if (fs.lstatSync(cliarg).isDirectory()) {
files = fs.readdirSync(cliarg);
// aggregate into zone id batches
for (var i = 0; i < files.length; i++) {
if (files[i].match(/^(.*)\.json$/)) {
conf = require(absPath(cliarg + '/' + files[i]));
confs.push(conf);
}
}
} else if (fs.lstatSync(cliarg).isFile() && (cliarg.match(/^(.*)\.json$/))) {
conf = require(absPath(cliarg));
confs.push(conf);
}
} else {
program
.version('1.0')
.description('Create Route 53 DNS Entries for VPC peered DB Clusters')
.option('-a, --r53access [value]', 'AWS Route 53 account Access Key ID ')
.option('-s, --r53secret [value]', 'AWS Route 53 account Secret Access Key')
.option('-z, --r53zoneid [value]', 'AWS Route 53 Zone ID')
.option('-d, --r53domain [value]', 'AWS Route 53 Domain')
.option('-o, --r53readpre [value]', 'AWS Route 53 read-only hostname prefix')
.option('-w, --r53writepre [value]', 'AWS Route 53 write hostname prefix')
.option('-b, --rdsaccess [value]', 'AWS RDS account Access Key ID')
.option('-t, --rdssecret [value]', 'AWS RDS account Secret Access Key')
.option('-r, --rdsregion [value]', 'AWS RDS account Region')
.option('-c, --rdscluster [value]', 'AWS RDS cluster endpoint')
.option('-l, --lookuphost [value]', 'Host that has proper access/networking to lookup internal IP')
.option('-u, --lookupuser [value]', 'User that has proper access/networking to lookup internal IP')
.option('-n, --nosyncwait', 'Do not wait until DNS servers are INSYNC')
.option('-i, --timetolive', 'DNS time to live')
.parse(process.argv);
confs.push({
r53access: program.r53access,
r53secret: program.r53secret,
r53zoneid: program.r53zoneid,
r53domain: program.r53domain,
r53readpre: program.r53readpre,
r53writepre: program.r53writepre,
rdsaccess: program.rdsaccess,
rdssecret: program.rdssecret,
rdsregion: program.rdsregion,
rdscluster: program.rdscluster,
lookuphost: program.lookuphost,
lookupuser: program.lookupuser,
timetolive: program.timetolive
});
}
}
checkRequired(confs, cb);
}
// check all configs that they have the required params
function checkRequired(confs, cb) {
var required = ['r53access', 'r53secret', 'r53zoneid', 'r53domain', 'r53readpre', 'r53writepre', 'rdsaccess', 'rdssecret', 'rdsregion', 'rdscluster', 'lookuphost', 'lookupuser'];
confs.forEach(function(conf) {
required.forEach(function(req) {
if (!conf[req]) {
throw new Error('--' + req + ' missing, it is required');
}
});
});
cb(confs);
}
// don't release until DNS is synced
function r53WaitForSync(r53, changeID, cb) {
r53.getChange({Id: changeID}, function(err, data) {
if (err) {
console.log(err, err.stack); // an error occurred
} else {
if (data.ChangeInfo.Status == 'PENDING') {
console.log(changeID + ' - ' + data.ChangeInfo.Status);
setTimeout(function() {
r53WaitForSync(r53, changeID, cb);
}, 2500);
} else {
cb(data.ChangeInfo.Status);
}
}
});
}
function getAWS(conf) {
var aws = require('aws-sdk');
aws.config.update(conf);
return aws;
}
// send a upsert call to enter the DNS records
function setRoute53Records(batch, cb) {
Object.keys(batch).forEach(function(cred_uniq) {
var cred = batch[cred_uniq].cred,
records = batch[cred_uniq].records,
aws = getAWS({accessKeyId: cred.r53access, secretAccessKey: cred.r53secret}),
r53 = new aws.Route53(),
params = {ChangeBatch: {Changes: []}, HostedZoneId: cred.r53zoneid}; // setup base params
// gather up all the records for the batch
records.forEach(function(record) {
params.ChangeBatch.Changes.push({
Action: 'UPSERT',
ResourceRecordSet: {
Name: record.host,
Type: 'A',
TTL: record.ttl,
ResourceRecords: [{Value: record.ip}]
}
})
});
if (params.ChangeBatch.Changes.length > 0) {
r53.changeResourceRecordSets(params, function(err, data) {
if (err) {
console.log(err, err.stack); // an error occurred
} else {
if (cred.nosyncwait) {
console.log('\nChanges sent to Route 53, not waiting for DNS sync: ' + data.ChangeInfo.Id);
cb();
} else {
console.log('\nSending changes to Route 53, waiting for DNS sync');
r53WaitForSync(r53, data.ChangeInfo.Id, function(status) {
console.log(data.ChangeInfo.Id + ' - ' + status);
cb();
});
}
}
});
}
});
}
// get the overall cluster information
function getDBClustersSummary(conf, cb) {
var aws = getAWS({accessKeyId: conf.rdsaccess, secretAccessKey: conf.rdssecret, region: conf.rdsregion}),
rds = new aws.RDS(),
rdsInstances = {read: [], write: [], instances: []};
rds.describeDBClusters({}, function(err, data) {
if (err) {
console.log(err, err.stack); // an error occurred
} else {
if (data.hasOwnProperty('DBClusters')) {
for (var i = 0; i < data.DBClusters.length; i++) {
if (data.DBClusters[i].hasOwnProperty('DBClusterMembers') && data.DBClusters[i].hasOwnProperty('Endpoint') && data.DBClusters[i].Endpoint == conf.rdscluster) {
for (var j = 0; j < data.DBClusters[i].DBClusterMembers.length; j++) {
var kind = data.DBClusters[i].DBClusterMembers[j].IsClusterWriter ? 'write' : 'read';
var info = {
name: data.DBClusters[i].DBClusterMembers[j].DBInstanceIdentifier,
kind: kind
};
console.log('Cluster ' + kind + ' instance : ' + info.name);
rdsInstances.instances[data.DBClusters[i].DBClusterMembers[j].DBInstanceIdentifier] = info;
rdsInstances[kind].push(info);
}
}
}
getDBInstancesSummary(conf, rds, rdsInstances, cb);
} else {
console.log('Count not reference data.DBClusters[i].DBClusterMembers');
}
}
});
}
// get the cluster instances information
function getDBInstancesSummary(conf, rds, rdsInstances, cb) {
rds.describeDBInstances({}, function(err, data) {
if (err) {
console.log(err, err.stack); // an error occurred
} else {
for (var i = 0; i < data.DBInstances.length; i++) {
if (
data.DBInstances[i].hasOwnProperty('DBInstanceIdentifier') &&
data.DBInstances[i].hasOwnProperty('Endpoint') &&
data.DBInstances[i].Endpoint.hasOwnProperty('Address') &&
rdsInstances.instances.hasOwnProperty(data.DBInstances[i].DBInstanceIdentifier)
) {
var host = data.DBInstances[i].Endpoint.Address,
ip;
try {
ip = getLocalIPFromHost(host, conf.lookuphost, conf.lookupuser);
console.log('Cluster Instance ' + host + ' ip : ' + ip);
rdsInstances.instances[data.DBInstances[i].DBInstanceIdentifier].host = host;
rdsInstances.instances[data.DBInstances[i].DBInstanceIdentifier].ip = ip;
} catch (err) {
console.error(err);
}
}
}
cb(rdsInstances);
}
});
}
function exitHandler(options, err) {
if (options.cleanup) {
removePID();
}
if (err) {
console.log(err.stack);
}
if (options.exit) {
console.log('User interuption');
removePID();
process.exit();
}
}
// unlink
function removePID() {
var fs = require('fs');
fs.unlink(pidfile);
}
function writePID() {
var fs = require('fs'),
pid = process.pid,
fd = fs.openSync(pidfile, 'wx');
fs.writeFileSync(pidfile, pid.toString());
fs.closeSync(fd);
}
// return the path that will work with a require()
function absPath(filepath) {
return filepath.match(/^[\/~]/) ? filepath : require('path').join(process.cwd(), filepath);
}
// function to issue on the host to get a IP for the host, "getent" which should be available on most linux systems
function getLocalIPFromHost(address, host, user) {
var execSync = require('child_process').execSync,
hostInfo = execSync('ssh -oUserKnownHostsFile=/dev/null -oStrictHostKeyChecking=no ' + user + '@' + host + ' getent hosts "' + address + '"', {encoding: 'utf8'});
// crude way to only get the IP address
return hostInfo.replace(/^((?:[0-9]{1,3}\.){3}[0-9]{1,3}).*\n*/m, '$1');
}