Make restoreConfigJson, oldConfigJson, updateConfigJson as task args

This commit is contained in:
Girish Ramakrishnan
2019-08-27 15:18:16 -07:00
parent f535b3de2f
commit bfe2f116a7
8 changed files with 75 additions and 92 deletions

View File

@@ -499,12 +499,13 @@ function downloadImage(manifest, callback) {
// - setup the container (requires image, volumes, addons)
// - setup collectd (requires container id)
// restore is also handled here since restore is just an install with some oldConfig to clean up
function install(app, progressCallback, callback) {
function install(app, restoreConfig, progressCallback, callback) {
assert.strictEqual(typeof app, 'object');
assert.strictEqual(typeof restoreConfig, 'object');
assert.strictEqual(typeof progressCallback, 'function');
assert.strictEqual(typeof callback, 'function');
const restoreConfig = app.restoreConfig, isRestoring = app.installationState === appdb.ISTATE_PENDING_RESTORE;
const isRestoring = app.installationState === appdb.ISTATE_PENDING_RESTORE;
async.series([
// this protects against the theoretical possibility of an app being marked for install/restore from
@@ -520,7 +521,7 @@ function install(app, progressCallback, callback) {
deleteContainers.bind(null, app, { managedOnly: true }),
function teardownAddons(next) {
// when restoring, app does not require these addons anymore. remove carefully to preserve the db passwords
var addonsToRemove = !isRestoring ? app.manifest.addons : _.omit(app.oldConfig.manifest.addons, Object.keys(app.manifest.addons));
var addonsToRemove = !isRestoring ? app.manifest.addons : _.omit(restoreConfig.oldManifest.addons, Object.keys(app.manifest.addons));
addons.teardownAddons(app, addonsToRemove, next);
},
@@ -528,9 +529,11 @@ function install(app, progressCallback, callback) {
// for restore case
function deleteImageIfChanged(done) {
if (!app.oldConfig || (app.oldConfig.manifest.dockerImage === app.manifest.dockerImage)) return done();
if (!restoreConfig.oldManifest) return done();
docker.deleteImage(app.oldConfig.manifest, done);
if (restoreConfig.oldManifest.dockerImage === app.manifest.dockerImage) return done();
docker.deleteImage(restoreConfig.oldManifest, done);
},
reserveHttpPort.bind(null, app),
@@ -551,7 +554,7 @@ function install(app, progressCallback, callback) {
createAppDir.bind(null, app),
function restoreFromBackup(next) {
if (!restoreConfig) {
if (!restoreConfig.backupId) {
async.series([
progressCallback.bind(null, { percent: 60, message: 'Setting up addons' }),
addons.setupAddons.bind(null, app, app.manifest.addons),
@@ -619,14 +622,15 @@ function backup(app, progressCallback, callback) {
}
// note that configure is called after an infra update as well
function configure(app, progressCallback, callback) {
function configure(app, oldConfig, progressCallback, callback) {
assert.strictEqual(typeof app, 'object');
assert.strictEqual(typeof oldConfig, 'object');
assert.strictEqual(typeof progressCallback, 'function');
assert.strictEqual(typeof callback, 'function');
// oldConfig can be null during an infra update
const locationChanged = app.oldConfig && (app.oldConfig.fqdn !== app.fqdn);
const dataDirChanged = app.oldConfig && (app.oldConfig.dataDir !== app.dataDir);
const locationChanged = oldConfig.fqdn !== app.fqdn;
const dataDirChanged = oldConfig.dataDir !== app.dataDir;
async.series([
progressCallback.bind(null, { percent: 10, message: 'Cleaning up old install' }),
@@ -639,7 +643,7 @@ function configure(app, progressCallback, callback) {
function (next) {
if (!locationChanged) return next();
unregisterSubdomain(app, app.oldConfig.location, app.oldConfig.domain, next);
unregisterSubdomain(app, oldConfig.location, oldConfig.domain, next);
},
reserveHttpPort.bind(null, app),
@@ -667,7 +671,7 @@ function configure(app, progressCallback, callback) {
function (next) {
if (!dataDirChanged) return next();
migrateDataDir(app, app.oldConfig.dataDir, next);
migrateDataDir(app, oldConfig.dataDir, next);
},
progressCallback.bind(null, { percent: 60, message: 'Creating container' }),
@@ -699,16 +703,17 @@ function configure(app, progressCallback, callback) {
}
// nginx configuration is skipped because app.httpPort is expected to be available
function update(app, progressCallback, callback) {
function update(app, updateConfig, progressCallback, callback) {
assert.strictEqual(typeof app, 'object');
assert.strictEqual(typeof updateConfig, 'object');
assert.strictEqual(typeof progressCallback, 'function');
assert.strictEqual(typeof callback, 'function');
debugApp(app, `Updating to ${app.updateConfig.manifest.version}`);
debugApp(app, `Updating to ${updateConfig.manifest.version}`);
// app does not want these addons anymore
// FIXME: this does not handle option changes (like multipleDatabases)
var unusedAddons = _.omit(app.manifest.addons, Object.keys(app.updateConfig.manifest.addons));
var unusedAddons = _.omit(app.manifest.addons, Object.keys(updateConfig.manifest.addons));
const FORCED_UPDATE = (app.installationState === appdb.ISTATE_PENDING_FORCE_UPDATE);
@@ -716,7 +721,7 @@ function update(app, progressCallback, callback) {
// this protects against the theoretical possibility of an app being marked for update from
// a previous version of box code
progressCallback.bind(null, { percent: 0, message: 'Verify manifest' }),
verifyManifest.bind(null, app.updateConfig.manifest),
verifyManifest.bind(null, updateConfig.manifest),
function (next) {
if (FORCED_UPDATE) return next(null);
@@ -736,7 +741,7 @@ function update(app, progressCallback, callback) {
// download new image before app is stopped. this is so we can reduce downtime
// and also not remove the 'common' layers when the old image is deleted
progressCallback.bind(null, { percent: 25, message: 'Downloading image' }),
downloadImage.bind(null, app.updateConfig.manifest),
downloadImage.bind(null, updateConfig.manifest),
// note: we cleanup first and then backup. this is done so that the app is not running should backup fail
// we cannot easily 'recover' from backup failures because we have to revert manfest and portBindings
@@ -746,7 +751,7 @@ function update(app, progressCallback, callback) {
stopApp.bind(null, app),
deleteContainers.bind(null, app, { managedOnly: true }),
function deleteImageIfChanged(done) {
if (app.manifest.dockerImage === app.updateConfig.manifest.dockerImage) return done();
if (app.manifest.dockerImage === updateConfig.manifest.dockerImage) return done();
docker.deleteImage(app.manifest, done);
},
@@ -757,8 +762,8 @@ function update(app, progressCallback, callback) {
// free unused ports
function (next) {
const currentPorts = app.portBindings || {};
const newTcpPorts = app.updateConfig.manifest.tcpPorts || {};
const newUdpPorts = app.updateConfig.manifest.udpPorts || {};
const newTcpPorts = updateConfig.manifest.tcpPorts || {};
const newUdpPorts = updateConfig.manifest.udpPorts || {};
async.each(Object.keys(currentPorts), function (portName, callback) {
if (newTcpPorts[portName] || newUdpPorts[portName]) return callback(); // port still in use
@@ -776,13 +781,13 @@ function update(app, progressCallback, callback) {
},
// switch over to the new config. manifest, memoryLimit, portBindings, appstoreId are updated here
updateApp.bind(null, app, app.updateConfig),
updateApp.bind(null, app, updateConfig),
progressCallback.bind(null, { percent: 45, message: 'Downloading icon' }),
downloadIcon.bind(null, app),
progressCallback.bind(null, { percent: 70, message: 'Updating addons' }),
addons.setupAddons.bind(null, app, app.updateConfig.manifest.addons),
addons.setupAddons.bind(null, app, updateConfig.manifest.addons),
progressCallback.bind(null, { percent: 80, message: 'Creating container' }),
createContainer.bind(null, app),
@@ -905,8 +910,9 @@ function handleRunCommand(app, progressCallback, callback) {
return callback(null);
}
function run(appId, progressCallback, callback) {
function run(appId, args, progressCallback, callback) {
assert.strictEqual(typeof appId, 'string');
assert.strictEqual(typeof args, 'object');
assert.strictEqual(typeof progressCallback, 'function');
assert.strictEqual(typeof callback, 'function');
@@ -917,13 +923,13 @@ function run(appId, progressCallback, callback) {
debugApp(app, 'startTask installationState: %s runState: %s', app.installationState, app.runState);
switch (app.installationState) {
case appdb.ISTATE_PENDING_INSTALL: return install(app, progressCallback, callback);
case appdb.ISTATE_PENDING_CONFIGURE: return configure(app, progressCallback, callback);
case appdb.ISTATE_PENDING_INSTALL: return install(app, args.restoreConfig || {}, progressCallback, callback);
case appdb.ISTATE_PENDING_CONFIGURE: return configure(app, args.oldConfig, progressCallback, callback);
case appdb.ISTATE_PENDING_UNINSTALL: return uninstall(app, progressCallback, callback);
case appdb.ISTATE_PENDING_CLONE: return install(app, progressCallback, callback);
case appdb.ISTATE_PENDING_RESTORE: return install(app, progressCallback, callback);
case appdb.ISTATE_PENDING_UPDATE: return update(app, progressCallback, callback);
case appdb.ISTATE_PENDING_FORCE_UPDATE: return update(app, progressCallback, callback);
case appdb.ISTATE_PENDING_CLONE: return install(app, args.restoreConfig || {}, progressCallback, callback);
case appdb.ISTATE_PENDING_RESTORE: return install(app, args.restoreConfig || {}, progressCallback, callback);
case appdb.ISTATE_PENDING_UPDATE: return update(app, args.updateConfig, progressCallback, callback);
case appdb.ISTATE_PENDING_FORCE_UPDATE: return update(app, args.updateConfig, progressCallback, callback);
case appdb.ISTATE_PENDING_BACKUP: return backup(app, progressCallback, callback);
case appdb.ISTATE_INSTALLED: return handleRunCommand(app, progressCallback, callback);