Files
cloudron-box/src/shell.js

110 lines
2.9 KiB
JavaScript
Raw Normal View History

'use strict';
exports = module.exports = {
2016-05-24 10:19:00 -07:00
exec: exec,
2016-10-31 15:10:27 +01:00
execSync: execSync,
2017-03-07 15:14:37 +01:00
sudo: sudo,
sudoSync: sudoSync
};
var assert = require('assert'),
child_process = require('child_process'),
2016-08-30 21:33:56 -07:00
debug = require('debug')('box:shell'),
fs = require('fs'),
once = require('once'),
util = require('util');
var SUDO = '/usr/bin/sudo';
2016-05-24 11:39:05 -07:00
function execSync(tag, cmd, callback) {
2016-05-24 10:19:00 -07:00
assert.strictEqual(typeof tag, 'string');
assert.strictEqual(typeof cmd, 'string');
2016-05-24 11:26:11 -07:00
debug(cmd);
try {
child_process.execSync(cmd, { stdio: 'inherit' });
} catch (e) {
if (callback) return callback(e);
throw e;
}
if (callback) callback();
2016-05-24 10:19:00 -07:00
}
function exec(tag, file, args, options, callback) {
assert.strictEqual(typeof tag, 'string');
assert.strictEqual(typeof file, 'string');
assert(util.isArray(args));
assert.strictEqual(typeof options, 'object');
2017-09-09 19:48:05 -07:00
assert.strictEqual(typeof callback, 'function');
callback = once(callback); // exit may or may not be called after an 'error'
2017-05-19 14:39:08 -07:00
debug(tag + ' execFile: %s', file); // do not dump args as it might have sensitive info
var cp = child_process.spawn(file, args, options);
if (options.logFile) {
var logFile = fs.createWriteStream(options.logFile);
cp.stdout.pipe(logFile);
cp.stderr.pipe(logFile);
} else {
cp.stdout.on('data', function (data) {
debug(tag + ' (stdout): %s', data.toString('utf8'));
});
cp.stderr.on('data', function (data) {
debug(tag + ' (stderr): %s', data.toString('utf8'));
});
}
cp.on('exit', function (code, signal) {
if (code || signal) debug(tag + ' code: %s, signal: %s', code, signal);
2017-04-23 17:39:43 -07:00
if (code === 0) return callback(null);
var e = new Error(util.format(tag + ' exited with error %s signal %s', code, signal));
e.code = code;
e.signal = signal;
callback(e);
});
cp.on('error', function (error) {
debug(tag + ' code: %s, signal: %s', error.code, error.signal);
callback(error);
});
return cp;
}
function sudo(tag, args, options, callback) {
assert.strictEqual(typeof tag, 'string');
assert(util.isArray(args));
if (typeof options === 'function') {
callback = options;
options = { };
}
assert.strictEqual(typeof options, 'object');
2017-09-21 14:36:20 -07:00
// -S makes sudo read stdin for password. -E preserves environment
var cp = exec(tag, SUDO, [ options.env ? '-SE' : '-S' ].concat(args), options, callback);
cp.stdin.end();
return cp;
}
2017-03-07 15:14:37 +01:00
function sudoSync(tag, cmd, callback) {
assert.strictEqual(typeof tag, 'string');
assert.strictEqual(typeof cmd, 'string');
// -S makes sudo read stdin for password
cmd = 'sudo -S ' + cmd;
debug(cmd);
try {
child_process.execSync(cmd, { stdio: 'inherit' });
} catch (e) {
if (callback) return callback(e);
throw e;
}
if (callback) callback();
}