Merge pull request #384 from resin-io/381-preloaded-deviceconfig

Issue #381: Set target deviceConfig values from preloaded apps
This commit is contained in:
Pablo Carranza Vélez 2017-02-03 11:05:00 -03:00 committed by GitHub
commit ca5d2a8fd2
4 changed files with 26 additions and 16 deletions

View File

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

View File

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

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