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 <pablo@resin.io>
This commit is contained in:
Pablo Carranza Velez 2017-01-19 13:56:20 -03:00
parent 1e2d3d1af7
commit e8fbadb8d6
4 changed files with 26 additions and 16 deletions

View File

@ -16,6 +16,7 @@ fs = Promise.promisifyAll(require('fs'))
JSONStream = require 'JSONStream' JSONStream = require 'JSONStream'
proxyvisor = require './proxyvisor' proxyvisor = require './proxyvisor'
{ checkInt } = require './lib/validation' { checkInt } = require './lib/validation'
deviceConfig = require './device-config'
class UpdatesLockedError extends TypedError class UpdatesLockedError extends TypedError
ImageNotFoundError = (err) -> ImageNotFoundError = (err) ->
@ -427,11 +428,11 @@ executeSpecialActionsAndHostConfig = (conf, oldConf) ->
device.setHostConfig(hostConfigVars, oldHostConfigVars, logSystemMessage) device.setHostConfig(hostConfigVars, oldHostConfigVars, logSystemMessage)
getAndApplyDeviceConfig = -> getAndApplyDeviceConfig = ->
device.getConfig() deviceConfig.get()
.then ({ values, targetValues }) -> .then ({ values, targetValues }) ->
executeSpecialActionsAndHostConfig(targetValues, values) executeSpecialActionsAndHostConfig(targetValues, values)
.tap -> .tap ->
device.setConfig({ values: targetValues }) deviceConfig.set({ values: targetValues })
.then (needsReboot) -> .then (needsReboot) ->
device.reboot() if needsReboot device.reboot() if needsReboot
@ -634,7 +635,7 @@ application.update = update = (force, scheduled = false) ->
remoteDeviceConfig = {} remoteDeviceConfig = {}
_.map remoteAppIds, (appId) -> _.map remoteAppIds, (appId) ->
_.merge(remoteDeviceConfig, JSON.parse(remoteApps[appId].config)) _.merge(remoteDeviceConfig, JSON.parse(remoteApps[appId].config))
device.setConfig({ targetValues: remoteDeviceConfig }) deviceConfig.set({ targetValues: remoteDeviceConfig })
.then -> .then ->
getAndApplyDeviceConfig() getAndApplyDeviceConfig()
.catch (err) -> .catch (err) ->

View File

@ -7,6 +7,8 @@ fs = Promise.promisifyAll(require('fs'))
config = require './config' config = require './config'
configPath = '/boot/config.json' configPath = '/boot/config.json'
appsPath = '/boot/apps.json' appsPath = '/boot/apps.json'
_ = require 'lodash'
deviceConfig = require './device-config'
userConfig = {} userConfig = {}
DuplicateUuidError = (err) -> DuplicateUuidError = (err) ->
@ -15,6 +17,7 @@ DuplicateUuidError = (err) ->
bootstrapper = {} bootstrapper = {}
loadPreloadedApps = -> loadPreloadedApps = ->
devConfig = {}
knex('app').truncate() knex('app').truncate()
.then -> .then ->
fs.readFileAsync(appsPath, 'utf8') fs.readFileAsync(appsPath, 'utf8')
@ -23,7 +26,11 @@ loadPreloadedApps = ->
utils.extendEnvVars(app.env, userConfig.uuid, app.appId, app.name, app.commit) utils.extendEnvVars(app.env, userConfig.uuid, app.appId, app.name, app.commit)
.then (extendedEnv) -> .then (extendedEnv) ->
app.env = JSON.stringify(extendedEnv) app.env = JSON.stringify(extendedEnv)
_.merge(devConfig, app.config)
app.config = JSON.stringify(app.config)
knex('app').insert(app) knex('app').insert(app)
.then ->
deviceConfig.set({ targetValues: devConfig })
.catch (err) -> .catch (err) ->
utils.mixpanelTrack('Loading preloaded apps failed', { error: err }) utils.mixpanelTrack('Loading preloaded apps failed', { error: err })

15
src/device-config.coffee Normal file
View File

@ -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)
}

View File

@ -224,16 +224,3 @@ do ->
exports.getOSVersion = memoizePromise -> exports.getOSVersion = memoizePromise ->
utils.getOSVersion(config.hostOsVersionPath) 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)