diff --git a/src/paths.js b/src/paths.js index 0df61a191..c592f5a3a 100644 --- a/src/paths.js +++ b/src/paths.js @@ -32,7 +32,6 @@ exports = module.exports = { APP_CERTS_DIR: path.join(config.baseDir(), 'boxdata/certs'), CLOUDRON_AVATAR_FILE: path.join(config.baseDir(), 'boxdata/avatar.png'), UPDATE_CHECKER_FILE: path.join(config.baseDir(), 'boxdata/updatechecker.json'), - PLATFORM_CONFIG_FILE: path.join(config.baseDir(), 'boxdata/platform.json'), AUTO_PROVISION_FILE: path.join(config.baseDir(), 'configs/autoprovision.json') }; diff --git a/src/platform.js b/src/platform.js index 0e5d21c63..a4e45fd08 100644 --- a/src/platform.js +++ b/src/platform.js @@ -22,6 +22,7 @@ var apps = require('./apps.js'), reverseProxy = require('./reverseproxy.js'), safe = require('safetydance'), semver = require('semver'), + settings = require('./settings.js'), shell = require('./shell.js'), taskmanager = require('./taskmanager.js'), util = require('util'), @@ -47,9 +48,15 @@ function start(callback) { // short-circuit for the restart case if (_.isEqual(infra, existingInfra)) { debug('platform is uptodate at version %s', infra.version); - updateAddons(); - emitPlatformReady(); - return callback(); + + updateAddons(function (error) { + if (error) return callback(error); + + emitPlatformReady(); + + callback(); + }); + return; } debug('Updating infrastructure from %s to %s', existingInfra.version, infra.version); @@ -62,13 +69,13 @@ function start(callback) { startAddons.bind(null, existingInfra), removeOldImages, startApps.bind(null, existingInfra), - fs.writeFile.bind(fs, paths.INFRA_VERSION_FILE, JSON.stringify(infra, null, 4)) + fs.writeFile.bind(fs, paths.INFRA_VERSION_FILE, JSON.stringify(infra, null, 4)), + updateAddons ], function (error) { if (error) return callback(error); locker.unlock(locker.OP_PLATFORM_START); - updateAddons(); emitPlatformReady(); callback(); @@ -82,19 +89,20 @@ function stop(callback) { taskmanager.pauseTasks(callback); } -function updateAddons() { - var platformConfig = safe.JSON.parse(safe.fs.readFileSync(paths.PLATFORM_CONFIG_FILE, 'utf8')); - if (!platformConfig) platformConfig = { }; +function updateAddons(callback) { + settings.getPlatformConfig(function (error, platformConfig) { + if (error) return callback(error); - for (var containerName of [ 'mysql', 'postgresql', 'mail', 'mongodb' ]) { - const containerConfig = platformConfig[containerName]; - if (!containerConfig) continue; + for (var containerName of [ 'mysql', 'postgresql', 'mail', 'mongodb' ]) { + const containerConfig = platformConfig[containerName]; + if (!containerConfig) continue; - if (!containerConfig.memory || !containerConfig.memorySwap) continue; + if (!containerConfig.memory || !containerConfig.memorySwap) continue; - const cmd = `docker update --memory ${containerConfig.memory} --memory-swap ${containerConfig.memorySwap} ${containerName}`; - shell.execSync(`update${containerName}`, cmd); - } + const cmd = `docker update --memory ${containerConfig.memory} --memory-swap ${containerConfig.memorySwap} ${containerName}`; + shell.execSync(`update${containerName}`, cmd); + } + }); } function emitPlatformReady() { diff --git a/src/settings.js b/src/settings.js index 2a0875e49..ec399fb5b 100644 --- a/src/settings.js +++ b/src/settings.js @@ -35,6 +35,9 @@ exports = module.exports = { getEmailDigest: getEmailDigest, setEmailDigest: setEmailDigest, + getPlatformConfig: getPlatformConfig, + setPlatformConfig: setPlatformConfig, + getAll: getAll, // booleans. if you add an entry here, be sure to fix getAll @@ -46,6 +49,7 @@ exports = module.exports = { UPDATE_CONFIG_KEY: 'update_config', APPSTORE_CONFIG_KEY: 'appstore_config', CAAS_CONFIG_KEY: 'caas_config', + PLATFORM_CONFIG_KEY: 'platform_config', // strings APP_AUTOUPDATE_PATTERN_KEY: 'app_autoupdate_pattern', @@ -88,6 +92,7 @@ var gDefaults = (function () { result[exports.APPSTORE_CONFIG_KEY] = {}; result[exports.CAAS_CONFIG_KEY] = {}; result[exports.EMAIL_DIGEST] = true; + result[exports.PLATFORM_CONFIG_KEY] = {}; return result; })(); @@ -371,6 +376,29 @@ function getAppstoreConfig(callback) { }); } +function getPlatformConfig(callback) { + assert.strictEqual(typeof callback, 'function'); + + settingsdb.get(exports.PLATFORM_CONFIG_KEY, function (error, value) { + if (error && error.reason === DatabaseError.NOT_FOUND) return callback(null, gDefaults[exports.PLATFORM_CONFIG_KEY]); + if (error) return callback(new SettingsError(SettingsError.INTERNAL_ERROR, error)); + + callback(null, JSON.parse(value)); + }); +} + +function setPlatformConfig(platformConfig, callback) { + assert.strictEqual(typeof callback, 'function'); + + settingsdb.set(exports.PLATFORM_CONFIG_KEY, JSON.stringify(platformConfig), function (error) { + if (error) return callback(new SettingsError(SettingsError.INTERNAL_ERROR, error)); + + exports.events.emit(exports.PLATFORM_CONFIG_KEY, platformConfig); + + callback(null); + }); +} + function setAppstoreConfig(appstoreConfig, callback) { assert.strictEqual(typeof appstoreConfig, 'object'); assert.strictEqual(typeof callback, 'function'); @@ -443,7 +471,7 @@ function getAll(callback) { result[exports.DYNAMIC_DNS_KEY] = !!result[exports.DYNAMIC_DNS_KEY]; // convert JSON objects - [exports.BACKUP_CONFIG_KEY, exports.UPDATE_CONFIG_KEY, exports.APPSTORE_CONFIG_KEY ].forEach(function (key) { + [exports.BACKUP_CONFIG_KEY, exports.UPDATE_CONFIG_KEY, exports.APPSTORE_CONFIG_KEY, exports.PLATFORM_CONFIG_KEY ].forEach(function (key) { result[key] = typeof result[key] === 'object' ? result[key] : safe.JSON.parse(result[key]); });