Permalink
Fetching contributors…
Cannot retrieve contributors at this time
executable file 92 lines (78 sloc) 2.22 KB
#!/usr/bin/env node
var fs = require('fs'),
path = require('path'),
optimist = require('optimist'),
winston = require('winston'),
solenoid = require('../'),
config,
logger,
options,
action,
argv;
argv = optimist
.alias('u', 'app-user')
.alias('a', 'app-name')
.alias('e', 'app-env')
.alias('v', 'app-version')
.alias('p', 'pidfile')
.alias('m', 'min-uptime')
.alias('r', 'retries')
.alias('t', 'min-delay')
.alias('o', 'old-mode')
.argv;
action = argv._[0];
if (argv.version || (argv.v && !action)) {
console.log(JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'package.json'))).version);
process.exit(0);
}
logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)(),
new (winston.transports.File)({ filename: '/root/solenoid.log' })
]
});
try {
config = JSON.parse(fs.readFileSync(path.join(process.env.HOME, '.solenoidconf')));
} catch (ex) {
logger.error('.solenoidconf is required in $HOME directory');
process.exit(1);
}
options = {
storage: config.storage,
instruments: config.instruments,
engines: config.engines,
minUptime: argv.m || config.minUptime || 2000,
retries: argv.r || config.retries || 6,
minDelay: argv.t || config.minDelay || 1000,
runDir: config.runDir,
logger: logger,
old: argv.o || false
};
if (['download', 'start', 'restart', 'stop'].indexOf(action) === -1) {
logger.error('Action is required and must be either "download", "start", "restart" or "stop"');
}
if (!argv.pidfile) {
logger.error('PID file is required');
process.exit(1);
}
options.pidFile = argv.pidfile;
if (action === 'download' || action === 'start') {
if (!argv['app-user'] || !argv['app-name'] || !argv['app-version']) {
logger.error('Application name, user and version are required');
process.exit(1);
}
options.app = {
user: argv['app-user'],
name: argv['app-name'],
version: argv['app-version'],
env: argv['app-env']
};
}
logger.info('Running ' + action + ' for app.');
solenoid[action](options, function (err) {
if (err) {
logger.error('Error: ' + err.message);
return process.exit(1);
}
logger.info('Success:' + action);
});