Promisify executeSpecialActionsAndBootConfig and remount /boot rw

This commit is contained in:
Pablo Carranza Vélez 2015-09-28 20:32:12 +00:00
parent c136793cdd
commit d25447d539
2 changed files with 26 additions and 20 deletions

View File

@ -291,16 +291,17 @@ specialActionEnvVars =
executedSpecialActionEnvVars = {}
executeSpecialActionsAndBootConfig = (env) ->
_.map specialActionEnvVars, (specialActionCallback, key) ->
if env[key]? && specialActionCallback?
# This makes the Special Action Envs only trigger their functions once.
if !_.has(executedSpecialActionEnvVars, key) or executedSpecialActionEnvVars[key] != env[key]
specialActionCallback(env[key])
executedSpecialActionEnvVars[key] = env[key]
bootConfigVars = _.pick env, (val, key) ->
return _.startsWith(key, device.bootConfigEnvVarPrefix)
if !_.isEmpty(bootConfigVars)
device.setBootConfig(bootConfigVars)
Promise.try ->
_.map specialActionEnvVars, (specialActionCallback, key) ->
if env[key]? && specialActionCallback?
# This makes the Special Action Envs only trigger their functions once.
if !_.has(executedSpecialActionEnvVars, key) or executedSpecialActionEnvVars[key] != env[key]
specialActionCallback(env[key])
executedSpecialActionEnvVars[key] = env[key]
bootConfigVars = _.pick env, (val, key) ->
return _.startsWith(key, device.bootConfigEnvVarPrefix)
if !_.isEmpty(bootConfigVars)
device.setBootConfig(bootConfigVars)
UPDATE_IDLE = 0
UPDATE_UPDATING = 1
@ -372,10 +373,6 @@ application.update = update = (force) ->
remoteApps = _.indexBy(remoteApps, 'appId')
remoteAppIds = _.keys(remoteApps)
# Run special functions against variables if remoteAppEnvs has the corresponding variable function mapping.
_.map remoteAppIds, (appId) ->
executeSpecialActionsAndBootConfig(remoteAppEnvs[appId])
apps = _.indexBy(apps, 'appId')
localApps = _.mapValues apps, (app) ->
app.env = JSON.stringify(_.omit(JSON.parse(app.env), _.keys(specialActionEnvVars)))
@ -395,7 +392,11 @@ application.update = update = (force) ->
allAppIds = _.union(localAppIds, remoteAppIds)
Promise.map allAppIds, (appId) ->
# Run special functions against variables if remoteAppEnvs has the corresponding variable function mapping.
Promise.map remoteAppIds, (appId) ->
executeSpecialActionsAndBootConfig(remoteAppEnvs[appId])
.return(allAppIds)
.map (appId) ->
Promise.try ->
fetch(remoteApps[appId]) if _.includes(toBeDownloaded, appId)
.then ->
@ -468,7 +469,8 @@ application.initialize = ->
.then (apps) ->
Promise.map apps, (app) ->
executeSpecialActionsAndBootConfig(JSON.parse(app.env))
unlockAndStart(app)
.then ->
unlockAndStart(app)
.catch (error) ->
console.error('Error starting apps:', error)
.then ->

View File

@ -8,7 +8,7 @@ config = require './config'
configPath = '/boot/config.json'
request = Promise.promisifyAll(require('request'))
execAsync = Promise.promisify(require('child_process').exec)
fs = Promise.promisifyAll(require('fs'))
exports.getID = do ->
deviceIdPromise = null
return ->
@ -39,7 +39,9 @@ rebootDevice = ->
request.postAsync(config.gosuperAddress + '/v1/reboot')
exports.bootConfigEnvVarPrefix = bootConfigEnvVarPrefix = 'RESIN_HOST_CONFIG_'
bootConfigPath = '/mnt/root/boot/config.txt'
bootBlockDevice = '/dev/mmcblk0p1'
bootMountPoint = '/mnt/root/boot'
bootConfigPath = bootMountPoint + '/config.txt'
configRegex = new RegExp('(' + _.escapeRegExp(bootConfigEnvVarPrefix) + ')(.+)')
forbiddenConfigKeys = [
'disable_commandline_tags'
@ -61,7 +63,7 @@ forbiddenConfigKeys = [
parseBootConfigFromEnv = (env) ->
# We ensure env doesn't have garbage
parsedEnv = _.pick env, (val, key) ->
return _.startsWith(bootConfigEnvVarPrefix)
return _.startsWith(key, bootConfigEnvVarPrefix)
parsedEnv = _.mapKeys parsedEnv, (val, key) ->
key.replace(configRegex, '$2')
parsedEnv = _.omit(parsedEnv, forbiddenConfigKeys)
@ -102,7 +104,9 @@ exports.setBootConfig = (env) ->
configStatement = configStatements[index]
return configStatement
# Here's the dangerous part:
fs.writeFileAsync(bootConfigPath + '.new', outputConfig.join('\n'))
execAsync("mount -t vfat -o remount,rw #{bootBlockDevice} #{bootMountPoint}")
.then ->
fs.writeFileAsync(bootConfigPath + '.new', outputConfig.join('\n'))
.then ->
fs.renameAsync(bootConfigPath + '.new', bootConfigPath)
.then ->