npmtest-pm2-gui (v0.0.1)

Code coverage report for node-npmtest-pm2-gui/pm2-gui/pm2-gui.js

Statements: 18.12% (25 / 138)      Branches: 1.61% (1 / 62)      Functions: 0% (0 / 17)      Lines: 18.12% (25 / 138)      Ignored: none     

All files » node-npmtest-pm2-gui/pm2-gui/ » 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 2321 1 1 1 1 1 1 1 1 1   1                                                           1 1 1 1   1                                           1                                         1                                                                   1             1                                       1                                                           1             1                     1               1                                                  
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')
 
Iif (path.basename(process.mainModule.filename, '.js') === 'pm2-gui') {
  var cmd, file
  if (process.argv.length > 2) {
    cmd = process.argv[2]
  }
  if (process.argv.length > 3) {
    file = process.argv[3]
  }
  cmd = cmd || 'start'
 
  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
 
function startWebServer (confFile) {
  var monitor = slave({
    confFile: confFile
  })
  var options = monitor.options
 
  options.port = options.port || 8088
  var server = Web({
    middleware: function (req, res, next) {
      req._config = options
      next()
    },
    port: options.port
  })
 
  monitor.sockio = socketIO(server, {
    origins: options.origins || '*:*'
  })
  monitor.run()
  console.info('Web server is listening on 127.0.0.1:' + options.port)
}
 
function startAgent (confFile) {
  var monitor = slave({
    confFile: confFile
  })
 
  var options = monitor.options
  options.agent = options.agent || {}
  if (options.agent.offline) {
    console.error('Agent is offline, can not start it.')
    return process.exit(0)
  }
  options.port = options.port || 8088
  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)
}
 
function dashboard (confFile) {
  // restore cursor
  process.on('exit', function () {
    process.stdout.write('\u001b[?25h')
  })
  var monitor = slave({
    confFile: confFile
  })
  var options = _.clone(monitor.options)
  var q = Monitor.available(options)
 
  if (!q) {
    console.error('No agent is online, can not 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('')
    _connectToDashboard(monitor, options, Monitor.parseConnectionString(answers.socket_server))
  })
}
 
function exitGraceful (code, signal) {
  code = code || 0
  if (signal !== '-f') {
    console.debug('Slave has exited, code: ' + code + ', signal: ' + (signal || 'NULL'))
  }
  var fds = 0
 
  function tryToExit () {
    if ((fds & 1) && (fds & 2)) {
      process.exit(code)
    }
  }
 
  [process.stdout, process.stderr].forEach(function (std) {
    var fd = std.fd
    if (!std.bufferSize) {
      fds = fds | fd
    } else {
      std.write && std.write('', function () {
        fds = fds | fd
        tryToExit()
      })
    }
  })
  tryToExit()
}
 
function slave (options) {
  process.title = 'pm2-gui slave'
  options = options || {}
  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)
    }
  }
  var monitor = Monitor({
    confFile: confFile
  })
 
  Log(monitor.options.log)
 
  console.log(chalk.cyan(
    '\n' +
    '█▀▀█ █▀▄▀█ █▀█ ░░ ▒█▀▀█ ▒█░▒█ ▀█▀\n' +
    '█░░█ █░▀░█ ░▄▀ ▀▀ ▒█░▄▄ ▒█░▒█ ▒█░\n' +
    '█▀▀▀ ▀░░░▀ █▄▄ ░░ ▒█▄▄█ ░▀▄▄▀ ▄█▄\n'))
 
  process.on('SIGTERM', shutdown)
  process.on('SIGINT', shutdown)
  process.on('SIGHUP', restart)
  process.on('uncaughtException', caughtException)
  process.on('exit', exitGraceful)
 
  function shutdown (code, signal) {
    console.info('Shutting down....')
    monitor.quit()
    console.info('Shutdown complete!')
    exitGraceful(code, '-f')
  }
 
  function restart () {
    if (process.send) {
      process.send({
        action: 'restart'
      })
    } else {
      console.error('No IPC found, could not restart monitor, shutting down.')
      shutdown(1)
    }
  }
 
  function caughtException (err) {
    console.error(err.stack)
    shutdown(1)
  }
 
  return monitor
}
 
function _connectToDashboard (monitor, options, connection) {
  connection = _.extend({}, options, connection)
  if (!!~['127.0.0.1', '0.0.0.0', 'localhost'].indexOf(connection.hostname)) { // eslint-disable-line no-extra-boolean-cast
    return monitor.connect(connection, function (socket) {
      console.info('Agent is online, try to connect it in dashboard directly.')
      layout(connection).render(monitor)
    }, function (err, socket) {
      if (err === 'unauthorized') {
        console.error('There was an error with the authentication:', err)
        return process.exit(0)
      }
      console.warn('Agent is offline, try to start it.')
      var sockio = socketIO()
      sockio.listen(connection.port, {
        origins: options.origins || '*:*'
      })
      monitor.sockio = sockio
      monitor.run()
      layout(connection).render(monitor)
    })
  }
 
  layout(connection).render(monitor)
}