Skip to content

Commit

Permalink
Added error reporting for run commands
Browse files Browse the repository at this point in the history
  • Loading branch information
tlaanemaa committed May 23, 2019
1 parent 8e455d5 commit a872920
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion __mocks__/dockerode.js
Expand Up @@ -60,7 +60,7 @@ Docker.prototype.getContainer = () => Docker.mockContainer;
Docker.prototype.getImage = () => Docker.mockImage;
Docker.prototype.getVolume = () => Docker.mockVolume;
Docker.prototype.createVolume = jest.fn().mockResolvedValue(Docker.mockVolume);
Docker.prototype.run = jest.fn().mockResolvedValue();
Docker.prototype.run = jest.fn().mockResolvedValue({ output: { StatusCode: 0 } });
Docker.prototype.createContainer = jest.fn().mockResolvedValue(Docker.mockContainer);
Docker.prototype.pull = jest.fn().mockImplementation((name, callback) => {
callback(null, 'stream');
Expand Down
2 changes: 1 addition & 1 deletion src/main.js
Expand Up @@ -21,7 +21,7 @@ module.exports = async () => {
// Print a summary of the results if there are any
if (results.length) {
// eslint-disable-next-line no-console
console.log('\nSummary (does not include errors from the tar command used for volume backup/restore):');
console.log('\nSummary:');
results.forEach(({ name, result }) => {
const success = !(result instanceof Error);
const mark = success ? '✔' : '✖';
Expand Down
15 changes: 13 additions & 2 deletions src/modules/docker.js
Expand Up @@ -209,7 +209,7 @@ const backupVolume = volumeLimit(async (name) => {

// eslint-disable-next-line no-console
console.log(`Starting volume backup for ${name}...`);
await docker.run(
const runResult = await docker.run(
volumeOperationsImage,
['tar', 'cvf', `${dockerBackupMountDir}/${name}.tar`, dockerBackupVolumeDir],
process.stdout,
Expand All @@ -223,6 +223,11 @@ const backupVolume = volumeLimit(async (name) => {
},
},
);

// Throw the returned error
if (runResult.output.StatusCode !== 0) {
throw new Error(runResult.output.Error || 'Volume backup tar command failed, see previous errors.');
}
});

// Back up single container by id
Expand Down Expand Up @@ -281,7 +286,7 @@ const restoreVolume = volumeLimit(async (name) => {
// eslint-disable-next-line no-console
console.log(`Restoring contents of ${inspect.Name}...`);

await docker.run(
const runResult = await docker.run(
volumeOperationsImage,
['tar', 'xvf', `${dockerBackupMountDir}/${name}.tar`, '--strip', '1', '--directory', dockerBackupVolumeDir],
process.stdout,
Expand All @@ -295,6 +300,12 @@ const restoreVolume = volumeLimit(async (name) => {
},
},
);


// Throw the returned error
if (runResult.output.StatusCode !== 0) {
throw new Error(runResult.output.Error || 'Volume restore tar command failed, see previous errors.');
}
}
});

Expand Down

0 comments on commit a872920

Please sign in to comment.