diff --git a/src/addons.js b/src/addons.js index 151eda66a..ea7a24df2 100644 --- a/src/addons.js +++ b/src/addons.js @@ -644,7 +644,7 @@ function importDatabase(addon, callback) { if (!error) return iteratorCallback(); debug(`importDatabase: Error importing ${addon} of app ${app.id}. Marking as errored`, error); - appdb.update(app.id, { installationState: appdb.ISTATE_ERROR, error: { message: error.message } }, iteratorCallback); + appdb.update(app.id, { installationState: apps.ISTATE_ERROR, error: { message: error.message } }, iteratorCallback); }); }, callback); }); diff --git a/src/appdb.js b/src/appdb.js index 00e0d5899..b841962e3 100644 --- a/src/appdb.js +++ b/src/appdb.js @@ -24,28 +24,6 @@ exports = module.exports = { setTask: setTask, getAppStoreIds: getAppStoreIds, - // installation codes (keep in sync in UI) - ISTATE_PENDING_INSTALL: 'pending_install', // installs and fresh reinstalls - ISTATE_PENDING_CLONE: 'pending_clone', // clone - ISTATE_PENDING_CONFIGURE: 'pending_configure', // config (location, port) changes and on infra update - ISTATE_PENDING_UNINSTALL: 'pending_uninstall', // uninstallation - ISTATE_PENDING_RESTORE: 'pending_restore', // restore to previous backup or on upgrade - ISTATE_PENDING_UPDATE: 'pending_update', // update from installed state preserving data - ISTATE_PENDING_BACKUP: 'pending_backup', // backup the app. this is state because it blocks other operations - ISTATE_ERROR: 'error', // error executing last pending_* command - ISTATE_INSTALLED: 'installed', // app is installed - - RSTATE_RUNNING: 'running', - RSTATE_PENDING_START: 'pending_start', - RSTATE_PENDING_STOP: 'pending_stop', - RSTATE_STOPPED: 'stopped', // app stopped by us - - // run codes (keep in sync in UI) - HEALTH_HEALTHY: 'healthy', - HEALTH_UNHEALTHY: 'unhealthy', - HEALTH_ERROR: 'error', - HEALTH_DEAD: 'dead', - // subdomain table types SUBDOMAIN_TYPE_PRIMARY: 'primary', SUBDOMAIN_TYPE_REDIRECT: 'redirect', @@ -263,7 +241,7 @@ function add(id, appStoreId, manifest, location, domain, portBindings, data, cal const accessRestriction = data.accessRestriction || null; const accessRestrictionJson = JSON.stringify(accessRestriction); const memoryLimit = data.memoryLimit || 0; - const installationState = data.installationState || exports.ISTATE_PENDING_INSTALL; + const installationState = data.installationState || 'pending_install'; // FIXME const sso = 'sso' in data ? data.sso : null; const robotsTxt = 'robotsTxt' in data ? data.robotsTxt : null; const debugModeJson = data.debugMode ? JSON.stringify(data.debugMode) : null; diff --git a/src/apphealthmonitor.js b/src/apphealthmonitor.js index dc3798939..5b0519812 100644 --- a/src/apphealthmonitor.js +++ b/src/apphealthmonitor.js @@ -36,16 +36,16 @@ function setHealth(app, health, callback) { let now = new Date(), healthTime = app.healthTime, curHealth = app.health; - if (health === appdb.HEALTH_HEALTHY) { + if (health === apps.HEALTH_HEALTHY) { healthTime = now; - if (curHealth && curHealth !== appdb.HEALTH_HEALTHY) { // app starts out with null health + if (curHealth && curHealth !== apps.HEALTH_HEALTHY) { // app starts out with null health debugApp(app, 'app switched from %s to healthy', curHealth); // do not send mails for dev apps if (!app.debugMode) eventlog.add(eventlog.ACTION_APP_UP, auditSource.HEALTH_MONITOR, { app: app }); } } else if (Math.abs(now - healthTime) > UNHEALTHY_THRESHOLD) { - if (curHealth === appdb.HEALTH_HEALTHY) { + if (curHealth === apps.HEALTH_HEALTHY) { debugApp(app, 'marking as unhealthy since not seen for more than %s minutes', UNHEALTHY_THRESHOLD/(60 * 1000)); // do not send mails for dev apps @@ -72,7 +72,7 @@ function checkAppHealth(app, callback) { assert.strictEqual(typeof app, 'object'); assert.strictEqual(typeof callback, 'function'); - if (app.installationState !== appdb.ISTATE_INSTALLED || app.runState !== appdb.RSTATE_RUNNING) { + if (app.installationState !== apps.ISTATE_INSTALLED || app.runState !== apps.RSTATE_RUNNING) { debugApp(app, 'skipped. istate:%s rstate:%s', app.installationState, app.runState); return callback(null); } @@ -82,16 +82,16 @@ function checkAppHealth(app, callback) { docker.inspect(app.containerId, function (error, data) { if (error || !data || !data.State) { debugApp(app, 'Error inspecting container'); - return setHealth(app, appdb.HEALTH_ERROR, callback); + return setHealth(app, apps.HEALTH_ERROR, callback); } if (data.State.Running !== true) { debugApp(app, 'exited'); - return setHealth(app, appdb.HEALTH_DEAD, callback); + return setHealth(app, apps.HEALTH_DEAD, callback); } // non-appstore apps may not have healthCheckPath - if (!manifest.healthCheckPath) return setHealth(app, appdb.HEALTH_HEALTHY, callback); + if (!manifest.healthCheckPath) return setHealth(app, apps.HEALTH_HEALTHY, callback); // poll through docker network instead of nginx to bypass any potential oauth proxy var healthCheckUrl = 'http://127.0.0.1:' + app.httpPort + manifest.healthCheckPath; @@ -104,12 +104,12 @@ function checkAppHealth(app, callback) { .end(function (error, res) { if (error && !error.response) { debugApp(app, 'not alive (network error): %s', error.message); - setHealth(app, appdb.HEALTH_UNHEALTHY, callback); + setHealth(app, apps.HEALTH_UNHEALTHY, callback); } else if (res.statusCode >= 400) { // 2xx and 3xx are ok debugApp(app, 'not alive : %s', error || res.status); - setHealth(app, appdb.HEALTH_UNHEALTHY, callback); + setHealth(app, apps.HEALTH_UNHEALTHY, callback); } else { - setHealth(app, appdb.HEALTH_HEALTHY, callback); + setHealth(app, apps.HEALTH_HEALTHY, callback); } }); }); @@ -187,7 +187,7 @@ function processApp(callback) { if (error) console.error(error); var alive = result - .filter(function (a) { return a.installationState === appdb.ISTATE_INSTALLED && a.runState === appdb.RSTATE_RUNNING && a.health === appdb.HEALTH_HEALTHY; }) + .filter(function (a) { return a.installationState === apps.ISTATE_INSTALLED && a.runState === apps.RSTATE_RUNNING && a.health === apps.HEALTH_HEALTHY; }) .map(function (a) { return (a.location || 'naked_domain') + '|' + a.manifest.id; }).join(', '); debug('apps alive: [%s]', alive); diff --git a/src/apps.js b/src/apps.js index f14af0127..7b741cc56 100644 --- a/src/apps.js +++ b/src/apps.js @@ -53,6 +53,29 @@ exports = module.exports = { ETASK_STOPPED: 'task_stopped', // user stopped a task ETASK_CRASHED: 'task_crashed', // apptask crashed + // installation codes (keep in sync in UI) + ISTATE_PENDING_INSTALL: 'pending_install', // installs and fresh reinstalls + ISTATE_PENDING_CLONE: 'pending_clone', // clone + ISTATE_PENDING_CONFIGURE: 'pending_configure', // config (location, port) changes and on infra update + ISTATE_PENDING_UNINSTALL: 'pending_uninstall', // uninstallation + ISTATE_PENDING_RESTORE: 'pending_restore', // restore to previous backup or on upgrade + ISTATE_PENDING_UPDATE: 'pending_update', // update from installed state preserving data + ISTATE_PENDING_BACKUP: 'pending_backup', // backup the app. this is state because it blocks other operations + ISTATE_ERROR: 'error', // error executing last pending_* command + ISTATE_INSTALLED: 'installed', // app is installed + + // run states + RSTATE_RUNNING: 'running', + RSTATE_PENDING_START: 'pending_start', + RSTATE_PENDING_STOP: 'pending_stop', + RSTATE_STOPPED: 'stopped', // app stopped by us + + // health states (keep in sync in UI) + HEALTH_HEALTHY: 'healthy', + HEALTH_UNHEALTHY: 'unhealthy', + HEALTH_ERROR: 'error', + HEALTH_DEAD: 'dead', + // exported for testing _validatePortBindings: validatePortBindings, _validateAccessRestriction: validateAccessRestriction, @@ -604,8 +627,8 @@ function scheduleTask(appId, args, values, callback) { if (error && (error.crashed || error.stopped)) { debug(`Apptask crashed/stopped: ${error.message}`); const code = error.crashed ? exports.ETASK_CRASHED : exports.ETASK_STOPPED; - appdb.update(appId, { installationState: appdb.ISTATE_ERROR, error: { code: code, message: error.message }, taskId: null }, NOOP_CALLBACK); - } else if (values.installationState !== appdb.ISTATE_PENDING_UNINSTALL) { + appdb.update(appId, { installationState: exports.ISTATE_ERROR, error: { code: code, message: error.message }, taskId: null }, NOOP_CALLBACK); + } else if (values.installationState !== exports.ISTATE_PENDING_UNINSTALL) { appdb.update(appId, { taskId: null }, NOOP_CALLBACK); } }); @@ -744,7 +767,7 @@ function install(data, user, auditSource, callback) { const restoreConfig = backupId ? { backupId: backupId, backupFormat: backupFormat } : null; - scheduleTask(appId, { restoreConfig }, { installationState: appdb.ISTATE_PENDING_INSTALL }, function (error, result) { + scheduleTask(appId, { restoreConfig }, { installationState: exports.ISTATE_PENDING_INSTALL }, function (error, result) { if (error) return callback(error); eventlog.add(eventlog.ACTION_APP_INSTALL, auditSource, { appId: appId, app: result }); @@ -768,7 +791,7 @@ function configure(appId, data, user, auditSource, callback) { if (error) return callback(error); if (app.taskId) return callback(new AppsError(AppsError.BAD_STATE, `Not allowed in this app state : ${app.installationState} / ${app.runState}`)); - let domain, location, portBindings, values = { installationState: appdb.ISTATE_PENDING_CONFIGURE }; + let domain, location, portBindings, values = { installationState: exports.ISTATE_PENDING_CONFIGURE }; if ('location' in data && 'domain' in data) { location = values.location = data.location.toLowerCase(); @@ -963,7 +986,7 @@ function update(appId, data, auditSource, callback) { updateConfig.memoryLimit = updateConfig.manifest.memoryLimit; } - scheduleTask(appId, { updateConfig: updateConfig }, { installationState: appdb.ISTATE_PENDING_UPDATE }, function (error, result) { + scheduleTask(appId, { updateConfig: updateConfig }, { installationState: exports.ISTATE_PENDING_UPDATE }, function (error, result) { if (error) return callback(error); eventlog.add(eventlog.ACTION_APP_UPDATE, auditSource, { appId: appId, toManifest: manifest, fromManifest: app.manifest, force: data.force, app: app }); @@ -1058,7 +1081,7 @@ function restore(appId, data, auditSource, callback) { if (error) return callback(error); let values = { - installationState: appdb.ISTATE_PENDING_RESTORE, + installationState: exports.ISTATE_PENDING_RESTORE, manifest: backupInfo.manifest }; @@ -1153,7 +1176,7 @@ function clone(appId, data, user, auditSource, callback) { var newAppId = uuid.v4(); var data = { - installationState: appdb.ISTATE_PENDING_CLONE, + installationState: exports.ISTATE_PENDING_CLONE, memoryLimit: app.memoryLimit, accessRestriction: app.accessRestriction, sso: !!app.sso, @@ -1172,7 +1195,7 @@ function clone(appId, data, user, auditSource, callback) { const restoreConfig = { backupId: backupId, backupFormat: backupInfo.format }; - scheduleTask(newAppId, { restoreConfig }, { installationState: appdb.ISTATE_PENDING_CLONE }, function (error, result) { + scheduleTask(newAppId, { restoreConfig }, { installationState: exports.ISTATE_PENDING_CLONE }, function (error, result) { if (error) return callback(error); eventlog.add(eventlog.ACTION_APP_CLONE, auditSource, { appId: newAppId, oldAppId: appId, backupId: backupId, oldApp: app, newApp: result }); @@ -1204,7 +1227,7 @@ function uninstall(appId, auditSource, callback) { if (error && error.reason === AppstoreError.EXTERNAL_ERROR) return callback(new AppsError(AppsError.EXTERNAL_ERROR, error.message)); if (error) return callback(new AppsError(AppsError.INTERNAL_ERROR, error)); - scheduleTask(appId, { /* args */ }, { installationState: appdb.ISTATE_PENDING_UNINSTALL }, function (error, result) { + scheduleTask(appId, { /* args */ }, { installationState: exports.ISTATE_PENDING_UNINSTALL }, function (error, result) { if (error) return callback(error); eventlog.add(eventlog.ACTION_APP_UNINSTALL, auditSource, { appId: appId, app: result }); @@ -1225,7 +1248,7 @@ function start(appId, callback) { if (error) return callback(error); if (app.taskId) return callback(new AppsError(AppsError.BAD_STATE, `Not allowed in this app state : ${app.installationState} / ${app.runState}`)); - scheduleTask(appId, { /* args */ }, { runState: appdb.RSTATE_PENDING_START }, callback); + scheduleTask(appId, { /* args */ }, { runState: exports.RSTATE_PENDING_START }, callback); }); } @@ -1239,7 +1262,7 @@ function stop(appId, callback) { if (error) return callback(error); if (app.taskId) return callback(new AppsError(AppsError.BAD_STATE, `Not allowed in this app state : ${app.installationState} / ${app.runState}`)); - scheduleTask(appId, { /* args */ }, { runState: appdb.RSTATE_PENDING_STOP }, callback); + scheduleTask(appId, { /* args */ }, { runState: exports.RSTATE_PENDING_STOP }, callback); }); } @@ -1272,7 +1295,7 @@ function exec(appId, options, callback) { get(appId, function (error, app) { if (error) return callback(error); - if (app.installationState !== appdb.ISTATE_INSTALLED || app.runState !== appdb.RSTATE_RUNNING) { + if (app.installationState !== exports.ISTATE_INSTALLED || app.runState !== exports.RSTATE_RUNNING) { return callback(new AppsError(AppsError.BAD_STATE, 'App not installed or running')); } @@ -1383,7 +1406,7 @@ function backup(appId, callback) { if (error) return callback(error); if (app.taskId) return callback(new AppsError(AppsError.BAD_STATE, `Not allowed in this app state : ${app.installationState} / ${app.runState}`)); - scheduleTask(appId, { /* args */ }, { installationState: appdb.ISTATE_PENDING_BACKUP }, (error, result) => { + scheduleTask(appId, { /* args */ }, { installationState: exports.ISTATE_PENDING_BACKUP }, (error, result) => { if (error) return callback(error); callback(null, { taskId: result.taskId }); @@ -1425,7 +1448,7 @@ function restoreInstalledApps(callback) { appdb.update(app.id, { taskId: null }, function (error) { // clear any stale taskId if (error) debug(`Error marking ${app.fqdn} for restore: ${JSON.stringify(error)}`); - scheduleTask(app.id, { restoreConfig }, { installationState: appdb.ISTATE_PENDING_RESTORE }, () => iteratorDone()); // always succeed + scheduleTask(app.id, { restoreConfig }, { installationState: exports.ISTATE_PENDING_RESTORE }, () => iteratorDone()); // always succeed }); }); }, callback); @@ -1444,7 +1467,7 @@ function configureInstalledApps(callback) { appdb.update(app.id, { taskId: null }, function (error) { // clear any stale taskId if (error) debug(`Error marking ${app.fqdn} for reconfigure: ${JSON.stringify(error)}`); - scheduleTask(app.id, { oldConfig: getAppConfig(app) }, { installationState: appdb.ISTATE_PENDING_CONFIGURE }, () => iteratorDone()); // always succeed + scheduleTask(app.id, { oldConfig: getAppConfig(app) }, { installationState: exports.ISTATE_PENDING_CONFIGURE }, () => iteratorDone()); // always succeed }); }, callback); }); diff --git a/src/apptask.js b/src/apptask.js index 7ac378e6d..cef033d3e 100644 --- a/src/apptask.js +++ b/src/apptask.js @@ -505,7 +505,7 @@ function install(app, restoreConfig, progressCallback, callback) { assert.strictEqual(typeof progressCallback, 'function'); assert.strictEqual(typeof callback, 'function'); - const isInstalling = app.installationState !== appdb.ISTATE_PENDING_RESTORE; // install or clone + const isInstalling = app.installationState !== apps.ISTATE_PENDING_RESTORE; // install or clone async.series([ // this protects against the theoretical possibility of an app being marked for install/restore from @@ -589,11 +589,11 @@ function install(app, restoreConfig, progressCallback, callback) { configureReverseProxy.bind(null, app), progressCallback.bind(null, { percent: 100, message: 'Done' }), - updateApp.bind(null, app, { installationState: appdb.ISTATE_INSTALLED, health: null }) + updateApp.bind(null, app, { installationState: apps.ISTATE_INSTALLED, health: null }) ], function seriesDone(error) { if (error) { debugApp(app, 'error installing app: %s', error); - return updateApp(app, { installationState: appdb.ISTATE_ERROR, error: { message: error.message } }, callback.bind(null, error)); + return updateApp(app, { installationState: apps.ISTATE_ERROR, error: { message: error.message } }, callback.bind(null, error)); } callback(null); }); @@ -611,11 +611,11 @@ function backup(app, progressCallback, callback) { }), progressCallback.bind(null, { percent: 100, message: 'Done' }), - updateApp.bind(null, app, { installationState: appdb.ISTATE_INSTALLED, error: null }) + updateApp.bind(null, app, { installationState: apps.ISTATE_INSTALLED, error: null }) ], function seriesDone(error) { if (error) { debugApp(app, 'error backing up app: %s', error); - return updateApp(app, { installationState: appdb.ISTATE_INSTALLED, error: { message: error.message } }, callback.bind(null, error)); // return to installed state intentionally + return updateApp(app, { installationState: apps.ISTATE_INSTALLED, error: { message: error.message } }, callback.bind(null, error)); // return to installed state intentionally } callback(null); }); @@ -692,11 +692,11 @@ function configure(app, oldConfig, progressCallback, callback) { configureReverseProxy.bind(null, app), progressCallback.bind(null, { percent: 100, message: 'Done' }), - updateApp.bind(null, app, { installationState: appdb.ISTATE_INSTALLED, error: null, health: null }) + updateApp.bind(null, app, { installationState: apps.ISTATE_INSTALLED, error: null, health: null }) ], function seriesDone(error) { if (error) { debugApp(app, 'error reconfiguring : %s', error); - return updateApp(app, { installationState: appdb.ISTATE_ERROR, error: { message: error.message }}, callback.bind(null, error)); + return updateApp(app, { installationState: apps.ISTATE_ERROR, error: { message: error.message }}, callback.bind(null, error)); } callback(null); }); @@ -798,14 +798,14 @@ function update(app, updateConfig, progressCallback, callback) { runApp.bind(null, app, progressCallback), progressCallback.bind(null, { percent: 100, message: 'Done' }), - updateApp.bind(null, app, { installationState: appdb.ISTATE_INSTALLED, error: null, health: null, updateTime: new Date() }) + updateApp.bind(null, app, { installationState: apps.ISTATE_INSTALLED, error: null, health: null, updateTime: new Date() }) ], function seriesDone(error) { if (error && error.backupError) { debugApp(app, 'update aborted because backup failed', error); - updateApp(app, { installationState: appdb.ISTATE_INSTALLED, error: null, health: null }, callback.bind(null, error)); + updateApp(app, { installationState: apps.ISTATE_INSTALLED, error: null, health: null }, callback.bind(null, error)); } else if (error) { debugApp(app, 'Error updating app: %s', error); - updateApp(app, { installationState: appdb.ISTATE_ERROR, error: { message: error.message }, updateTime: new Date() }, callback.bind(null, error)); + updateApp(app, { installationState: apps.ISTATE_ERROR, error: { message: error.message }, updateTime: new Date() }, callback.bind(null, error)); } else { if (updateConfig.skipNotification) return callback(); @@ -859,7 +859,7 @@ function uninstall(app, progressCallback, callback) { ], function seriesDone(error) { if (error) { debugApp(app, 'error uninstalling app: %s', error); - return updateApp(app, { installationState: appdb.ISTATE_ERROR, error: { message: error.message } }, callback.bind(null, error)); + return updateApp(app, { installationState: apps.ISTATE_ERROR, error: { message: error.message } }, callback.bind(null, error)); } callback(null); }); @@ -875,7 +875,7 @@ function runApp(app, progressCallback, callback) { docker.startContainer(app.containerId, function (error) { if (error) return callback(error); - updateApp(app, { runState: appdb.RSTATE_RUNNING }, callback); + updateApp(app, { runState: apps.RSTATE_RUNNING }, callback); }); } @@ -889,7 +889,7 @@ function stopApp(app, progressCallback, callback) { docker.stopContainers(app.id, function (error) { if (error) return callback(error); - updateApp(app, { runState: appdb.RSTATE_STOPPED, health: null }, callback); + updateApp(app, { runState: apps.RSTATE_STOPPED, health: null }, callback); }); } @@ -906,17 +906,17 @@ function run(appId, args, progressCallback, callback) { debugApp(app, 'startTask installationState: %s runState: %s', app.installationState, app.runState); switch (app.installationState) { - 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, 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_BACKUP: return backup(app, progressCallback, callback); - case appdb.ISTATE_INSTALLED: + case apps.ISTATE_PENDING_INSTALL: return install(app, args.restoreConfig || {}, progressCallback, callback); + case apps.ISTATE_PENDING_CONFIGURE: return configure(app, args.oldConfig, progressCallback, callback); + case apps.ISTATE_PENDING_UNINSTALL: return uninstall(app, progressCallback, callback); + case apps.ISTATE_PENDING_CLONE: return install(app, args.restoreConfig || {}, progressCallback, callback); + case apps.ISTATE_PENDING_RESTORE: return install(app, args.restoreConfig || {}, progressCallback, callback); + case apps.ISTATE_PENDING_UPDATE: return update(app, args.updateConfig, progressCallback, callback); + case apps.ISTATE_PENDING_BACKUP: return backup(app, progressCallback, callback); + case apps.ISTATE_INSTALLED: switch (app.runState) { - case appdb.RSTATE_PENDING_STOP: return stopApp(app, progressCallback, callback); - case appdb.RSTATE_PENDING_START: return runApp(app, progressCallback, callback); + case apps.RSTATE_PENDING_STOP: return stopApp(app, progressCallback, callback); + case apps.RSTATE_PENDING_START: return runApp(app, progressCallback, callback); default: return callback(new Error('Unknown run command in apptask:' + app.runState)); } diff --git a/src/apptaskmanager.js b/src/apptaskmanager.js index e8d350703..4e9c11765 100644 --- a/src/apptaskmanager.js +++ b/src/apptaskmanager.js @@ -5,8 +5,7 @@ exports = module.exports = { scheduleTask: scheduleTask }; -var appdb = require('./appdb.js'), - apps = require('./apps.js'), +let apps = require('./apps.js'), assert = require('assert'), debug = require('debug')('box:taskmanager'), fs = require('fs'), @@ -91,8 +90,8 @@ function resumeTasks(callback) { if (error) return callback(error); result.forEach(function (app) { - if (app.installationState === appdb.ISTATE_INSTALLED && app.runState === appdb.RSTATE_RUNNING) return; - if (app.installationState === appdb.ISTATE_ERROR) return; + if (app.installationState === apps.ISTATE_INSTALLED && app.runState === apps.RSTATE_RUNNING) return; + if (app.installationState === apps.ISTATE_ERROR) return; debug(`resumeTask: starting app task for ${app.fqdn} ${app.id} and state ${app.installationState}`); diff --git a/src/backups.js b/src/backups.js index a2abd5c73..b47ed0cfa 100644 --- a/src/backups.js +++ b/src/backups.js @@ -40,7 +40,6 @@ exports = module.exports = { }; var addons = require('./addons.js'), - appdb = require('./appdb.js'), apps = require('./apps.js'), AppsError = require('./apps.js').AppsError, async = require('async'), @@ -847,10 +846,10 @@ function backupBoxWithAppBackupIds(appBackupIds, tag, progressCallback, callback function canBackupApp(app) { // only backup apps that are installed or pending configure or called from apptask. Rest of them are in some // state not good for consistent backup (i.e addons may not have been setup completely) - return (app.installationState === appdb.ISTATE_INSTALLED && app.health === appdb.HEALTH_HEALTHY) || - app.installationState === appdb.ISTATE_PENDING_CONFIGURE || - app.installationState === appdb.ISTATE_PENDING_BACKUP || // called from apptask - app.installationState === appdb.ISTATE_PENDING_UPDATE; // called from apptask + return (app.installationState === apps.ISTATE_INSTALLED && app.health === apps.HEALTH_HEALTHY) || + app.installationState === apps.ISTATE_PENDING_CONFIGURE || + app.installationState === apps.ISTATE_PENDING_BACKUP || // called from apptask + app.installationState === apps.ISTATE_PENDING_UPDATE; // called from apptask } function snapshotApp(app, progressCallback, callback) { diff --git a/src/dyndns.js b/src/dyndns.js index 5be245b07..c3c8a5e8c 100644 --- a/src/dyndns.js +++ b/src/dyndns.js @@ -4,8 +4,7 @@ exports = module.exports = { sync: sync }; -var appdb = require('./appdb.js'), - apps = require('./apps.js'), +let apps = require('./apps.js'), assert = require('assert'), async = require('async'), constants = require('./constants.js'), @@ -43,7 +42,7 @@ function sync(auditSource, callback) { async.each(result, function (app, callback) { // do not change state of installing apps since apptask will error if dns record already exists - if (app.installationState !== appdb.ISTATE_INSTALLED) return callback(); + if (app.installationState !== apps.ISTATE_INSTALLED) return callback(); domains.upsertDnsRecords(app.location, app.domain, 'A', [ ip ], callback); }, function (error) { diff --git a/src/routes/test/apps-test.js b/src/routes/test/apps-test.js index 3ce38b8fb..2ede21894 100644 --- a/src/routes/test/apps-test.js +++ b/src/routes/test/apps-test.js @@ -6,8 +6,7 @@ /* global after:false */ /* global xit:false */ -var appdb = require('../../appdb.js'), - apps = require('../../apps.js'), +let apps = require('../../apps.js'), assert = require('assert'), async = require('async'), child_process = require('child_process'), @@ -612,8 +611,8 @@ describe('App installation', function () { .end(function (err, res) { expect(res.statusCode).to.equal(200); - if (res.body.installationState === appdb.ISTATE_INSTALLED) { appResult = res.body; return done(null); } - if (res.body.installationState === appdb.ISTATE_ERROR) return done(new Error('Install error')); + if (res.body.installationState === apps.ISTATE_INSTALLED) { appResult = res.body; return done(null); } + if (res.body.installationState === apps.ISTATE_ERROR) return done(new Error('Install error')); if (++count > 500) return done(new Error('Timedout')); setTimeout(checkInstallStatus, 1000); @@ -902,8 +901,8 @@ describe('App installation', function () { .query({ access_token: token }) .end(function (err, res) { expect(res.statusCode).to.equal(200); - if (res.body.installationState === appdb.ISTATE_INSTALLED) { appResult = res.body; expect(appResult).to.be.ok(); return done(null); } - if (res.body.installationState === appdb.ISTATE_ERROR) return done(new Error('Install error')); + if (res.body.installationState === apps.ISTATE_INSTALLED) { appResult = res.body; expect(appResult).to.be.ok(); return done(null); } + if (res.body.installationState === apps.ISTATE_ERROR) return done(new Error('Install error')); if (++count > 50) return done(new Error('Timedout')); setTimeout(checkConfigureStatus.bind(null, count, done), 1000); }); diff --git a/src/scheduler.js b/src/scheduler.js index 5efd25de1..48f49f40a 100644 --- a/src/scheduler.js +++ b/src/scheduler.js @@ -4,8 +4,7 @@ exports = module.exports = { sync: sync }; -var appdb = require('./appdb.js'), - apps = require('./apps.js'), +let apps = require('./apps.js'), assert = require('assert'), async = require('async'), constants = require('./constants.js'), @@ -149,7 +148,7 @@ function runTask(appId, taskName, callback) { apps.get(appId, function (error, app) { if (error) return callback(error); - if (app.installationState !== appdb.ISTATE_INSTALLED || app.runState !== appdb.RSTATE_RUNNING || app.health !== appdb.HEALTH_HEALTHY) { + if (app.installationState !== apps.ISTATE_INSTALLED || app.runState !== apps.RSTATE_RUNNING || app.health !== apps.HEALTH_HEALTHY) { debug(`runTask: skipped task ${taskName} because app ${app.fqdn} has run state ${app.installationState}`); return callback(); } diff --git a/src/test/apps-test.js b/src/test/apps-test.js index 93d3e6635..c929aa970 100644 --- a/src/test/apps-test.js +++ b/src/test/apps-test.js @@ -1,5 +1,6 @@ /* global it:false */ /* global describe:false */ +/* global xdescribe:false */ /* global before:false */ /* global after:false */ @@ -366,9 +367,9 @@ describe('Apps', function () { xdescribe('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 }) + appdb.update.bind(null, APP_0.id, { installationState: apps.ISTATE_INSTALLED }), + appdb.update.bind(null, APP_1.id, { installationState: apps.ISTATE_ERROR }), + appdb.update.bind(null, APP_2.id, { installationState: apps.ISTATE_INSTALLED }) ], done); }); @@ -377,9 +378,9 @@ describe('Apps', function () { expect(error).to.be(null); apps.getAll(function (error, apps) { - expect(apps[0].installationState).to.be(appdb.ISTATE_PENDING_CONFIGURE); - expect(apps[1].installationState).to.be(appdb.ISTATE_PENDING_CONFIGURE); // erorred app can be reconfigured after restore - expect(apps[2].installationState).to.be(appdb.ISTATE_PENDING_CONFIGURE); + expect(apps[0].installationState).to.be(apps.ISTATE_PENDING_CONFIGURE); + expect(apps[1].installationState).to.be(apps.ISTATE_PENDING_CONFIGURE); // erorred app can be reconfigured after restore + expect(apps[2].installationState).to.be(apps.ISTATE_PENDING_CONFIGURE); done(); }); @@ -390,9 +391,9 @@ describe('Apps', function () { xdescribe('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 }) + appdb.update.bind(null, APP_0.id, { installationState: apps.ISTATE_INSTALLED }), + appdb.update.bind(null, APP_1.id, { installationState: apps.ISTATE_ERROR }), + appdb.update.bind(null, APP_2.id, { installationState: apps.ISTATE_INSTALLED }) ], done); }); @@ -401,9 +402,9 @@ describe('Apps', function () { expect(error).to.be(null); apps.getAll(function (error, result) { - expect(result[0].installationState).to.be(appdb.ISTATE_PENDING_RESTORE); - expect(result[1].installationState).to.be(appdb.ISTATE_PENDING_RESTORE); - expect(result[2].installationState).to.be(appdb.ISTATE_PENDING_RESTORE); + expect(result[0].installationState).to.be(apps.ISTATE_PENDING_RESTORE); + expect(result[1].installationState).to.be(apps.ISTATE_PENDING_RESTORE); + expect(result[2].installationState).to.be(apps.ISTATE_PENDING_RESTORE); done(); }); diff --git a/src/test/apptask-test.js b/src/test/apptask-test.js index 15ef3d54b..91daeb6c5 100644 --- a/src/test/apptask-test.js +++ b/src/test/apptask-test.js @@ -7,6 +7,7 @@ var addons = require('../addons.js'), appdb = require('../appdb.js'), + apps = require('../apps.js'), apptask = require('../apptask.js'), async = require('async'), database = require('../database.js'), @@ -81,7 +82,7 @@ var ADMIN = { var APP = { id: 'appid', appStoreId: 'appStoreId', - installationState: appdb.ISTATE_PENDING_INSTALL, + installationState: apps.ISTATE_PENDING_INSTALL, runState: null, location: 'applocation', domain: DOMAIN_0.domain, diff --git a/src/test/database-test.js b/src/test/database-test.js index 6df264890..1922b5897 100644 --- a/src/test/database-test.js +++ b/src/test/database-test.js @@ -6,6 +6,7 @@ 'use strict'; var appdb = require('../appdb.js'), + apps = require('../apps.js'), async = require('async'), authcodedb = require('../authcodedb.js'), backupdb = require('../backupdb.js'), @@ -391,7 +392,7 @@ describe('database', function () { var APP_0 = { id: 'appid-0', appStoreId: 'appStoreId-0', - installationState: appdb.ISTATE_PENDING_INSTALL, + installationState: apps.ISTATE_PENDING_INSTALL, error: null, runState: null, location: 'some-location-0', @@ -969,7 +970,7 @@ describe('database', function () { var APP_0 = { id: 'appid-0', appStoreId: 'appStoreId-0', - installationState: appdb.ISTATE_PENDING_INSTALL, + installationState: apps.ISTATE_PENDING_INSTALL, error: null, runState: null, location: 'some-location-0', @@ -1000,7 +1001,7 @@ describe('database', function () { var APP_1 = { id: 'appid-1', appStoreId: 'appStoreId-1', - installationState: appdb.ISTATE_PENDING_INSTALL, // app health tests rely on this initial state + installationState: apps.ISTATE_PENDING_INSTALL, // app health tests rely on this initial state error: null, runState: null, location: 'some-location-1', @@ -1200,52 +1201,15 @@ describe('database', function () { }); }); - it('cannot set app as healthy because app is not installed', function (done) { - appdb.setHealth(APP_1.id, appdb.HEALTH_HEALTHY, new Date(), function (error) { - expect(error).to.be.ok(); + it('can set app as healthy', function (done) { + appdb.setHealth(APP_1.id, apps.HEALTH_HEALTHY, new Date(), function (error) { + expect(error).to.be(null); done(); }); }); - it('cannot set app as healthy because app has pending run state', function (done) { - appdb.update(APP_1.id, { runState: appdb.RSTATE_PENDING_STOP, installationState: appdb.ISTATE_INSTALLED }, function (error) { - expect(error).to.be(null); - - appdb.setHealth(APP_1.id, appdb.HEALTH_HEALTHY, new Date(), function (error) { - expect(error).to.be.ok(); - done(); - }); - }); - }); - - it('cannot set app as healthy because app has null run state', function (done) { - appdb.update(APP_1.id, { runState: null, installationState: appdb.ISTATE_INSTALLED }, function (error) { - expect(error).to.be(null); - - appdb.setHealth(APP_1.id, appdb.HEALTH_HEALTHY, new Date(), function (error) { - expect(error).to.be.ok(); - done(); - }); - }); - }); - - it('can set app as healthy when installed and no pending runState', function (done) { - appdb.update(APP_1.id, { runState: appdb.RSTATE_RUNNING, installationState: appdb.ISTATE_INSTALLED }, function (error) { - expect(error).to.be(null); - - appdb.setHealth(APP_1.id, appdb.HEALTH_HEALTHY, new Date(), function (error) { - expect(error).to.be(null); - appdb.get(APP_1.id, function (error, app) { - expect(error).to.be(null); - expect(app.health).to.be(appdb.HEALTH_HEALTHY); - done(); - }); - }); - }); - }); - it('cannot set health of unknown app', function (done) { - appdb.setHealth('randomId', appdb.HEALTH_HEALTHY, new Date(), function (error) { + appdb.setHealth('randomId', apps.HEALTH_HEALTHY, new Date(), function (error) { expect(error).to.be.ok(); done(); }); diff --git a/src/test/ldap-test.js b/src/test/ldap-test.js index 064e8e673..eae345330 100644 --- a/src/test/ldap-test.js +++ b/src/test/ldap-test.js @@ -63,9 +63,9 @@ var AUDIT_SOURCE = { var APP_0 = { id: 'appid-0', appStoreId: 'appStoreId-0', - installationState: appdb.ISTATE_INSTALLED, + installationState: apps.ISTATE_INSTALLED, error: null, - runState: appdb.RSTATE_RUNNING, + runState: apps.RSTATE_RUNNING, location: 'some-location-0', domain: DOMAIN_0.domain, manifest: { version: '0.1', dockerImage: 'docker/app0', healthCheckPath: '/', httpPort: 80, title: 'app0' }, diff --git a/src/test/updatechecker-test.js b/src/test/updatechecker-test.js index fe5fa291e..a4f1b7831 100644 --- a/src/test/updatechecker-test.js +++ b/src/test/updatechecker-test.js @@ -216,7 +216,7 @@ describe('updatechecker - app - manual (email)', function () { var APP_0 = { id: 'appid-0', appStoreId: 'io.cloudron.app', - installationState: appdb.ISTATE_PENDING_INSTALL, + installationState: apps.ISTATE_PENDING_INSTALL, error: null, runState: null, location: 'some-location-0', @@ -323,7 +323,7 @@ describe('updatechecker - app - automatic (no email)', function () { var APP_0 = { id: 'appid-0', appStoreId: 'io.cloudron.app', - installationState: appdb.ISTATE_PENDING_INSTALL, + installationState: apps.ISTATE_PENDING_INSTALL, error: null, runState: null, location: 'some-location-0', @@ -386,7 +386,7 @@ describe('updatechecker - app - automatic free (email)', function () { var APP_0 = { id: 'appid-0', appStoreId: 'io.cloudron.app', - installationState: appdb.ISTATE_PENDING_INSTALL, + installationState: apps.ISTATE_PENDING_INSTALL, error: null, runState: null, location: 'some-location-0',