diff --git a/src/platform.js b/src/platform.js index bbbc79bef..446aa13cb 100644 --- a/src/platform.js +++ b/src/platform.js @@ -65,7 +65,6 @@ function start(callback) { startApps.bind(null, existingInfra), graphs.startGraphite.bind(null, existingInfra), addons.startAddons.bind(null, existingInfra), - pruneInfraImages, fs.writeFile.bind(fs, paths.INFRA_VERSION_FILE, JSON.stringify(infra, null, 4)) ], function (error) { if (error) return callback(error); @@ -86,7 +85,9 @@ function onPlatformReady() { debug('onPlatformReady: platform is ready'); exports._isReady = true; taskmanager.resumeTasks(); + applyPlatformConfig(NOOP_CALLBACK); + pruneInfraImages(NOOP_CALLBACK); } function applyPlatformConfig(callback) { @@ -110,9 +111,9 @@ function pruneInfraImages(callback) { debug('pruneInfraImages: checking existing images'); // cannot blindly remove all unused images since redis image may not be used - let images = infra.baseImages.concat(Object.keys(infra.images).map(function (addon) { return infra.images[addon]; })); + const images = infra.baseImages.concat(Object.keys(infra.images).map(function (addon) { return infra.images[addon]; })); - for (let image of images) { + async.eachSeries(images, function (image, iteratorCallback) { let output = execSync(`docker images --digests ${image.repo} --format "{{.ID}} {{.Repository}}:{{.Tag}}@{{.Digest}}"`, { encoding: 'utf8' }); let lines = output.trim().split('\n'); for (let line of lines) { @@ -120,11 +121,10 @@ function pruneInfraImages(callback) { let parts = line.split(' '); // [ ID, Repo:Tag@Digest ] if (image.tag === parts[1]) continue; // keep debug(`pruneInfraImages: removing unused image of ${image.repo}: ${line}`); - shell.execSync('pruneInfraImages', `docker rmi ${parts[0]}`); - } - } - callback(); + shell.exec('pruneInfraImages', `docker rmi ${parts[0]}`, iteratorCallback); + } + }, callback); } function stopContainers(existingInfra, callback) { diff --git a/src/shell.js b/src/shell.js index 87dda5e54..c476307aa 100644 --- a/src/shell.js +++ b/src/shell.js @@ -2,6 +2,7 @@ exports = module.exports = { spawn: spawn, + exec: exec, execSync: execSync, sudo: sudo, sudoSync: sudoSync @@ -29,6 +30,21 @@ function execSync(tag, cmd, callback) { if (callback) callback(); } +function exec(tag, cmd, callback) { + assert.strictEqual(typeof tag, 'string'); + assert.strictEqual(typeof cmd, 'string'); + assert.strictEqual(typeof callback, 'function'); + + debug(`${tag} exec: ${cmd}`); + + child_process.execSync(cmd, function (error, stdout, stderr) { + debug(`${tag} (stdout): %s`, stdout.toString('utf8')); + debug(`${tag} (stderr): %s`, stderr.toString('utf8')); + + callback(error); + }); +} + function spawn(tag, file, args, options, callback) { assert.strictEqual(typeof tag, 'string'); assert.strictEqual(typeof file, 'string'); @@ -38,7 +54,7 @@ function spawn(tag, file, args, options, callback) { callback = once(callback); // exit may or may not be called after an 'error' - debug(tag + ' execFile: %s %s', file, args.join(' ')); + debug(tag + ' spawn: %s %s', file, args.join(' ')); var cp = child_process.spawn(file, args, options); if (options.logStream) { @@ -50,7 +66,7 @@ function spawn(tag, file, args, options, callback) { }); cp.stderr.on('data', function (data) { - debug(tag + ' (stderr): %s', data.toString('utf8')); + debug(tag + ' (stdout): %s', data.toString('utf8')); }); }