-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
238 lines (223 loc) · 6.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
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
var Fleetctl = require('fleetctl');
var Etcd = require('node-etcd');
var fleetctl, etcd;
var async = require('async');
var _ = require('lodash');
var api = module.exports = {};
var ACTIVE_TIMEOUT = 1200000; // 20 minutes
var WAIT_TO_DESTROY = 120000; // 120 seconds (2 minutes)
api.deploy = function(unit, etcdKey, count, sidekick, endpoint, noEtcd) {
var opts = {
binary: process.env.FLEET_BINARY || '/usr/bin/fleetctl',
endpoint: process.env.FLEET_ENDPOINT || endpoint || 'http://172.17.42.1:4001'
}
console.log('fleetctl options', opts);
console.log('noEtcd:', noEtcd);
fleetctl = new Fleetctl(opts);
etcd = new Etcd(
process.env.ETCD_HOST || '172.17.42.1',
process.env.ETCD_PORT || 4001
);
if (sidekick) {
sidekick = unit + '-sk';
}
async.waterfall([
function(callback) {
return startThisService(unit, sidekick, count, callback);
},
function(callback) {
return waitForActive(unit, count, callback);
},
function(callback) {
if (noEtcd) {
console.log('not waiting for etcd');
return callback();
}
return waitForEtcd(unit, etcdKey, count, callback);
},
function(callback) {
console.log('Waiting for ', WAIT_TO_DESTROY, ' ms before destroying');
setTimeout(callback, WAIT_TO_DESTROY);
},
function(callback) {
return getUnitsToDestroy(unit, callback);
},
function(destroy, callback) {
return destroyUnits(destroy, callback);
}
//getAllUnits,
], function(err, success) {
if (err) {
console.error(err);
}
})
};
/*
1) Start new instances
2) Wait for new instances to all be active
3) Check etcd keys for all instances
4) Destroy old instances, but leave templates
*/
function getUnitsToDestroy(unitName, callback) {
var prefix = unitName.slice(0, unitName.lastIndexOf('-'));
fleetctl.list_units(function(err, units) {
if (err) {
return callback(err);
}
var destroy = units.filter(function(unit) {
if (unit.unit.indexOf(unitName) === -1 && unit.unit.indexOf(prefix) === 0) {
return true;
}
return false;
});
return callback(null, destroy);
});
}
function destroyUnits(units, callback) {
units = _.pluck(units, 'unit');
console.log('about to destroy old units', units);
fleetctl.destroy(units, function(err) {
if (err) {
console.log('error destroying old units');
return callback(err);
}
console.log('Old units successfully destroyed');
return callback();
});
}
// waits for all service to be active
function waitForActive(unit, count, callback) {
var units = getUnits(unit, count);
var allActive = false;
var startTime = new Date().getTime();
async.doWhilst(
function getStatus(callback) {
setTimeout(function() {
var countActive = 0;
fleetctl.list_units(function(err, fltUnits) {
if (err) {
console.log(err);
return callback();
}
units.forEach(function(unit) {
var item = _.find(fltUnits, function(u) {
return u.unit === unit+'.service';
})
if (item && item.active === 'active' && item.sub === 'running') {
countActive++;
}
else if (item) {
console.log('Service not active', item.unit, item.active, item.sub);
}
else {
console.log('Service not found?', item, unit, fltUnits);
}
});
console.log(countActive, 'services are active of expected ', count);
if (countActive === count) {
allActive = true;
}
return callback();
})
}, 1000);
},
function checkStatus() {
var now = new Date().getTime();
if (now - startTime > ACTIVE_TIMEOUT) {
return false;
}
return !allActive;
},
function() {
if (!allActive) {
return callback(new Error('Failed to start all units'));
}
else {
console.log('All services have been started');
return callback();
}
}
);
}
function waitForEtcd(unit, etcdKey, count, callback) {
var keys = getEtcdKeys(unit, etcdKey, count);
var allActive = false;
var startTime = new Date().getTime();
console.log('etcd keys to check:', keys);
async.doWhilst(
function getStatus(callback) {
setTimeout(function() {
async.each(keys, function(key, callback) {
console.log('checking: ', key);
etcd.get(key, function(err, data) {
if (err) {
console.log('data not ready for ', key);
return callback(err);
}
console.log('data exists for ', key);
return callback();
});
}, function(err) {
if (err) {
return callback();
}
allActive = true;
return callback();
});
}, 1000);
},
function checkStatus() {
var now = new Date().getTime();
if (now - startTime > ACTIVE_TIMEOUT) {
return false;
}
return !allActive;
},
function() {
if (!allActive) {
return callback(new Error('Failed to start all units'));
}
else {
console.log('All services have been started');
return callback();
}
}
);
}
function getEtcdKeys(unit, etcdKey, count) {
var units = _.chain(_.range(1, count+1))
.map(function(iter) {
return etcdKey + unit + '-' + iter;
})
.value();
return units;
}
function getUnits(unit, count, sidekick) {
var units = _.chain(_.range(1, count+1))
.map(function(iter) {
return unit + '@' + iter;
})
.value();
var sidekicks = _.chain(_.range(1, count+1))
.map(function(iter) {
return unit + '-sk@' + iter;
})
.value();
if (sidekick) {
units = units.concat(sidekicks);
}
return units;
}
function startThisService(unit, sidekick, count, callback) {
var units = getUnits(unit, count, sidekick);
console.log('Starting these units:', units);
fleetctl.start(units, callback);
}
// function getAllUnits(service, environment, callback) {
// fleetctl.list_units(function(err, units) {
// if (err) {
// return callback(err);
// }
// return callback(null, units);
// })
// }