-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
192 lines (172 loc) · 4.74 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
190
191
192
/*
* This is pretty ugly code, but it seems to work.
* Needs to be refactored, but priorities! -RHK
*/
var pkgcloud = require('pkgcloud');
var async = require('async');
var util = require('util');
var _ = require('underscore');
var config = {};
config.etcd = {
host: process.env.ETCD_PORT_4001_TCP_ADDR || process.env.ETCD || '172.17.42.1',
port: process.env.ETCD_PORT_4001_TCP_PORT || 4001
};
var Etcd = require('node-etcd');
var etcd = new Etcd(config.etcd.host, config.etcd.port);
var RAX_LBS = {};
var RAX_NODES = {};
var ETCD_DATA = {};
var rax = pkgcloud.loadbalancer.createClient({
region: process.env.REGION || process.env.OS_REGION_NAME || 'DFW',
provider: 'rackspace',
username: process.env.RAX_ACCOUNT || process.env.OS_USERNAME,
apiKey: process.env.RAX_API || process.env.OS_PASSWORD
});
getEtcd();
function getEtcd() {
etcd.get('/loadbalancers', {recursive: true}, function(err, results) {
if (err) {
console.log(err);
process.exit(1);
}
var lbs = results.node.nodes;
_.each(lbs, function(lb) {
var lbName = lb.key.split('/').pop();
ETCD_DATA[lbName] = [];
_.each(lb.nodes, function(node) {
var name = node.key.split('/').pop();
var address = node.value;
ETCD_DATA[lbName].push(address);
});
});
console.log('ETCD_DATA:', util.inspect(ETCD_DATA, {depth: null}));
rax.getLoadBalancers(function(err, lbs) {
if (err) {
console.log(err);
process.exit(1);
}
lbs = lbs.filter(function(lb) {
if (lb.name in ETCD_DATA) {
return lb;
}
});
_.each(lbs, function(lb) {
RAX_LBS[lb.name] = lb;
RAX_NODES[lb.name] = [];
});
getNodes();
});
});
}
function getNodes() {
async.each(Object.keys(RAX_LBS), function(lb_key, callback) {
var lb = RAX_LBS[lb_key];
rax.getNodes(lb, function(err, nodes) {
if (err) {
console.log(err);
process.exit(1);
}
console.log('LB',lb.name,'has',nodes.length,'nodes');
_.each(nodes, function(node) {
RAX_NODES[lb.name].push(node);
});
callback();
});
}, function(err) {
if (err) {
console.log(err);
process.exit(1);
}
nodeDiff();
});
}
var NODE_REM = {};
var NODE_ADD = {};
function nodeDiff() {
_.each(RAX_NODES, function(nodes, lbname) {
var etcdnodes = ETCD_DATA[lbname];
var rmNodes = nodes.filter(function(node) {
var nodestr = node.address+':'+node.port;
if (!_.contains(ETCD_DATA[lbname], nodestr)) {
console.log('Node:',nodestr,'is not in etcd. Adding to remove list');
return true;
}
});
if (rmNodes.length) {
NODE_REM[lbname] = rmNodes;
}
});
console.log('nodes to remove:',NODE_REM);
_.each(ETCD_DATA, function(nodes, lbname) {
var raxnodes = RAX_NODES[lbname];
var addNodes = nodes.filter(function(node) {
var found = false;
_.each(raxnodes, function(raxnode) {
var nodestr = raxnode.address+':'+raxnode.port;
if (node === nodestr) {
console.log('ETCD node',node,'already in load balancer');
found = true;
}
});
if (!found) {
return node;
}
});
addNodes = addNodes.map(function(node) {
var arr = node.split(':');
return {
condition: 'ENABLED',
type: 'PRIMARY',
address: arr[0],
port: arr[1]
};
});
if (addNodes.length) {
NODE_ADD[lbname] = addNodes;
}
});
console.log('nodes to add:',NODE_ADD);
addNodes();
}
function addNodes() {
async.each(Object.keys(NODE_ADD), function(lbname, callback) {
var nodes = NODE_ADD[lbname];
if (!nodes.length) {
return callback();
}
var lb = RAX_LBS[lbname];
console.log('Adding nodes to',lbname);
rax.addNodes(lb, nodes, callback);
}, function(err, results) {
if (err) {
if (err.failCode && err.failCode == 'Unprocessable Entity') {
console.log('LB not ready, retrying in 5 seconds');
return setTimeout(addNodes, 5000);
}
console.log(err);
}
console.log('Done adding nodes');
removeNodes();
});
}
function removeNodes() {
async.each(Object.keys(NODE_REM), function(lbname, callback) {
var nodes = NODE_REM[lbname];
if (!nodes.length) {
return callback();
}
var lb = RAX_LBS[lbname];
console.log('Removing nodes from',lbname);
rax.removeNodes(lb, nodes, callback);
}, function(err, results) {
if (err) {
if (err.failCode && err.failCode == 'Unprocessable Entity') {
console.log('LB not ready, retrying in 5 seconds');
return setTimeout(removeNodes, 5000);
}
console.log(err);
process.exit(1);
}
console.log('Done removing nodes');
});
}