Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added image pulling where needed #6

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/modules/docker.js
Expand Up @@ -28,14 +28,23 @@ const operateOnContainers = onlyContainers || (!onlyContainers && !onlyVolumes);
const operateOnVolumes = onlyVolumes || (!onlyVolumes && !onlyContainers);

// If we know that we will operate on volumes, get all volume files in advance
// Also initialize a variable for pulling the volume operations image if needed
const volumeFiles = operateOnVolumes ? getVolumeFilesSync() : [];
let volumeImagePromise = Promise.resolve();

// Get all containers
const getContainers = async (all = true) => {
const containers = await docker.listContainers({ all });
return containers.map(container => container.Id);
};

// Helper to pull an image and log
const pullImage = (name) => {
// eslint-disable-next-line no-console
console.log(`Pulling image: ${name}`);
return docker.pull(name);
};

// Helper to start container and log
const startContainer = (container) => {
// eslint-disable-next-line no-console
Expand Down Expand Up @@ -69,6 +78,10 @@ const backupVolume = volumeLimit((containerName, volumeName, mountPoint) => dock
const backupContainer = containerLimit(async (id) => {
// eslint-disable-next-line no-console
console.log(`== Backing up container: ${id} ==`);

// Wait for the volume operations image to be downloaded before proceeding
await volumeImagePromise;

const container = docker.getContainer(id);
const inspect = await container.inspect();
const name = formatContainerName(inspect.Name);
Expand Down Expand Up @@ -130,11 +143,17 @@ const restoreVolume = volumeLimit((containerName, tarName, mountPoint) => docker
const restoreContainer = containerLimit(async (name) => {
// eslint-disable-next-line no-console
console.log(`== Restoring container: ${name} ==`);

// Wait for the volume operations image to be downloaded before proceeding
await volumeImagePromise;

const backupInspect = await loadInspect(name);

// Restore container
let container = null;
if (operateOnContainers) {
await pullImage(backupInspect.Config.Image);

// eslint-disable-next-line no-console
console.log('Creating container...');
container = await docker.createContainer(inspect2Config(backupInspect));
Expand Down Expand Up @@ -188,6 +207,11 @@ const restoreContainer = containerLimit(async (name) => {
return true;
});

// Start pulling the volume operations image if we plan to work on volumes
if (operateOnVolumes) {
volumeImagePromise = pullImage(volumeOperationsImage);
}

// Exports
module.exports = {
getContainers,
Expand Down