diff --git a/src/apps.js b/src/apps.js index 40b191d96..946adafe4 100644 --- a/src/apps.js +++ b/src/apps.js @@ -32,6 +32,9 @@ exports = module.exports = { autoupdateApps: autoupdateApps, + restoreInstalledApps: restoreInstalledApps, + configureInstalledApps: configureInstalledApps, + // exported for testing _validateHostname: validateHostname, _validatePortBindings: validatePortBindings, @@ -873,3 +876,35 @@ function listBackups(page, perPage, appId, callback) { }); }); } + +function restoreInstalledApps(callback) { + assert.strictEqual(typeof callback, 'function'); + + appdb.getAll(function (error, apps) { + if (error) return callback(new AppsError(AppsError.INTERNAL_ERROR, error)); + + async.map(apps, function (app, iteratorDone) { + if (app.installationState !== appdb.ISTATE_INSTALLED) return iteratorDone(); + + debug('marking %s for restore', app.location || app.id); + + appdb.setInstallationCommand(app.id, appdb.ISTATE_PENDING_RESTORE, { oldConfig: null }, iteratorDone); + }, callback); + }); +} + +function configureInstalledApps(callback) { + assert.strictEqual(typeof callback, 'function'); + + appdb.getAll(function (error, apps) { + if (error) return callback(new AppsError(AppsError.INTERNAL_ERROR, error)); + + async.map(apps, function (app, iteratorDone) { + if (app.installationState !== appdb.ISTATE_INSTALLED) return iteratorDone(); + + debug('marking %s for reconfigure', app.location || app.id); + + appdb.setInstallationCommand(app.id, appdb.ISTATE_PENDING_CONFIGURE, { oldConfig: null }, iteratorDone); + }, callback); + }); +} diff --git a/src/test/apps-test.js b/src/test/apps-test.js index cd3601425..fc3f1ea90 100644 --- a/src/test/apps-test.js +++ b/src/test/apps-test.js @@ -326,4 +326,57 @@ describe('Apps', function () { }); }); }); + + describe('configureInstalledApps', function () { + before(function (done) { + async.series([ + appdb.update.bind(null, APP_0.id, { installationState: appdb.ISTATE_INSTALLED }), + appdb.update.bind(null, APP_1.id, { installationState: appdb.ISTATE_ERROR }), + appdb.update.bind(null, APP_2.id, { installationState: appdb.ISTATE_INSTALLED }) + ], done); + }); + + it('can mark apps for reconfigure', function (done) { + apps.configureInstalledApps(function (error) { + expect(error).to.be(null); + + apps.getAll(function (error, apps) { + expect(apps[0].installationState).to.be(appdb.ISTATE_PENDING_CONFIGURE); + expect(apps[0].oldConfig).to.be(null); + expect(apps[1].installationState).to.be(appdb.ISTATE_ERROR); + expect(apps[2].installationState).to.be(appdb.ISTATE_PENDING_CONFIGURE); + expect(apps[2].oldConfig).to.be(null); + + done(); + }); + }); + }); + }); + + describe('restoreInstalledApps', function () { + before(function (done) { + async.series([ + appdb.update.bind(null, APP_0.id, { installationState: appdb.ISTATE_INSTALLED }), + appdb.update.bind(null, APP_1.id, { installationState: appdb.ISTATE_ERROR }), + appdb.update.bind(null, APP_2.id, { installationState: appdb.ISTATE_INSTALLED }) + ], done); + }); + + it('can mark apps for reconfigure', function (done) { + apps.restoreInstalledApps(function (error) { + expect(error).to.be(null); + + apps.getAll(function (error, apps) { + expect(apps[0].installationState).to.be(appdb.ISTATE_PENDING_RESTORE); + expect(apps[0].oldConfig).to.be(null); + expect(apps[1].installationState).to.be(appdb.ISTATE_ERROR); + expect(apps[2].installationState).to.be(appdb.ISTATE_PENDING_RESTORE); + expect(apps[2].oldConfig).to.be(null); + + done(); + }); + }); + }); + }); }); +