-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathpm2-gui.js
284 lines (261 loc) · 7.15 KB
/
pm2-gui.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
'use strict'
var chalk = require('chalk')
var path = require('path')
var fs = require('fs')
var _ = require('lodash')
var socketIO = require('socket.io')
var inquirer = require('inquirer')
var Monitor = require('./lib/monitor')
var Log = require('./lib/util/log')
var Web = require('./web/index')
var Layout = require('./lib/blessed-widget/layout')
var regLocal = /^(127\.0\.0\.1|0\.0\.0\.0|localhost)$/i
// cli
if (path.basename(process.mainModule.filename, '.js') === 'pm2-gui') {
var cmd = 'start'
var file
var processArgvLen = process.argv.length
if (processArgvLen > 2) {
cmd = process.argv[2]
}
if (processArgvLen > 3) {
file = process.argv[3]
}
switch (cmd) {
case 'start':
startWebServer(file)
break
case 'agent':
startAgent(file)
break
case 'mon':
dashboard(file)
break
default:
Log({
level: 0,
prefix: true
})
console.error('Command', cmd, 'is not supported!')
break
}
}
exports.startWebServer = startWebServer
exports.startAgent = startAgent
exports.dashboard = dashboard
exports.exitGraceful = exitGraceful
/**
* Strat the web server by a configured file.
* @param {String} confFile full path of config file
* @return {N/A}
*/
function startWebServer (confFile) {
var monitor = slave({
confFile: confFile
})
var options = monitor.options
// express server
var server = Web({
middleware: function (req, res, next) {
req._config = options
next()
},
port: options.port
})
// socket.io server
monitor.sockio = socketIO(server, {
origins: options.origins || '*:*'
})
monitor.run()
console.info('Web server is listening on 127.0.0.1:' + options.port)
}
/**
* Simply start the agent
* @param {String} confFile full path of config file
* @return {N/A}
*/
function startAgent (confFile) {
var monitor = slave({
confFile: confFile
})
// check agent status
var options = monitor.options
if (options.agent && options.agent.offline) {
console.error('Agent is offline, fatal to start it.')
return process.exit(0)
}
// socket.io server
var sockio = socketIO()
sockio.listen(options.port, {
origins: options.origins || '*:*'
})
monitor.sockio = sockio
monitor.run()
console.info('Socket.io server is listening on 0.0.0.0:' + options.port)
}
/**
* Curses like dashboard
* @param {String} confFile full path of config file
* @return {N/A}
*/
function dashboard (confFile) {
// restore cursor
// process.on('exit', function () {
// process.stderr.write('\u001b[?25h')
// })
// which server would you like to connect to.
var monitor = slave({
confFile: confFile
})
var options = _.clone(monitor.options)
var q = Monitor.available(options)
if (!q) {
console.error('No agent is online, fatal to start it.')
return process.exit(0)
}
var ql = q.choices.length
if (ql === 1) {
if (q.choices[0].short !== 'localhost') {
console.info('There is just one remoting server online, try to connect it.')
}
return _connectToDashboard(monitor, options, Monitor.parseConnectionString(q.choices[0].value))
}
if (!options.agent || !options.agent.offline) {
q.choices.splice(ql - 1, 0, new inquirer.Separator())
}
console.info('Remoting servers are online, choose one you are intrested in.')
console.log('')
inquirer.prompt(q).then(function (answers) {
console.log('')
// connecting...
_connectToDashboard(monitor, options, Monitor.parseConnectionString(answers.socket_server))
})
}
/**
* Exit process graceful
* @param {Number} code
* @param {String} signal
* @return {N/A}
*/
function exitGraceful (code, signal) {
code = code || 0
if (signal !== '-f') {
console.debug('Slave has exited, code: ' + code + ', signal: ' + (signal || 'N/A'))
}
// exit process after std flushed
var fds = 0
var stds = [process.stdout, process.stderr]
stds.forEach(function (std) {
var fd = std.fd
if (!std.bufferSize) {
fds = fds | fd
return
}
std.write && std.write('', function () {
fds = fds | fd
tryToExit()
})
})
tryToExit()
function tryToExit () {
if ((fds & 1) && (fds & 2)) {
process.exit(code)
}
}
}
/**
* Spawn a slave-worker
* @param {Object} options
* @return {N/A}
*/
function slave (options) {
process.title = 'pm2-gui slave'
options = options || {}
// check config file.
var confFile = options.confFile
if (!confFile) {
confFile = path.resolve(__dirname, './pm2-gui.ini')
if (!fs.existsSync(confFile)) {
console.error(chalk.bold(confFile), 'does not exist!')
return process.exit(0)
}
}
// initialize monitor.
var monitor = Monitor({
confFile: confFile
})
// initialize logger.
Log(monitor.options.log)
// logo
console.log(chalk.cyan(
'\n' +
'█▀▀█ █▀▄▀█ █▀█ ░░ ▒█▀▀█ ▒█░▒█ ▀█▀\n' +
'█░░█ █░▀░█ ░▄▀ ▀▀ ▒█░▄▄ ▒█░▒█ ▒█░\n' +
'█▀▀▀ ▀░░░▀ █▄▄ ░░ ▒█▄▄█ ░▀▄▄▀ ▄█▄\n'))
// listening signal (CTRL+C...)
process.on('uncaughtException', caughtException)
process.on('SIGTERM', shutdown)
process.on('SIGINT', shutdown)
process.on('SIGHUP', restart)
process.on('exit', exitGraceful)
return monitor
function shutdown (code, signal) {
console.info('Shutting down....')
monitor.quit()
console.info('Completed!')
exitGraceful(code, '-f')
}
function restart () {
if (process.send) {
process.send({
action: 'restart'
})
} else {
console.error('No IPC found, fatal to restart monitor')
shutdown(1)
}
}
function caughtException (err) {
process.stderr.write(chalk.red.bold('[PROCESS EXCEPTION]') + ' ' + err.stack + '\n')
shutdown(1)
}
}
/**
* Connect to socket.io server and render the curses dashboard
* @param {Monitor} monitor
* @param {Object} options
* @param {Object} connection
* @return {N/A}
*/
function _connectToDashboard (monitor, options, connection) {
connection = _.extend({}, options, connection)
if (regLocal.test(connection.hostname)) {
console.info('Connecting to local dashboard')
return monitor.connect(connection, function (err, socket) {
if (err) {
if (err === 'unauthorized') {
console.error('There was an error caught when verifying the auth-code:', err.message)
return process.exit(0)
}
console.warn('Fatal to connect to monitor:', err.message)
console.warn('Agent is offline, try to start it:', '127.0.0.1:' + connection.port)
// start socket.io server.
var sockio = socketIO()
sockio.listen(connection.port, {
origins: options.origins || '*:*'
})
// run monitor
monitor.sockio = sockio
monitor.run()
// render dashboard
Layout(connection).render(monitor)
return
}
// render dashboard
console.info('Agent is online, try to connect it in dashboard directly.')
Layout(connection).render(monitor)
})
}
console.info('Connecting to remote dashboard')
Layout(connection).render(monitor)
}