-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Daemon.js
443 lines (385 loc) · 12.3 KB
/
Daemon.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/**
* Copyright 2013 the PM2 project authors. All rights reserved.
* Use of this source code is governed by a license that
* can be found in the LICENSE file.
*/
var fs = require('fs');
var ipm2 = require('./pm2-interface.js');
var rpc = require('pm2-axon-rpc');
var axon = require('pm2-axon');
var debug = require('debug')('interface:driver'); // Interface
var chalk = require('chalk');
var Url = require('url');
var os = require('os');
var domain = require('domain');
var fmt = require('../tools/fmt.js');
var pkg = require('../../package.json');
var PM2 = require('../..');
var cst = require('../../constants.js');
var Cipher = require('./Cipher.js');
var ReverseInteractor = require('./ReverseInteractor.js');
var PushInteractor = require('./PushInteractor.js');
var Utility = require('../Utility.js');
var WatchDog = require('./WatchDog.js');
var Conf = require('../Configuration.js');
var HttpRequest = require('./HttpRequest.js');
var InternalIP = require('./internal-ip.js');
global._pm2_password_protected = false;
// Flag for log streaming status
global._logs = false;
var Daemon = module.exports = {
connectToPM2 : function() {
return ipm2();
},
exit : function() {
var self = this;
this.opts.pm2_instance.disconnect(function() {
console.log('Connection to PM2 via CLI closed');
});
process.nextTick(function() {
try {
fs.unlinkSync(cst.INTERACTOR_RPC_PORT);
fs.unlinkSync(cst.INTERACTOR_PID_PATH);
} catch(e) {}
if (self.opts.ipm2)
self.opts.ipm2.disconnect();
console.log('Exiting Interactor');
if (!this._rpc || !this._rpc.sock)
return process.exit(cst.ERROR_EXIT);
this._rpc.sock.close(function() {
console.log('RPC closed - Interactor killed');
process.exit(cst.SUCCESS_EXIT);
});
});
},
activateRPC : function() {
console.log('Launching Interactor exposure');
var self = this;
var rep = axon.socket('rep');
var daemon_server = new rpc.Server(rep);
var sock = rep.bind(cst.INTERACTOR_RPC_PORT);
daemon_server.expose({
kill : function(cb) {
console.log('Killing interactor');
cb(null);
return Daemon.exit();
},
passwordSet : function(cb) {
global._pm2_password_protected = true;
return cb(null);
},
getInfos : function(cb) {
if (self.opts &&
self.opts.DAEMON_ACTIVE == true)
return cb(null, {
machine_name : self.opts.MACHINE_NAME,
public_key : self.opts.PUBLIC_KEY,
secret_key : self.opts.SECRET_KEY,
remote_host : cst.REMOTE_HOST,
remote_port : cst.REMOTE_PORT,
reverse_interaction : self.opts.REVERSE_INTERACT,
socket_path : cst.INTERACTOR_RPC_PORT,
pm2_home_monitored : cst.PM2_HOME
});
else {
return cb(null);
}
}
});
return daemon_server;
},
formatMetada : function() {
var cpu, memory;
var self = this;
try {
cpu = os.cpus();
memory = Math.floor(os.totalmem() / 1024 / 1024);
} catch(e) {
cpu = 0;
memory = 0;
};
var ciphered_data = Cipher.cipherMessage(JSON.stringify({
MACHINE_NAME : this.opts.MACHINE_NAME,
PUBLIC_KEY : this.opts.PUBLIC_KEY,
PM2_VERSION : this.opts.PM2_VERSION,
RECYCLE : this.opts.RECYCLE || false,
MEMORY : memory,
HOSTNAME : os.hostname(),
CPUS : cpu.length
}), this.opts.SECRET_KEY);
return ciphered_data;
},
pingKeepAlive : function() {
var self = this;
(function checkInternet() {
require('dns').lookup('google.com',function(err) {
if (err && (err.code == 'ENOTFOUND' || err.code == 'EAI_AGAIN')) {
if (self.opts._connection_is_up == true)
console.error('[CRITICAL] Internet is unreachable (via DNS lookup strategy)');
self.opts._connection_is_up = false;
} else {
if (self.opts._connection_is_up == false) {
console.log('[TENTATIVE] Reactivating connection');
PushInteractor.connectRemote();
ReverseInteractor.reconnect();
}
self.opts._connection_is_up = true;
}
setTimeout(checkInternet, 15000);
});
})();
},
changeUrls : function(push_url, reverse) {
if (push_url)
PushInteractor.connectRemote(push_url);
if (reverse)
ReverseInteractor.changeUrl(reverse);
},
refreshWorker : function() {
var self = this;
function refreshMetadata() {
var ciphered_data = Daemon.formatMetada();
HttpRequest.post({
url : self.opts.ROOT_URL,
port : self.opts.ROOT_PORT,
data : {
public_id : self.opts.PUBLIC_KEY,
data : ciphered_data
}
}, function(err, km_data) {
if (err) return console.error(err);
/** protect against malformated data **/
if (!km_data ||
!km_data.endpoints ||
!km_data.endpoints.push ||
!km_data.endpoints.reverse) {
console.error('[CRITICAL] Malformated data received, skipping...');
return false;
}
/**************************************
* Urls has changed = update workers *
**************************************/
if ((Daemon.current_km_data.endpoints.push != km_data.endpoints.push) ||
(Daemon.current_km_data.endpoints.reverse != km_data.endpoints.reverse)) {
self.changeUrls(km_data.endpoints.push, km_data.endpoints.reverse);
Daemon.current_km_data = km_data;
}
else {
debug('[REFRESH META] No need to update URL (same)', km_data);
}
return false;
});
};
// Refresh metadata every minutes
setInterval(function() {
refreshMetadata();
}, 60000);
},
validateData : function() {
var opts = {};
opts.MACHINE_NAME = process.env.PM2_MACHINE_NAME;
opts.PUBLIC_KEY = process.env.PM2_PUBLIC_KEY;
opts.SECRET_KEY = process.env.PM2_SECRET_KEY;
opts.RECYCLE = process.env.KM_RECYCLE ? JSON.parse(process.env.KM_RECYCLE) : false;
opts.REVERSE_INTERACT = JSON.parse(process.env.PM2_REVERSE_INTERACT);
opts.PM2_VERSION = pkg.version;
if (!opts.MACHINE_NAME) {
console.error('You must provide a PM2_MACHINE_NAME environment variable');
process.exit(cst.ERROR_EXIT);
}
else if (!opts.PUBLIC_KEY) {
console.error('You must provide a PM2_PUBLIC_KEY environment variable');
process.exit(cst.ERROR_EXIT);
}
else if (!opts.SECRET_KEY) {
console.error('You must provide a PM2_SECRET_KEY environment variable');
process.exit(cst.ERROR_EXIT);
}
return opts;
},
welcome : function(cb) {
var self = this;
var ciphered_data = Daemon.formatMetada();
if (!ciphered_data) {
process.send({
msg : 'Error while ciphering data',
error : true
});
return process.exit(1);
}
var retries = 0;
function doWelcomeQuery(cb) {
HttpRequest.post({
url : self.opts.ROOT_URL,
data : {
public_id : self.opts.PUBLIC_KEY,
data : ciphered_data
}
}, function(err, km_data) {
self.current_km_data = km_data;
if (err) {
console.error('Got error while connecting: %s', err.message || err);
if (retries < 30) {
retries++;
setTimeout(function() {
doWelcomeQuery(cb);
}, 200 * retries);
return false;
}
return cb(err);
}
if (self.opts.RECYCLE) {
if (!km_data.name) {
console.error('Error no previous machine name for recycle option returned!');
}
self.opts.MACHINE_NAME = km_data.name;
};
// For Human feedback
if (process.send) {
try {
process.send({
error : false,
km_data : km_data,
online : true,
pid : process.pid,
machine_name : self.opts.MACHINE_NAME,
public_key : self.opts.PUBLIC_KEY,
secret_key : self.opts.SECRET_KEY,
reverse_interaction : self.opts.REVERSE_INTERACT
});
} catch(e) {
// Just in case the CLI has been disconected
}
}
// Return get data
return cb(null, km_data);
})
}
doWelcomeQuery(function(err, meta) {
return cb(err, meta);
});
},
protectedStart : function() {
var self = this;
var d = domain.create();
d.once('error', function(err) {
fmt.sep();
fmt.title('Agent global error caught');
fmt.field('Time', new Date());
console.error(err.message);
console.error(err.stack);
fmt.sep();
console.error('[Agent] Resurrecting');
var KMDaemon = require('../Interactor/InteractorDaemonizer');
KMDaemon.rescueStart(cst, function(err, dt) {
if (err) {
console.error('[Agent] Failed to rescue agent, error:');
console.error(err.message || err);
process.exit(1);
}
console.log('[Agent] Rescued.');
process.exit(0);
});
});
d.run(function() {
self.start();
});
},
start : function() {
var self = this;
self.opts = self.validateData();
self.opts.ipm2 = null;
self.opts.internal_ip = InternalIP();
self.opts.pm2_instance = PM2;
self.opts._connection_is_up = true;
self.current_km_data = null;
self.opts.pm2_instance.connect(function() {
console.log('Connected to PM2');
});
self._rpc = self.activateRPC();
// Test mode #1
if (cst.DEBUG) {
self.opts.ROOT_URL = '127.0.0.1';
if (process.env.NODE_ENV == 'test')
self.opts.ROOT_PORT = 3400;
else
self.opts.ROOT_PORT = 3000;
}
else {
self.opts.ROOT_URL = cst.KEYMETRICS_ROOT_URL;
}
if (Conf.getSync('pm2:passwd'))
global._pm2_password_protected = true;
// Test mode #2
if (process.env.NODE_ENV == 'local_test') {
self.opts.DAEMON_ACTIVE = true;
self.opts.ipm2 = self.connectToPM2();
PushInteractor.start({
url : 'http://127.0.0.1:4321',
conf : self.opts
});
ReverseInteractor.start({
url : 'http://127.0.0.1:4322',
conf : self.opts
});
if (process.send)
process.send({
success : true,
debug : true
});
return false;
}
Daemon.welcome(function(err, km_data) {
if (err) {
if (process.send)
process.send({
error : true,
msg : err.stack || err
});
console.log(err.stack || err);
return Daemon.exit();
}
if (km_data.disabled == true) {
console.error('Interactor disabled');
return Daemon.exit();
}
if (km_data.pending == true) {
console.error('Interactor pending');
return Daemon.exit();
}
if (km_data.active == true) {
self.opts.DAEMON_ACTIVE = true;
self.opts.ipm2 = self.connectToPM2();
WatchDog.start({
conf : self.opts
});
PushInteractor.start({
url : km_data.endpoints.push,
conf : self.opts
});
if (self.opts.REVERSE_INTERACT == true) {
ReverseInteractor.start({
url : km_data.endpoints.reverse,
conf : self.opts
});
}
Daemon.refreshWorker();
Daemon.pingKeepAlive();
}
else {
console.log('Nothing to do, exiting');
Daemon.exit();
}
return false;
});
}
};
/**
* MAIN
*/
if (require.main === module) {
console.log(chalk.cyan.bold('[Keymetrics.io]') + ' Launching agent');
process.title = 'PM2: KM Agent (' + process.env.PM2_HOME + ')';
Utility.overrideConsole();
Daemon.protectedStart();
}