@ohos-ports/pm2 0.0.0-stage → 7.1.2-beta.0
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/CHANGELOG.md +2607 -0
- package/CONTRIBUTING.md +124 -0
- package/GNU-AGPL-3.0.txt +665 -0
- package/LICENSE +1 -0
- package/README.md +255 -2
- package/SECURITY.md +41 -0
- package/bin/pm2 +3 -0
- package/bin/pm2-dev +3 -0
- package/bin/pm2-docker +3 -0
- package/bin/pm2-runtime +3 -0
- package/bin/pm2.ps1 +3 -0
- package/constants.js +113 -0
- package/index.js +12 -0
- package/lib/API/Configuration.js +212 -0
- package/lib/API/Containerizer.js +335 -0
- package/lib/API/Dashboard.js +726 -0
- package/lib/API/Deploy.js +117 -0
- package/lib/API/Extra.js +798 -0
- package/lib/API/ExtraMgmt/Docker.js +30 -0
- package/lib/API/Log.js +388 -0
- package/lib/API/LogManagement.js +351 -0
- package/lib/API/Modules/LOCAL.js +122 -0
- package/lib/API/Modules/Modularizer.js +148 -0
- package/lib/API/Modules/NPM.js +443 -0
- package/lib/API/Modules/TAR.js +368 -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 +497 -0
- package/lib/API/Startup.js +630 -0
- package/lib/API/UX/helpers.js +213 -0
- package/lib/API/UX/index.js +9 -0
- package/lib/API/UX/pm2-describe.js +197 -0
- package/lib/API/UX/pm2-ls-minimal.js +31 -0
- package/lib/API/UX/pm2-ls.js +581 -0
- package/lib/API/Version.js +383 -0
- package/lib/API/interpreter.json +12 -0
- package/lib/API/pm2-plus/PM2IO.js +381 -0
- package/lib/API/pm2-plus/auth-strategies/CliAuth.js +306 -0
- package/lib/API/pm2-plus/auth-strategies/WebAuth.js +195 -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 +375 -0
- package/lib/API.js +1980 -0
- package/lib/Client.js +793 -0
- package/lib/Common.js +949 -0
- package/lib/Configuration.js +309 -0
- package/lib/Daemon.js +468 -0
- package/lib/Event.js +37 -0
- package/lib/God/ActionMethods.js +955 -0
- package/lib/God/ClusterMode.js +192 -0
- package/lib/God/ForkMode.js +205 -0
- package/lib/God/Methods.js +292 -0
- package/lib/God/Reload.js +300 -0
- package/lib/God/StdioLogger.js +239 -0
- package/lib/God.js +653 -0
- package/lib/HttpInterface.js +75 -0
- package/lib/OtelManager.js +74 -0
- package/lib/ProcessContainer.js +321 -0
- package/lib/ProcessContainerBun.js +355 -0
- package/lib/ProcessContainerFork.js +42 -0
- package/lib/ProcessContainerForkBun.js +31 -0
- package/lib/ProcessUtils.js +74 -0
- package/lib/TreeKill.js +111 -0
- package/lib/Utility.js +304 -0
- package/lib/VersionCheck.js +46 -0
- package/lib/Watcher.js +117 -0
- package/lib/Worker.js +200 -0
- package/lib/binaries/CLI.js +1092 -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-nodejs.tpl +8 -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 +251 -0
- package/lib/tools/SysMetrics.js +375 -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/interpreter.js +88 -0
- package/lib/tools/isbinaryfile.js +94 -0
- package/lib/tools/json5.js +752 -0
- package/lib/tools/multimeter/bar.js +111 -0
- package/lib/tools/multimeter/charm/encode.js +18 -0
- package/lib/tools/multimeter/charm/index.js +305 -0
- package/lib/tools/multimeter/index.js +76 -0
- package/lib/tools/open.js +56 -0
- package/lib/tools/passwd.js +58 -0
- package/lib/tools/pm2-config-keys.js +165 -0
- package/lib/tools/prompt.js +76 -0
- package/lib/tools/serviceStop.js +149 -0
- package/lib/tools/sexec.js +55 -0
- package/lib/tools/treeify.js +113 -0
- package/lib/tools/typestrip.js +55 -0
- package/lib/tools/which.js +120 -0
- package/lib/tools/xdg-open +861 -0
- package/modules/blessed/LICENSE +20 -0
- package/modules/blessed/README.md +34 -0
- package/modules/blessed/index.js +1 -0
- package/modules/blessed/lib/alias.js +526 -0
- package/modules/blessed/lib/blessed.js +32 -0
- package/modules/blessed/lib/colors.js +530 -0
- package/modules/blessed/lib/events.js +189 -0
- package/modules/blessed/lib/gpmclient.js +221 -0
- package/modules/blessed/lib/helpers.js +165 -0
- package/modules/blessed/lib/keys.js +339 -0
- package/modules/blessed/lib/program.js +4297 -0
- package/modules/blessed/lib/tput.js +3022 -0
- package/modules/blessed/lib/unicode.js +885 -0
- package/modules/blessed/lib/widget.js +32 -0
- package/modules/blessed/lib/widgets/box.js +34 -0
- package/modules/blessed/lib/widgets/element.js +2570 -0
- package/modules/blessed/lib/widgets/list.js +599 -0
- package/modules/blessed/lib/widgets/log.js +83 -0
- package/modules/blessed/lib/widgets/node.js +282 -0
- package/modules/blessed/lib/widgets/screen.js +2298 -0
- package/modules/blessed/lib/widgets/scrollablebox.js +389 -0
- package/modules/blessed/lib/widgets/scrollabletext.js +35 -0
- package/modules/blessed/lib/widgets/text.js +35 -0
- package/modules/blessed/usr/linux +0 -0
- package/modules/blessed/usr/windows-ansi +0 -0
- package/modules/blessed/usr/xterm +0 -0
- package/modules/blessed/usr/xterm-256color +0 -0
- package/modules/blessed/usr/xterm.termcap +243 -0
- package/modules/chokidar/LICENSE +21 -0
- package/modules/chokidar/VENDORED.md +76 -0
- package/modules/chokidar/index.js +979 -0
- package/modules/chokidar/lib/brace-expand.js +180 -0
- package/modules/chokidar/lib/constants.js +66 -0
- package/modules/chokidar/lib/fsevents-handler.js +526 -0
- package/modules/chokidar/lib/nodefs-handler.js +654 -0
- package/modules/fclone.js +49 -0
- package/modules/pm2-axon/index.js +2 -0
- package/modules/pm2-axon/lib/configurable/History.md +5 -0
- package/modules/pm2-axon/lib/configurable/Makefile +7 -0
- package/modules/pm2-axon/lib/configurable/Readme.md +57 -0
- package/modules/pm2-axon/lib/configurable/index.js +2 -0
- package/modules/pm2-axon/lib/configurable/lib/configurable.js +47 -0
- package/modules/pm2-axon/lib/configurable/package.json +68 -0
- package/modules/pm2-axon/lib/index.js +44 -0
- package/modules/pm2-axon/lib/plugins/queue.js +71 -0
- package/modules/pm2-axon/lib/plugins/round-robin.js +55 -0
- package/modules/pm2-axon/lib/sockets/pub-emitter.js +26 -0
- package/modules/pm2-axon/lib/sockets/pub.js +77 -0
- package/modules/pm2-axon/lib/sockets/pull.js +37 -0
- package/modules/pm2-axon/lib/sockets/push.js +32 -0
- package/modules/pm2-axon/lib/sockets/rep.js +73 -0
- package/modules/pm2-axon/lib/sockets/req.js +101 -0
- package/modules/pm2-axon/lib/sockets/sock.js +430 -0
- package/modules/pm2-axon/lib/sockets/sub-emitter.js +92 -0
- package/modules/pm2-axon/lib/sockets/sub.js +147 -0
- package/modules/pm2-axon/lib/utils.js +19 -0
- package/modules/pm2-axon-rpc/index.js +3 -0
- package/modules/pm2-axon-rpc/lib/client.js +63 -0
- package/modules/pm2-axon-rpc/lib/server.js +139 -0
- package/modules/pm2-io-agent/LICENSE +661 -0
- package/modules/pm2-io-agent/README.md +62 -0
- package/modules/pm2-io-agent/config.js +30 -0
- package/modules/pm2-io-agent/constants.js +118 -0
- package/modules/pm2-io-agent/index.js +2 -0
- package/modules/pm2-io-agent/src/InteractorClient.js +513 -0
- package/modules/pm2-io-agent/src/InteractorDaemon.js +455 -0
- package/modules/pm2-io-agent/src/PM2Client.js +112 -0
- package/modules/pm2-io-agent/src/PM2Interface.js +180 -0
- package/modules/pm2-io-agent/src/TransporterInterface.js +173 -0
- package/modules/pm2-io-agent/src/Utility.js +392 -0
- package/modules/pm2-io-agent/src/WatchDog.js +70 -0
- package/modules/pm2-io-agent/src/push/DataRetriever.js +77 -0
- package/modules/pm2-io-agent/src/push/PushInteractor.js +206 -0
- package/modules/pm2-io-agent/src/push/TransactionAggregator.js +670 -0
- package/modules/pm2-io-agent/src/reverse/ReverseInteractor.js +128 -0
- package/modules/pm2-io-agent/src/transporters/Transporter.js +129 -0
- package/modules/pm2-io-agent/src/transporters/WebsocketTransport.js +202 -0
- package/modules/pm2-io-agent/src/utils/BinaryHeap.js +135 -0
- package/modules/pm2-io-agent/src/utils/EDS.js +111 -0
- package/modules/pm2-io-agent/src/utils/probes/Histogram.js +204 -0
- package/modules/pm2-io-agent/src/utils/units.js +10 -0
- package/modules/pm2-io-bpm/configuration.js +141 -0
- package/modules/pm2-io-bpm/constants.js +10 -0
- package/modules/pm2-io-bpm/featureManager.js +110 -0
- package/modules/pm2-io-bpm/features/dependencies.js +48 -0
- package/modules/pm2-io-bpm/features/events.js +50 -0
- package/modules/pm2-io-bpm/features/metrics.js +102 -0
- package/modules/pm2-io-bpm/features/notify.js +238 -0
- package/modules/pm2-io-bpm/features/profiling.js +70 -0
- package/modules/pm2-io-bpm/features/tracing.js +174 -0
- package/modules/pm2-io-bpm/index.js +11 -0
- package/modules/pm2-io-bpm/metrics/eventLoopMetrics.js +136 -0
- package/modules/pm2-io-bpm/metrics/httpMetrics.js +175 -0
- package/modules/pm2-io-bpm/metrics/network.js +132 -0
- package/modules/pm2-io-bpm/metrics/runtime.js +161 -0
- package/modules/pm2-io-bpm/metrics/v8.js +104 -0
- package/modules/pm2-io-bpm/otel/constants.js +7 -0
- package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/platform/index.js +17 -0
- package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/platform/node/index.js +17 -0
- package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/platform/node/util.js +51 -0
- package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/transform.js +137 -0
- package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/types.js +24 -0
- package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/utils.js +23 -0
- package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/zipkin.js +149 -0
- package/modules/pm2-io-bpm/pmx.js +380 -0
- package/modules/pm2-io-bpm/profilers/addonProfiler.js +193 -0
- package/modules/pm2-io-bpm/profilers/inspectorProfiler.js +302 -0
- package/modules/pm2-io-bpm/serviceManager.js +21 -0
- package/modules/pm2-io-bpm/services/actions.js +159 -0
- package/modules/pm2-io-bpm/services/inspector.js +43 -0
- package/modules/pm2-io-bpm/services/metrics.js +186 -0
- package/modules/pm2-io-bpm/services/runtimeStats.js +54 -0
- package/modules/pm2-io-bpm/services/transport.js +11 -0
- package/modules/pm2-io-bpm/transports/IPCTransport.js +115 -0
- package/modules/pm2-io-bpm/utils/BinaryHeap.js +134 -0
- package/modules/pm2-io-bpm/utils/EDS.js +113 -0
- package/modules/pm2-io-bpm/utils/EWMA.js +32 -0
- package/modules/pm2-io-bpm/utils/autocast.js +61 -0
- package/modules/pm2-io-bpm/utils/metrics/counter.js +33 -0
- package/modules/pm2-io-bpm/utils/metrics/gauge.js +23 -0
- package/modules/pm2-io-bpm/utils/metrics/histogram.js +192 -0
- package/modules/pm2-io-bpm/utils/metrics/meter.js +46 -0
- package/modules/pm2-io-bpm/utils/miscellaneous.js +19 -0
- package/modules/pm2-io-bpm/utils/module.js +67 -0
- package/modules/pm2-io-bpm/utils/shimmer.js +19 -0
- package/modules/pm2-io-bpm/utils/stackParser.js +160 -0
- package/modules/pm2-io-bpm/utils/transactionAggregator.js +429 -0
- package/modules/pm2-io-bpm/utils/units.js +16 -0
- package/modules/vizion/LICENSE +13 -0
- package/modules/vizion/README.md +133 -0
- package/modules/vizion/index.js +1 -0
- package/modules/vizion/lib/git/git.js +247 -0
- package/modules/vizion/lib/vizion.js +52 -0
- package/package.json +170 -4
- package/paths.js +121 -0
- package/pm2 +11 -0
- package/types/index.d.ts +732 -0
- package/types/tsconfig.json +14 -0
package/lib/Common.js
ADDED
|
@@ -0,0 +1,949 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2013-present 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
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Common Utilities ONLY USED IN ->CLI<-
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
var fs = require('fs');
|
|
12
|
+
var path = require('path');
|
|
13
|
+
var os = require('os');
|
|
14
|
+
var util = require('util');
|
|
15
|
+
var chalk = require('ansis');
|
|
16
|
+
var fclone = require('../modules/fclone');
|
|
17
|
+
var dayjs = require('dayjs');
|
|
18
|
+
var execSync = require('child_process').execSync;
|
|
19
|
+
var isBinary = require('./tools/isbinaryfile.js');
|
|
20
|
+
var cst = require('../constants.js');
|
|
21
|
+
var extItps = require('./API/interpreter.json');
|
|
22
|
+
var Config = require('./tools/Config');
|
|
23
|
+
var pkg = require('../package.json');
|
|
24
|
+
var which = require('./tools/which.js');
|
|
25
|
+
var pm2Keys = require('./tools/pm2-config-keys.js');
|
|
26
|
+
var interpreter = require('./tools/interpreter.js');
|
|
27
|
+
var Common = module.exports;
|
|
28
|
+
|
|
29
|
+
function homedir() {
|
|
30
|
+
return require('os').homedir();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function resolveHome(filepath) {
|
|
34
|
+
if (filepath[0] === '~') {
|
|
35
|
+
return path.join(homedir(), filepath.slice(1));
|
|
36
|
+
}
|
|
37
|
+
return filepath;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
Common.determineSilentCLI = function() {
|
|
41
|
+
// pm2 should ignore -s --silent -v if they are after '--'
|
|
42
|
+
var variadicArgsDashesPos = process.argv.indexOf('--');
|
|
43
|
+
var s1opt = process.argv.indexOf('--silent')
|
|
44
|
+
var s2opt = process.argv.indexOf('-s')
|
|
45
|
+
|
|
46
|
+
if (process.env.PM2_SILENT || (variadicArgsDashesPos > -1 &&
|
|
47
|
+
(s1opt != -1 && s1opt < variadicArgsDashesPos) &&
|
|
48
|
+
(s2opt != -1 != s2opt < variadicArgsDashesPos)) ||
|
|
49
|
+
(variadicArgsDashesPos == -1 && (s1opt > -1 || s2opt > -1))) {
|
|
50
|
+
for (var key in console){
|
|
51
|
+
var code = key.charCodeAt(0);
|
|
52
|
+
if (code >= 97 && code <= 122){
|
|
53
|
+
console[key] = function(){};
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
process.env.PM2_DISCRETE_MODE = true;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
Common.printVersion = function() {
|
|
61
|
+
var variadicArgsDashesPos = process.argv.indexOf('--');
|
|
62
|
+
|
|
63
|
+
if (process.argv.indexOf('-v') > -1 && process.argv.indexOf('-v') < variadicArgsDashesPos) {
|
|
64
|
+
console.log(pkg.version);
|
|
65
|
+
process.exit(0);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
Common.lockReload = function() {
|
|
70
|
+
try {
|
|
71
|
+
var t1 = fs.readFileSync(cst.PM2_RELOAD_LOCKFILE).toString();
|
|
72
|
+
|
|
73
|
+
// Check if content and if time < 30 return locked
|
|
74
|
+
// Else if content detected (lock file staled), allow and rewritte
|
|
75
|
+
if (t1 && t1 != '') {
|
|
76
|
+
var diff = dayjs().diff(parseInt(t1));
|
|
77
|
+
if (diff < cst.RELOAD_LOCK_TIMEOUT)
|
|
78
|
+
return diff;
|
|
79
|
+
}
|
|
80
|
+
} catch(e) {}
|
|
81
|
+
|
|
82
|
+
try {
|
|
83
|
+
// Write latest timestamp
|
|
84
|
+
fs.writeFileSync(cst.PM2_RELOAD_LOCKFILE, dayjs().valueOf().toString());
|
|
85
|
+
return 0;
|
|
86
|
+
} catch(e) {
|
|
87
|
+
console.error(e.message || e);
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
Common.unlockReload = function() {
|
|
92
|
+
try {
|
|
93
|
+
fs.writeFileSync(cst.PM2_RELOAD_LOCKFILE, '');
|
|
94
|
+
} catch(e) {
|
|
95
|
+
console.error(e.message || e);
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Resolve app paths and replace missing values with defaults.
|
|
101
|
+
* @method prepareAppConf
|
|
102
|
+
* @param app {Object}
|
|
103
|
+
* @param {} cwd
|
|
104
|
+
* @param {} outputter
|
|
105
|
+
* @return app
|
|
106
|
+
*/
|
|
107
|
+
Common.prepareAppConf = function(opts, app) {
|
|
108
|
+
/**
|
|
109
|
+
* Minimum validation
|
|
110
|
+
*/
|
|
111
|
+
if (!app.script)
|
|
112
|
+
return new Error('No script path - aborting');
|
|
113
|
+
|
|
114
|
+
var cwd = null;
|
|
115
|
+
|
|
116
|
+
if (app.cwd) {
|
|
117
|
+
cwd = path.resolve(app.cwd);
|
|
118
|
+
process.env.PWD = app.cwd;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (!app.node_args) {
|
|
122
|
+
app.node_args = [];
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (app.port && app.env) {
|
|
126
|
+
app.env.PORT = app.port;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// CWD option resolving
|
|
130
|
+
cwd && (cwd[0] != '/') && (cwd = path.resolve(process.cwd(), cwd));
|
|
131
|
+
cwd = cwd || opts.cwd;
|
|
132
|
+
|
|
133
|
+
// Full path script resolution
|
|
134
|
+
app.pm_exec_path = path.resolve(cwd, app.script);
|
|
135
|
+
|
|
136
|
+
// If script does not exist after resolution
|
|
137
|
+
if (!fs.existsSync(app.pm_exec_path)) {
|
|
138
|
+
var ckd;
|
|
139
|
+
// Try resolve command available in $PATH
|
|
140
|
+
if ((ckd = which(app.script))) {
|
|
141
|
+
if (typeof(ckd) !== 'string')
|
|
142
|
+
ckd = ckd.toString();
|
|
143
|
+
app.pm_exec_path = ckd;
|
|
144
|
+
}
|
|
145
|
+
else
|
|
146
|
+
// Throw critical error
|
|
147
|
+
return new Error(`Script not found: ${app.pm_exec_path}`);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
delete app.script;
|
|
152
|
+
|
|
153
|
+
// Set current env by first adding the process environment and then extending/replacing it
|
|
154
|
+
// with env specified on command-line or JSON file.
|
|
155
|
+
|
|
156
|
+
var env = {};
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Do not copy internal pm2 environment variables if acting on process
|
|
160
|
+
* is made from a programmatic script started by PM2 or if a pm_id is present in env
|
|
161
|
+
*/
|
|
162
|
+
if (cst.PM2_PROGRAMMATIC || process.env.pm_id)
|
|
163
|
+
Common.safeExtend(env, process.env);
|
|
164
|
+
else
|
|
165
|
+
env = process.env;
|
|
166
|
+
|
|
167
|
+
// The ambient environment must not supply PM2 options: a stray `name=` or
|
|
168
|
+
// `port=` variable would otherwise become app configuration. Strip them here,
|
|
169
|
+
// at ingestion, while the two origins are still distinguishable -- `env:` is
|
|
170
|
+
// layered on top afterwards, so what the user declares is kept as-is and
|
|
171
|
+
// reaches the child (#6006 #6016 #5747 #5622 #5030 #5032 #4943 #4778 #4821 #3210)
|
|
172
|
+
var shell_env = pm2Keys.filterShellEnv(env, app.filter_env);
|
|
173
|
+
|
|
174
|
+
app.env = [
|
|
175
|
+
{}, pm2Keys.omitPm2ConfigKeys(shell_env), app.env || {}
|
|
176
|
+
].reduce(function(e1, e2){
|
|
177
|
+
return Object.assign(e1, e2);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
app.pm_cwd = cwd;
|
|
181
|
+
// Interpreter
|
|
182
|
+
try {
|
|
183
|
+
Common.sink.resolveInterpreter(app);
|
|
184
|
+
} catch(e) {
|
|
185
|
+
return e
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Exec mode and cluster stuff
|
|
189
|
+
Common.sink.determineExecMode(app);
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Scary
|
|
193
|
+
*/
|
|
194
|
+
var formated_app_name = app.name.replace(/[^a-zA-Z0-9\\.\\-]/g, '-');
|
|
195
|
+
|
|
196
|
+
['log', 'out', 'error', 'pid'].forEach(function(f){
|
|
197
|
+
var af = app[f + '_file'], ps, ext = (f == 'pid' ? 'pid':'log'), isStd = !~['log', 'pid'].indexOf(f);
|
|
198
|
+
if (af) af = resolveHome(af);
|
|
199
|
+
|
|
200
|
+
if ((f == 'log' && typeof af == 'boolean' && af) || (f != 'log' && !af)) {
|
|
201
|
+
ps = [cst['DEFAULT_' + ext.toUpperCase() + '_PATH'], formated_app_name + (isStd ? '-' + f : '') + '.' + ext];
|
|
202
|
+
} else if ((f != 'log' || (f == 'log' && af)) && af !== 'NULL' && af !== '/dev/null') {
|
|
203
|
+
ps = [cwd, af];
|
|
204
|
+
|
|
205
|
+
var dir = path.dirname(path.resolve(cwd, af));
|
|
206
|
+
if (!fs.existsSync(dir)) {
|
|
207
|
+
Common.printError(cst.PREFIX_MSG_WARNING + 'Folder does not exist: ' + dir);
|
|
208
|
+
Common.printOut(cst.PREFIX_MSG + 'Creating folder: ' + dir);
|
|
209
|
+
try {
|
|
210
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
211
|
+
} catch (err) {
|
|
212
|
+
Common.printError(cst.PREFIX_MSG_ERR + 'Could not create folder: ' + path.dirname(af));
|
|
213
|
+
throw new Error('Could not create folder');
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
}
|
|
218
|
+
// PM2 paths
|
|
219
|
+
if (af !== 'NULL' && af !== '/dev/null') {
|
|
220
|
+
ps && (app['pm_' + (isStd ? f.substr(0, 3) + '_' : '') + ext + '_path'] = path.resolve.apply(null, ps));
|
|
221
|
+
} else if (path.sep === '\\') {
|
|
222
|
+
app['pm_' + (isStd ? f.substr(0, 3) + '_' : '') + ext + '_path'] = '\\\\.\\NUL';
|
|
223
|
+
} else {
|
|
224
|
+
app['pm_' + (isStd ? f.substr(0, 3) + '_' : '') + ext + '_path'] = '/dev/null';
|
|
225
|
+
}
|
|
226
|
+
delete app[f + '_file'];
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
return app;
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Definition of known config file extensions with their type
|
|
234
|
+
*/
|
|
235
|
+
Common.knonwConfigFileExtensions = {
|
|
236
|
+
'.json': 'json',
|
|
237
|
+
'.yml': 'yaml',
|
|
238
|
+
'.yaml': 'yaml',
|
|
239
|
+
'.config.js': 'js',
|
|
240
|
+
'.config.cjs': 'js',
|
|
241
|
+
'.config.mjs': 'mjs'
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Check if filename is a configuration file
|
|
246
|
+
* @param {string} filename
|
|
247
|
+
* @return {mixed} null if not conf file, json or yaml if conf
|
|
248
|
+
*/
|
|
249
|
+
Common.isConfigFile = function (filename) {
|
|
250
|
+
if (typeof (filename) !== 'string')
|
|
251
|
+
return null;
|
|
252
|
+
|
|
253
|
+
for (let extension in Common.knonwConfigFileExtensions) {
|
|
254
|
+
if (filename.indexOf(extension) !== -1) {
|
|
255
|
+
return Common.knonwConfigFileExtensions[extension];
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
return null;
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
Common.getConfigFileCandidates = function (name) {
|
|
263
|
+
return Object.keys(Common.knonwConfigFileExtensions).map((extension) => name + extension);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Parses a config file like ecosystem.config.js. Supported formats: JS, JSON, JSON5, YAML.
|
|
268
|
+
* @param {string} confString contents of the config file
|
|
269
|
+
* @param {string} filename path to the config file
|
|
270
|
+
* @return {Object} config object
|
|
271
|
+
*/
|
|
272
|
+
Common.parseConfig = function(confObj, filename) {
|
|
273
|
+
var yamljs = require('js-yaml');
|
|
274
|
+
var vm = require('vm');
|
|
275
|
+
|
|
276
|
+
var isConfigFile = Common.isConfigFile(filename);
|
|
277
|
+
|
|
278
|
+
if (!filename ||
|
|
279
|
+
filename == 'pipe' ||
|
|
280
|
+
filename == 'none' ||
|
|
281
|
+
isConfigFile == 'json') {
|
|
282
|
+
var code = '(' + confObj + ')';
|
|
283
|
+
var sandbox = {};
|
|
284
|
+
|
|
285
|
+
return vm.runInThisContext(code, sandbox, {
|
|
286
|
+
filename: path.resolve(filename),
|
|
287
|
+
displayErrors: false,
|
|
288
|
+
timeout: 1000
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
else if (isConfigFile == 'yaml') {
|
|
292
|
+
return yamljs.load(confObj.toString());
|
|
293
|
+
}
|
|
294
|
+
else if (isConfigFile == 'js' || isConfigFile == 'mjs') {
|
|
295
|
+
var confPath = require.resolve(path.resolve(filename));
|
|
296
|
+
delete require.cache[confPath];
|
|
297
|
+
return require(confPath);
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
Common.retErr = function(e) {
|
|
302
|
+
if (!e)
|
|
303
|
+
return new Error('Unidentified error');
|
|
304
|
+
if (e instanceof Error)
|
|
305
|
+
return e;
|
|
306
|
+
return new Error(e);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
Common.sink = {};
|
|
310
|
+
|
|
311
|
+
Common.sink.determineCron = function(app) {
|
|
312
|
+
if (app.cron_restart == 0 || app.cron_restart == '0') {
|
|
313
|
+
Common.printOut(cst.PREFIX_MSG + 'disabling cron restart');
|
|
314
|
+
return
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
if (app.cron_restart) {
|
|
318
|
+
const Croner = require('croner');
|
|
319
|
+
|
|
320
|
+
try {
|
|
321
|
+
Common.printOut(cst.PREFIX_MSG + 'cron restart at ' + app.cron_restart);
|
|
322
|
+
Croner(app.cron_restart);
|
|
323
|
+
} catch(ex) {
|
|
324
|
+
return new Error(`Cron pattern error: ${ex.message}`);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Handle alias (fork <=> fork_mode, cluster <=> cluster_mode)
|
|
331
|
+
*/
|
|
332
|
+
Common.sink.determineExecMode = function(app) {
|
|
333
|
+
if (app.exec_mode)
|
|
334
|
+
app.exec_mode = app.exec_mode.replace(/^(fork|cluster)$/, '$1_mode');
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Here we put the default exec mode
|
|
338
|
+
*/
|
|
339
|
+
var isNodeInterpreter = interpreter.isJsRuntime(app.exec_interpreter);
|
|
340
|
+
|
|
341
|
+
if (!app.exec_mode &&
|
|
342
|
+
(app.instances >= 1 || app.instances === 0 || app.instances === -1) &&
|
|
343
|
+
isNodeInterpreter) {
|
|
344
|
+
app.exec_mode = 'cluster_mode';
|
|
345
|
+
} else if (!app.exec_mode) {
|
|
346
|
+
app.exec_mode = 'fork_mode';
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// Cluster mode require()s the script inside a Node.js worker: a binary or
|
|
350
|
+
// a script run by another interpreter (bash, python...) would fail with
|
|
351
|
+
// a SyntaxError on its first bytes (#4775) — fall back to fork mode.
|
|
352
|
+
// JS/TS run by a custom loader (tsx, ts-node) keeps cluster mode
|
|
353
|
+
if (app.exec_mode === 'cluster_mode' &&
|
|
354
|
+
!interpreter.canCluster(app.pm_exec_path, app.exec_interpreter)) {
|
|
355
|
+
Common.printError(cst.PREFIX_MSG_WARNING + chalk.bold.yellow(
|
|
356
|
+
`Cluster mode is only available for Node.js/Bun scripts (${app.pm_exec_path} runs with interpreter '${app.exec_interpreter}'), falling back to fork mode`));
|
|
357
|
+
app.exec_mode = 'fork_mode';
|
|
358
|
+
}
|
|
359
|
+
if (typeof app.instances == 'undefined')
|
|
360
|
+
app.instances = 1;
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
var resolveNodeInterpreter = function(app) {
|
|
364
|
+
if (app.exec_mode && app.exec_mode.indexOf('cluster') > -1) {
|
|
365
|
+
Common.printError(cst.PREFIX_MSG_WARNING + chalk.bold.yellow('Choosing the Node.js version in cluster mode is not supported'));
|
|
366
|
+
return false;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
var nvm_path = cst.IS_WINDOWS ? process.env.NVM_HOME : process.env.NVM_DIR;
|
|
370
|
+
if (!nvm_path) {
|
|
371
|
+
Common.printError(cst.PREFIX_MSG_ERR + chalk.red('NVM is not available in PATH'));
|
|
372
|
+
Common.printError(cst.PREFIX_MSG_ERR + chalk.red('Fallback to node in PATH'));
|
|
373
|
+
var msg = cst.IS_WINDOWS
|
|
374
|
+
? 'https://github.com/coreybutler/nvm-windows/releases/'
|
|
375
|
+
: '$ curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash';
|
|
376
|
+
Common.printOut(cst.PREFIX_MSG_ERR + chalk.bold('Install NVM:\n' + msg));
|
|
377
|
+
}
|
|
378
|
+
else {
|
|
379
|
+
var node_version = app.exec_interpreter.split('@')[1];
|
|
380
|
+
var path_to_node = cst.IS_WINDOWS
|
|
381
|
+
? '/v' + node_version + '/node.exe'
|
|
382
|
+
: '/versions/node/v' + node_version + '/bin/node';
|
|
383
|
+
var nvm_node_path = path.join(nvm_path, path_to_node);
|
|
384
|
+
try {
|
|
385
|
+
fs.accessSync(nvm_node_path);
|
|
386
|
+
} catch(e) {
|
|
387
|
+
Common.printOut(cst.PREFIX_MSG + 'Installing Node v%s', node_version);
|
|
388
|
+
var nvm_bin = path.join(nvm_path, 'nvm.' + (cst.IS_WINDOWS ? 'exe' : 'sh'));
|
|
389
|
+
var nvm_cmd = cst.IS_WINDOWS
|
|
390
|
+
? nvm_bin + ' install ' + node_version
|
|
391
|
+
: '. ' + nvm_bin + ' ; nvm install ' + node_version;
|
|
392
|
+
|
|
393
|
+
Common.printOut(cst.PREFIX_MSG + 'Executing: %s', nvm_cmd);
|
|
394
|
+
|
|
395
|
+
execSync(nvm_cmd, {
|
|
396
|
+
cwd: path.resolve(process.cwd()),
|
|
397
|
+
env: process.env,
|
|
398
|
+
maxBuffer: 20 * 1024 * 1024
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
// in order to support both arch, nvm for Windows renames 'node.exe' to:
|
|
402
|
+
// 'node32.exe' for x32 arch
|
|
403
|
+
// 'node64.exe' for x64 arch
|
|
404
|
+
if (cst.IS_WINDOWS)
|
|
405
|
+
nvm_node_path = nvm_node_path.replace(/node/, 'node' + process.arch.slice(1))
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
Common.printOut(cst.PREFIX_MSG + chalk.green.bold('Setting Node to v%s (path=%s)'),
|
|
409
|
+
node_version,
|
|
410
|
+
nvm_node_path);
|
|
411
|
+
|
|
412
|
+
app.exec_interpreter = nvm_node_path;
|
|
413
|
+
}
|
|
414
|
+
};
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* Resolve interpreter
|
|
418
|
+
*/
|
|
419
|
+
Common.sink.resolveInterpreter = function(app) {
|
|
420
|
+
var noInterpreter = !app.exec_interpreter;
|
|
421
|
+
var extName = path.extname(app.pm_exec_path);
|
|
422
|
+
var betterInterpreter = extItps[extName];
|
|
423
|
+
|
|
424
|
+
// Bun support
|
|
425
|
+
if (noInterpreter && (extName == '.js' || extName == '.ts') && cst.IS_BUN === true) {
|
|
426
|
+
noInterpreter = false
|
|
427
|
+
app.exec_interpreter = process.execPath;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
// TypeScript support: .ts maps to bun, but when bun is not installed
|
|
431
|
+
// fall back to node type stripping when the node version supports it
|
|
432
|
+
if (noInterpreter && extName == '.ts' && betterInterpreter == 'bun' &&
|
|
433
|
+
cst.IS_BUN === false && which('bun') == null) {
|
|
434
|
+
var typestrip = require('./tools/typestrip.js');
|
|
435
|
+
var ts_support = typestrip.supportLevel(typestrip.resolvedNodeVersion());
|
|
436
|
+
|
|
437
|
+
if (ts_support === false)
|
|
438
|
+
throw new Error('TypeScript apps require bun, Node.js >= 22.18 (native type stripping) or a custom interpreter (e.g. --interpreter ts-node)');
|
|
439
|
+
|
|
440
|
+
betterInterpreter = 'node';
|
|
441
|
+
if (ts_support === 'flag') {
|
|
442
|
+
if (!app.node_args)
|
|
443
|
+
app.node_args = [];
|
|
444
|
+
if (app.node_args.indexOf('--experimental-strip-types') === -1)
|
|
445
|
+
app.node_args.push('--experimental-strip-types');
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// No interpreter defined and correspondance in schema hashmap
|
|
450
|
+
if (noInterpreter && betterInterpreter) {
|
|
451
|
+
app.exec_interpreter = betterInterpreter;
|
|
452
|
+
|
|
453
|
+
if (betterInterpreter == "python") {
|
|
454
|
+
if (which('python') == null) {
|
|
455
|
+
if (which('python3') == null)
|
|
456
|
+
Common.printError(cst.PREFIX_MSG_WARNING + chalk.bold.yellow('python and python3 binaries not available in PATH'));
|
|
457
|
+
else
|
|
458
|
+
app.exec_interpreter = 'python3';
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
// Else if no Interpreter detect if process is binary
|
|
463
|
+
else if (noInterpreter) {
|
|
464
|
+
app.exec_interpreter = isBinary(app.pm_exec_path) ? 'none' : process.execPath;
|
|
465
|
+
}
|
|
466
|
+
else if (app.exec_interpreter.indexOf('node@') > -1)
|
|
467
|
+
resolveNodeInterpreter(app);
|
|
468
|
+
|
|
469
|
+
if (app.exec_interpreter.indexOf('python') > -1)
|
|
470
|
+
app.env.PYTHONUNBUFFERED = '1'
|
|
471
|
+
|
|
472
|
+
if (app.exec_interpreter == 'lsc') {
|
|
473
|
+
app.exec_interpreter = path.resolve(__dirname, '../node_modules/.bin/lsc');
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
if (app.exec_interpreter == 'coffee') {
|
|
477
|
+
app.exec_interpreter = path.resolve(__dirname, '../node_modules/.bin/coffee');
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
if (app.exec_interpreter != 'none' && which(app.exec_interpreter) == null) {
|
|
481
|
+
// If node is not present
|
|
482
|
+
if (app.exec_interpreter == 'node') {
|
|
483
|
+
Common.warn(`Using builtin node.js version on version ${process.version}`)
|
|
484
|
+
app.exec_interpreter = cst.BUILTIN_NODE_PATH
|
|
485
|
+
}
|
|
486
|
+
else
|
|
487
|
+
throw new Error(`Interpreter ${app.exec_interpreter} is NOT AVAILABLE in PATH. (type 'which ${app.exec_interpreter}' to double check.)`)
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
return app;
|
|
491
|
+
};
|
|
492
|
+
|
|
493
|
+
Common.deepCopy = Common.serialize = Common.clone = function(obj) {
|
|
494
|
+
if (obj === null || obj === undefined) return {};
|
|
495
|
+
return fclone(obj);
|
|
496
|
+
};
|
|
497
|
+
|
|
498
|
+
Common.errMod = function(msg) {
|
|
499
|
+
if (process.env.PM2_SILENT || process.env.PM2_PROGRAMMATIC === 'true') return false;
|
|
500
|
+
if (msg instanceof Error)
|
|
501
|
+
return console.error(msg.message);
|
|
502
|
+
return console.error(`${cst.PREFIX_MSG_MOD_ERR}${msg}`);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
Common.err = function(msg) {
|
|
506
|
+
if (process.env.PM2_SILENT || process.env.PM2_PROGRAMMATIC === 'true') return false;
|
|
507
|
+
if (msg instanceof Error)
|
|
508
|
+
return console.error(`${cst.PREFIX_MSG_ERR}${msg.message}`);
|
|
509
|
+
return console.error(`${cst.PREFIX_MSG_ERR}${msg}`);
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
Common.printError = function(msg) {
|
|
513
|
+
if (process.env.PM2_SILENT || process.env.PM2_PROGRAMMATIC === 'true') return false;
|
|
514
|
+
if (msg instanceof Error)
|
|
515
|
+
return console.error(msg.message);
|
|
516
|
+
return console.error.apply(console, arguments);
|
|
517
|
+
};
|
|
518
|
+
|
|
519
|
+
Common.log = function(msg) {
|
|
520
|
+
if (process.env.PM2_SILENT || process.env.PM2_PROGRAMMATIC === 'true') return false;
|
|
521
|
+
return console.log(`${cst.PREFIX_MSG}${msg}`);
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
Common.info = function(msg) {
|
|
525
|
+
if (process.env.PM2_SILENT || process.env.PM2_PROGRAMMATIC === 'true') return false;
|
|
526
|
+
return console.log(`${cst.PREFIX_MSG_INFO}${msg}`);
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
Common.warn = function(msg) {
|
|
530
|
+
if (process.env.PM2_SILENT || process.env.PM2_PROGRAMMATIC === 'true') return false;
|
|
531
|
+
return console.log(`${cst.PREFIX_MSG_WARNING}${msg}`);
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
Common.logMod = function(msg) {
|
|
535
|
+
if (process.env.PM2_SILENT || process.env.PM2_PROGRAMMATIC === 'true') return false;
|
|
536
|
+
return console.log(`${cst.PREFIX_MSG_MOD}${msg}`);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
var ANSI_RE = /\x1b\[[0-9;]*m/g;
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* Crop a (possibly ANSI-colored) single line to `width` visible columns so
|
|
543
|
+
* it does not wrap on narrow terminals. ANSI escapes are copied through
|
|
544
|
+
* without counting toward the width, and a reset is appended after the
|
|
545
|
+
* ellipsis so the truncation cannot leak color into following output.
|
|
546
|
+
*/
|
|
547
|
+
function cropToWidth(str, width) {
|
|
548
|
+
var out = '', visible = 0, i = 0;
|
|
549
|
+
while (i < str.length) {
|
|
550
|
+
if (str[i] === '\x1b') {
|
|
551
|
+
var m = str.slice(i).match(/^\x1b\[[0-9;]*m/);
|
|
552
|
+
if (m) { out += m[0]; i += m[0].length; continue; }
|
|
553
|
+
}
|
|
554
|
+
if (visible >= width - 1)
|
|
555
|
+
return out + '\u2026\x1b[0m';
|
|
556
|
+
out += str[i]; visible++; i++;
|
|
557
|
+
}
|
|
558
|
+
return out;
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
Common.printOut = function() {
|
|
562
|
+
if (process.env.PM2_SILENT === 'true' || process.env.PM2_PROGRAMMATIC === 'true') return false;
|
|
563
|
+
var msg = util.format.apply(util, arguments);
|
|
564
|
+
var cols = process.stdout.columns;
|
|
565
|
+
if (cols && msg.indexOf('\n') === -1 && msg.replace(ANSI_RE, '').length > cols)
|
|
566
|
+
msg = cropToWidth(msg, cols);
|
|
567
|
+
return console.log(msg);
|
|
568
|
+
};
|
|
569
|
+
|
|
570
|
+
|
|
571
|
+
/**
|
|
572
|
+
* Raw extend
|
|
573
|
+
*/
|
|
574
|
+
Common.extend = function(destination, source) {
|
|
575
|
+
if (typeof destination !== 'object') {
|
|
576
|
+
destination = {};
|
|
577
|
+
}
|
|
578
|
+
if (!source || typeof source !== 'object') {
|
|
579
|
+
return destination;
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
Object.keys(source).forEach(function(new_key) {
|
|
583
|
+
if (source[new_key] != '[object Object]')
|
|
584
|
+
destination[new_key] = source[new_key];
|
|
585
|
+
});
|
|
586
|
+
|
|
587
|
+
return destination;
|
|
588
|
+
};
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* Copy `add` into `origin`, minus everything PM2 owns.
|
|
592
|
+
*
|
|
593
|
+
* Used to lift an environment out of a pm2_env-shaped object: when starting a
|
|
594
|
+
* script programmatically from inside a managed app, and when listing an app's
|
|
595
|
+
* env back to the user. The key list is shared with the daemon -- it used to be
|
|
596
|
+
* hand-maintained here and had drifted (no `port`, `time`, `trace`, `uid`/`gid`,
|
|
597
|
+
* no `pm_*`/`axm_*` prefixes).
|
|
598
|
+
*/
|
|
599
|
+
Common.safeExtend = function(origin, add){
|
|
600
|
+
if (!add || typeof add != 'object') return origin;
|
|
601
|
+
|
|
602
|
+
var keys = Object.keys(add);
|
|
603
|
+
var i = keys.length;
|
|
604
|
+
while (i--) {
|
|
605
|
+
if (!pm2Keys.isPm2ConfigKey(keys[i]) && add[keys[i]] != '[object Object]')
|
|
606
|
+
origin[keys[i]] = add[keys[i]];
|
|
607
|
+
}
|
|
608
|
+
return origin;
|
|
609
|
+
};
|
|
610
|
+
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* Extend the app.env object of with the properties taken from the
|
|
614
|
+
* app.env_[envName] and deploy configuration.
|
|
615
|
+
* Also update current json attributes
|
|
616
|
+
*
|
|
617
|
+
* Used only for Configuration file processing
|
|
618
|
+
*
|
|
619
|
+
* @param {Object} app The app object.
|
|
620
|
+
* @param {string} envName The given environment name.
|
|
621
|
+
* @param {Object} deployConf Deployment configuration object (from JSON file or whatever).
|
|
622
|
+
* @returns {Object} The app.env variables object.
|
|
623
|
+
*/
|
|
624
|
+
Common.mergeEnvironmentVariables = function(app_env, env_name, deploy_conf) {
|
|
625
|
+
var app = fclone(app_env);
|
|
626
|
+
|
|
627
|
+
var new_conf = {
|
|
628
|
+
env : {}
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
// Stringify possible object
|
|
632
|
+
for (var key in app.env) {
|
|
633
|
+
if (typeof app.env[key] == 'object') {
|
|
634
|
+
app.env[key] = JSON.stringify(app.env[key]);
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
/**
|
|
639
|
+
* Extra configuration update
|
|
640
|
+
*/
|
|
641
|
+
Object.assign(new_conf, app);
|
|
642
|
+
|
|
643
|
+
if (env_name) {
|
|
644
|
+
// First merge variables from deploy.production.env object as least priority.
|
|
645
|
+
if (deploy_conf && deploy_conf[env_name] && deploy_conf[env_name]['env']) {
|
|
646
|
+
Object.assign(new_conf.env, deploy_conf[env_name]['env']);
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
Object.assign(new_conf.env, app.env);
|
|
650
|
+
|
|
651
|
+
// Then, last and highest priority, merge the app.env_production object.
|
|
652
|
+
if ('env_' + env_name in app) {
|
|
653
|
+
Object.assign(new_conf.env, app['env_' + env_name]);
|
|
654
|
+
}
|
|
655
|
+
else {
|
|
656
|
+
Common.printOut(cst.PREFIX_MSG_WARNING + chalk.bold('Environment [%s] is not defined in process file'), env_name);
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
delete new_conf.exec_mode
|
|
661
|
+
|
|
662
|
+
var res = {
|
|
663
|
+
current_conf: {}
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
Object.assign(res, new_conf.env);
|
|
667
|
+
Object.assign(res.current_conf, new_conf);
|
|
668
|
+
|
|
669
|
+
// #2541 force resolution of node interpreter
|
|
670
|
+
if (app.exec_interpreter &&
|
|
671
|
+
app.exec_interpreter.indexOf('@') > -1) {
|
|
672
|
+
resolveNodeInterpreter(app);
|
|
673
|
+
res.current_conf.exec_interpreter = app.exec_interpreter
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
return res
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
/**
|
|
680
|
+
* This function will resolve paths, option and environment
|
|
681
|
+
* CALLED before 'prepare' God call (=> PROCESS INITIALIZATION)
|
|
682
|
+
* @method resolveAppAttributes
|
|
683
|
+
* @param {Object} opts
|
|
684
|
+
* @param {Object} opts.cwd
|
|
685
|
+
* @param {Object} opts.pm2_home
|
|
686
|
+
* @param {Object} appConf application configuration
|
|
687
|
+
* @return app
|
|
688
|
+
*/
|
|
689
|
+
Common.resolveAppAttributes = function(opts, conf) {
|
|
690
|
+
var conf_copy = fclone(conf);
|
|
691
|
+
|
|
692
|
+
var app = Common.prepareAppConf(opts, conf_copy);
|
|
693
|
+
if (app instanceof Error) {
|
|
694
|
+
throw new Error(app.message);
|
|
695
|
+
}
|
|
696
|
+
return app;
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
/**
|
|
700
|
+
* Verify configurations
|
|
701
|
+
* Called on EVERY Operation (start/restart/reload/stop...)
|
|
702
|
+
* @param {Array} appConfs
|
|
703
|
+
* @returns {Array}
|
|
704
|
+
*/
|
|
705
|
+
Common.verifyConfs = function(appConfs) {
|
|
706
|
+
if (!appConfs || appConfs.length == 0) {
|
|
707
|
+
return [];
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
// Make sure it is an Array.
|
|
711
|
+
appConfs = [].concat(appConfs);
|
|
712
|
+
|
|
713
|
+
var verifiedConf = [];
|
|
714
|
+
|
|
715
|
+
for (var i = 0; i < appConfs.length; i++) {
|
|
716
|
+
var app = appConfs[i];
|
|
717
|
+
|
|
718
|
+
if (app.exec_mode)
|
|
719
|
+
app.exec_mode = app.exec_mode.replace(/^(fork|cluster)$/, '$1_mode');
|
|
720
|
+
|
|
721
|
+
// JSON conf: alias cmd to script
|
|
722
|
+
if (app.cmd && !app.script) {
|
|
723
|
+
app.script = app.cmd
|
|
724
|
+
delete app.cmd
|
|
725
|
+
}
|
|
726
|
+
// JSON conf: alias command to script
|
|
727
|
+
if (app.command && !app.script) {
|
|
728
|
+
app.script = app.command
|
|
729
|
+
delete app.command
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
if (!app.env) {
|
|
733
|
+
app.env = {}
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
// Render an app name if not existing.
|
|
737
|
+
Common.renderApplicationName(app);
|
|
738
|
+
|
|
739
|
+
if (app.execute_command == true) {
|
|
740
|
+
app.exec_mode = 'fork'
|
|
741
|
+
delete app.execute_command
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
app.username = Common.getCurrentUsername();
|
|
745
|
+
|
|
746
|
+
/**
|
|
747
|
+
* If command is like pm2 start "python xx.py --ok"
|
|
748
|
+
* Then automatically start the script with bash -c and set a name eq to command
|
|
749
|
+
*/
|
|
750
|
+
if (app.script && app.script.indexOf(' ') > -1 && cst.IS_WINDOWS === false) {
|
|
751
|
+
var _script = app.script;
|
|
752
|
+
|
|
753
|
+
if (which('bash')) {
|
|
754
|
+
app.script = 'bash';
|
|
755
|
+
app.args = ['-c', _script];
|
|
756
|
+
if (!app.name) {
|
|
757
|
+
app.name = _script
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
else if (which('sh')) {
|
|
761
|
+
app.script = 'sh';
|
|
762
|
+
app.args = ['-c', _script];
|
|
763
|
+
if (!app.name) {
|
|
764
|
+
app.name = _script
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
else {
|
|
768
|
+
warn('bash or sh not available in $PATH, keeping script as is')
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
/**
|
|
773
|
+
* Add log_date_format by default
|
|
774
|
+
*/
|
|
775
|
+
if (app.time || process.env.ASZ_MODE) {
|
|
776
|
+
app.log_date_format = 'YYYY-MM-DDTHH:mm:ss'
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
/**
|
|
780
|
+
* Checks + Resolve UID/GID
|
|
781
|
+
* comes from pm2 --uid <> --gid <> or --user
|
|
782
|
+
*/
|
|
783
|
+
if (app.uid || app.gid || app.user) {
|
|
784
|
+
// 1/ Check if windows
|
|
785
|
+
if (cst.IS_WINDOWS === true) {
|
|
786
|
+
Common.printError(cst.PREFIX_MSG_ERR + '--uid and --git does not works on windows');
|
|
787
|
+
return new Error('--uid and --git does not works on windows');
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
// 2/ Verify that user is root (todo: verify if other has right)
|
|
791
|
+
if (process.env.NODE_ENV != 'test' && process.getuid && process.getuid() !== 0) {
|
|
792
|
+
Common.printError(cst.PREFIX_MSG_ERR + 'To use --uid and --gid please run pm2 as root');
|
|
793
|
+
return new Error('To use UID and GID please run PM2 as root');
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
// 3/ Resolve user info via /etc/password
|
|
797
|
+
var passwd = require('./tools/passwd.js')
|
|
798
|
+
var users
|
|
799
|
+
try {
|
|
800
|
+
users = passwd.getUsers()
|
|
801
|
+
} catch(e) {
|
|
802
|
+
Common.printError(e);
|
|
803
|
+
return new Error(e);
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
var user_info = users[app.uid || app.user]
|
|
807
|
+
if (!user_info) {
|
|
808
|
+
Common.printError(`${cst.PREFIX_MSG_ERR} User ${app.uid || app.user} cannot be found`);
|
|
809
|
+
return new Error(`${cst.PREFIX_MSG_ERR} User ${app.uid || app.user} cannot be found`);
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
app.env.HOME = user_info.homedir
|
|
813
|
+
app.uid = parseInt(user_info.userId)
|
|
814
|
+
|
|
815
|
+
// 4/ Resolve group id if gid is specified
|
|
816
|
+
if (app.gid) {
|
|
817
|
+
var groups
|
|
818
|
+
try {
|
|
819
|
+
groups = passwd.getGroups()
|
|
820
|
+
} catch(e) {
|
|
821
|
+
Common.printError(e);
|
|
822
|
+
return new Error(e);
|
|
823
|
+
}
|
|
824
|
+
var group_info = groups[app.gid]
|
|
825
|
+
if (!group_info) {
|
|
826
|
+
Common.printError(`${cst.PREFIX_MSG_ERR} Group ${app.gid} cannot be found`);
|
|
827
|
+
return new Error(`${cst.PREFIX_MSG_ERR} Group ${app.gid} cannot be found`);
|
|
828
|
+
}
|
|
829
|
+
app.gid = parseInt(group_info.id)
|
|
830
|
+
} else {
|
|
831
|
+
app.gid = parseInt(user_info.groupId)
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
/**
|
|
836
|
+
* Specific options of PM2.io
|
|
837
|
+
*/
|
|
838
|
+
if (process.env.PM2_DEEP_MONITORING) {
|
|
839
|
+
app.deep_monitoring = true;
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
if (app.automation == false) {
|
|
843
|
+
app.pmx = false;
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
if (app.disable_trace) {
|
|
847
|
+
app.trace = false
|
|
848
|
+
delete app.disable_trace;
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
if (app.trace === true || app.deep_monitoring === true) {
|
|
852
|
+
var OtelManager = require('./OtelManager');
|
|
853
|
+
if (!OtelManager.ensureInstalled()) {
|
|
854
|
+
Common.printError(cst.PREFIX_MSG_ERR + 'Tracing disabled: OpenTelemetry packages not available');
|
|
855
|
+
app.trace = false;
|
|
856
|
+
delete app.deep_monitoring;
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
/**
|
|
861
|
+
* Instances params
|
|
862
|
+
*/
|
|
863
|
+
if (app.instances == 'max') {
|
|
864
|
+
app.instances = 0;
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
if (typeof(app.instances) === 'string') {
|
|
868
|
+
app.instances = parseInt(app.instances) || 0;
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
if (app.exec_mode != 'cluster_mode' &&
|
|
872
|
+
!app.instances &&
|
|
873
|
+
typeof(app.merge_logs) == 'undefined') {
|
|
874
|
+
app.merge_logs = true;
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
var ret;
|
|
878
|
+
|
|
879
|
+
if (app.cron_restart) {
|
|
880
|
+
if ((ret = Common.sink.determineCron(app)) instanceof Error)
|
|
881
|
+
return ret;
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
// validateJSON drops a false: keep it as "no filter", so a file can turn
|
|
885
|
+
// off a filter_env the app was started with (#6176)
|
|
886
|
+
if (app.filter_env === false)
|
|
887
|
+
app.filter_env = [];
|
|
888
|
+
|
|
889
|
+
/**
|
|
890
|
+
* Now validation configuration
|
|
891
|
+
*/
|
|
892
|
+
var ret = Config.validateJSON(app);
|
|
893
|
+
if (ret.errors && ret.errors.length > 0){
|
|
894
|
+
ret.errors.forEach(function(err) { warn(err) });
|
|
895
|
+
return new Error(ret.errors);
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
verifiedConf.push(ret.config);
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
return verifiedConf;
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
/**
|
|
905
|
+
* Get current username
|
|
906
|
+
* Called on EVERY starting app
|
|
907
|
+
*
|
|
908
|
+
* @returns {String}
|
|
909
|
+
*/
|
|
910
|
+
Common.getCurrentUsername = function(){
|
|
911
|
+
var current_user = '';
|
|
912
|
+
|
|
913
|
+
if (os.userInfo) {
|
|
914
|
+
try {
|
|
915
|
+
current_user = os.userInfo().username;
|
|
916
|
+
} catch (err) {
|
|
917
|
+
// For the case of unhandled error for uv_os_get_passwd
|
|
918
|
+
// https://github.com/Unitech/pm2/issues/3184
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
if(current_user === '') {
|
|
923
|
+
current_user = process.env.USER || process.env.LNAME || process.env.USERNAME || process.env.SUDO_USER || process.env.C9_USER || process.env.LOGNAME;
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
return current_user;
|
|
927
|
+
}
|
|
928
|
+
|
|
929
|
+
/**
|
|
930
|
+
* Render an app name if not existing.
|
|
931
|
+
* @param {Object} conf
|
|
932
|
+
*/
|
|
933
|
+
Common.renderApplicationName = function(conf){
|
|
934
|
+
if (!conf.name && conf.script){
|
|
935
|
+
conf.name = conf.script !== undefined ? path.basename(conf.script) : 'undefined';
|
|
936
|
+
var lastDot = conf.name.lastIndexOf('.');
|
|
937
|
+
if (lastDot > 0){
|
|
938
|
+
conf.name = conf.name.slice(0, lastDot);
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
/**
|
|
944
|
+
* Show warnings
|
|
945
|
+
* @param {String} warning
|
|
946
|
+
*/
|
|
947
|
+
function warn(warning){
|
|
948
|
+
Common.printOut(cst.PREFIX_MSG_WARNING + warning);
|
|
949
|
+
}
|