|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +':' //; exec "`command -v nodejs || command -v node`" $PM2_NODE_OPTIONS "$0" "$@" |
| 4 | + |
| 5 | +var commander = require('commander'); |
| 6 | +var fs = require('fs'); |
| 7 | +var path = p = require('path'); |
| 8 | +var util = require('util'); |
| 9 | +var watch = require('watch'); |
| 10 | +var cronJob = require('cron').CronJob; |
| 11 | + |
| 12 | +var Monit = require('../lib/Monit'); |
| 13 | +var UX = require('../lib/CliUx'); |
| 14 | +var Log = require('../lib/Log'); |
| 15 | +var Satan = require('../lib/Satan'); |
| 16 | +var CLI = require('../lib/CLI'); |
| 17 | +var cst = require('../constants.js'); |
| 18 | +var pkg = require('../package.json'); |
| 19 | + |
| 20 | +commander.version(pkg.version) |
| 21 | + .option('-v --verbose', 'Verbose level') |
| 22 | + .option('-s --silent', 'Throw less messages', false) |
| 23 | + .option('-f --force', 'Force actions') |
| 24 | + .option('-n --name <name>', 'Set a <name> for script') |
| 25 | + .option('-i --instances <number>', 'Launch [number] instances (for networked app)(load balanced)') |
| 26 | + .option('-o --output <path>', 'Out log file output') |
| 27 | + .option('-x --execute-command', 'Execute a program using fork system') |
| 28 | + .option('--interpreter <interpreter>', 'The interpreter pm2 should use for executing app (bash, python...)') |
| 29 | + .option('-e --error <path>', 'Error log file output') |
| 30 | + .option('-p --pid <pid>', 'PID file') |
| 31 | + .option('-u --user <username>', 'When generating startup script defined the username') |
| 32 | + .option('-c --cron <cron_pattern>', 'Restart a running process based on a cron pattern') |
| 33 | + .option('-w --write', 'Write configuration in local folder') |
| 34 | + .usage('[cmd] app'); |
| 35 | + |
| 36 | +// |
| 37 | +// Start command |
| 38 | +// |
| 39 | +commander.command('start <script>') |
| 40 | + .description('start specific part') |
| 41 | + .action(function(cmd) { |
| 42 | + if (cmd.indexOf('.json') > 0) |
| 43 | + CLI.startFromJson(cmd); |
| 44 | + else |
| 45 | + CLI.startFile(cmd); |
| 46 | + }); |
| 47 | + |
| 48 | +// |
| 49 | +// Stop specific id |
| 50 | +// |
| 51 | +commander.command('stop <pm2_id|name|all>') |
| 52 | + .description('stop specific process pm2 id or script name (set with --name or script name)') |
| 53 | + .action(function(pm2_id) { |
| 54 | + UX.processing.start(); |
| 55 | + |
| 56 | + if (pm2_id == 'all') |
| 57 | + CLI.stopAll(); |
| 58 | + else if (isNaN(parseInt(pm2_id))) { |
| 59 | + CLI.stopProcessName(pm2_id); |
| 60 | + } else { |
| 61 | + console.log(cst.PREFIX_MSG + 'Stopping process by id ' + pm2_id); |
| 62 | + CLI.stopId(pm2_id); |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | +// |
| 67 | +// Stop and delete a process by name from database |
| 68 | +// |
| 69 | +commander.command('delete <name|id|script|all>') |
| 70 | + .description('Stop and delete a process by name from PM2 database') |
| 71 | + .action(function(name) { |
| 72 | + CLI.deleteProcess(name); |
| 73 | + }); |
| 74 | + |
| 75 | +// |
| 76 | +// Web interface |
| 77 | +// |
| 78 | +commander.command('web') |
| 79 | + .description('launch process/server monit api on ' + cst.WEB_INTERFACE) |
| 80 | + .action(function() { |
| 81 | + console.log('Launching web interface on port ' + cst.WEB_INTERFACE); |
| 82 | + CLI.web(); |
| 83 | + }); |
| 84 | + |
| 85 | +// |
| 86 | +// Save processes to file |
| 87 | +// |
| 88 | +commander.command('dump') |
| 89 | + .description('dump all processes for resurecting them later') |
| 90 | + .action(function() { |
| 91 | + console.log(cst.PREFIX_MSG + 'Dumping processes'); |
| 92 | + UX.processing.start(); |
| 93 | + CLI.dump(); |
| 94 | + }); |
| 95 | + |
| 96 | +// |
| 97 | +// Save processes to file |
| 98 | +// |
| 99 | +commander.command('resurrect') |
| 100 | + .description('resurect previously dumped processes') |
| 101 | + .action(function() { |
| 102 | + console.log(cst.PREFIX_MSG + 'Resurrecting'); |
| 103 | + UX.processing.start(); |
| 104 | + CLI.resurrect(); |
| 105 | + }); |
| 106 | + |
| 107 | +// |
| 108 | +// Stop All processes |
| 109 | +// |
| 110 | +commander.command('restart <pm2_id|name|all>') |
| 111 | + .description('restart specific process pm2 id') |
| 112 | + .action(function(pm2_id) { |
| 113 | + UX.processing.start(); |
| 114 | + |
| 115 | + if (pm2_id == 'all') { |
| 116 | + CLI.restartAll(); |
| 117 | + } |
| 118 | + else if (isNaN(parseInt(pm2_id))) { |
| 119 | + console.log(cst.PREFIX_MSG + 'Restarting process by name ' + pm2_id); |
| 120 | + CLI.restartProcessByName(pm2_id); |
| 121 | + } else { |
| 122 | + console.log(cst.PREFIX_MSG + 'Restarting process by id ' + pm2_id); |
| 123 | + CLI.restartProcessById(pm2_id); |
| 124 | + } |
| 125 | + }); |
| 126 | + |
| 127 | +// |
| 128 | +// Reload process(es) |
| 129 | +// |
| 130 | +commander.command('reload <pm2_id|all>') |
| 131 | + .description('reload all processes or a specific process pm2 id') |
| 132 | + .action(function(pm2_id) { |
| 133 | + UX.processing.start(); |
| 134 | + CLI.reload(pm2_id); |
| 135 | + }); |
| 136 | + |
| 137 | +// |
| 138 | +// Set pm2 to startup |
| 139 | +// |
| 140 | +commander.command('startup [platform]') |
| 141 | + .description('auto resurect process at startup. [platform] can be centos, default use update-rc.d') |
| 142 | + .action(function(platform) { |
| 143 | + CLI.startup(platform); |
| 144 | + }); |
| 145 | + |
| 146 | + |
| 147 | +// |
| 148 | +// Sample generate |
| 149 | +// |
| 150 | +commander.command('generate <name>') |
| 151 | + .description('generate sample JSON') |
| 152 | + .action(function(name) { |
| 153 | + CLI.generateSample(name); |
| 154 | + }); |
| 155 | + |
| 156 | +// |
| 157 | +// List command |
| 158 | +// |
| 159 | +commander.command('list') |
| 160 | + .description('list all processes') |
| 161 | + .action(CLI.list); |
| 162 | + |
| 163 | +commander.command('ls') |
| 164 | + .description('(alias) list all processes') |
| 165 | + .action(CLI.list); |
| 166 | + |
| 167 | +commander.command('l') |
| 168 | + .description('(alias) list all processes') |
| 169 | + .action(CLI.list); |
| 170 | + |
| 171 | +commander.command('status') |
| 172 | + .description('(alias) list all processes') |
| 173 | + .action(CLI.list); |
| 174 | + |
| 175 | + |
| 176 | +// List in raw json |
| 177 | +commander.command('jlist') |
| 178 | + .description('list all processes in JSON format') |
| 179 | + .action(function() { |
| 180 | + CLI.jlist(); |
| 181 | + }); |
| 182 | + |
| 183 | +// List in prettified Json |
| 184 | +commander.command('prettylist') |
| 185 | + .description('print json in a prettified JSON') |
| 186 | + .action(function() { |
| 187 | + CLI.jlist(true); |
| 188 | + }); |
| 189 | + |
| 190 | +// |
| 191 | +// Monitoring command |
| 192 | +// |
| 193 | +commander.command('monit') |
| 194 | + .description('launch termcaps monitoring') |
| 195 | + .action(CLI.monit); |
| 196 | + |
| 197 | +commander.command('m') |
| 198 | + .description('(alias) launch termcaps monitoring') |
| 199 | + .action(CLI.monit); |
| 200 | + |
| 201 | +// |
| 202 | +// Flushing command |
| 203 | +// |
| 204 | +commander.command('flush') |
| 205 | + .description('flush logs') |
| 206 | + .action(function() { |
| 207 | + CLI.flush(); |
| 208 | + }); |
| 209 | + |
| 210 | +// |
| 211 | +// Log streaming |
| 212 | +// |
| 213 | +commander.command('logs [id|name]') |
| 214 | + .description('stream logs file. Default stream all logs') |
| 215 | + .action(function(id) { |
| 216 | + CLI.streamLogs(id); |
| 217 | + }); |
| 218 | + |
| 219 | + |
| 220 | +// |
| 221 | +// Kill |
| 222 | +// |
| 223 | +commander.command('kill') |
| 224 | + .description('kill daemon') |
| 225 | + .action(function() { |
| 226 | + CLI.killDaemon(); |
| 227 | + }); |
| 228 | + |
| 229 | +// |
| 230 | +// Catch all |
| 231 | +// |
| 232 | +commander.command('*') |
| 233 | + .action(function() { |
| 234 | + console.log(cst.PREFIX_MSG + '\nCommand not found'); |
| 235 | + commander.outputHelp(); |
| 236 | + process.exit(cst.ERROR_EXIT); |
| 237 | + }); |
| 238 | + |
| 239 | +// |
| 240 | +// Display help |
| 241 | +// |
| 242 | +if (process.argv.length == 2) { |
| 243 | + commander.parse(process.argv); |
| 244 | + commander.outputHelp(); |
| 245 | + process.exit(cst.ERROR_EXIT); |
| 246 | +} |
| 247 | + |
| 248 | + |
| 249 | +// |
| 250 | +// Wait Satan is connected to God to launch parsing |
| 251 | +// |
| 252 | +// This message is triggered once the RPC Client is connected to the Daemon |
| 253 | +// in file Satan.js, method Satan.launchRPC |
| 254 | +// |
| 255 | +process.on('satan:client:ready', function() { |
| 256 | + commander.parse(process.argv); |
| 257 | +}); |
| 258 | + |
| 259 | +// |
| 260 | +// Init |
| 261 | +// |
| 262 | +(function init() { |
| 263 | + fs.exists(cst.DEFAULT_FILE_PATH, function(exist) { |
| 264 | + if (!exist) { |
| 265 | + fs.mkdirSync(cst.DEFAULT_FILE_PATH); |
| 266 | + fs.mkdirSync(cst.DEFAULT_LOG_PATH); |
| 267 | + fs.mkdirSync(cst.DEFAULT_PID_PATH); |
| 268 | + } |
| 269 | + }); |
| 270 | +})(); |
| 271 | + |
| 272 | +// |
| 273 | +// Harmony test |
| 274 | +// |
| 275 | +try { |
| 276 | + var assert = require('assert') |
| 277 | + , s = new Set(); |
| 278 | + s.add('a'); |
| 279 | + assert.ok(s.has('a')); |
| 280 | + console.log('ES6 Harmony succesfully enabled'); |
| 281 | +} catch(e) { |
| 282 | +} |
0 commit comments