balena-supervisor/src/app.coffee

105 lines
3.0 KiB
CoffeeScript
Raw Normal View History

Promise = require 'bluebird'
fs = Promise.promisifyAll(require('fs'))
os = require 'os'
2013-12-17 00:33:42 +00:00
knex = require './db'
utils = require './utils'
{spawn} = require 'child_process'
bootstrap = require './bootstrap'
config = require './config'
2014-06-18 16:54:36 +00:00
utils.mixpanelTrack('Supervisor start')
connectivityState = true # Used to prevent multiple messages when disconnected
2014-09-15 11:31:14 +00:00
ensureConnected = (continuous = false) ->
utils.checkConnectivity()
.then (connected) ->
if not connected
if connectivityState
console.log('Waiting for connectivity...')
connectivityState = false
interval = setInterval(utils.blink,400)
Promise.delay(2000)
.then ->
# Clear the blinks after 2 second
clearInterval(interval)
ensureConnected(continuous)
else
if not connectivityState
console.log('Internet Connectivity: OK')
connectivityState = true
if continuous
setTimeout(->
ensureConnected(continuous)
, 10 * 1000) # Every 10 seconds perform this check.
2014-09-15 11:31:14 +00:00
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()
2014-06-18 16:54:36 +00:00
else
uuid.value
.then (uuid) ->
# Persist the uuid in subsequent metrics
utils.mixpanelProperties.uuid = uuid
api = require './api'
application = require './application'
supervisor = require './supervisor-update'
console.log('Starting OpenVPN..')
2014-09-15 11:31:14 +00:00
openvpn = spawn('openvpn', [ 'client.conf' ], cwd: '/data')
# Prefix and log all OpenVPN output
2014-03-19 19:54:39 +00:00
openvpn.stdout.on 'data', (data) ->
prefix = 'OPENVPN: '
2013-12-23 04:33:38 +00:00
console.log((prefix + data).trim().replace(/\n/gm, "\n#{prefix}"))
# Prefix and log all OpenVPN output
2014-03-19 19:54:39 +00:00
openvpn.stderr.on 'data', (data) ->
prefix = 'OPENVPN: '
2013-12-23 04:33:38 +00:00
console.log((prefix + data).trim().replace(/\n/gm, "\n#{prefix}"))
2013-12-18 00:55:47 +00:00
console.log('Starting API server..')
api.listen(80)
2013-12-23 04:33:16 +00:00
console.log('Starting Apps..')
2014-03-19 19:54:39 +00:00
knex('app').select()
.then (apps) ->
Promise.all(apps.map(application.start))
2014-03-19 19:54:39 +00:00
.catch (error) ->
2014-09-15 11:31:14 +00:00
console.error('Error starting apps:', error)
2014-03-19 19:54:39 +00:00
.then ->
utils.mixpanelTrack('Start application update poll', {interval: config.appUpdatePollInterval})
setInterval(->
application.update()
, config.appUpdatePollInterval)
application.update()
updateIpAddr = ->
utils.findIpAddrs().then (ipAddrs) ->
application.updateDeviceInfo(
ip_address: ipAddrs.join(' ')
)
console.log('Starting periodic check for IP addresses..')
setInterval(updateIpAddr, 5 * 60 * 1000) # Every 5 mins
updateIpAddr()
console.log('Starting connectivity check..')
ensureConnected(true)
# Tell the supervisor updater that we have successfully started, so that it can do whatever it needs to.
supervisor.startupSuccessful()
2014-10-08 18:19:27 +00:00
# Let API know we are running a new version
application.updateDeviceInfo(
supervisor_version: utils.supervisorVersion
2014-10-08 18:19:27 +00:00
)