@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.
Files changed (267) hide show
  1. package/CHANGELOG.md +2607 -0
  2. package/CONTRIBUTING.md +124 -0
  3. package/GNU-AGPL-3.0.txt +665 -0
  4. package/LICENSE +1 -0
  5. package/README.md +255 -2
  6. package/SECURITY.md +41 -0
  7. package/bin/pm2 +3 -0
  8. package/bin/pm2-dev +3 -0
  9. package/bin/pm2-docker +3 -0
  10. package/bin/pm2-runtime +3 -0
  11. package/bin/pm2.ps1 +3 -0
  12. package/constants.js +113 -0
  13. package/index.js +12 -0
  14. package/lib/API/Configuration.js +212 -0
  15. package/lib/API/Containerizer.js +335 -0
  16. package/lib/API/Dashboard.js +726 -0
  17. package/lib/API/Deploy.js +117 -0
  18. package/lib/API/Extra.js +798 -0
  19. package/lib/API/ExtraMgmt/Docker.js +30 -0
  20. package/lib/API/Log.js +388 -0
  21. package/lib/API/LogManagement.js +351 -0
  22. package/lib/API/Modules/LOCAL.js +122 -0
  23. package/lib/API/Modules/Modularizer.js +148 -0
  24. package/lib/API/Modules/NPM.js +443 -0
  25. package/lib/API/Modules/TAR.js +368 -0
  26. package/lib/API/Modules/flagExt.js +46 -0
  27. package/lib/API/Modules/index.js +120 -0
  28. package/lib/API/Monit.js +247 -0
  29. package/lib/API/Serve.js +497 -0
  30. package/lib/API/Startup.js +630 -0
  31. package/lib/API/UX/helpers.js +213 -0
  32. package/lib/API/UX/index.js +9 -0
  33. package/lib/API/UX/pm2-describe.js +197 -0
  34. package/lib/API/UX/pm2-ls-minimal.js +31 -0
  35. package/lib/API/UX/pm2-ls.js +581 -0
  36. package/lib/API/Version.js +383 -0
  37. package/lib/API/interpreter.json +12 -0
  38. package/lib/API/pm2-plus/PM2IO.js +381 -0
  39. package/lib/API/pm2-plus/auth-strategies/CliAuth.js +306 -0
  40. package/lib/API/pm2-plus/auth-strategies/WebAuth.js +195 -0
  41. package/lib/API/pm2-plus/helpers.js +97 -0
  42. package/lib/API/pm2-plus/link.js +126 -0
  43. package/lib/API/pm2-plus/pres/motd +16 -0
  44. package/lib/API/pm2-plus/pres/motd.update +26 -0
  45. package/lib/API/pm2-plus/pres/welcome +28 -0
  46. package/lib/API/pm2-plus/process-selector.js +52 -0
  47. package/lib/API/schema.json +375 -0
  48. package/lib/API.js +1980 -0
  49. package/lib/Client.js +793 -0
  50. package/lib/Common.js +949 -0
  51. package/lib/Configuration.js +309 -0
  52. package/lib/Daemon.js +468 -0
  53. package/lib/Event.js +37 -0
  54. package/lib/God/ActionMethods.js +955 -0
  55. package/lib/God/ClusterMode.js +192 -0
  56. package/lib/God/ForkMode.js +205 -0
  57. package/lib/God/Methods.js +292 -0
  58. package/lib/God/Reload.js +300 -0
  59. package/lib/God/StdioLogger.js +239 -0
  60. package/lib/God.js +653 -0
  61. package/lib/HttpInterface.js +75 -0
  62. package/lib/OtelManager.js +74 -0
  63. package/lib/ProcessContainer.js +321 -0
  64. package/lib/ProcessContainerBun.js +355 -0
  65. package/lib/ProcessContainerFork.js +42 -0
  66. package/lib/ProcessContainerForkBun.js +31 -0
  67. package/lib/ProcessUtils.js +74 -0
  68. package/lib/TreeKill.js +111 -0
  69. package/lib/Utility.js +304 -0
  70. package/lib/VersionCheck.js +46 -0
  71. package/lib/Watcher.js +117 -0
  72. package/lib/Worker.js +200 -0
  73. package/lib/binaries/CLI.js +1092 -0
  74. package/lib/binaries/DevCLI.js +183 -0
  75. package/lib/binaries/Runtime.js +101 -0
  76. package/lib/binaries/Runtime4Docker.js +192 -0
  77. package/lib/completion.js +229 -0
  78. package/lib/completion.sh +40 -0
  79. package/lib/motd +36 -0
  80. package/lib/templates/Dockerfiles/Dockerfile-nodejs.tpl +8 -0
  81. package/lib/templates/ecosystem-es.tpl +24 -0
  82. package/lib/templates/ecosystem-simple-es.tpl +8 -0
  83. package/lib/templates/ecosystem-simple.tpl +6 -0
  84. package/lib/templates/ecosystem.tpl +22 -0
  85. package/lib/templates/init-scripts/launchd.tpl +35 -0
  86. package/lib/templates/init-scripts/openrc.tpl +52 -0
  87. package/lib/templates/init-scripts/pm2-init-amazon.sh +86 -0
  88. package/lib/templates/init-scripts/rcd-openbsd.tpl +41 -0
  89. package/lib/templates/init-scripts/rcd.tpl +44 -0
  90. package/lib/templates/init-scripts/smf.tpl +43 -0
  91. package/lib/templates/init-scripts/systemd-online.tpl +22 -0
  92. package/lib/templates/init-scripts/systemd.tpl +22 -0
  93. package/lib/templates/init-scripts/upstart.tpl +103 -0
  94. package/lib/templates/logrotate.d/pm2 +10 -0
  95. package/lib/templates/sample-apps/http-server/README.md +14 -0
  96. package/lib/templates/sample-apps/http-server/api.js +9 -0
  97. package/lib/templates/sample-apps/http-server/ecosystem.config.js +14 -0
  98. package/lib/templates/sample-apps/http-server/package.json +11 -0
  99. package/lib/templates/sample-apps/pm2-plus-metrics-actions/README.md +45 -0
  100. package/lib/templates/sample-apps/pm2-plus-metrics-actions/custom-metrics.js +66 -0
  101. package/lib/templates/sample-apps/pm2-plus-metrics-actions/ecosystem.config.js +12 -0
  102. package/lib/templates/sample-apps/pm2-plus-metrics-actions/package.json +11 -0
  103. package/lib/templates/sample-apps/python-app/README.md +4 -0
  104. package/lib/templates/sample-apps/python-app/echo.py +7 -0
  105. package/lib/templates/sample-apps/python-app/ecosystem.config.js +12 -0
  106. package/lib/templates/sample-apps/python-app/package.json +11 -0
  107. package/lib/tools/Config.js +251 -0
  108. package/lib/tools/SysMetrics.js +375 -0
  109. package/lib/tools/copydirSync.js +101 -0
  110. package/lib/tools/deleteFolderRecursive.js +19 -0
  111. package/lib/tools/find-package-json.js +74 -0
  112. package/lib/tools/fmt.js +72 -0
  113. package/lib/tools/interpreter.js +88 -0
  114. package/lib/tools/isbinaryfile.js +94 -0
  115. package/lib/tools/json5.js +752 -0
  116. package/lib/tools/multimeter/bar.js +111 -0
  117. package/lib/tools/multimeter/charm/encode.js +18 -0
  118. package/lib/tools/multimeter/charm/index.js +305 -0
  119. package/lib/tools/multimeter/index.js +76 -0
  120. package/lib/tools/open.js +56 -0
  121. package/lib/tools/passwd.js +58 -0
  122. package/lib/tools/pm2-config-keys.js +165 -0
  123. package/lib/tools/prompt.js +76 -0
  124. package/lib/tools/serviceStop.js +149 -0
  125. package/lib/tools/sexec.js +55 -0
  126. package/lib/tools/treeify.js +113 -0
  127. package/lib/tools/typestrip.js +55 -0
  128. package/lib/tools/which.js +120 -0
  129. package/lib/tools/xdg-open +861 -0
  130. package/modules/blessed/LICENSE +20 -0
  131. package/modules/blessed/README.md +34 -0
  132. package/modules/blessed/index.js +1 -0
  133. package/modules/blessed/lib/alias.js +526 -0
  134. package/modules/blessed/lib/blessed.js +32 -0
  135. package/modules/blessed/lib/colors.js +530 -0
  136. package/modules/blessed/lib/events.js +189 -0
  137. package/modules/blessed/lib/gpmclient.js +221 -0
  138. package/modules/blessed/lib/helpers.js +165 -0
  139. package/modules/blessed/lib/keys.js +339 -0
  140. package/modules/blessed/lib/program.js +4297 -0
  141. package/modules/blessed/lib/tput.js +3022 -0
  142. package/modules/blessed/lib/unicode.js +885 -0
  143. package/modules/blessed/lib/widget.js +32 -0
  144. package/modules/blessed/lib/widgets/box.js +34 -0
  145. package/modules/blessed/lib/widgets/element.js +2570 -0
  146. package/modules/blessed/lib/widgets/list.js +599 -0
  147. package/modules/blessed/lib/widgets/log.js +83 -0
  148. package/modules/blessed/lib/widgets/node.js +282 -0
  149. package/modules/blessed/lib/widgets/screen.js +2298 -0
  150. package/modules/blessed/lib/widgets/scrollablebox.js +389 -0
  151. package/modules/blessed/lib/widgets/scrollabletext.js +35 -0
  152. package/modules/blessed/lib/widgets/text.js +35 -0
  153. package/modules/blessed/usr/linux +0 -0
  154. package/modules/blessed/usr/windows-ansi +0 -0
  155. package/modules/blessed/usr/xterm +0 -0
  156. package/modules/blessed/usr/xterm-256color +0 -0
  157. package/modules/blessed/usr/xterm.termcap +243 -0
  158. package/modules/chokidar/LICENSE +21 -0
  159. package/modules/chokidar/VENDORED.md +76 -0
  160. package/modules/chokidar/index.js +979 -0
  161. package/modules/chokidar/lib/brace-expand.js +180 -0
  162. package/modules/chokidar/lib/constants.js +66 -0
  163. package/modules/chokidar/lib/fsevents-handler.js +526 -0
  164. package/modules/chokidar/lib/nodefs-handler.js +654 -0
  165. package/modules/fclone.js +49 -0
  166. package/modules/pm2-axon/index.js +2 -0
  167. package/modules/pm2-axon/lib/configurable/History.md +5 -0
  168. package/modules/pm2-axon/lib/configurable/Makefile +7 -0
  169. package/modules/pm2-axon/lib/configurable/Readme.md +57 -0
  170. package/modules/pm2-axon/lib/configurable/index.js +2 -0
  171. package/modules/pm2-axon/lib/configurable/lib/configurable.js +47 -0
  172. package/modules/pm2-axon/lib/configurable/package.json +68 -0
  173. package/modules/pm2-axon/lib/index.js +44 -0
  174. package/modules/pm2-axon/lib/plugins/queue.js +71 -0
  175. package/modules/pm2-axon/lib/plugins/round-robin.js +55 -0
  176. package/modules/pm2-axon/lib/sockets/pub-emitter.js +26 -0
  177. package/modules/pm2-axon/lib/sockets/pub.js +77 -0
  178. package/modules/pm2-axon/lib/sockets/pull.js +37 -0
  179. package/modules/pm2-axon/lib/sockets/push.js +32 -0
  180. package/modules/pm2-axon/lib/sockets/rep.js +73 -0
  181. package/modules/pm2-axon/lib/sockets/req.js +101 -0
  182. package/modules/pm2-axon/lib/sockets/sock.js +430 -0
  183. package/modules/pm2-axon/lib/sockets/sub-emitter.js +92 -0
  184. package/modules/pm2-axon/lib/sockets/sub.js +147 -0
  185. package/modules/pm2-axon/lib/utils.js +19 -0
  186. package/modules/pm2-axon-rpc/index.js +3 -0
  187. package/modules/pm2-axon-rpc/lib/client.js +63 -0
  188. package/modules/pm2-axon-rpc/lib/server.js +139 -0
  189. package/modules/pm2-io-agent/LICENSE +661 -0
  190. package/modules/pm2-io-agent/README.md +62 -0
  191. package/modules/pm2-io-agent/config.js +30 -0
  192. package/modules/pm2-io-agent/constants.js +118 -0
  193. package/modules/pm2-io-agent/index.js +2 -0
  194. package/modules/pm2-io-agent/src/InteractorClient.js +513 -0
  195. package/modules/pm2-io-agent/src/InteractorDaemon.js +455 -0
  196. package/modules/pm2-io-agent/src/PM2Client.js +112 -0
  197. package/modules/pm2-io-agent/src/PM2Interface.js +180 -0
  198. package/modules/pm2-io-agent/src/TransporterInterface.js +173 -0
  199. package/modules/pm2-io-agent/src/Utility.js +392 -0
  200. package/modules/pm2-io-agent/src/WatchDog.js +70 -0
  201. package/modules/pm2-io-agent/src/push/DataRetriever.js +77 -0
  202. package/modules/pm2-io-agent/src/push/PushInteractor.js +206 -0
  203. package/modules/pm2-io-agent/src/push/TransactionAggregator.js +670 -0
  204. package/modules/pm2-io-agent/src/reverse/ReverseInteractor.js +128 -0
  205. package/modules/pm2-io-agent/src/transporters/Transporter.js +129 -0
  206. package/modules/pm2-io-agent/src/transporters/WebsocketTransport.js +202 -0
  207. package/modules/pm2-io-agent/src/utils/BinaryHeap.js +135 -0
  208. package/modules/pm2-io-agent/src/utils/EDS.js +111 -0
  209. package/modules/pm2-io-agent/src/utils/probes/Histogram.js +204 -0
  210. package/modules/pm2-io-agent/src/utils/units.js +10 -0
  211. package/modules/pm2-io-bpm/configuration.js +141 -0
  212. package/modules/pm2-io-bpm/constants.js +10 -0
  213. package/modules/pm2-io-bpm/featureManager.js +110 -0
  214. package/modules/pm2-io-bpm/features/dependencies.js +48 -0
  215. package/modules/pm2-io-bpm/features/events.js +50 -0
  216. package/modules/pm2-io-bpm/features/metrics.js +102 -0
  217. package/modules/pm2-io-bpm/features/notify.js +238 -0
  218. package/modules/pm2-io-bpm/features/profiling.js +70 -0
  219. package/modules/pm2-io-bpm/features/tracing.js +174 -0
  220. package/modules/pm2-io-bpm/index.js +11 -0
  221. package/modules/pm2-io-bpm/metrics/eventLoopMetrics.js +136 -0
  222. package/modules/pm2-io-bpm/metrics/httpMetrics.js +175 -0
  223. package/modules/pm2-io-bpm/metrics/network.js +132 -0
  224. package/modules/pm2-io-bpm/metrics/runtime.js +161 -0
  225. package/modules/pm2-io-bpm/metrics/v8.js +104 -0
  226. package/modules/pm2-io-bpm/otel/constants.js +7 -0
  227. package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/platform/index.js +17 -0
  228. package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/platform/node/index.js +17 -0
  229. package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/platform/node/util.js +51 -0
  230. package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/transform.js +137 -0
  231. package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/types.js +24 -0
  232. package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/utils.js +23 -0
  233. package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/zipkin.js +149 -0
  234. package/modules/pm2-io-bpm/pmx.js +380 -0
  235. package/modules/pm2-io-bpm/profilers/addonProfiler.js +193 -0
  236. package/modules/pm2-io-bpm/profilers/inspectorProfiler.js +302 -0
  237. package/modules/pm2-io-bpm/serviceManager.js +21 -0
  238. package/modules/pm2-io-bpm/services/actions.js +159 -0
  239. package/modules/pm2-io-bpm/services/inspector.js +43 -0
  240. package/modules/pm2-io-bpm/services/metrics.js +186 -0
  241. package/modules/pm2-io-bpm/services/runtimeStats.js +54 -0
  242. package/modules/pm2-io-bpm/services/transport.js +11 -0
  243. package/modules/pm2-io-bpm/transports/IPCTransport.js +115 -0
  244. package/modules/pm2-io-bpm/utils/BinaryHeap.js +134 -0
  245. package/modules/pm2-io-bpm/utils/EDS.js +113 -0
  246. package/modules/pm2-io-bpm/utils/EWMA.js +32 -0
  247. package/modules/pm2-io-bpm/utils/autocast.js +61 -0
  248. package/modules/pm2-io-bpm/utils/metrics/counter.js +33 -0
  249. package/modules/pm2-io-bpm/utils/metrics/gauge.js +23 -0
  250. package/modules/pm2-io-bpm/utils/metrics/histogram.js +192 -0
  251. package/modules/pm2-io-bpm/utils/metrics/meter.js +46 -0
  252. package/modules/pm2-io-bpm/utils/miscellaneous.js +19 -0
  253. package/modules/pm2-io-bpm/utils/module.js +67 -0
  254. package/modules/pm2-io-bpm/utils/shimmer.js +19 -0
  255. package/modules/pm2-io-bpm/utils/stackParser.js +160 -0
  256. package/modules/pm2-io-bpm/utils/transactionAggregator.js +429 -0
  257. package/modules/pm2-io-bpm/utils/units.js +16 -0
  258. package/modules/vizion/LICENSE +13 -0
  259. package/modules/vizion/README.md +133 -0
  260. package/modules/vizion/index.js +1 -0
  261. package/modules/vizion/lib/git/git.js +247 -0
  262. package/modules/vizion/lib/vizion.js +52 -0
  263. package/package.json +170 -4
  264. package/paths.js +121 -0
  265. package/pm2 +11 -0
  266. package/types/index.d.ts +732 -0
  267. package/types/tsconfig.json +14 -0
@@ -0,0 +1,798 @@
1
+
2
+ /***************************
3
+ *
4
+ * Extra methods
5
+ *
6
+ **************************/
7
+
8
+ var cst = require('../../constants.js');
9
+ var Common = require('../Common.js');
10
+ var UX = require('./UX');
11
+ var chalk = require('ansis');
12
+ var path = require('path');
13
+ var fs = require('fs');
14
+ var fmt = require('../tools/fmt.js');
15
+ var dayjs = require('dayjs');
16
+ var pkg = require('../../package.json');
17
+ const copyDirSync = require('../tools/copydirSync.js')
18
+
19
+ module.exports = function(CLI) {
20
+ /**
21
+ * Get version of the daemonized PM2
22
+ * @method getVersion
23
+ * @callback cb
24
+ */
25
+ CLI.prototype.getVersion = function(cb) {
26
+ var that = this;
27
+
28
+ that.Client.executeRemote('getVersion', {}, function(err) {
29
+ return cb ? cb.apply(null, arguments) : that.exitCli(cst.SUCCESS_EXIT);
30
+ });
31
+ };
32
+
33
+ /**
34
+ * Show application environment
35
+ * @method env
36
+ * @callback cb
37
+ */
38
+ CLI.prototype.env = function(app_id, cb) {
39
+ var procs = []
40
+ var printed = 0
41
+
42
+ this.Client.executeRemote('getMonitorData', {}, (err, list) => {
43
+ list.forEach(l => {
44
+ if (app_id == l.pm_id) {
45
+ printed++
46
+ var env = Common.safeExtend({}, l.pm2_env)
47
+ Object.keys(env).forEach(key => {
48
+ console.log(`${key}: ${chalk.green(env[key])}`)
49
+ })
50
+ }
51
+ })
52
+
53
+ if (printed == 0) {
54
+ Common.err(`Modules with id ${app_id} not found`)
55
+ return cb ? cb.apply(null, arguments) : this.exitCli(cst.ERROR_EXIT);
56
+ }
57
+ return cb ? cb.apply(null, arguments) : this.exitCli(cst.SUCCESS_EXIT);
58
+ })
59
+ };
60
+
61
+ /**
62
+ * Get version of the daemonized PM2
63
+ * @method getVersion
64
+ * @callback cb
65
+ */
66
+ CLI.prototype.report = function() {
67
+ var that = this;
68
+
69
+ var Log = require('./Log');
70
+
71
+ that.Client.executeRemote('getReport', {}, function(err, report) {
72
+
73
+ console.log()
74
+ console.log()
75
+ console.log()
76
+ console.log('```')
77
+ fmt.title('PM2 report')
78
+ fmt.field('Date', new Date());
79
+ fmt.sep();
80
+
81
+ if (report && !err) {
82
+ fmt.title(chalk.bold.blue('Daemon'));
83
+ fmt.field('pm2d version', report.pm2_version);
84
+ fmt.field('node version', report.node_version);
85
+ fmt.field('node path', report.node_path);
86
+ fmt.field('argv', report.argv);
87
+ fmt.field('argv0', report.argv0);
88
+ fmt.field('user', report.user);
89
+ fmt.field('uid', report.uid);
90
+ fmt.field('gid', report.gid);
91
+ fmt.field('uptime', dayjs(new Date()).diff(report.started_at, 'minute') + 'min');
92
+ }
93
+
94
+ fmt.sep();
95
+ fmt.title(chalk.bold.blue('CLI'));
96
+ fmt.field('local pm2', pkg.version);
97
+ fmt.field('node version', process.versions.node);
98
+ fmt.field('node path', process.env['_'] || 'not found');
99
+ fmt.field('argv', process.argv);
100
+ fmt.field('argv0', process.argv0);
101
+ fmt.field('user', process.env.USER || process.env.LNAME || process.env.USERNAME);
102
+ if (cst.IS_WINDOWS === false && process.geteuid)
103
+ fmt.field('uid', process.geteuid());
104
+ if (cst.IS_WINDOWS === false && process.getegid)
105
+ fmt.field('gid', process.getegid());
106
+
107
+ var os = require('os');
108
+
109
+ fmt.sep();
110
+ fmt.title(chalk.bold.blue('System info'));
111
+ fmt.field('arch', os.arch());
112
+ fmt.field('platform', os.platform());
113
+ fmt.field('type', os.type());
114
+ fmt.field('cpus', os.cpus()[0].model);
115
+ fmt.field('cpus nb', Object.keys(os.cpus()).length);
116
+ fmt.field('freemem', os.freemem());
117
+ fmt.field('totalmem', os.totalmem());
118
+ fmt.field('home', os.homedir());
119
+
120
+ that.Client.executeRemote('getMonitorData', {}, function(err, list) {
121
+
122
+ fmt.sep();
123
+ fmt.title(chalk.bold.blue('PM2 list'));
124
+ UX.list(list, that.gl_interact_infos);
125
+
126
+ fmt.sep();
127
+ fmt.title(chalk.bold.blue('Daemon logs'));
128
+ Log.tail([{
129
+ path : cst.PM2_LOG_FILE_PATH,
130
+ app_name : 'PM2',
131
+ type : 'PM2'
132
+ }], 20, false, function() {
133
+ console.log('```')
134
+ console.log()
135
+ console.log()
136
+
137
+ console.log(chalk.bold.green('Please copy/paste the above report in your issue on https://github.com/Unitech/pm2/issues'));
138
+
139
+ console.log()
140
+ console.log()
141
+ that.exitCli(cst.SUCCESS_EXIT);
142
+ });
143
+ });
144
+ });
145
+ };
146
+
147
+ CLI.prototype.getPID = function(app_name, cb) {
148
+ var that = this;
149
+
150
+ if (typeof(app_name) === 'function') {
151
+ cb = app_name;
152
+ app_name = null;
153
+ }
154
+
155
+ this.Client.executeRemote('getMonitorData', {}, function(err, list) {
156
+ if (err) {
157
+ Common.printError(cst.PREFIX_MSG_ERR + err);
158
+ return cb ? cb(Common.retErr(err)) : that.exitCli(cst.ERROR_EXIT);
159
+ }
160
+
161
+ var pids = [];
162
+
163
+ list.forEach(function(app) {
164
+ if (!app_name || app_name == app.name)
165
+ pids.push(app.pid);
166
+ })
167
+
168
+ if (!cb) {
169
+ Common.printOut(pids.join("\n"))
170
+ return that.exitCli(cst.SUCCESS_EXIT);
171
+ }
172
+ return cb(null, pids);
173
+ })
174
+ }
175
+
176
+ /**
177
+ * Create PM2 memory snapshot
178
+ * @method getVersion
179
+ * @callback cb
180
+ */
181
+ CLI.prototype.profile = function(type, time, cb) {
182
+ var that = this;
183
+ var dayjs = require('dayjs');
184
+ var cmd
185
+
186
+ if (type == 'cpu') {
187
+ cmd = {
188
+ ext: '.cpuprofile',
189
+ action: 'profileCPU'
190
+ }
191
+ }
192
+ if (type == 'mem') {
193
+ cmd = {
194
+ ext: '.heapprofile',
195
+ action: 'profileMEM'
196
+ }
197
+ }
198
+
199
+ var file = path.join(process.cwd(), dayjs().format('dd-HH:mm:ss') + cmd.ext);
200
+ time = time || 10000
201
+
202
+ console.log(`Starting ${cmd.action} profiling for ${time}ms...`)
203
+ that.Client.executeRemote(cmd.action, {
204
+ pwd : file,
205
+ timeout: time
206
+ }, function(err) {
207
+ if (err) {
208
+ console.error(err);
209
+ return that.exitCli(1);
210
+ }
211
+ console.log(`Profile done in ${file}`)
212
+ return cb ? cb.apply(null, arguments) : that.exitCli(cst.SUCCESS_EXIT);
213
+ });
214
+ };
215
+
216
+
217
+ function basicMDHighlight(lines) {
218
+ console.log('\n\n+-------------------------------------+')
219
+ console.log(chalk.bold('README.md content:'))
220
+ lines = lines.split('\n')
221
+ var isInner = false
222
+ lines.forEach(l => {
223
+ if (l.startsWith('#'))
224
+ console.log(chalk.bold.green(l))
225
+ else if (isInner || l.startsWith('```')) {
226
+ if (isInner && l.startsWith('```'))
227
+ isInner = false
228
+ else if (isInner == false)
229
+ isInner = true
230
+ console.log(chalk.gray(l))
231
+ }
232
+ else if (l.startsWith('`'))
233
+ console.log(chalk.gray(l))
234
+ else
235
+ console.log(l)
236
+ })
237
+ console.log('+-------------------------------------+')
238
+ }
239
+ /**
240
+ * pm2 create command
241
+ * create boilerplate of application for fast try
242
+ * @method boilerplate
243
+ */
244
+ CLI.prototype.boilerplate = function(cb) {
245
+ var i = 0
246
+ var projects = []
247
+ var promptly = require('../tools/prompt')
248
+ const forEach = require('async/forEach')
249
+
250
+ fs.readdir(path.join(__dirname, '../templates/sample-apps'), (err, items) => {
251
+ forEach(items, (app, next) => {
252
+ var fp = path.join(__dirname, '../templates/sample-apps', app)
253
+ fs.readFile(path.join(fp, 'package.json'), (err, dt) => {
254
+ var meta = JSON.parse(dt)
255
+ meta.fullpath = fp
256
+ meta.folder_name = app
257
+ projects.push(meta)
258
+ next()
259
+ })
260
+ }, () => {
261
+ var choices = projects.map((p) => `${p.name} - ${p.description}`)
262
+
263
+ promptly.choose('Select a boilerplate:', choices, (err, answer) => {
264
+ if (err) return cb ? cb(err) : this.speedList(cst.ERROR_EXIT);
265
+ var idx = choices.indexOf(answer)
266
+ var p = projects[idx]
267
+ basicMDHighlight(fs.readFileSync(path.join(p.fullpath, 'README.md')).toString())
268
+ console.log(chalk.bold(`>> Project copied inside folder ./${p.folder_name}/\n`))
269
+ copyDirSync(p.fullpath, path.join(process.cwd(), p.folder_name));
270
+ this.start(path.join(p.fullpath, 'ecosystem.config.js'), {
271
+ cwd: p.fullpath
272
+ }, () => {
273
+ return cb ? cb.apply(null, arguments) : this.speedList(cst.SUCCESS_EXIT);
274
+ })
275
+ })
276
+
277
+ })
278
+ })
279
+ }
280
+
281
+ /**
282
+ * Description
283
+ * @method sendLineToStdin
284
+ */
285
+ CLI.prototype.sendLineToStdin = function(pm_id, line, separator, cb) {
286
+ var that = this;
287
+
288
+ if (!cb && typeof(separator) == 'function') {
289
+ cb = separator;
290
+ separator = null;
291
+ }
292
+
293
+ var packet = {
294
+ pm_id : pm_id,
295
+ line : line + (separator || '\n')
296
+ };
297
+
298
+ that.Client.executeRemote('sendLineToStdin', packet, function(err, res) {
299
+ if (err) {
300
+ Common.printError(cst.PREFIX_MSG_ERR + err);
301
+ return cb ? cb(Common.retErr(err)) : that.exitCli(cst.ERROR_EXIT);
302
+ }
303
+ return cb ? cb(null, res) : that.speedList();
304
+ });
305
+ };
306
+
307
+ /**
308
+ * Description
309
+ * @method attachToProcess
310
+ */
311
+ CLI.prototype.attach = function(pm_id, separator, cb) {
312
+ var that = this;
313
+ var readline = require('readline');
314
+
315
+ if (isNaN(pm_id)) {
316
+ Common.printError('pm_id must be a process number (not a process name)');
317
+ return cb ? cb(Common.retErr('pm_id must be number')) : that.exitCli(cst.ERROR_EXIT);
318
+ }
319
+
320
+ if (typeof(separator) == 'function') {
321
+ cb = separator;
322
+ separator = null;
323
+ }
324
+
325
+ var rl = readline.createInterface({
326
+ input: process.stdin,
327
+ output: process.stdout
328
+ });
329
+
330
+ rl.on('close', function() {
331
+ return cb ? cb() : that.exitCli(cst.SUCCESS_EXIT);
332
+ });
333
+
334
+ that.Client.launchBus(function(err, bus, socket) {
335
+ if (err) {
336
+ Common.printError(err);
337
+ return cb ? cb(Common.retErr(err)) : that.exitCli(cst.ERROR_EXIT);
338
+ }
339
+
340
+ bus.on('log:*', function(type, packet) {
341
+ if (packet.process.pm_id !== parseInt(pm_id))
342
+ return;
343
+ process.stdout.write(packet.data);
344
+ });
345
+ });
346
+
347
+ rl.on('line', function(line) {
348
+ that.sendLineToStdin(pm_id, line, separator, function() {});
349
+ });
350
+ };
351
+
352
+ /**
353
+ * Description
354
+ * @method sendDataToProcessId
355
+ */
356
+ CLI.prototype.sendDataToProcessId = function(proc_id, packet, cb) {
357
+ var that = this;
358
+
359
+ if (typeof proc_id === 'object' && typeof packet === 'function') {
360
+ // the proc_id is packet.
361
+ cb = packet;
362
+ packet = proc_id;
363
+ } else {
364
+ packet.id = proc_id;
365
+ }
366
+
367
+ that.Client.executeRemote('sendDataToProcessId', packet, function(err, res) {
368
+ if (err) {
369
+ Common.printError(err);
370
+ return cb ? cb(Common.retErr(err)) : that.exitCli(cst.ERROR_EXIT);
371
+ }
372
+ Common.printOut('successfully sent data to process');
373
+ return cb ? cb(null, res) : that.speedList();
374
+ });
375
+ };
376
+
377
+ /**
378
+ * Used for custom actions, allows to trigger function inside an app
379
+ * To expose a function you need to use keymetrics/pmx
380
+ *
381
+ * @method msgProcess
382
+ * @param {Object} opts
383
+ * @param {String} id process id
384
+ * @param {String} action_name function name to trigger
385
+ * @param {Object} [opts.opts] object passed as first arg of the function
386
+ * @param {String} [uuid] optional unique identifier when logs are emitted
387
+ *
388
+ */
389
+ CLI.prototype.msgProcess = function(opts, cb) {
390
+ var that = this;
391
+
392
+ that.Client.executeRemote('msgProcess', opts, cb);
393
+ };
394
+
395
+ /**
396
+ * Trigger a PMX custom action in target application
397
+ * Custom actions allows to interact with an application
398
+ *
399
+ * @method trigger
400
+ * @param {String|Number} pm_id process id or application name
401
+ * @param {String} action_name name of the custom action to trigger
402
+ * @param {Mixed} params parameter to pass to target action
403
+ * @param {Function} cb callback
404
+ */
405
+ CLI.prototype.trigger = function(pm_id, action_name, params, cb) {
406
+ if (typeof(params) === 'function') {
407
+ cb = params;
408
+ params = null;
409
+ }
410
+ var cmd = {
411
+ msg : action_name
412
+ };
413
+ var counter = 0;
414
+ var process_wait_count = 0;
415
+ var that = this;
416
+ var results = [];
417
+
418
+ if (params)
419
+ cmd.opts = params;
420
+ if (isNaN(pm_id))
421
+ cmd.name = pm_id;
422
+ else
423
+ cmd.id = pm_id;
424
+
425
+ this.launchBus(function(err, bus) {
426
+ bus.on('axm:reply', function(ret) {
427
+ if (ret.process.name == pm_id || ret.process.pm_id == pm_id || ret.process.namespace == pm_id || pm_id == 'all') {
428
+ results.push(ret);
429
+ Common.printOut('[%s:%s:%s]=%j', ret.process.name, ret.process.pm_id, ret.process.namespace, ret.data.return);
430
+ if (++counter == process_wait_count)
431
+ return cb ? cb(null, results) : that.exitCli(cst.SUCCESS_EXIT);
432
+ }
433
+ });
434
+
435
+ that.msgProcess(cmd, function(err, data) {
436
+ if (err) {
437
+ Common.printError(err);
438
+ return cb ? cb(Common.retErr(err)) : that.exitCli(cst.ERROR_EXIT);
439
+ }
440
+
441
+ if (data.process_count == 0) {
442
+ Common.printError('Not any process has received a command (offline or unexistent)');
443
+ return cb ? cb(Common.retErr('Unknown process')) : that.exitCli(cst.ERROR_EXIT);
444
+ }
445
+
446
+ process_wait_count = data.process_count;
447
+ Common.printOut(chalk.bold('%s processes have received command %s'),
448
+ data.process_count, action_name);
449
+ });
450
+ });
451
+ };
452
+
453
+ /**
454
+ * Description
455
+ * @method sendSignalToProcessName
456
+ * @param {} signal
457
+ * @param {} process_name
458
+ * @return
459
+ */
460
+ CLI.prototype.sendSignalToProcessName = function(signal, process_name, cb) {
461
+ var that = this;
462
+
463
+ that.Client.executeRemote('sendSignalToProcessName', {
464
+ signal : signal,
465
+ process_name : process_name
466
+ }, function(err, list) {
467
+ if (err) {
468
+ Common.printError(err);
469
+ return cb ? cb(Common.retErr(err)) : that.exitCli(cst.ERROR_EXIT);
470
+ }
471
+ Common.printOut('successfully sent signal %s to process name %s', signal, process_name);
472
+ return cb ? cb(null, list) : that.speedList();
473
+ });
474
+ };
475
+
476
+ /**
477
+ * Description
478
+ * @method sendSignalToProcessId
479
+ * @param {} signal
480
+ * @param {} process_id
481
+ * @return
482
+ */
483
+ CLI.prototype.sendSignalToProcessId = function(signal, process_id, cb) {
484
+ var that = this;
485
+
486
+ that.Client.executeRemote('sendSignalToProcessId', {
487
+ signal : signal,
488
+ process_id : process_id
489
+ }, function(err, list) {
490
+ if (err) {
491
+ Common.printError(err);
492
+ return cb ? cb(Common.retErr(err)) : that.exitCli(cst.ERROR_EXIT);
493
+ }
494
+ Common.printOut('successfully sent signal %s to process id %s', signal, process_id);
495
+ return cb ? cb(null, list) : that.speedList();
496
+ });
497
+ };
498
+
499
+ /**
500
+ * API method to launch a process that will serve directory over http
501
+ */
502
+ CLI.prototype.autoinstall = function (cb) {
503
+ var filepath = path.resolve(path.dirname(module.filename), '../Sysinfo/ServiceDetection/ServiceDetection.js');
504
+
505
+ this.start(filepath, (err, res) => {
506
+ if (err) {
507
+ Common.printError(cst.PREFIX_MSG_ERR + 'Error while trying to serve : ' + err.message || err);
508
+ return cb ? cb(err) : this.speedList(cst.ERROR_EXIT);
509
+ }
510
+ return cb ? cb(null) : this.speedList();
511
+ });
512
+ }
513
+
514
+ /**
515
+ * API method to launch a process that will serve directory over http
516
+ *
517
+ * @param {Object} opts options
518
+ * @param {String} opts.path path to be served
519
+ * @param {Number} opts.port port on which http will bind
520
+ * @param {Boolean} opts.spa single page app served
521
+ * @param {String} opts.basicAuthUsername basic auth username
522
+ * @param {String} opts.basicAuthPassword basic auth password
523
+ * @param {Object} commander commander object
524
+ * @param {Function} cb optional callback
525
+ */
526
+ CLI.prototype.serve = function (target_path, port, opts, commander, cb) {
527
+ var that = this;
528
+ var servePort = process.env.PM2_SERVE_PORT || port || 8080;
529
+ var servePath = path.resolve(process.env.PM2_SERVE_PATH || target_path || '.');
530
+
531
+ var filepath = path.resolve(path.dirname(module.filename), './Serve.js');
532
+
533
+ if (typeof commander.name === 'string')
534
+ opts.name = commander.name
535
+ else
536
+ opts.name = 'static-page-server-' + servePort
537
+ if (!opts.env)
538
+ opts.env = {};
539
+ opts.env.PM2_SERVE_PORT = servePort;
540
+ opts.env.PM2_SERVE_PATH = servePath;
541
+ opts.env.PM2_SERVE_SPA = opts.spa;
542
+ opts.env.PM2_SERVE_FTP = opts.ftp || false;
543
+ if (opts.basicAuthUsername && opts.basicAuthPassword) {
544
+ opts.env.PM2_SERVE_BASIC_AUTH = 'true';
545
+ opts.env.PM2_SERVE_BASIC_AUTH_USERNAME = opts.basicAuthUsername;
546
+ opts.env.PM2_SERVE_BASIC_AUTH_PASSWORD = opts.basicAuthPassword;
547
+ }
548
+ if (opts.monitor) {
549
+ opts.env.PM2_SERVE_MONITOR = opts.monitor
550
+ }
551
+ opts.cwd = servePath;
552
+
553
+ this.start(filepath, opts, function (err, res) {
554
+ if (err) {
555
+ Common.printError(cst.PREFIX_MSG_ERR + 'Error while trying to serve : ' + err.message || err);
556
+ return cb ? cb(err) : that.speedList(cst.ERROR_EXIT);
557
+ }
558
+ Common.printOut(cst.PREFIX_MSG + 'Serving ' + servePath + ' on port ' + servePort);
559
+ return cb ? cb(null, res) : that.speedList();
560
+ });
561
+ }
562
+
563
+ /**
564
+ * Ping daemon - if PM2 daemon not launched, it will launch it
565
+ * @method ping
566
+ */
567
+ CLI.prototype.ping = function(cb) {
568
+ var that = this;
569
+
570
+ that.Client.executeRemote('ping', {}, function(err, res) {
571
+ if (err) {
572
+ Common.printError(err);
573
+ return cb ? cb(new Error(err)) : that.exitCli(cst.ERROR_EXIT);
574
+ }
575
+ Common.printOut(res);
576
+ return cb ? cb(null, res) : that.exitCli(cst.SUCCESS_EXIT);
577
+ });
578
+ };
579
+
580
+
581
+ /**
582
+ * Execute remote command
583
+ */
584
+ CLI.prototype.remote = function(command, opts, cb) {
585
+ var that = this;
586
+
587
+ that[command](opts.name, function(err_cmd, ret) {
588
+ if (err_cmd)
589
+ console.error(err_cmd);
590
+ console.log('Command %s finished', command);
591
+ return cb(err_cmd, ret);
592
+ });
593
+ };
594
+
595
+ /**
596
+ * This remote method allows to pass multiple arguments
597
+ * to PM2
598
+ * It is used for the new scoped PM2 action system
599
+ */
600
+ CLI.prototype.remoteV2 = function(command, opts, cb) {
601
+ var that = this;
602
+
603
+ if (that[command].length == 1)
604
+ return that[command](cb);
605
+
606
+ opts.args.push(cb);
607
+ return that[command].apply(this, opts.args);
608
+ };
609
+
610
+
611
+ /**
612
+ * Description
613
+ * @method generateSample
614
+ * @param {} name
615
+ * @return
616
+ */
617
+ CLI.prototype.generateSample = function(mode) {
618
+ var that = this;
619
+ var templatePath;
620
+
621
+ if (mode == 'simple')
622
+ templatePath = path.join(cst.TEMPLATE_FOLDER, cst.APP_CONF_TPL_SIMPLE);
623
+ else
624
+ templatePath = path.join(cst.TEMPLATE_FOLDER, cst.APP_CONF_TPL);
625
+
626
+ var sample = fs.readFileSync(templatePath);
627
+ var dt = sample.toString();
628
+ var f_name = 'ecosystem.config.js';
629
+ var pwd = process.env.PWD || process.cwd();
630
+
631
+ try {
632
+ fs.writeFileSync(path.join(pwd, f_name), dt);
633
+ } catch (e) {
634
+ console.error(e.stack || e);
635
+ return that.exitCli(cst.ERROR_EXIT);
636
+ }
637
+ Common.printOut('File %s generated', path.join(pwd, f_name));
638
+ that.exitCli(cst.SUCCESS_EXIT);
639
+ };
640
+
641
+ /**
642
+ * Description
643
+ * @method dashboard
644
+ * @return
645
+ */
646
+ CLI.prototype.dashboard = function(opts, cb) {
647
+ var that = this;
648
+
649
+ var Dashboard = require('./Dashboard');
650
+ var Log = require('./Log');
651
+ var forEachLimit = require('async/forEachLimit');
652
+
653
+ if (typeof(opts) === 'function') {
654
+ cb = opts;
655
+ opts = {};
656
+ }
657
+ opts = opts || {};
658
+
659
+ if (cb)
660
+ return cb(new Error('Dashboard cant be called programmatically'));
661
+
662
+ var lines = Dashboard.parseLines(opts.lines);
663
+ // max time spent reading log files before showing the dashboard
664
+ var PRELOAD_TIMEOUT = 2000;
665
+
666
+ Dashboard.init({ lines: lines });
667
+
668
+ // Live log events received while the history is being read from the
669
+ // log files are buffered, then replayed after the history so the
670
+ // stream stays chronological and nothing is lost
671
+ var preloading = true;
672
+ var pending = [];
673
+
674
+ this.Client.launchBus(function (err, bus) {
675
+ if (err) {
676
+ console.error('Error launchBus: ' + err);
677
+ return that.exitCli(cst.ERROR_EXIT);
678
+ }
679
+ bus.on('log:*', function(type, data) {
680
+ if (preloading)
681
+ return pending.push([type, data]);
682
+ Dashboard.log(type, data)
683
+ })
684
+ });
685
+
686
+ // Preload the last `lines` lines (out + err merged) of every process
687
+ function preloadHistory(list, done) {
688
+ var finished = false;
689
+ var finish = function() {
690
+ if (finished) return;
691
+ finished = true;
692
+ preloading = false;
693
+ // replay skips lines already loaded from the files
694
+ pending.forEach(function(ev) { Dashboard.replay(ev[0], ev[1]); });
695
+ pending = [];
696
+ done();
697
+ };
698
+ // never block the dashboard on slow/huge log files
699
+ var timer = setTimeout(finish, PRELOAD_TIMEOUT);
700
+
701
+ forEachLimit(list, 4, function(proc, next) {
702
+ var env = proc.pm2_env || {};
703
+ var identity = { pm_id: proc.pm_id, name: env.name };
704
+
705
+ Log.getLastLines(env.pm_out_log_path, lines, function(out_lines) {
706
+ Log.getLastLines(env.pm_err_log_path, lines, function(err_lines) {
707
+ // too late (timeout hit, live lines already flowing): history
708
+ // must not be appended after live lines
709
+ if (!finished)
710
+ Dashboard.preload(identity, out_lines, err_lines);
711
+ next();
712
+ });
713
+ });
714
+ }, function() {
715
+ clearTimeout(timer);
716
+ finish();
717
+ });
718
+ }
719
+
720
+ process.on('SIGINT', function() {
721
+ this.Client.disconnectBus(function() {
722
+ process.exit(cst.SUCCESS_EXIT);
723
+ });
724
+ });
725
+
726
+ var history_loaded = false;
727
+
728
+ function refreshDashboard() {
729
+ that.Client.executeRemote('getMonitorData', {}, function(err, list) {
730
+ if (err) {
731
+ console.error('Error retrieving process list: ' + err);
732
+ that.exitCli(cst.ERROR_EXIT);
733
+ }
734
+
735
+ var render = function() {
736
+ Dashboard.refresh(list);
737
+
738
+ setTimeout(function() {
739
+ refreshDashboard();
740
+ }, 800);
741
+ };
742
+
743
+ if (history_loaded)
744
+ return render();
745
+
746
+ history_loaded = true;
747
+ preloadHistory(list, render);
748
+ });
749
+ }
750
+
751
+ refreshDashboard();
752
+ };
753
+
754
+ CLI.prototype.monit = function(cb) {
755
+ var that = this;
756
+
757
+ var Monit = require('./Monit.js');
758
+
759
+ if (cb) return cb(new Error('Monit cant be called programmatically'));
760
+
761
+ Monit.init();
762
+
763
+ function launchMonitor() {
764
+ that.Client.executeRemote('getMonitorData', {}, function(err, list) {
765
+ if (err) {
766
+ console.error('Error retrieving process list: ' + err);
767
+ that.exitCli(conf.ERROR_EXIT);
768
+ }
769
+
770
+ Monit.refresh(list);
771
+
772
+ setTimeout(function() {
773
+ launchMonitor();
774
+ }, 400);
775
+ });
776
+ }
777
+
778
+ launchMonitor();
779
+ };
780
+
781
+ CLI.prototype.inspect = function(app_name, cb) {
782
+ const that = this;
783
+ this.trigger(app_name, 'internal:inspect', function (err, res) {
784
+
785
+ if(res && res[0]) {
786
+ if (res[0].data.return === '') {
787
+ Common.printOut(`Inspect disabled on ${app_name}`);
788
+ } else {
789
+ Common.printOut(`Inspect enabled on ${app_name} => go to chrome : chrome://inspect !!!`);
790
+ }
791
+ } else {
792
+ Common.printOut(`Unable to activate inspect mode on ${app_name} !!!`);
793
+ }
794
+
795
+ that.exitCli(cst.SUCCESS_EXIT);
796
+ });
797
+ };
798
+ };