@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.
Files changed (133) hide show
  1. package/.claude/settings.local.json +8 -0
  2. package/.gitattributes +4 -0
  3. package/.mocharc.js +14 -0
  4. package/CHANGELOG.md +2416 -0
  5. package/CLAUDE.md +84 -0
  6. package/CONTRIBUTING.md +124 -0
  7. package/GNU-AGPL-3.0.txt +665 -0
  8. package/LICENSE +1 -0
  9. package/README.md +248 -0
  10. package/bin/zm2 +3 -0
  11. package/bin/zm2-dev +3 -0
  12. package/bin/zm2-docker +3 -0
  13. package/bin/zm2-runtime +3 -0
  14. package/bin/zm2-windows +3 -0
  15. package/bin/zm2.ps1 +3 -0
  16. package/bun.lock +421 -0
  17. package/constants.js +114 -0
  18. package/index.js +13 -0
  19. package/lib/API/Configuration.js +212 -0
  20. package/lib/API/Containerizer.js +335 -0
  21. package/lib/API/Dashboard.js +459 -0
  22. package/lib/API/Deploy.js +117 -0
  23. package/lib/API/Extra.js +775 -0
  24. package/lib/API/ExtraMgmt/Docker.js +30 -0
  25. package/lib/API/Log.js +315 -0
  26. package/lib/API/LogManagement.js +371 -0
  27. package/lib/API/Modules/LOCAL.js +122 -0
  28. package/lib/API/Modules/Modularizer.js +148 -0
  29. package/lib/API/Modules/NPM.js +445 -0
  30. package/lib/API/Modules/TAR.js +362 -0
  31. package/lib/API/Modules/flagExt.js +46 -0
  32. package/lib/API/Modules/index.js +120 -0
  33. package/lib/API/Monit.js +247 -0
  34. package/lib/API/Serve.js +343 -0
  35. package/lib/API/Startup.js +629 -0
  36. package/lib/API/UX/helpers.js +213 -0
  37. package/lib/API/UX/index.js +9 -0
  38. package/lib/API/UX/pm2-describe.js +193 -0
  39. package/lib/API/UX/pm2-ls-minimal.js +31 -0
  40. package/lib/API/UX/pm2-ls.js +483 -0
  41. package/lib/API/Version.js +382 -0
  42. package/lib/API/interpreter.json +12 -0
  43. package/lib/API/pm2-plus/PM2IO.js +372 -0
  44. package/lib/API/pm2-plus/auth-strategies/CliAuth.js +288 -0
  45. package/lib/API/pm2-plus/auth-strategies/WebAuth.js +187 -0
  46. package/lib/API/pm2-plus/helpers.js +97 -0
  47. package/lib/API/pm2-plus/link.js +126 -0
  48. package/lib/API/pm2-plus/pres/motd +16 -0
  49. package/lib/API/pm2-plus/pres/motd.update +26 -0
  50. package/lib/API/pm2-plus/pres/welcome +28 -0
  51. package/lib/API/pm2-plus/process-selector.js +52 -0
  52. package/lib/API/schema.json +379 -0
  53. package/lib/API.js +1931 -0
  54. package/lib/Client.js +776 -0
  55. package/lib/Common.js +911 -0
  56. package/lib/Configuration.js +304 -0
  57. package/lib/Daemon.js +456 -0
  58. package/lib/Event.js +37 -0
  59. package/lib/God/ActionMethods.js +909 -0
  60. package/lib/God/ClusterMode.js +97 -0
  61. package/lib/God/ForkMode.js +297 -0
  62. package/lib/God/Methods.js +265 -0
  63. package/lib/God/Reload.js +240 -0
  64. package/lib/God.js +632 -0
  65. package/lib/HttpInterface.js +76 -0
  66. package/lib/ProcessContainer.js +305 -0
  67. package/lib/ProcessContainerBun.js +360 -0
  68. package/lib/ProcessContainerFork.js +42 -0
  69. package/lib/ProcessContainerForkBun.js +33 -0
  70. package/lib/ProcessUtils.js +55 -0
  71. package/lib/TreeKill.js +118 -0
  72. package/lib/Utility.js +430 -0
  73. package/lib/VersionCheck.js +46 -0
  74. package/lib/Watcher.js +117 -0
  75. package/lib/Worker.js +169 -0
  76. package/lib/binaries/CLI.js +1041 -0
  77. package/lib/binaries/DevCLI.js +183 -0
  78. package/lib/binaries/Runtime.js +101 -0
  79. package/lib/binaries/Runtime4Docker.js +192 -0
  80. package/lib/completion.js +229 -0
  81. package/lib/completion.sh +40 -0
  82. package/lib/motd +36 -0
  83. package/lib/templates/Dockerfiles/Dockerfile-java.tpl +7 -0
  84. package/lib/templates/Dockerfiles/Dockerfile-nodejs.tpl +8 -0
  85. package/lib/templates/Dockerfiles/Dockerfile-ruby.tpl +7 -0
  86. package/lib/templates/ecosystem-es.tpl +24 -0
  87. package/lib/templates/ecosystem-simple-es.tpl +8 -0
  88. package/lib/templates/ecosystem-simple.tpl +6 -0
  89. package/lib/templates/ecosystem.tpl +22 -0
  90. package/lib/templates/init-scripts/launchd.tpl +35 -0
  91. package/lib/templates/init-scripts/openrc.tpl +52 -0
  92. package/lib/templates/init-scripts/pm2-init-amazon.sh +86 -0
  93. package/lib/templates/init-scripts/rcd-openbsd.tpl +41 -0
  94. package/lib/templates/init-scripts/rcd.tpl +44 -0
  95. package/lib/templates/init-scripts/smf.tpl +43 -0
  96. package/lib/templates/init-scripts/systemd-online.tpl +22 -0
  97. package/lib/templates/init-scripts/systemd.tpl +22 -0
  98. package/lib/templates/init-scripts/upstart.tpl +103 -0
  99. package/lib/templates/logrotate.d/pm2 +10 -0
  100. package/lib/templates/sample-apps/http-server/README.md +14 -0
  101. package/lib/templates/sample-apps/http-server/api.js +9 -0
  102. package/lib/templates/sample-apps/http-server/ecosystem.config.js +14 -0
  103. package/lib/templates/sample-apps/http-server/package.json +11 -0
  104. package/lib/templates/sample-apps/pm2-plus-metrics-actions/README.md +45 -0
  105. package/lib/templates/sample-apps/pm2-plus-metrics-actions/custom-metrics.js +66 -0
  106. package/lib/templates/sample-apps/pm2-plus-metrics-actions/ecosystem.config.js +12 -0
  107. package/lib/templates/sample-apps/pm2-plus-metrics-actions/package.json +11 -0
  108. package/lib/templates/sample-apps/python-app/README.md +4 -0
  109. package/lib/templates/sample-apps/python-app/echo.py +7 -0
  110. package/lib/templates/sample-apps/python-app/ecosystem.config.js +12 -0
  111. package/lib/templates/sample-apps/python-app/package.json +11 -0
  112. package/lib/tools/Config.js +248 -0
  113. package/lib/tools/IsAbsolute.js +20 -0
  114. package/lib/tools/copydirSync.js +101 -0
  115. package/lib/tools/deleteFolderRecursive.js +19 -0
  116. package/lib/tools/find-package-json.js +74 -0
  117. package/lib/tools/fmt.js +72 -0
  118. package/lib/tools/isbinaryfile.js +94 -0
  119. package/lib/tools/json5.js +752 -0
  120. package/lib/tools/open.js +63 -0
  121. package/lib/tools/passwd.js +58 -0
  122. package/lib/tools/promise.min.js +1 -0
  123. package/lib/tools/sexec.js +55 -0
  124. package/lib/tools/treeify.js +113 -0
  125. package/lib/tools/which.js +120 -0
  126. package/lib/tools/xdg-open +861 -0
  127. package/package.json +219 -0
  128. package/paths.js +93 -0
  129. package/pm2 +11 -0
  130. package/preinstall.js +24 -0
  131. package/run.sh +9 -0
  132. package/types/index.d.ts +722 -0
  133. package/types/tsconfig.json +14 -0
@@ -0,0 +1,30 @@
1
+
2
+ const util = require('util')
3
+ const spawn = require('child_process').spawn
4
+ const DockerMgmt = {}
5
+
6
+ module.exports = DockerMgmt
7
+
8
+ function execDocker(cmd, cb) {
9
+ var i = spawn('docker', cmd, {
10
+ stdio : 'inherit',
11
+ env: process.env,
12
+ shell : true
13
+ })
14
+
15
+ i.on('close', cb)
16
+ }
17
+
18
+ DockerMgmt.processCommand = function(PM2, start_id, select_id, action, cb) {
19
+ PM2.Client.executeRemote('getSystemData', {}, (err, sys_infos) => {
20
+ if (sys_infos.containers && sys_infos.containers.length == 0)
21
+ return cb(new Error(`Process ${select_id} not found`))
22
+ var container = sys_infos.containers[select_id - start_id - 1]
23
+ if (action == 'stopProcessId')
24
+ execDocker(['stop', container.id], cb)
25
+ if (action == 'deleteProcessId')
26
+ execDocker(['rm', container.id], cb)
27
+ if (action == 'restartProcessId')
28
+ execDocker(['restart', container.id], cb)
29
+ })
30
+ }
package/lib/API/Log.js ADDED
@@ -0,0 +1,315 @@
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
+ util = require('util'),
8
+ chalk = require('ansis'),
9
+ forEachLimit = require('async/forEachLimit'),
10
+ dayjs = require('dayjs');
11
+
12
+ var Log = module.exports = {};
13
+
14
+ var DEFAULT_PADDING = ' ';
15
+
16
+ /**
17
+ * Tail logs from file stream.
18
+ * @param {Object} apps_list
19
+ * @param {Number} lines
20
+ * @param {Boolean} raw
21
+ * @param {Function} callback
22
+ * @return
23
+ */
24
+
25
+ Log.tail = function(apps_list, lines, raw, callback) {
26
+ var that = this;
27
+
28
+ if (lines === 0 || apps_list.length === 0)
29
+ return callback && callback();
30
+
31
+ var count = 0;
32
+
33
+ var getLastLines = function (filename, lines, callback) {
34
+ var chunk = '';
35
+ var size = Math.max(0, fs.statSync(filename).size - (lines * 200));
36
+
37
+ var fd = fs.createReadStream(filename, {start : size});
38
+ fd.on('data', function(data) { chunk += data.toString(); });
39
+ fd.on('end', function() {
40
+ chunk = chunk.split('\n').slice(-(lines+1));
41
+ chunk.pop();
42
+ callback(chunk);
43
+ });
44
+ };
45
+
46
+ apps_list.sort(function(a, b) {
47
+ return (fs.existsSync(a.path) ? fs.statSync(a.path).mtime.valueOf() : 0) -
48
+ (fs.existsSync(b.path) ? fs.statSync(b.path).mtime.valueOf() : 0);
49
+ });
50
+
51
+ forEachLimit(apps_list, 1, function(app, next) {
52
+ if (!fs.existsSync(app.path || ''))
53
+ return next();
54
+
55
+ getLastLines(app.path, lines, function(output) {
56
+ console.log(chalk.gray('%s last %d lines:'), app.path, lines);
57
+ output.forEach(function(out) {
58
+ if (raw)
59
+ return app.type === 'err' ? console.error(out) : console.log(out);
60
+ if (app.type === 'out')
61
+ process.stdout.write(chalk.green(pad(DEFAULT_PADDING, app.app_name) + ' | '));
62
+ else if (app.type === 'err')
63
+ process.stdout.write(chalk.red(pad(DEFAULT_PADDING, app.app_name) + ' | '));
64
+ else
65
+ process.stdout.write(chalk.blue(pad(DEFAULT_PADDING, 'PM2') + ' | '));
66
+ console.log(out);
67
+ });
68
+ if (output.length)
69
+ process.stdout.write('\n');
70
+ next();
71
+ });
72
+ }, function() {
73
+ callback && callback();
74
+ });
75
+ };
76
+
77
+ /**
78
+ * Stream logs in realtime from the bus eventemitter.
79
+ * @param {String} id
80
+ * @param {Boolean} raw
81
+ * @return
82
+ */
83
+
84
+ Log.stream = function(Client, id, raw, timestamp, exclusive, highlight) {
85
+ var that = this;
86
+
87
+ Client.launchBus(function(err, bus, socket) {
88
+
89
+ socket.on('reconnect attempt', function() {
90
+ if (global._auto_exit === true) {
91
+ if (timestamp)
92
+ process.stdout.write(chalk['dim'](chalk.gray(dayjs().format(timestamp) + ' ')));
93
+ process.stdout.write(chalk.blue(pad(DEFAULT_PADDING, 'PM2') + ' | ') + '[[[ Target PM2 killed. ]]]');
94
+ process.exit(0);
95
+ }
96
+ });
97
+
98
+ var min_padding = 3
99
+
100
+ bus.on('log:*', function(type, packet) {
101
+ var isMatchingProcess = id === 'all'
102
+ || packet.process.name == id
103
+ || packet.process.pm_id == id
104
+ || packet.process.namespace == id;
105
+
106
+ if (!isMatchingProcess)
107
+ return;
108
+
109
+ if ((type === 'out' && exclusive === 'err')
110
+ || (type === 'err' && exclusive === 'out')
111
+ || (type === 'PM2' && exclusive !== false))
112
+ return;
113
+
114
+ var lines;
115
+
116
+ if (typeof(packet.data) === 'string')
117
+ lines = (packet.data || '').split('\n');
118
+ else
119
+ return;
120
+
121
+ lines.forEach(function(line) {
122
+ if (!line || line.length === 0) return;
123
+
124
+ if (raw)
125
+ return type === 'err' ? process.stderr.write(util.format(line) + '\n') : process.stdout.write(util.format(line) + '\n');
126
+
127
+ if (timestamp)
128
+ process.stdout.write(chalk['dim'](chalk.gray(dayjs().format(timestamp) + ' ')));
129
+
130
+ var name = packet.process.pm_id + '|' + packet.process.name;
131
+
132
+ if (name.length > min_padding)
133
+ min_padding = name.length + 1
134
+
135
+ if (type === 'out')
136
+ process.stdout.write(chalk.green(pad(' '.repeat(min_padding), name) + ' | '));
137
+ else if (type === 'err')
138
+ process.stdout.write(chalk.red(pad(' '.repeat(min_padding), name) + ' | '));
139
+ else if (!raw && (id === 'all' || id === 'PM2'))
140
+ process.stdout.write(chalk.blue(pad(' '.repeat(min_padding), 'PM2') + ' | '));
141
+ if (highlight)
142
+ process.stdout.write(util.format(line).replace(highlight, chalk.bgBlackBright(highlight)) + '\n');
143
+ else
144
+ process.stdout.write(util.format(line)+ '\n');
145
+ });
146
+ });
147
+ });
148
+ };
149
+
150
+ Log.devStream = function(Client, id, raw, timestamp, exclusive) {
151
+ var that = this;
152
+
153
+ Client.launchBus(function(err, bus) {
154
+
155
+ setTimeout(function() {
156
+ bus.on('process:event', function(packet) {
157
+ if (packet.event == 'online')
158
+ console.log(chalk.green('[rundev] App %s restarted'), packet.process.name);
159
+ });
160
+ }, 1000);
161
+
162
+ var min_padding = 3
163
+
164
+ bus.on('log:*', function(type, packet) {
165
+ if (id !== 'all'
166
+ && packet.process.name != id
167
+ && packet.process.pm_id != id)
168
+ return;
169
+
170
+ if ((type === 'out' && exclusive === 'err')
171
+ || (type === 'err' && exclusive === 'out')
172
+ || (type === 'PM2' && exclusive !== false))
173
+ return;
174
+
175
+ if (type === 'PM2')
176
+ return;
177
+
178
+ var name = packet.process.pm_id + '|' + packet.process.name;
179
+
180
+ var lines;
181
+
182
+ if (typeof(packet.data) === 'string')
183
+ lines = (packet.data || '').split('\n');
184
+ else
185
+ return;
186
+
187
+ lines.forEach(function(line) {
188
+ if (!line || line.length === 0) return;
189
+
190
+ if (raw)
191
+ return process.stdout.write(util.format(line) + '\n');
192
+
193
+ if (timestamp)
194
+ process.stdout.write(chalk['dim'](chalk.gray(dayjs().format(timestamp) + ' ')));
195
+
196
+ var name = packet.process.name + '-' + packet.process.pm_id;
197
+
198
+ if (name.length > min_padding)
199
+ min_padding = name.length + 1
200
+
201
+ if (type === 'out')
202
+ process.stdout.write(chalk.green(pad(' '.repeat(min_padding), name) + ' | '));
203
+ else if (type === 'err')
204
+ process.stdout.write(chalk.red(pad(' '.repeat(min_padding), name) + ' | '));
205
+ else if (!raw && (id === 'all' || id === 'PM2'))
206
+ process.stdout.write(chalk.blue(pad(' '.repeat(min_padding), 'PM2') + ' | '));
207
+ process.stdout.write(util.format(line) + '\n');
208
+ });
209
+ });
210
+ });
211
+ };
212
+
213
+ Log.jsonStream = function(Client, id) {
214
+ var that = this;
215
+
216
+ Client.launchBus(function(err, bus) {
217
+ if (err) console.error(err);
218
+
219
+ bus.on('process:event', function(packet) {
220
+ process.stdout.write(JSON.stringify({
221
+ timestamp : dayjs(packet.at),
222
+ type : 'process_event',
223
+ status : packet.event,
224
+ app_name : packet.process.name
225
+ }));
226
+ process.stdout.write('\n');
227
+ });
228
+
229
+ bus.on('log:*', function(type, packet) {
230
+ if (id !== 'all'
231
+ && packet.process.name != id
232
+ && packet.process.pm_id != id)
233
+ return;
234
+
235
+ if (type === 'PM2')
236
+ return;
237
+
238
+ if (typeof(packet.data) == 'string')
239
+ packet.data = packet.data.replace(/(\r\n|\n|\r)/gm,'');
240
+
241
+ process.stdout.write(JSON.stringify({
242
+ message : packet.data,
243
+ timestamp : dayjs(packet.at),
244
+ type : type,
245
+ process_id : packet.process.pm_id,
246
+ app_name : packet.process.name
247
+ }));
248
+ process.stdout.write('\n');
249
+ });
250
+ });
251
+ };
252
+
253
+ Log.formatStream = function(Client, id, raw, timestamp, exclusive, highlight) {
254
+ var that = this;
255
+
256
+ Client.launchBus(function(err, bus) {
257
+
258
+ bus.on('log:*', function(type, packet) {
259
+ if (id !== 'all'
260
+ && packet.process.name != id
261
+ && packet.process.pm_id != id)
262
+ return;
263
+
264
+ if ((type === 'out' && exclusive === 'err')
265
+ || (type === 'err' && exclusive === 'out')
266
+ || (type === 'PM2' && exclusive !== false))
267
+ return;
268
+
269
+ if (type === 'PM2' && raw)
270
+ return;
271
+
272
+ var name = packet.process.name + '-' + packet.process.pm_id;
273
+
274
+ var lines;
275
+
276
+ if (typeof(packet.data) === 'string')
277
+ lines = (packet.data || '').split('\n');
278
+ else
279
+ return;
280
+
281
+ lines.forEach(function(line) {
282
+ if (!line || line.length === 0) return;
283
+
284
+ if (!raw) {
285
+ if (timestamp)
286
+ process.stdout.write('timestamp=' + dayjs().format(timestamp) + ' ');
287
+ if (packet.process.name === 'PM2')
288
+ process.stdout.write('app=pm2 ');
289
+ if (packet.process.name !== 'PM2')
290
+ process.stdout.write('app=' + packet.process.name + ' id=' + packet.process.pm_id + ' ');
291
+ if (type === 'out')
292
+ process.stdout.write('type=out ');
293
+ else if (type === 'err')
294
+ process.stdout.write('type=error ');
295
+ }
296
+
297
+ process.stdout.write('message=');
298
+ if (highlight)
299
+ process.stdout.write(util.format(line).replace(highlight, chalk.bgBlackBright(highlight)) + '\n');
300
+ else
301
+ process.stdout.write(util.format(line) + '\n');
302
+ });
303
+ });
304
+ });
305
+ };
306
+
307
+ function pad(pad, str, padLeft) {
308
+ if (typeof str === 'undefined')
309
+ return pad;
310
+ if (padLeft) {
311
+ return (pad + str).slice(-pad.length);
312
+ } else {
313
+ return (str + pad).substring(0, pad.length);
314
+ }
315
+ }