shell: make shell.sudo promise based and waitable
This commit is contained in:
+38
-5
@@ -5,7 +5,8 @@ const assert = require('assert'),
|
||||
child_process = require('child_process'),
|
||||
debug = require('debug')('box:shell'),
|
||||
once = require('./once.js'),
|
||||
util = require('util');
|
||||
path = require('path'),
|
||||
_ = require('./underscore.js');
|
||||
|
||||
exports = module.exports = shell;
|
||||
|
||||
@@ -16,11 +17,12 @@ function shell(tag) {
|
||||
bash: bash.bind(null, tag),
|
||||
spawn: spawn.bind(null, tag),
|
||||
sudo: sudo.bind(null, tag),
|
||||
promises: { sudo: util.promisify(sudo.bind(null, tag)) }
|
||||
sudoCallback: sudoCallback.bind(null, tag)
|
||||
};
|
||||
}
|
||||
|
||||
const SUDO = '/usr/bin/sudo';
|
||||
const KILL_CHILD_CMD = path.join(__dirname, 'scripts/kill-child.sh');
|
||||
|
||||
function lineCount(buffer) {
|
||||
assert(Buffer.isBuffer(buffer));
|
||||
@@ -44,18 +46,23 @@ function spawn(tag, file, args, options) {
|
||||
debug(`${tag}: ${file} ${args.join(' ').replace(/\n/g, '\\n')}`);
|
||||
|
||||
const maxLines = options.maxLines || Number.MAX_SAFE_INTEGER;
|
||||
const logger = options.logger || null;
|
||||
const signal = options.signal || null; // note: we use our own handler and not the child_process one
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const cp = child_process.spawn(file, args, options);
|
||||
const spawnOptions = _.omit(options, [ 'maxLines', 'logger', 'signal', 'onMessage', 'input', 'encoding' ]);
|
||||
const cp = child_process.spawn(file, args, spawnOptions);
|
||||
const stdoutBuffers = [], stderrBuffers = [];
|
||||
let stdoutLineCount = 0, stderrLineCount = 0;
|
||||
|
||||
cp.stdout.on('data', (data) => {
|
||||
if (logger) return logger(data);
|
||||
stdoutBuffers.push(data);
|
||||
stdoutLineCount += lineCount(data);
|
||||
if (stdoutLineCount >= maxLines) return cp.kill('SIGKILL');
|
||||
});
|
||||
cp.stderr.on('data', (data) => {
|
||||
if (logger) return logger(data);
|
||||
stderrBuffers.push(data);
|
||||
stderrLineCount += lineCount(data);
|
||||
if (stderrLineCount >= maxLines) return cp.kill('SIGKILL');
|
||||
@@ -86,8 +93,16 @@ function spawn(tag, file, args, options) {
|
||||
debug(`${tag}: ${file} ${args.join(' ').replace(/\n/g, '\\n')} errored`, error);
|
||||
});
|
||||
|
||||
signal?.addEventListener('abort', () => {
|
||||
child_process.execFile('/usr/bin/sudo', [ KILL_CHILD_CMD, cp.pid, process.pid ], { encoding: 'utf8' }, (error, stdout, stderr) => {
|
||||
if (error) debug(`${tag}: failed to kill children`, stdout, stderr);
|
||||
});
|
||||
}, { once: true });
|
||||
|
||||
if (options.onMessage) cp.on('message', options.onMessage); // ipc mode messages
|
||||
|
||||
// https://github.com/nodejs/node/issues/25231
|
||||
if (options.input) {
|
||||
if ('input' in options) { // when empty, just closes
|
||||
cp.stdin.write(options.input);
|
||||
cp.stdin.end();
|
||||
}
|
||||
@@ -102,7 +117,25 @@ async function bash(tag, script, options) {
|
||||
return await spawn(tag, '/bin/bash', [ '-c', script ], options);
|
||||
}
|
||||
|
||||
function sudo(tag, args, options, callback) {
|
||||
async function sudo(tag, args, options) {
|
||||
assert.strictEqual(typeof tag, 'string');
|
||||
assert(Array.isArray(args));
|
||||
assert.strictEqual(typeof options, 'object');
|
||||
|
||||
const sudoArgs = [];
|
||||
if (options.preserveEnv) sudoArgs.push('-E'); // -E preserves environment
|
||||
|
||||
if (options.onMessage) { // enable ipc
|
||||
sudoArgs.push('--close-from=4'); // keep the ipc open. requires closefrom_override in sudoers file
|
||||
options.stdio = ['pipe', 'pipe', 'pipe', 'ipc'];
|
||||
}
|
||||
|
||||
const spawnArgs = [ ...sudoArgs, ...args ];
|
||||
|
||||
return await spawn(tag, SUDO, spawnArgs, options);
|
||||
}
|
||||
|
||||
function sudoCallback(tag, args, options, callback) {
|
||||
assert.strictEqual(typeof tag, 'string');
|
||||
assert(Array.isArray(args));
|
||||
assert.strictEqual(typeof options, 'object');
|
||||
|
||||
Reference in New Issue
Block a user