-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
190 lines (154 loc) · 4.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
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
'use strict';
/**
* Module dependencies.
*/
var util = require('util');
var dns = require('dns');
var events = require('events');
var _ = require('underscore');
var AWS = require('aws-sdk');
var CronJob = require('cron').CronJob;
/**
* Expose `Updater`.
*/
module.exports = Updater;
/**
* Validate global configuration.
*
* @api private
*/
function validateOptions(options) {
if (!options) throw new Error('options are required');
if (!options.aws) throw new Error('options.aws required');
if (!options.heroku) throw new Error('options.heroku required');
if (!options.aws.id) throw new Error('options.aws.id required');
if (!options.aws.key) throw new Error('options.aws.key required');
if (!options.aws.region) throw new Error('options.aws.region required');
if (!options.aws.zoneid) throw new Error('options.aws.zoneid required');
if (!options.aws.name) throw new Error('options.aws.name required');
if (!options.heroku.app) throw new Error('options.heroku.app required');
}
/**
* Initialize a new `Updater` with options.
*
* Options:
*
* - `aws.id` an aws access key ID with permissions to change a route53 resource
* - `aws.key` the aws secret access key for that ID
* - `aws.region` the aws region you working in
* - `aws.zoneid` the aws zoneid for the domain you want to point at heroku
* - `aws.name` the domain name eg. example.com
*
* - `heroku.app` the domain of the heroku app eg. example.herokuapp.com
*
* @param {Object} [options]
* @api public
*/
function Updater(options) {
validateOptions(options);
this.aws = options.aws;
this.heroku = options.heroku;
events.EventEmitter.call(this);
// AWS Config
AWS.config.update({accessKeyId: this.aws.id, secretAccessKey: this.aws.key});
// Set your region for future requests.
AWS.config.update({region: this.aws.region});
// get the Route53 library
this.route53 = new AWS.Route53();
// Local vars
this.addresses = {};
this.lastDNS = [];
}
util.inherits(Updater, events.EventEmitter);
/**
* Resolve an IP from Heroku and save it.
*
* @param {Function} [fn]
* @api public
*/
Updater.prototype.lookForHerokuIPs = function() {
var self = this;
dns.resolve(self.heroku.app, function (err, address) {
if (err) throw err;
address.forEach(function (val) {
self.addresses[val] = new Date();
});
var newDNS = [];
for (var key in self.addresses) {
if(self.addresses.hasOwnProperty(key)){
var isOld = ((self.addresses[key] - new Date) > 30000);
if (isOld) {
delete self.addresses[key];
} else {
newDNS.push(key);
}
}
}
var difference = _.difference(newDNS, self.lastDNS);
if (difference.length === 0) {
return;
}
self.lastDNS = newDNS;
self.emit('newDNS', newDNS);
self.updateRoute53IPs();
});
};
Updater.prototype.updateRoute53IPs = function () {
var self = this;
var resourceRecords = [];
this.lastDNS.forEach(function (val) {
resourceRecords.push({
'Value': val
});
});
var params = {
'HostedZoneId': '/hostedzone/'+this.aws.zoneid,
'ChangeBatch': {
'Changes': [
{
'Action': 'UPSERT',
'ResourceRecordSet': {
'Name': this.aws.name,
'Type': 'A',
'TTL': 300,
'ResourceRecords': resourceRecords
}
}
]
}
};
this.route53.changeResourceRecordSets(params, function(err,data) {
if(self.listeners('error').length && err) {
self.emit('error', err);
} else if (err) {
throw err;
} else if (data) {
self.emit('aws-response', data);
}
});
};
Updater.prototype.start = function () {
var self = this;
// Look Heroku IP every secound
new CronJob('* * * * * *', function(){
self.lookForHerokuIPs();
}, null, true);
};
if (!module.parent) {
var route53heroku = new Updater({
aws: {
id: process.env.AWS_ID,
key: process.env.AWS_KEY,
region: process.env.AWS_REGION,
zoneid: process.env.AWS_53_ZONEID,
name: process.env.AWS_53_DOMAIN
},
heroku: {
app: process.env.HEROKU_DOMAIN
}
});
route53heroku.on('newDNS', function (newDNS) {
console.log(newDNS);
})
route53heroku.start();
}