backup: use ipc for communicating with upload process

This commit is contained in:
Girish Ramakrishnan
2018-11-26 15:21:48 -08:00
parent e4512e12c5
commit a2da9bea58
4 changed files with 17 additions and 7 deletions
+8 -2
View File
@@ -600,16 +600,22 @@ function runBackupUpload(backupId, format, dataDir, callback) {
assert.strictEqual(typeof dataDir, 'string');
assert.strictEqual(typeof callback, 'function');
shell.sudo(`backup-${backupId}`, [ BACKUP_UPLOAD_CMD, backupId, format, dataDir ], { preserveEnv: true }, function (error) {
let result = '';
let cp = shell.sudo(`backup-${backupId}`, [ BACKUP_UPLOAD_CMD, backupId, format, dataDir ], { preserveEnv: true, ipc: true }, function (error) {
if (error && (error.code === null /* signal */ || (error.code !== 0 && error.code !== 50))) { // backuptask crashed
return callback(new BackupsError(BackupsError.INTERNAL_ERROR, 'Backuptask crashed'));
} else if (error && error.code === 50) { // exited with error
var result = safe.fs.readFileSync(paths.BACKUP_RESULT_FILE, 'utf8') || safe.error.message;
return callback(new BackupsError(BackupsError.EXTERNAL_ERROR, result));
}
callback();
});
cp.on('message', function (message) {
debug(`runBackupUpload: message - ${message}`);
result = message.result;
});
}
function getSnapshotInfo(id) {
+1 -3
View File
@@ -36,14 +36,12 @@ process.on('SIGTERM', function () {
initialize(function (error) {
if (error) throw error;
safe.fs.writeFileSync(paths.BACKUP_RESULT_FILE, '');
backups.upload(backupId, format, dataDir, function resultHandler(error) {
if (error) debug('upload completed with error', error);
debug('upload completed');
safe.fs.writeFileSync(paths.BACKUP_RESULT_FILE, error ? error.message : '');
process.send({ result: error ? error.message : '' });
// https://nodejs.org/api/process.html are exit codes used by node. apps.js uses the value below
// to check apptask crashes
+7 -2
View File
@@ -40,6 +40,8 @@ function spawn(tag, file, args, options, callback) {
debug(tag + ' spawn: %s %s', file, args.join(' '));
if (options.ipc) options.stdio = ['pipe', 'pipe', 'pipe', 'ipc'];
var cp = child_process.spawn(file, args, options);
if (options.logStream) {
cp.stdout.pipe(options.logStream);
@@ -78,8 +80,11 @@ function sudo(tag, args, options, callback) {
assert.strictEqual(typeof options, 'object');
assert.strictEqual(typeof callback, 'function');
// -S makes sudo read stdin for password. -E preserves environment
var cp = spawn(tag, SUDO, [ options.preserveEnv ? '-SE' : '-S' ].concat(args), options, callback);
let sudoArgs = [ '-S' ]; // -S makes sudo read stdin for password
if (options.preserveEnv) sudoArgs.push('-E'); // -E preserves environment
if (options.ipc) sudoArgs.push('--close-from=4'); // keep the ipc open. requires closefrom_override in sudoers file
var cp = spawn(tag, SUDO, sudoArgs.concat(args), options, callback);
cp.stdin.end();
return cp;
}