forked from froooze/DEXBot2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpm2.js
More file actions
652 lines (584 loc) · 24.1 KB
/
pm2.js
File metadata and controls
652 lines (584 loc) · 24.1 KB
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
#!/usr/bin/env node
/**
* pm2.js - PM2 Orchestration Launcher
*
* Unified PM2 launcher for DEXBot2 multi-bot system.
* One-command startup with all setup required before starting bots.
* Handles process management, configuration generation, and daemon startup.
*
* ===============================================================================
* COMMANDS
* ===============================================================================
*
* node pm2.js - Default: unlock keystore and start all bots
* node pm2.js unlock-start - Explicit: unlock keystore and start all bots
* node pm2.js unlock-start <bot> - Unlock keystore and start specific bot
* node pm2.js update - Run the update script immediately
* node pm2.js stop all - Stop all dexbot PM2 processes
* node pm2.js stop <bot-name> - Stop specific bot process
* node pm2.js delete all - Delete all dexbot processes from PM2
* node pm2.js delete <bot-name> - Delete specific bot from PM2
* node pm2.js help - Show help message
*
* ===============================================================================
* SETUP SEQUENCE
* ===============================================================================
*
* Step 0: BITSHARES CONNECTION VERIFICATION
* - Waits for BitShares blockchain network connection
* - Suppresses debug output to keep terminal clean
* - Validates node availability before proceeding
*
* Step 1: PM2 INSTALLATION CHECK
* - Detects local and global PM2 installations
* - Prompts to install PM2 if missing
* - Validates PM2 is available before proceeding
*
* Step 2: ECOSYSTEM CONFIGURATION GENERATION
* - Reads bot definitions from profiles/bots.json
* - Generates profiles/ecosystem.config.js with absolute paths
* - Filters only active bots (active !== false)
* - If bot-name provided, filters to only that bot
* - Each bot configured with:
* * Unique app name
* * Log file paths
* * Restart/memory policies
*
* Step 3: AUTHENTICATION & CLEANUP
* - Cleans up stale daemon socket files
* - Prompts interactively for master password (once at startup)
* - Authenticates against profiles/keys.json
* - Sets password in process.env.DAEMON_PASSWORD for PM2 child processes
*
* Step 4: PM2 DAEMON STARTUP
* - Starts PM2 daemon if not already running
* - Waits for PM2 to be ready
* - Starts each configured bot as PM2 app
* - Credential daemon is started as a PM2 app (defined in ecosystem config)
* - Bots request private keys from credential daemon via Unix socket
*/
const fs = require('fs');
const path = require('path');
const { spawn, exec, execSync } = require('child_process');
const { promisify } = require('util');
const { parseJsonWithComments } = require('./modules/account_bots');
const { readBotsFileSync, readBotsFileWithLock } = require('./modules/bots_file_lock');
const { setupGracefulShutdown } = require('./modules/graceful_shutdown');
const { UPDATER, TIMING } = require('./modules/constants');
// Setup graceful shutdown handlers
setupGracefulShutdown();
const execAsync = promisify(exec);
const ROOT = __dirname;
const PROFILES_DIR = path.join(ROOT, 'profiles');
const BOTS_JSON = path.join(PROFILES_DIR, 'bots.json');
const ECOSYSTEM_FILE = path.join(PROFILES_DIR, 'ecosystem.config.js');
const LOGS_DIR = path.join(PROFILES_DIR, 'logs');
function usesAmaGridPrice(bot) {
const gridPrice = typeof bot?.gridPrice === 'string' ? bot.gridPrice.trim().toLowerCase() : '';
return /^ama(?:[1-4])?$/.test(gridPrice);
}
function isServiceApp(app) {
const name = String(app?.name || '');
return name === 'dexbot-update' || name === 'dexbot-cred' || name === 'dexbot-price-adapter';
}
function countManagedBots(apps) {
return (apps || []).filter((app) => !isServiceApp(app)).length;
}
/**
* Generate ecosystem.config.js from bots.json.
* @param {string|null} [botNameFilter=null] - Optional bot name to filter by.
* @returns {Array<Object>} The generated app configurations.
* @throws {Error} If configuration loading fails.
*/
function generateEcosystemConfig(botNameFilter = null) {
if (!fs.existsSync(BOTS_JSON)) {
console.error('profiles/bots.json not found. Run: dexbot bots');
process.exit(1);
}
// Ensure logs directory exists
if (!fs.existsSync(LOGS_DIR)) {
fs.mkdirSync(LOGS_DIR, { recursive: true });
}
try {
const { config } = readBotsFileSync(BOTS_JSON, parseJsonWithComments);
let bots = (config.bots || []).filter(b => b.active !== false);
// Filter to specific bot if name provided
if (botNameFilter) {
bots = bots.filter(b => b.name === botNameFilter);
if (bots.length === 0) {
console.error(`Bot '${botNameFilter}' not found or not active in profiles/bots.json`);
process.exit(1);
}
} else if (bots.length === 0) {
console.error('No active bots found in profiles/bots.json');
process.exit(1);
}
const apps = bots.map((bot, index) => {
const botName = bot.name || `bot-${index}`;
return {
name: botName,
script: path.join(ROOT, 'bot.js'),
args: botName,
cwd: ROOT,
max_memory_restart: '250M',
watch: false,
autorestart: true,
error_file: path.join(LOGS_DIR, `${botName}-error.log`),
out_file: path.join(LOGS_DIR, `${botName}.log`),
log_date_format: 'YY-MM-DD HH:mm:ss.SSS',
merge_logs: false,
combine_logs: true,
max_restarts: 13,
min_uptime: 86400000,
restart_delay: 3000
};
});
const needsPriceAdapter = bots.some((bot) => usesAmaGridPrice(bot));
if (needsPriceAdapter) {
apps.unshift({
name: 'dexbot-price-adapter',
script: path.join(ROOT, 'market_adapter', 'price_adapter.js'),
cwd: ROOT,
watch: false,
autorestart: true,
max_memory_restart: '150M',
error_file: path.join(LOGS_DIR, 'dexbot-price-adapter-error.log'),
out_file: path.join(LOGS_DIR, 'dexbot-price-adapter.log'),
log_date_format: 'YY-MM-DD HH:mm:ss.SSS',
merge_logs: false,
combine_logs: true,
max_restarts: 13,
min_uptime: 60000,
restart_delay: 3000
});
}
// Add credential daemon as a managed service
apps.unshift({
name: 'dexbot-cred',
script: path.join(ROOT, 'credential-daemon.js'),
cwd: ROOT,
autorestart: true,
max_memory_restart: '100M',
error_file: path.join(LOGS_DIR, 'dexbot-cred-error.log'),
out_file: path.join(LOGS_DIR, 'dexbot-cred.log'),
log_date_format: 'YY-MM-DD HH:mm:ss.SSS'
});
// Add weekly updater (if active and not filtering for a specific bot)
if (!botNameFilter && UPDATER.ACTIVE) {
apps.push({
name: "dexbot-update",
script: path.join(ROOT, 'scripts', 'update.js'),
cwd: ROOT,
autorestart: false,
cron_restart: UPDATER.SCHEDULE,
error_file: path.join(LOGS_DIR, `dexbot-update-error.log`),
out_file: path.join(LOGS_DIR, `dexbot-update.log`),
log_date_format: "YY-MM-DD HH:mm:ss.SSS"
});
}
const ecosystemContent = `// Auto-generated by pm2.js - DO NOT EDIT
// Regenerate with: node pm2.js or node dexbot.js pm2
module.exports = { apps: ${JSON.stringify(apps, null, 2)} };
`;
fs.writeFileSync(ECOSYSTEM_FILE, ecosystemContent);
console.log(`Ecosystem configuration generated`);
return apps;
} catch (err) {
console.error('Error reading bots.json:', err.message);
process.exit(1);
}
}
/**
* Main application entry point for PM2 orchestration.
* @param {string|null} [botNameFilter=null] - Optional bot name to start.
* @returns {Promise<void>}
*/
async function main(botNameFilter = null) {
console.log('='.repeat(50));
console.log('DEXBot2 PM2 Launcher');
if (botNameFilter) {
console.log(`Starting bot: ${botNameFilter}`);
}
console.log('='.repeat(50));
console.log();
// Step 0: Wait for BitShares connection (suppress BitShares client logs)
const { waitForConnected } = require('./modules/bitshares_client');
const chainKeys = require('./modules/chain_keys');
console.log('Connecting to BitShares...');
// Suppress BitShares console output during connection
const originalLog = console.log;
try {
console.log = (...args) => {
// Only suppress BitShares-specific messages
const msg = args.join(' ');
if (!msg.includes('bitshares_client') && !msg.includes('modules/')) {
originalLog(...args);
}
};
await waitForConnected(TIMING.CONNECTION_TIMEOUT_MS);
} finally {
// Always restore console output, even if waitForConnected throws
console.log = originalLog;
}
console.log('Connected to BitShares');
console.log();
// Step 1: Check PM2
if (!checkPM2Installed()) {
console.error('PM2 is not installed');
await installPM2();
}
// Step 2: Generate ecosystem config
const apps = generateEcosystemConfig(botNameFilter);
const botCount = countManagedBots(apps);
console.log(`Number active bots: ${botCount}`);
console.log();
// Step 3: Clean up and authenticate
// Clean up any stale daemon socket files
try {
try { fs.unlinkSync('/tmp/dexbot-cred-daemon.sock'); } catch (e) { }
try { fs.unlinkSync('/tmp/dexbot-cred-daemon.ready'); } catch (e) { }
} catch (e) {
// Socket files already cleaned, that's fine
}
// Authenticate password and set in environment for PM2 apps
console.log('Authenticating master password...');
try {
const masterPassword = await chainKeys.authenticate();
process.env.DAEMON_PASSWORD = masterPassword;
} catch (error) {
console.error('\n❌', error.message);
process.exit(1);
}
console.log('✓ Authentication successful\n');
// Step 4: Start PM2
console.log('Starting PM2 with all services...');
await startPM2();
console.log();
console.log('='.repeat(50));
console.log('DEXBot2 started successfully!');
console.log('='.repeat(50));
console.log();
}
/**
* Start PM2 with provided configuration.
* @returns {Promise<void>}
*/
function startPM2() {
return new Promise((resolve, reject) => {
// Use 'pm2 start' to handle both cases:
// 1. First run (processes don't exist yet) - creates new processes
// 2. Subsequent runs (processes exist) - restarts existing processes gracefully
// This prevents duplicate processes while supporting both fresh start and restart scenarios
const pm2 = spawn('pm2', ['start', ECOSYSTEM_FILE], {
cwd: ROOT,
env: process.env,
stdio: 'inherit',
detached: false,
shell: process.platform === 'win32'
});
pm2.on('close', code => {
if (code === 0) {
// Ensure we disconnect from PM2's file descriptors
setImmediate(resolve);
} else {
reject(new Error(`PM2 exited with code ${code}`));
}
});
pm2.on('error', reject);
});
}
/**
* Check if PM2 is installed locally or globally.
* @returns {boolean} True if PM2 is found.
*/
function checkPM2Installed() {
try {
require.resolve('pm2');
return true;
} catch (e) {
// Check if pm2 is available in PATH
const { execSync } = require('child_process');
try {
execSync('pm2 --version', { stdio: 'ignore' });
return true;
} catch (err) {
return false;
}
}
}
/**
* Prompt the user to install PM2 globally.
* @returns {Promise<void>}
*/
async function installPM2() {
const readline = require('readline');
const { spawn } = require('child_process');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
return new Promise((resolve, reject) => {
rl.question('PM2 is not installed. Install now? (Y/n): ', (answer) => {
rl.close();
if (answer.toLowerCase() === 'n') {
console.log('PM2 installation cancelled. Run: npm install -g pm2');
process.exit(1);
}
console.log('Installing PM2...');
// Helper to run installation command
const runInstall = (command, args) => {
return new Promise((res, rej) => {
const proc = spawn(command, args, {
stdio: 'inherit',
shell: process.platform === 'win32'
});
proc.on('close', (code) => {
if (code === 0) res();
else rej(code);
});
proc.on('error', (err) => rej(err));
});
};
// Try standard install first
runInstall('npm', ['install', '-g', 'pm2'])
.then(() => {
console.log('PM2 installed successfully!');
resolve();
})
.catch((err) => {
// If failed and not on Windows, try sudo
if (process.platform !== 'win32') {
console.log('\nStandard installation failed (likely permissions). Trying with sudo...');
console.log('This covers Linux and macOS. Please enter your password if prompted:');
runInstall('sudo', ['npm', 'install', '-g', 'pm2'])
.then(() => {
console.log('PM2 installed successfully with sudo!');
resolve();
})
.catch((finalErr) => {
reject(new Error('PM2 installation failed even with sudo'));
});
} else {
// Windows handling
console.log('\nStandard installation failed (likely permissions).');
console.log('Attempting to install with Administrator privileges...');
console.log('Please accept the UAC dialog to proceed.');
// Run npm install in a new elevated window
// We use timeout so the user can see the result before window closes
const psCommand = "Start-Process cmd -ArgumentList '/c npm install -g pm2 & echo. & echo Installation complete. Closing in 5 seconds... & timeout /t 5' -Verb RunAs -Wait";
runInstall('powershell', ['-Command', psCommand])
.then(() => {
// Verify installation succeeded since we can't easily get the exit code from the elevated process
if (checkPM2Installed()) {
console.log('PM2 installed successfully (Elevated)!');
resolve();
} else {
reject(new Error('PM2 installation failed or was cancelled in the elevated window.'));
}
})
.catch((winErr) => {
console.error('\nFailed to elevate permissions.');
console.error('Please manually run "npm install -g pm2" as Administrator.');
reject(new Error('PM2 installation failed.'));
});
}
});
});
});
}
/**
* Execute a PM2 command safely.
* @param {string} action - PM2 action (start, stop, etc.).
* @param {string} [target] - The target process name or configuration file.
* @returns {Promise<Object>} Command result.
* @throws {Error} If action is invalid or command fails.
*/
async function execPM2Command(action, target) {
// Validate action to prevent injection
const validActions = ['start', 'stop', 'delete', 'restart', 'reload'];
if (!validActions.includes(action)) {
throw new Error(`Invalid PM2 action: ${action}`);
}
// Use spawn instead of shell to avoid injection vulnerabilities
// spawn passes arguments as array, preventing shell interpretation
return new Promise((resolve, reject) => {
const args = [action];
if (target) {
args.push(target);
}
const { spawn } = require('child_process');
const pm2 = spawn('pm2', args, {
stdio: 'pipe',
shell: process.platform === 'win32'
});
let stdout = '';
let stderr = '';
pm2.stdout.on('data', (data) => {
stdout += data.toString();
});
pm2.stderr.on('data', (data) => {
stderr += data.toString();
});
pm2.on('close', (code) => {
if (code === 0) {
if (stdout) console.log(stdout);
resolve({ success: true, stdout, stderr });
} else {
if (stderr) console.error(stderr);
reject(new Error(`PM2 command failed with code ${code}: ${stderr || stdout}`));
}
});
pm2.on('error', reject);
});
}
/**
* Stop PM2 processes based on target.
* @param {string} target - 'all' or specific bot name.
* @returns {Promise<void>}
* @throws {Error} If target not found or stopping fails.
*/
async function stopPM2Processes(target) {
// Ensure ecosystem config exists (validates bot configuration)
if (!fs.existsSync(BOTS_JSON)) {
throw new Error('profiles/bots.json not found. Run: npm run bootstrap:profiles');
}
console.log(`Stopping PM2 processes: ${target}`);
if (target === 'all') {
// Regenerate ecosystem config to ensure it's current
generateEcosystemConfig();
// Stop all dexbot processes via ecosystem config
await execPM2Command('stop', ECOSYSTEM_FILE);
console.log('All dexbot PM2 processes stopped.');
} else {
// Validate bot exists in configuration before stopping (with lock protection)
try {
const { config } = await readBotsFileWithLock(BOTS_JSON, parseJsonWithComments);
const botExists = config.bots && config.bots.some(b => b.name === target && b.active !== false);
if (!botExists) {
throw new Error(`Bot '${target}' not found or not active in profiles/bots.json`);
}
} catch (err) {
throw new Error(`Failed to read bots configuration: ${err.message}`);
}
// Stop specific bot by name
await execPM2Command('stop', target);
console.log(`PM2 process '${target}' stopped.`);
}
}
/**
* Delete PM2 processes based on target.
* @param {string} target - 'all' or specific bot name.
* @returns {Promise<void>}
* @throws {Error} If target not found or deleting fails.
*/
async function deletePM2Processes(target) {
// Ensure ecosystem config exists (validates bot configuration)
if (!fs.existsSync(BOTS_JSON)) {
throw new Error('profiles/bots.json not found. Run: npm run bootstrap:profiles');
}
console.log(`Deleting PM2 processes: ${target}`);
if (target === 'all') {
// Regenerate ecosystem config to ensure it's current
generateEcosystemConfig();
// Delete all dexbot processes via ecosystem config
await execPM2Command('delete', ECOSYSTEM_FILE);
console.log('All dexbot PM2 processes deleted.');
} else {
// Validate bot exists in configuration before deleting (with lock protection)
try {
const { config } = await readBotsFileWithLock(BOTS_JSON, parseJsonWithComments);
const botExists = config.bots && config.bots.some(b => b.name === target && b.active !== false);
if (!botExists) {
throw new Error(`Bot '${target}' not found or not active in profiles/bots.json`);
}
} catch (err) {
throw new Error(`Failed to read bots configuration: ${err.message}`);
}
// Delete specific bot by name
await execPM2Command('delete', target);
console.log(`PM2 process '${target}' deleted.`);
}
console.log('Bot configs remain in profiles/bots.json.');
console.log('Run "node dexbot.js bots" to manage bot configurations.');
}
/**
* Show help text for PM2 CLI usage.
*/
function showPM2Help() {
console.log(`
Usage: node pm2.js <command> [target]
Commands:
unlock-start Unlock keystore and start all bots with PM2 (default)
update Run the update script immediately
stop <bot-name|all> Stop PM2 process(es) - only dexbot processes
delete <bot-name|all> Delete PM2 process(es) - only dexbot processes
help Show this help message
Examples:
node pm2.js # Start all bots (unlock + start)
node pm2.js stop all # Stop all dexbot processes
node pm2.js stop XRP-BTS # Stop specific bot
node pm2.js delete all # Delete all dexbot processes from PM2
node pm2.js delete XRP-BTS # Delete specific bot from PM2
node pm2.js help # Show help
`);
}
// Run if called directly
if (require.main === module) {
// Parse command line arguments
const args = process.argv.slice(2);
const command = args[0] || 'unlock-start';
const target = args[1];
(async () => {
try {
if (command === 'unlock-start') {
// Full setup: unlock, generate config, authenticate, start PM2
// Optional: filter to specific bot if provided
await main(target || null);
// Close stdin to prevent hanging
if (process.stdin) process.stdin.destroy();
process.exit(0);
} else if (command === 'update') {
const { spawn } = require('child_process');
const update = spawn('node', [path.join(ROOT, 'scripts', 'update.js')], { stdio: 'inherit' });
update.on('close', code => process.exit(code));
} else if (command === 'stop') {
if (!target) {
console.error('Error: Target required. Specify bot name or "all".');
showPM2Help();
process.exit(1);
}
try {
await stopPM2Processes(target);
process.exit(0);
} catch (err) {
console.error(`Failed to stop processes: ${err.message}`);
process.exit(1);
}
} else if (command === 'delete') {
if (!target) {
console.error('Error: Target required. Specify bot name or "all".');
showPM2Help();
process.exit(1);
}
try {
await deletePM2Processes(target);
process.exit(0);
} catch (err) {
console.error(`Failed to delete processes: ${err.message}`);
process.exit(1);
}
} else if (command === 'help') {
showPM2Help();
process.exit(0);
} else {
console.error(`Unknown command: ${command}`);
showPM2Help();
process.exit(1);
}
} catch (err) {
console.error('Error:', err.message);
process.exit(1);
}
})();
}
module.exports = { main, generateEcosystemConfig, countManagedBots, isServiceApp };