From e8fbadb8d6085595eaa1c9a1981266a3016c81ab Mon Sep 17 00:00:00 2001 From: Pablo Carranza Velez Date: Thu, 19 Jan 2017 13:56:20 -0300 Subject: [PATCH] Issue #381: Set target deviceConfig values from preloaded apps Also split out deviceConfig set and get to a separate module to avoid circular dependencies. Change-Type: patch Signed-off-by: Pablo Carranza Velez --- src/application.coffee | 7 ++++--- src/bootstrap.coffee | 7 +++++++ src/device-config.coffee | 15 +++++++++++++++ src/device.coffee | 13 ------------- 4 files changed, 26 insertions(+), 16 deletions(-) create mode 100644 src/device-config.coffee diff --git a/src/application.coffee b/src/application.coffee index e817d88b..0e0ce064 100644 --- a/src/application.coffee +++ b/src/application.coffee @@ -16,6 +16,7 @@ fs = Promise.promisifyAll(require('fs')) JSONStream = require 'JSONStream' proxyvisor = require './proxyvisor' { checkInt } = require './lib/validation' +deviceConfig = require './device-config' class UpdatesLockedError extends TypedError ImageNotFoundError = (err) -> @@ -427,11 +428,11 @@ executeSpecialActionsAndHostConfig = (conf, oldConf) -> device.setHostConfig(hostConfigVars, oldHostConfigVars, logSystemMessage) getAndApplyDeviceConfig = -> - device.getConfig() + deviceConfig.get() .then ({ values, targetValues }) -> executeSpecialActionsAndHostConfig(targetValues, values) .tap -> - device.setConfig({ values: targetValues }) + deviceConfig.set({ values: targetValues }) .then (needsReboot) -> device.reboot() if needsReboot @@ -634,7 +635,7 @@ application.update = update = (force, scheduled = false) -> remoteDeviceConfig = {} _.map remoteAppIds, (appId) -> _.merge(remoteDeviceConfig, JSON.parse(remoteApps[appId].config)) - device.setConfig({ targetValues: remoteDeviceConfig }) + deviceConfig.set({ targetValues: remoteDeviceConfig }) .then -> getAndApplyDeviceConfig() .catch (err) -> diff --git a/src/bootstrap.coffee b/src/bootstrap.coffee index 8662067b..e9a451a1 100644 --- a/src/bootstrap.coffee +++ b/src/bootstrap.coffee @@ -7,6 +7,8 @@ fs = Promise.promisifyAll(require('fs')) config = require './config' configPath = '/boot/config.json' appsPath = '/boot/apps.json' +_ = require 'lodash' +deviceConfig = require './device-config' userConfig = {} DuplicateUuidError = (err) -> @@ -15,6 +17,7 @@ DuplicateUuidError = (err) -> bootstrapper = {} loadPreloadedApps = -> + devConfig = {} knex('app').truncate() .then -> fs.readFileAsync(appsPath, 'utf8') @@ -23,7 +26,11 @@ loadPreloadedApps = -> utils.extendEnvVars(app.env, userConfig.uuid, app.appId, app.name, app.commit) .then (extendedEnv) -> app.env = JSON.stringify(extendedEnv) + _.merge(devConfig, app.config) + app.config = JSON.stringify(app.config) knex('app').insert(app) + .then -> + deviceConfig.set({ targetValues: devConfig }) .catch (err) -> utils.mixpanelTrack('Loading preloaded apps failed', { error: err }) diff --git a/src/device-config.coffee b/src/device-config.coffee new file mode 100644 index 00000000..b37f97a6 --- /dev/null +++ b/src/device-config.coffee @@ -0,0 +1,15 @@ +knex = require './db' + +exports.set = (conf) -> + confToUpdate = {} + confToUpdate.values = JSON.stringify(conf.values) if conf.values? + confToUpdate.targetValues = JSON.stringify(conf.targetValues) if conf.targetValues? + knex('deviceConfig').update(confToUpdate) + +exports.get = -> + knex('deviceConfig').select() + .then ([ deviceConfig ]) -> + return { + values: JSON.parse(deviceConfig.values) + targetValues: JSON.parse(deviceConfig.targetValues) + } diff --git a/src/device.coffee b/src/device.coffee index 5f392754..dcf82187 100644 --- a/src/device.coffee +++ b/src/device.coffee @@ -224,16 +224,3 @@ do -> exports.getOSVersion = memoizePromise -> utils.getOSVersion(config.hostOsVersionPath) - -exports.getConfig = -> - knex('deviceConfig').select() - .then ([ deviceConfig ]) -> - return { - values: JSON.parse(deviceConfig.values) - targetValues: JSON.parse(deviceConfig.targetValues) - } -exports.setConfig = (conf) -> - confToUpdate = {} - confToUpdate.values = JSON.stringify(conf.values) if conf.values? - confToUpdate.targetValues = JSON.stringify(conf.targetValues) if conf.targetValues? - knex('deviceConfig').update(confToUpdate)