Merge pull request #768 from resin-io/preloading-when-no-target

Fix preloading in flasher images by reading apps.json if target hasn'…
This commit is contained in:
Pablo Carranza Vélez 2018-10-19 15:35:51 +02:00 committed by GitHub
commit b29a275e49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 2 deletions

View File

@ -75,6 +75,7 @@ class Config extends EventEmitter {
// a JSON value, which is either null, or { app: number, commit: string } // a JSON value, which is either null, or { app: number, commit: string }
pinDevice: { source: 'db', mutable: true, default: 'null' }, pinDevice: { source: 'db', mutable: true, default: 'null' },
currentCommit: { source: 'db', mutable: true }, currentCommit: { source: 'db', mutable: true },
targetStateSet: { source: 'db', mutable: true, default: 'false' },
}; };
public constructor({ db, configPath }: ConfigOpts) { public constructor({ db, configPath }: ConfigOpts) {

View File

@ -166,7 +166,8 @@ module.exports = class DeviceState extends EventEmitter
@config.getMany([ @config.getMany([
'initialConfigSaved', 'listenPort', 'apiSecret', 'osVersion', 'osVariant', 'initialConfigSaved', 'listenPort', 'apiSecret', 'osVersion', 'osVariant',
'version', 'provisioned', 'apiEndpoint', 'connectivityCheckEnabled', 'legacyAppsPresent' 'version', 'provisioned', 'apiEndpoint', 'connectivityCheckEnabled', 'legacyAppsPresent',
'targetStateSet'
]) ])
.then (conf) => .then (conf) =>
Promise.try => Promise.try =>
@ -195,8 +196,20 @@ module.exports = class DeviceState extends EventEmitter
update_downloaded: false update_downloaded: false
) )
.then => .then =>
if !conf.provisioned @applications.getTargetApps()
.then (targetApps) =>
if !conf.provisioned or (_.isEmpty(targetApps) and !validation.checkTruthy(conf.targetStateSet))
@loadTargetFromFile() @loadTargetFromFile()
.finally =>
@config.set({ targetStateSet: 'true' })
else
console.log('Skipping preloading')
if conf.provisioned and !_.isEmpty(targetApps)
# If we're in this case, it's because we've updated from an older supervisor
# and we need to mark that the target state has been set so that
# the supervisor doesn't try to preload again if in the future target
# apps are empty again (which may happen with multi-app).
@config.set({ targetStateSet: 'true' })
.then => .then =>
@triggerApplyTarget({ initial: true }) @triggerApplyTarget({ initial: true })
@ -315,12 +328,14 @@ module.exports = class DeviceState extends EventEmitter
return { apps, config: filteredDeviceConf } return { apps, config: filteredDeviceConf }
loadTargetFromFile: (appsPath) -> loadTargetFromFile: (appsPath) ->
console.log('Attempting to load preloaded apps...')
appsPath ?= constants.appsJsonPath appsPath ?= constants.appsJsonPath
fs.readFileAsync(appsPath, 'utf8') fs.readFileAsync(appsPath, 'utf8')
.then(JSON.parse) .then(JSON.parse)
.then (stateFromFile) => .then (stateFromFile) =>
if _.isArray(stateFromFile) if _.isArray(stateFromFile)
# This is a legacy apps.json # This is a legacy apps.json
console.log('Legacy apps.json detected')
return @_convertLegacyAppsJson(stateFromFile) return @_convertLegacyAppsJson(stateFromFile)
else else
return stateFromFile return stateFromFile
@ -357,9 +372,11 @@ module.exports = class DeviceState extends EventEmitter
local: stateFromFile local: stateFromFile
}) })
.then => .then =>
console.log('Preloading complete')
if stateFromFile.pinDevice if stateFromFile.pinDevice
# multi-app warning! # multi-app warning!
# The following will need to be changed once running multiple applications is possible # The following will need to be changed once running multiple applications is possible
console.log('Device will be pinned')
if commitToPin? and appToPin? if commitToPin? and appToPin?
@config.set @config.set
pinDevice: JSON.stringify { pinDevice: JSON.stringify {