change the bootstrap process

This commit is contained in:
Pablo Carranza Vélez 2015-09-01 14:13:01 -03:00
parent 94d8703eef
commit 0bfd329ebc
4 changed files with 105 additions and 52 deletions

View File

@ -24,7 +24,7 @@
"pubnub": "^3.7.13",
"randomstring": "~1.0.3",
"request": "^2.51.0",
"resin-register-device": "^1.0.1",
"resin-register-device": "^1.1.0",
"rwlock": "^5.0.0",
"server-destroy": "^1.0.0",
"sqlite3": "~3.0.4",

View File

@ -13,18 +13,7 @@ knex.init.then ->
console.log('Starting connectivity check..')
utils.connectivityCheck()
knex('config').select('value').where(key: 'uuid').then ([ uuid ]) ->
if not uuid?.value
console.log('New device detected. Bootstrapping..')
retryingBootstrap = ->
utils.mixpanelTrack('Device bootstrap')
bootstrap().catch (err) ->
utils.mixpanelTrack('Device bootstrap failed, retrying', {error: err, delay: config.bootstrapRetryDelay})
Promise.delay(config.bootstrapRetryDelay)
.then(retryingBootstrap)
retryingBootstrap()
else
uuid.value
bootstrap.startBootstrapping()
.then (uuid) ->
# Persist the uuid in subsequent metrics
utils.mixpanelProperties.uuid = uuid
@ -39,16 +28,22 @@ knex.init.then ->
secret = config.forceApiSecret ? randomstring.generate()
api(secret).listen(config.listenPort)
# Let API know what version we are, and our api connection info.
console.log('Updating supervisor version and api info')
device.updateState(
api_port: config.listenPort
api_secret: secret
supervisor_version: utils.supervisorVersion
provisioning_progress: null
provisioning_state: ''
download_progress: null
)
initialStateUpdate = ->
# Let API know what version we are, and our api connection info.
console.log('Updating supervisor version and api info')
device.updateState(
api_port: config.listenPort
api_secret: secret
supervisor_version: utils.supervisorVersion
provisioning_progress: null
provisioning_state: ''
download_progress: null
)
if bootstrap.bootstrapped
initialStateUpdate()
else
bootstrap.on('done', initialStateUpdate)
console.log('Starting Apps..')
knex('app').select()

View File

@ -12,6 +12,7 @@ logger = require './lib/logger'
{ cachedResinApi } = require './request'
device = require './device'
lockFile = Promise.promisifyAll(require('lockfile'))
bootstrap = require './bootstrap'
{ docker } = dockerUtils
@ -293,6 +294,7 @@ updateStatus =
failed: 0
forceNext: false
exports.update = update = (force) ->
return if !bootstrap.bootstrapped
if updateStatus.state isnt UPDATE_IDLE
# Mark an update required after the current.
updateStatus.forceNext = force

View File

@ -5,34 +5,90 @@ utils = require './utils'
deviceRegister = require 'resin-register-device'
{ resinApi } = require './request'
fs = Promise.promisifyAll(require('fs'))
crypto = require 'crypto'
appConfig = require './config'
EventEmitter = require('events').EventEmitter
module.exports = ->
# Load config file
fs.readFileAsync('/boot/config.json', 'utf8')
.then(JSON.parse)
.then (userConfig) ->
userConfig.deviceType ?= 'raspberry-pi'
if userConfig.uuid? and userConfig.registered_at?
return userConfig
deviceRegister.register(resinApi, userConfig)
.then (device) ->
userConfig.uuid = device.uuid
userConfig.registered_at = Date.now()
userConfig.deviceId = device.id
fs.writeFileAsync('/boot/config.json', JSON.stringify(userConfig))
.return(userConfig)
.then (userConfig) ->
console.log('Finishing bootstrapping')
Promise.all([
knex('config').truncate()
module.exports = do ->
configPath = '/boot/config.json'
userConfig = {}
bootstrapper = new EventEmitter()
loadPreloadedApps = ->
#To-Do
bootstrap = ->
Promise.try ->
userConfig.deviceType ?= 'raspberry-pi'
if userConfig.registered_at?
return userConfig
deviceRegister.register(resinApi, userConfig)
.catch (err) ->
# Do not fail if device already exists
return {} if err.message = '"uuid" must be unique.'
.then (device) ->
userConfig.registered_at = Date.now()
userConfig.deviceId = device.id if device.id?
fs.writeFileAsync(configPath, JSON.stringify(userConfig))
.return(userConfig)
.then (userConfig) ->
console.log('Finishing bootstrapping')
Promise.all([
knex('config').truncate()
.then ->
knex('config').insert([
{ key: 'uuid', value: userConfig.uuid }
{ key: 'apiKey', value: userConfig.apiKey }
{ key: 'username', value: userConfig.username }
{ key: 'userId', value: userConfig.userId }
{ key: 'version', value: utils.supervisorVersion }
])
])
.tap ->
doneBootstrapping()
readConfigAndEnsureUUID = ->
# Load config file
fs.readFileAsync(configPath, 'utf8')
.then(JSON.parse)
.then (config) ->
userConfig = config
return userConfig.uuid if userConfig.uuid?
Promise.try ->
deviceRegister.generateUUID()
.then (uuid) ->
userConfig.uuid = uuid
fs.writeFileAsync(configPath, JSON.stringify(userConfig))
.return(userConfig.uuid)
.catch (err) ->
console.log('Error generating UUID: ', err)
Promise.delay(config.bootstrapRetryDelay)
.then ->
knex('config').insert([
{ key: 'uuid', value: userConfig.uuid }
{ key: 'apiKey', value: userConfig.apiKey }
{ key: 'username', value: userConfig.username }
{ key: 'userId', value: userConfig.userId }
{ key: 'version', value: utils.supervisorVersion }
])
knex('app').truncate()
])
.return(userConfig.uuid)
readConfigAndEnsureUUID()
bootstrapOrRetry = ->
utils.mixpanelTrack('Device bootstrap')
bootstrap().catch (err) ->
utils.mixpanelTrack('Device bootstrap failed, retrying', {error: err, delay: config.bootstrapRetryDelay})
setTimeout(bootstrapOrRetry, config.bootstrapRetryDelay)
doneBootstrapping = ->
bootstrapper.bootstrapped = true
bootstrapper.emit('done')
bootstrapper.bootstrapped = false
bootstrapper.startBootstrapping = ->
knex('config').select('value').where(key: 'uuid')
.then ([ uuid ]) ->
if uuid?.value
doneBootstrapping()
return uuid.value
console.log('New device detected. Bootstrapping..')
readConfigAndEnsureUUID()
.tap ->
loadPreloadedApps()
.tap ->
bootstrapOrRetry()
return bootstrapper