@zappinginc/zm2 6.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/settings.local.json +8 -0
- package/.gitattributes +4 -0
- package/.mocharc.js +14 -0
- package/CHANGELOG.md +2416 -0
- package/CLAUDE.md +84 -0
- package/CONTRIBUTING.md +124 -0
- package/GNU-AGPL-3.0.txt +665 -0
- package/LICENSE +1 -0
- package/README.md +248 -0
- package/bin/zm2 +3 -0
- package/bin/zm2-dev +3 -0
- package/bin/zm2-docker +3 -0
- package/bin/zm2-runtime +3 -0
- package/bin/zm2-windows +3 -0
- package/bin/zm2.ps1 +3 -0
- package/bun.lock +421 -0
- package/constants.js +114 -0
- package/index.js +13 -0
- package/lib/API/Configuration.js +212 -0
- package/lib/API/Containerizer.js +335 -0
- package/lib/API/Dashboard.js +459 -0
- package/lib/API/Deploy.js +117 -0
- package/lib/API/Extra.js +775 -0
- package/lib/API/ExtraMgmt/Docker.js +30 -0
- package/lib/API/Log.js +315 -0
- package/lib/API/LogManagement.js +371 -0
- package/lib/API/Modules/LOCAL.js +122 -0
- package/lib/API/Modules/Modularizer.js +148 -0
- package/lib/API/Modules/NPM.js +445 -0
- package/lib/API/Modules/TAR.js +362 -0
- package/lib/API/Modules/flagExt.js +46 -0
- package/lib/API/Modules/index.js +120 -0
- package/lib/API/Monit.js +247 -0
- package/lib/API/Serve.js +343 -0
- package/lib/API/Startup.js +629 -0
- package/lib/API/UX/helpers.js +213 -0
- package/lib/API/UX/index.js +9 -0
- package/lib/API/UX/pm2-describe.js +193 -0
- package/lib/API/UX/pm2-ls-minimal.js +31 -0
- package/lib/API/UX/pm2-ls.js +483 -0
- package/lib/API/Version.js +382 -0
- package/lib/API/interpreter.json +12 -0
- package/lib/API/pm2-plus/PM2IO.js +372 -0
- package/lib/API/pm2-plus/auth-strategies/CliAuth.js +288 -0
- package/lib/API/pm2-plus/auth-strategies/WebAuth.js +187 -0
- package/lib/API/pm2-plus/helpers.js +97 -0
- package/lib/API/pm2-plus/link.js +126 -0
- package/lib/API/pm2-plus/pres/motd +16 -0
- package/lib/API/pm2-plus/pres/motd.update +26 -0
- package/lib/API/pm2-plus/pres/welcome +28 -0
- package/lib/API/pm2-plus/process-selector.js +52 -0
- package/lib/API/schema.json +379 -0
- package/lib/API.js +1931 -0
- package/lib/Client.js +776 -0
- package/lib/Common.js +911 -0
- package/lib/Configuration.js +304 -0
- package/lib/Daemon.js +456 -0
- package/lib/Event.js +37 -0
- package/lib/God/ActionMethods.js +909 -0
- package/lib/God/ClusterMode.js +97 -0
- package/lib/God/ForkMode.js +297 -0
- package/lib/God/Methods.js +265 -0
- package/lib/God/Reload.js +240 -0
- package/lib/God.js +632 -0
- package/lib/HttpInterface.js +76 -0
- package/lib/ProcessContainer.js +305 -0
- package/lib/ProcessContainerBun.js +360 -0
- package/lib/ProcessContainerFork.js +42 -0
- package/lib/ProcessContainerForkBun.js +33 -0
- package/lib/ProcessUtils.js +55 -0
- package/lib/TreeKill.js +118 -0
- package/lib/Utility.js +430 -0
- package/lib/VersionCheck.js +46 -0
- package/lib/Watcher.js +117 -0
- package/lib/Worker.js +169 -0
- package/lib/binaries/CLI.js +1041 -0
- package/lib/binaries/DevCLI.js +183 -0
- package/lib/binaries/Runtime.js +101 -0
- package/lib/binaries/Runtime4Docker.js +192 -0
- package/lib/completion.js +229 -0
- package/lib/completion.sh +40 -0
- package/lib/motd +36 -0
- package/lib/templates/Dockerfiles/Dockerfile-java.tpl +7 -0
- package/lib/templates/Dockerfiles/Dockerfile-nodejs.tpl +8 -0
- package/lib/templates/Dockerfiles/Dockerfile-ruby.tpl +7 -0
- package/lib/templates/ecosystem-es.tpl +24 -0
- package/lib/templates/ecosystem-simple-es.tpl +8 -0
- package/lib/templates/ecosystem-simple.tpl +6 -0
- package/lib/templates/ecosystem.tpl +22 -0
- package/lib/templates/init-scripts/launchd.tpl +35 -0
- package/lib/templates/init-scripts/openrc.tpl +52 -0
- package/lib/templates/init-scripts/pm2-init-amazon.sh +86 -0
- package/lib/templates/init-scripts/rcd-openbsd.tpl +41 -0
- package/lib/templates/init-scripts/rcd.tpl +44 -0
- package/lib/templates/init-scripts/smf.tpl +43 -0
- package/lib/templates/init-scripts/systemd-online.tpl +22 -0
- package/lib/templates/init-scripts/systemd.tpl +22 -0
- package/lib/templates/init-scripts/upstart.tpl +103 -0
- package/lib/templates/logrotate.d/pm2 +10 -0
- package/lib/templates/sample-apps/http-server/README.md +14 -0
- package/lib/templates/sample-apps/http-server/api.js +9 -0
- package/lib/templates/sample-apps/http-server/ecosystem.config.js +14 -0
- package/lib/templates/sample-apps/http-server/package.json +11 -0
- package/lib/templates/sample-apps/pm2-plus-metrics-actions/README.md +45 -0
- package/lib/templates/sample-apps/pm2-plus-metrics-actions/custom-metrics.js +66 -0
- package/lib/templates/sample-apps/pm2-plus-metrics-actions/ecosystem.config.js +12 -0
- package/lib/templates/sample-apps/pm2-plus-metrics-actions/package.json +11 -0
- package/lib/templates/sample-apps/python-app/README.md +4 -0
- package/lib/templates/sample-apps/python-app/echo.py +7 -0
- package/lib/templates/sample-apps/python-app/ecosystem.config.js +12 -0
- package/lib/templates/sample-apps/python-app/package.json +11 -0
- package/lib/tools/Config.js +248 -0
- package/lib/tools/IsAbsolute.js +20 -0
- package/lib/tools/copydirSync.js +101 -0
- package/lib/tools/deleteFolderRecursive.js +19 -0
- package/lib/tools/find-package-json.js +74 -0
- package/lib/tools/fmt.js +72 -0
- package/lib/tools/isbinaryfile.js +94 -0
- package/lib/tools/json5.js +752 -0
- package/lib/tools/open.js +63 -0
- package/lib/tools/passwd.js +58 -0
- package/lib/tools/promise.min.js +1 -0
- package/lib/tools/sexec.js +55 -0
- package/lib/tools/treeify.js +113 -0
- package/lib/tools/which.js +120 -0
- package/lib/tools/xdg-open +861 -0
- package/package.json +219 -0
- package/paths.js +93 -0
- package/pm2 +11 -0
- package/preinstall.js +24 -0
- package/run.sh +9 -0
- package/types/index.d.ts +722 -0
- package/types/tsconfig.json +14 -0
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
process.env.PM2_NO_INTERACTION = 'true';
|
|
5
|
+
// Do not print banner
|
|
6
|
+
process.env.PM2_DISCRETE_MODE = true;
|
|
7
|
+
|
|
8
|
+
var commander = require('commander');
|
|
9
|
+
|
|
10
|
+
var PM2 = require('../..');
|
|
11
|
+
var Log = require('../API/Log');
|
|
12
|
+
var cst = require('../../constants.js');
|
|
13
|
+
var pkg = require('../../package.json');
|
|
14
|
+
var chalk = require('ansis');
|
|
15
|
+
var path = require('path');
|
|
16
|
+
var fmt = require('../tools/fmt.js');
|
|
17
|
+
var exec = require('child_process').exec;
|
|
18
|
+
var os = require('os');
|
|
19
|
+
|
|
20
|
+
commander.version(pkg.version)
|
|
21
|
+
.description('zm2-dev monitor for any file changes and automatically restart it')
|
|
22
|
+
.option('--raw', 'raw log output')
|
|
23
|
+
.option('--timestamp', 'print timestamp')
|
|
24
|
+
.option('--node-args <node_args>', 'space delimited arguments to pass to node in cluster mode - e.g. --node-args="--debug=7001 --trace-deprecation"')
|
|
25
|
+
.option('--ignore [files]', 'files to ignore while watching')
|
|
26
|
+
.option('--post-exec [cmd]', 'execute extra command after change detected')
|
|
27
|
+
.option('--silent-exec', 'do not output result of post command', false)
|
|
28
|
+
.option('--test-mode', 'debug mode for test suit')
|
|
29
|
+
.option('--interpreter <interpreter>', 'the interpreter zm2 should use for executing app (bash, python...)')
|
|
30
|
+
.option('--env [name]', 'select env_[name] env variables in process config file')
|
|
31
|
+
.option('--auto-exit', 'exit if all processes are errored/stopped or 0 apps launched')
|
|
32
|
+
.usage('zm2-dev app.js');
|
|
33
|
+
|
|
34
|
+
var pm2 = new PM2.custom({
|
|
35
|
+
pm2_home : path.join(os.homedir ? os.homedir() : (process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE), '.zm2-dev')
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
pm2.connect(function() {
|
|
39
|
+
commander.parse(process.argv);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
function postExecCmd(command, cb) {
|
|
43
|
+
var exec_cmd = exec(command);
|
|
44
|
+
|
|
45
|
+
if (commander.silentExec !== true) {
|
|
46
|
+
exec_cmd.stdout.on('data', function(data) {
|
|
47
|
+
process.stdout.write(data);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
exec_cmd.stderr.on('data', function(data) {
|
|
51
|
+
process.stderr.write(data);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
exec_cmd.on('close', function done() {
|
|
56
|
+
if (cb) cb(null);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
exec_cmd.on('error', function (err) {
|
|
60
|
+
console.error(err.stack || err);
|
|
61
|
+
});
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
function run(cmd, opts) {
|
|
65
|
+
var timestamp = opts.timestamp;
|
|
66
|
+
|
|
67
|
+
opts.watch = true;
|
|
68
|
+
opts.autostart = true;
|
|
69
|
+
opts.autorestart = true;
|
|
70
|
+
opts.restart_delay = 1000
|
|
71
|
+
if (opts.autoExit)
|
|
72
|
+
autoExit();
|
|
73
|
+
|
|
74
|
+
if (opts.ignore) {
|
|
75
|
+
opts.ignore_watch = opts.ignore.split(',')
|
|
76
|
+
opts.ignore_watch.push('node_modules');
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (timestamp === true)
|
|
80
|
+
timestamp = 'YYYY-MM-DD-HH:mm:ss';
|
|
81
|
+
|
|
82
|
+
pm2.start(cmd, opts, function(err, procs) {
|
|
83
|
+
|
|
84
|
+
if (err) {
|
|
85
|
+
console.error(err);
|
|
86
|
+
pm2.destroy(function() {
|
|
87
|
+
process.exit(0);
|
|
88
|
+
});
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (opts.testMode) {
|
|
93
|
+
return pm2.disconnect(function() {
|
|
94
|
+
console.log('disconnected succesfully from zm2-dev')
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
fmt.title('ZM2 development mode');
|
|
99
|
+
fmt.field('Apps started', procs.map(function(p) { return p.pm2_env.name } ));
|
|
100
|
+
fmt.field('Processes started', chalk.bold(procs.length));
|
|
101
|
+
fmt.field('Watch and Restart', chalk.green('Enabled'));
|
|
102
|
+
fmt.field('Ignored folder', opts.ignore_watch || 'node_modules');
|
|
103
|
+
if (opts.postExec)
|
|
104
|
+
fmt.field('Post restart cmd', opts.postExec);
|
|
105
|
+
fmt.sep();
|
|
106
|
+
|
|
107
|
+
setTimeout(function() {
|
|
108
|
+
pm2.Client.launchBus(function(err, bus) {
|
|
109
|
+
bus.on('process:event', function(packet) {
|
|
110
|
+
if (packet.event == 'online') {
|
|
111
|
+
if (opts.postExec)
|
|
112
|
+
postExecCmd(opts.postExec);
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
}, 1000);
|
|
117
|
+
|
|
118
|
+
Log.devStream(pm2.Client, 'all', opts.raw, timestamp, false);
|
|
119
|
+
|
|
120
|
+
process.on('SIGINT', function() {
|
|
121
|
+
console.log('>>>>> [ZM2 DEV] Stopping current development session');
|
|
122
|
+
pm2.delete('all', function() {
|
|
123
|
+
pm2.destroy(function() {
|
|
124
|
+
process.exit(0);
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
commander.command('*')
|
|
133
|
+
.action(function(cmd, opts){
|
|
134
|
+
run(cmd, commander);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
commander.command('start <file|json_file>')
|
|
138
|
+
.description('start target config file/script in development mode')
|
|
139
|
+
.action(function(cmd, opts) {
|
|
140
|
+
run(cmd, commander);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
function exitPM2() {
|
|
144
|
+
if (pm2 && pm2.connected == true) {
|
|
145
|
+
console.log(chalk.green.bold('>>> Exiting PM2'));
|
|
146
|
+
pm2.kill(function() {
|
|
147
|
+
process.exit(0);
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
else
|
|
151
|
+
process.exit(0);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function autoExit(final) {
|
|
155
|
+
setTimeout(function() {
|
|
156
|
+
pm2.list(function(err, apps) {
|
|
157
|
+
if (err) console.error(err.stack || err);
|
|
158
|
+
|
|
159
|
+
var online_count = 0;
|
|
160
|
+
|
|
161
|
+
apps.forEach(function(app) {
|
|
162
|
+
if (app.pm2_env.status == cst.ONLINE_STATUS ||
|
|
163
|
+
app.pm2_env.status == cst.LAUNCHING_STATUS)
|
|
164
|
+
online_count++;
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
if (online_count == 0) {
|
|
168
|
+
console.log('0 application online, exiting');
|
|
169
|
+
if (final == true)
|
|
170
|
+
process.exit(1);
|
|
171
|
+
else
|
|
172
|
+
autoExit(true);
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
autoExit(false);
|
|
176
|
+
});
|
|
177
|
+
}, 3000);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (process.argv.length == 2) {
|
|
181
|
+
commander.outputHelp();
|
|
182
|
+
exitPM2();
|
|
183
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var commander = require('commander');
|
|
5
|
+
|
|
6
|
+
var PM2 = require('../..');
|
|
7
|
+
var Log = require('../../lib/API/Log');
|
|
8
|
+
var cst = require('../../constants.js');
|
|
9
|
+
var pkg = require('../../package.json');
|
|
10
|
+
var path = require('path');
|
|
11
|
+
|
|
12
|
+
var pm2;
|
|
13
|
+
|
|
14
|
+
// Do not print banner
|
|
15
|
+
process.env.PM2_DISCRETE_MODE = true;
|
|
16
|
+
|
|
17
|
+
commander.version(pkg.version)
|
|
18
|
+
.description('zm2-runtime is an automatic pmx injection that runs in simulated no-daemon environment')
|
|
19
|
+
.option('--auto-manage', 'keep application online after command exit')
|
|
20
|
+
.option('--fast-boot', 'boot app faster by keeping zm2 runtime online in background (effective at second exit/start)')
|
|
21
|
+
.option('--web [port]', 'launch process web api on [port] default to 9615')
|
|
22
|
+
.option('--secret [key]', 'ZM2 plus secret key')
|
|
23
|
+
.option('--public [key]', 'ZM2 plus public key')
|
|
24
|
+
.option('--machine-name [name]', 'ZM2 plus machine name')
|
|
25
|
+
.option('--env [name]', 'select env_[name] env variables in process config file')
|
|
26
|
+
.option('--watch', 'Watch and Restart')
|
|
27
|
+
.option('-i --instances <number>', 'launch [number] instances with load-balancer')
|
|
28
|
+
.usage('zm2-runtime app.js');
|
|
29
|
+
|
|
30
|
+
commander.command('*')
|
|
31
|
+
.action(function(cmd){
|
|
32
|
+
pm2 = new PM2.custom({
|
|
33
|
+
pm2_home : path.join(process.env.HOME, '.pm3'),
|
|
34
|
+
secret_key : cst.SECRET_KEY || commander.secret,
|
|
35
|
+
public_key : cst.PUBLIC_KEY || commander.public,
|
|
36
|
+
machine_name : cst.MACHINE_NAME || commander.machineName
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
pm2.connect(function() {
|
|
40
|
+
if (commander.web) {
|
|
41
|
+
var port = commander.web === true ? cst.WEB_PORT : commander.web;
|
|
42
|
+
pm2.web(port);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
pm2.start(cmd, commander, function(err, obj) {
|
|
46
|
+
if (process.env.PM2_RUNTIME_DEBUG) {
|
|
47
|
+
return pm2.disconnect(function() {});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (err) {
|
|
51
|
+
console.error(err);
|
|
52
|
+
return process.exit(1);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
var pm_id = obj[0].pm2_env.pm_id;
|
|
56
|
+
|
|
57
|
+
if (commander.instances == undefined) {
|
|
58
|
+
return pm2.attach(pm_id, function() {
|
|
59
|
+
exitPM2();
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (commander.json === true)
|
|
64
|
+
Log.jsonStream(pm2.Client, pm_id);
|
|
65
|
+
else if (commander.format === true)
|
|
66
|
+
Log.formatStream(pm2.Client, pm_id, false, 'YYYY-MM-DD-HH:mm:ssZZ');
|
|
67
|
+
else
|
|
68
|
+
Log.stream(pm2.Client, 'all', true);
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
if (process.argv.length == 2) {
|
|
74
|
+
commander.outputHelp();
|
|
75
|
+
process.exit(1);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
process.on('SIGINT', function() {
|
|
79
|
+
exitPM2();
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
process.on('SIGTERM', function() {
|
|
83
|
+
exitPM2();
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
commander.parse(process.argv);
|
|
87
|
+
|
|
88
|
+
function exitPM2() {
|
|
89
|
+
console.log('Exited at %s', new Date());
|
|
90
|
+
if (commander.autoManage)
|
|
91
|
+
return process.exit(0);
|
|
92
|
+
|
|
93
|
+
if (commander.fastBoot) {
|
|
94
|
+
return pm2.delete('all', function() {
|
|
95
|
+
process.exit(0);
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
pm2.kill(function() {
|
|
99
|
+
process.exit(0);
|
|
100
|
+
});
|
|
101
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Specialized PM2 CLI for Containers
|
|
5
|
+
*/
|
|
6
|
+
var commander = require('commander');
|
|
7
|
+
var PM2 = require('../..');
|
|
8
|
+
var Log = require('../../lib/API/Log');
|
|
9
|
+
var cst = require('../../constants.js');
|
|
10
|
+
var pkg = require('../../package.json');
|
|
11
|
+
var path = require('path');
|
|
12
|
+
var DEFAULT_FAIL_COUNT = 3;
|
|
13
|
+
|
|
14
|
+
process.env.PM2_DISCRETE_MODE = true;
|
|
15
|
+
|
|
16
|
+
commander.version(pkg.version)
|
|
17
|
+
.description('zm2-runtime is a drop-in replacement Node.js binary for containers')
|
|
18
|
+
.option('-i --instances <number>', 'launch [number] of processes automatically load-balanced. Increase overall performances and performance stability.')
|
|
19
|
+
.option('--secret [key]', '[MONITORING] PM2 plus secret key')
|
|
20
|
+
.option('--no-autostart', 'add an app without automatic start')
|
|
21
|
+
.option('--no-autorestart', 'start an app without automatic restart')
|
|
22
|
+
.option('--stop-exit-codes <exit_codes...>', 'specify a list of exit codes that should skip automatic restart')
|
|
23
|
+
.option('--node-args <node_args>', 'space delimited arguments to pass to node in cluster mode - e.g. --node-args="--debug=7001 --trace-deprecation"')
|
|
24
|
+
.option('-n --name <name>', 'set a <name> for script')
|
|
25
|
+
.option('--max-memory-restart <memory>', 'specify max memory amount used to autorestart (in octet or use syntax like 100M)')
|
|
26
|
+
.option('-c --cron <cron_pattern>', 'restart a running process based on a cron pattern')
|
|
27
|
+
.option('--interpreter <interpreter>', 'the interpreter zm2 should use for executing app (bash, python...)')
|
|
28
|
+
.option('--public [key]', '[MONITORING] ZM2 plus public key')
|
|
29
|
+
.option('--machine-name [name]', '[MONITORING] ZM2 plus machine name')
|
|
30
|
+
.option('--trace', 'enable transaction tracing with km')
|
|
31
|
+
.option('--v8', 'enable v8 data collecting')
|
|
32
|
+
.option('--format', 'output logs formated like key=val')
|
|
33
|
+
.option('--raw', 'raw output (default mode)')
|
|
34
|
+
.option('--formatted', 'formatted log output |id|app|log')
|
|
35
|
+
.option('--json', 'output logs in json format')
|
|
36
|
+
.option('--delay <seconds>', 'delay start of configuration file by <seconds>', 0)
|
|
37
|
+
.option('--web [port]', 'launch process web api on [port] (default to 9615)')
|
|
38
|
+
.option('--only <application-name>', 'only act on one application of configuration')
|
|
39
|
+
.option('--no-auto-exit', 'do not exit if all processes are errored/stopped or 0 apps launched')
|
|
40
|
+
.option('--env [name]', 'inject env_[name] env variables in process config file')
|
|
41
|
+
.option('--watch', 'watch and restart application on file change')
|
|
42
|
+
.option('--error <path>', 'error log file destination (default disabled)', '/dev/null')
|
|
43
|
+
.option('--output <path>', 'output log file destination (default disabled)', '/dev/null')
|
|
44
|
+
.option('--deep-monitoring', 'enable all monitoring tools (equivalent to --v8 --event-loop-inspector --trace)')
|
|
45
|
+
.allowUnknownOption()
|
|
46
|
+
.usage('app.js');
|
|
47
|
+
|
|
48
|
+
commander.command('*')
|
|
49
|
+
.action(function(cmd){
|
|
50
|
+
Runtime.instanciate(cmd);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
commander.command('start <app.js|json_file>')
|
|
54
|
+
.description('start an application or json ecosystem file')
|
|
55
|
+
.action(function(cmd) {
|
|
56
|
+
Runtime.instanciate(cmd);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
if (process.argv.length == 2) {
|
|
60
|
+
commander.outputHelp();
|
|
61
|
+
process.exit(1);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
var Runtime = {
|
|
65
|
+
pm2 : null,
|
|
66
|
+
instanciate : function(cmd) {
|
|
67
|
+
this.pm2 = new PM2.custom({
|
|
68
|
+
pm2_home : process.env.ZM2_HOME || process.env.PM2_HOME || path.join(process.env.HOME, '.zm2'),
|
|
69
|
+
secret_key : cst.SECRET_KEY || commander.secret,
|
|
70
|
+
public_key : cst.PUBLIC_KEY || commander.public,
|
|
71
|
+
machine_name : cst.MACHINE_NAME || commander.machineName,
|
|
72
|
+
daemon_mode : process.env.PM2_RUNTIME_DEBUG || false
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
this.pm2.connect(function(err, pm2_meta) {
|
|
76
|
+
process.on('SIGINT', function() {
|
|
77
|
+
Runtime.exit();
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
process.on('SIGTERM', function() {
|
|
81
|
+
Runtime.exit();
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
Runtime.startLogStreaming();
|
|
85
|
+
Runtime.startApp(cmd, function(err) {
|
|
86
|
+
if (err) {
|
|
87
|
+
console.error(err.message || err);
|
|
88
|
+
return Runtime.exit();
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
},
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Log Streaming Management
|
|
96
|
+
*/
|
|
97
|
+
startLogStreaming : function() {
|
|
98
|
+
if (commander.json === true)
|
|
99
|
+
Log.jsonStream(this.pm2.Client, 'all');
|
|
100
|
+
else if (commander.format === true)
|
|
101
|
+
Log.formatStream(this.pm2.Client, 'all', false, 'YYYY-MM-DD-HH:mm:ssZZ');
|
|
102
|
+
else
|
|
103
|
+
Log.stream(this.pm2.Client, 'all', !commander.formatted, commander.timestamp, true);
|
|
104
|
+
},
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Application Startup
|
|
108
|
+
*/
|
|
109
|
+
startApp : function(cmd, cb) {
|
|
110
|
+
function exec() {
|
|
111
|
+
this.pm2.start(cmd, commander, function(err, obj) {
|
|
112
|
+
if (err)
|
|
113
|
+
return cb(err);
|
|
114
|
+
if (obj && obj.length == 0)
|
|
115
|
+
return cb(new Error(`0 application started (no apps to run on ${cmd})`))
|
|
116
|
+
|
|
117
|
+
if (commander.web) {
|
|
118
|
+
var port = commander.web === true ? cst.WEB_PORT : commander.web;
|
|
119
|
+
Runtime.pm2.web(port);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (commander.autoExit) {
|
|
123
|
+
setTimeout(function() {
|
|
124
|
+
Runtime.autoExitWorker();
|
|
125
|
+
}, 4000);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// For Testing purpose (allow to auto exit CLI)
|
|
129
|
+
if (process.env.PM2_RUNTIME_DEBUG)
|
|
130
|
+
Runtime.pm2.disconnect(function() {});
|
|
131
|
+
|
|
132
|
+
return cb(null, obj);
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
// via --delay <seconds> option
|
|
136
|
+
setTimeout(exec.bind(this), commander.delay * 1000);
|
|
137
|
+
},
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Exit runtime mgmt
|
|
141
|
+
*/
|
|
142
|
+
exit : function(code) {
|
|
143
|
+
if (!this.pm2) return process.exit(1);
|
|
144
|
+
|
|
145
|
+
this.pm2.kill(function() {
|
|
146
|
+
process.exit(code || 0);
|
|
147
|
+
});
|
|
148
|
+
},
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Exit current PM2 instance if 0 app is online
|
|
152
|
+
* function activated via --auto-exit
|
|
153
|
+
*/
|
|
154
|
+
autoExitWorker : function(fail_count) {
|
|
155
|
+
var interval = 2000;
|
|
156
|
+
|
|
157
|
+
if (typeof(fail_count) =='undefined')
|
|
158
|
+
fail_count = DEFAULT_FAIL_COUNT;
|
|
159
|
+
|
|
160
|
+
var timer = setTimeout(function () {
|
|
161
|
+
Runtime.pm2.list(function (err, apps) {
|
|
162
|
+
if (err) {
|
|
163
|
+
console.error('Could not run pm2 list');
|
|
164
|
+
return Runtime.autoExitWorker();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
var appOnline = 0;
|
|
168
|
+
|
|
169
|
+
apps.forEach(function (app) {
|
|
170
|
+
if (!app.pm2_env.pmx_module &&
|
|
171
|
+
(app.pm2_env.status === cst.ONLINE_STATUS ||
|
|
172
|
+
app.pm2_env.status === cst.LAUNCHING_STATUS)) {
|
|
173
|
+
appOnline++;
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
if (appOnline === 0) {
|
|
178
|
+
console.log('0 application online, retry =', fail_count);
|
|
179
|
+
if (fail_count <= 0)
|
|
180
|
+
return Runtime.exit(2);
|
|
181
|
+
return Runtime.autoExitWorker(--fail_count);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
Runtime.autoExitWorker();
|
|
185
|
+
});
|
|
186
|
+
}, interval);
|
|
187
|
+
|
|
188
|
+
timer.unref();
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
commander.parse(process.argv);
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2013-2022 the PM2 project authors. All rights reserved.
|
|
3
|
+
* Use of this source code is governed by a license that
|
|
4
|
+
* can be found in the LICENSE file.
|
|
5
|
+
*/
|
|
6
|
+
var fs = require('fs'),
|
|
7
|
+
pth = require('path');
|
|
8
|
+
|
|
9
|
+
// hacked from node-tabtab 0.0.4 https://github.com/mklabs/node-tabtab.git
|
|
10
|
+
// Itself based on npm completion by @isaac
|
|
11
|
+
|
|
12
|
+
exports.complete = function complete(name, completer, cb) {
|
|
13
|
+
|
|
14
|
+
// cb not there, assume callback is completer and
|
|
15
|
+
// the completer is the executable itself
|
|
16
|
+
if(!cb) {
|
|
17
|
+
cb = completer;
|
|
18
|
+
completer = name;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
var env = parseEnv();
|
|
22
|
+
|
|
23
|
+
// if not a complete command, return here.
|
|
24
|
+
if(!env.complete) return cb();
|
|
25
|
+
|
|
26
|
+
// if install cmd, add complete script to either ~/.bashrc or ~/.zshrc
|
|
27
|
+
if(env.install) return install(name, completer, function(err, state) {
|
|
28
|
+
console.log(state || err.message);
|
|
29
|
+
if(err) return cb(err);
|
|
30
|
+
cb(null, null, state);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// if install cmd, add complete script to either ~/.bashrc or ~/.zshrc
|
|
34
|
+
if(env.uninstall) return uninstall(name, completer, function(err, state) {
|
|
35
|
+
console.log(state || err.message);
|
|
36
|
+
if(err) return cb(err);
|
|
37
|
+
cb(null, null, state);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// if the COMP_* are not in the env, then dump the install script.
|
|
41
|
+
if(!env.words || !env.point || !env.line) return script(name, completer, function(err, content) {
|
|
42
|
+
if(err) return cb(err);
|
|
43
|
+
process.stdout.write(content, function (n) { cb(null, null, content); });
|
|
44
|
+
process.stdout.on("error", function (er) {
|
|
45
|
+
// Darwin is a real dick sometimes.
|
|
46
|
+
//
|
|
47
|
+
// This is necessary because the "source" or "." program in
|
|
48
|
+
// bash on OS X closes its file argument before reading
|
|
49
|
+
// from it, meaning that you get exactly 1 write, which will
|
|
50
|
+
// work most of the time, and will always raise an EPIPE.
|
|
51
|
+
//
|
|
52
|
+
// Really, one should not be tossing away EPIPE errors, or any
|
|
53
|
+
// errors, so casually. But, without this, `. <(npm completion)`
|
|
54
|
+
// can never ever work on OS X.
|
|
55
|
+
// -- isaacs
|
|
56
|
+
// https://github.com/isaacs/npm/blob/master/lib/completion.js#L162
|
|
57
|
+
if (er.errno === "EPIPE") er = null
|
|
58
|
+
cb(er, null, content);
|
|
59
|
+
});
|
|
60
|
+
cb(null, null, content);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
var partial = env.line.substr(0, env.point),
|
|
64
|
+
last = env.line.split(' ').slice(-1).join(''),
|
|
65
|
+
lastPartial = partial.split(' ').slice(-1).join(''),
|
|
66
|
+
prev = env.line.split(' ').slice(0, -1).slice(-1)[0];
|
|
67
|
+
|
|
68
|
+
cb(null, {
|
|
69
|
+
line: env.line,
|
|
70
|
+
words: env.words,
|
|
71
|
+
point: env.point,
|
|
72
|
+
partial: partial,
|
|
73
|
+
last: last,
|
|
74
|
+
prev: prev,
|
|
75
|
+
lastPartial: lastPartial
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
// simple helper function to know if the script is run
|
|
80
|
+
// in the context of a completion command. Also mapping the
|
|
81
|
+
// special `<pkgname> completion` cmd.
|
|
82
|
+
exports.isComplete = function isComplete() {
|
|
83
|
+
var env = parseEnv();
|
|
84
|
+
return env.complete || (env.words && env.point && env.line);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
exports.parseOut = function parseOut(str) {
|
|
88
|
+
var shorts = str.match(/\s-\w+/g);
|
|
89
|
+
var longs = str.match(/\s--\w+/g);
|
|
90
|
+
|
|
91
|
+
return {
|
|
92
|
+
shorts: shorts.map(trim).map(cleanPrefix),
|
|
93
|
+
longs: longs.map(trim).map(cleanPrefix)
|
|
94
|
+
};
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
// specific to cake case
|
|
98
|
+
exports.parseTasks = function(str, prefix, reg) {
|
|
99
|
+
var tasks = str.match(reg || new RegExp('^' + prefix + '\\s[^#]+', 'gm')) || [];
|
|
100
|
+
return tasks.map(trim).map(function(s) {
|
|
101
|
+
return s.replace(prefix + ' ', '');
|
|
102
|
+
});
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
exports.log = function log(arr, o, prefix) {
|
|
106
|
+
prefix = prefix || '';
|
|
107
|
+
arr = Array.isArray(arr) ? arr : [arr];
|
|
108
|
+
arr.filter(abbrev(o)).forEach(function(v) {
|
|
109
|
+
console.log(prefix + v);
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function trim (s) {
|
|
114
|
+
return s.trim();
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function cleanPrefix(s) {
|
|
118
|
+
return s.replace(/-/g, '');
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function abbrev(o) { return function(it) {
|
|
122
|
+
return new RegExp('^' + o.last.replace(/^--?/g, '')).test(it);
|
|
123
|
+
}}
|
|
124
|
+
|
|
125
|
+
// output the completion.sh script to the console for install instructions.
|
|
126
|
+
// This is actually a 'template' where the package name is used to setup
|
|
127
|
+
// the completion on the right command, and properly name the bash/zsh functions.
|
|
128
|
+
function script(name, completer, cb) {
|
|
129
|
+
var p = pth.join(__dirname, 'completion.sh');
|
|
130
|
+
|
|
131
|
+
fs.readFile(p, 'utf8', function (er, d) {
|
|
132
|
+
if (er) return cb(er);
|
|
133
|
+
cb(null, d);
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function install(name, completer, cb) {
|
|
138
|
+
var markerIn = '###-begin-' + name + '-completion-###',
|
|
139
|
+
markerOut = '###-end-' + name + '-completion-###';
|
|
140
|
+
|
|
141
|
+
var rc, scriptOutput;
|
|
142
|
+
|
|
143
|
+
readRc(completer, function(err, file) {
|
|
144
|
+
if(err) return cb(err);
|
|
145
|
+
|
|
146
|
+
var part = file.split(markerIn)[1];
|
|
147
|
+
if(part) {
|
|
148
|
+
return cb(null, ' ✗ ' + completer + ' tab-completion has been already installed. Do nothing.');
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
rc = file;
|
|
152
|
+
next();
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
script(name, completer, function(err, file) {
|
|
156
|
+
scriptOutput = file;
|
|
157
|
+
next();
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
function next() {
|
|
161
|
+
if(!rc || !scriptOutput) return;
|
|
162
|
+
|
|
163
|
+
writeRc(rc + scriptOutput, function(err) {
|
|
164
|
+
if(err) return cb(err);
|
|
165
|
+
return cb(null, ' ✓ ' + completer + ' tab-completion installed.');
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function uninstall(name, completer, cb) {
|
|
171
|
+
var markerIn = '\n\n###-begin-' + name + '-completion-###',
|
|
172
|
+
markerOut = '###-end-' + name + '-completion-###\n';
|
|
173
|
+
|
|
174
|
+
readRc(completer, function(err, file) {
|
|
175
|
+
if(err) return cb(err);
|
|
176
|
+
|
|
177
|
+
var part = file.split(markerIn)[1];
|
|
178
|
+
if(!part) {
|
|
179
|
+
return cb(null, ' ✗ ' + completer + ' tab-completion has been already uninstalled. Do nothing.');
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
part = markerIn + part.split(markerOut)[0] + markerOut;
|
|
183
|
+
writeRc(file.replace(part, ''), function(err) {
|
|
184
|
+
if(err) return cb(err);
|
|
185
|
+
return cb(null, ' ✓ ' + completer + ' tab-completion uninstalled.');
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function readRc(completer, cb) {
|
|
191
|
+
var file = '.' + process.env.SHELL.match(/\/bin\/(\w+)/)[1] + 'rc',
|
|
192
|
+
filepath = pth.join(process.env.HOME, file);
|
|
193
|
+
fs.lstat(filepath, function (err, stats) {
|
|
194
|
+
if(err) return cb(new Error("No " + file + " file. You'll have to run instead: " + completer + " completion >> ~/" + file));
|
|
195
|
+
fs.readFile(filepath, 'utf8', cb);
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function writeRc(content, cb) {
|
|
200
|
+
var file = '.' + process.env.SHELL.match(/\/bin\/(\w+)/)[1] + 'rc',
|
|
201
|
+
filepath = pth.join(process.env.HOME, file);
|
|
202
|
+
fs.lstat(filepath, function (err, stats) {
|
|
203
|
+
if(err) return cb(new Error("No " + file + " file. You'll have to run instead: " + completer + " completion >> ~/" + file));
|
|
204
|
+
fs.writeFile(filepath, content, cb);
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function installed (marker, completer, cb) {
|
|
209
|
+
readRc(completer, function(err, file) {
|
|
210
|
+
if(err) return cb(err);
|
|
211
|
+
var installed = file.match(marker);
|
|
212
|
+
return cb(!!installed);
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function parseEnv() {
|
|
217
|
+
var args = process.argv.slice(2),
|
|
218
|
+
complete = args[0] === 'completion';
|
|
219
|
+
|
|
220
|
+
return {
|
|
221
|
+
args: args,
|
|
222
|
+
complete: complete,
|
|
223
|
+
install: complete && args[1] === 'install',
|
|
224
|
+
uninstall: complete && args[1] === 'uninstall',
|
|
225
|
+
words: +process.env.COMP_CWORD,
|
|
226
|
+
point: +process.env.COMP_POINT,
|
|
227
|
+
line: process.env.COMP_LINE
|
|
228
|
+
}
|
|
229
|
+
};
|