2013-12-14 05:18:20 +00:00
|
|
|
Promise = require 'bluebird'
|
2013-12-17 00:33:42 +00:00
|
|
|
knex = require './db'
|
2013-12-14 05:18:20 +00:00
|
|
|
utils = require './utils'
|
|
|
|
bootstrap = require './bootstrap'
|
2014-10-14 09:08:26 +00:00
|
|
|
config = require './config'
|
2013-12-14 05:18:20 +00:00
|
|
|
|
2014-06-18 16:54:36 +00:00
|
|
|
utils.mixpanelTrack('Supervisor start')
|
2013-12-14 05:18:20 +00:00
|
|
|
|
2014-08-14 21:32:03 +00:00
|
|
|
|
2014-12-04 16:01:20 +00:00
|
|
|
console.log('Starting connectivity check..')
|
2014-12-10 19:44:32 +00:00
|
|
|
utils.connectivityCheck()
|
2014-08-14 21:32:03 +00:00
|
|
|
|
2014-09-15 11:31:14 +00:00
|
|
|
knex('config').select('value').where(key: 'uuid').then ([ uuid ]) ->
|
2014-05-10 16:27:25 +00:00
|
|
|
if not uuid?.value
|
|
|
|
console.log('New device detected. Bootstrapping..')
|
2014-10-01 16:00:21 +00:00
|
|
|
retryingBootstrap = ->
|
2014-08-14 21:32:03 +00:00
|
|
|
utils.mixpanelTrack('Device bootstrap')
|
2014-10-01 16:00:21 +00:00
|
|
|
bootstrap().catch (err) ->
|
2014-10-01 17:37:35 +00:00
|
|
|
utils.mixpanelTrack('Device bootstrap failed, retrying', {error: err, delay: config.bootstrapRetryDelay})
|
2014-10-01 16:00:21 +00:00
|
|
|
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
|
|
|
|
|
2014-05-10 16:27:25 +00:00
|
|
|
api = require './api'
|
|
|
|
application = require './application'
|
2014-10-14 09:21:19 +00:00
|
|
|
supervisor = require './supervisor-update'
|
2014-12-23 19:11:28 +00:00
|
|
|
vpn = require './lib/vpn'
|
2014-05-10 16:27:25 +00:00
|
|
|
|
2013-12-14 05:18:20 +00:00
|
|
|
console.log('Starting OpenVPN..')
|
2014-12-23 19:11:28 +00:00
|
|
|
setImmediate(vpn.connect)
|
2013-12-14 05:18:20 +00:00
|
|
|
|
2013-12-18 00:55:47 +00:00
|
|
|
console.log('Starting API server..')
|
2013-12-14 05:18:20 +00:00
|
|
|
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) ->
|
2014-08-08 14:25:19 +00:00
|
|
|
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 ->
|
2015-01-02 18:34:50 +00:00
|
|
|
utils.mixpanelTrack('Start application update poll', {interval: config.appUpdatePollInterval})
|
2013-12-29 18:07:58 +00:00
|
|
|
setInterval(->
|
|
|
|
application.update()
|
2014-10-14 10:38:50 +00:00
|
|
|
, config.appUpdatePollInterval)
|
2013-12-29 18:07:58 +00:00
|
|
|
application.update()
|
2014-05-10 16:27:25 +00:00
|
|
|
|
2014-07-11 14:57:49 +00:00
|
|
|
updateIpAddr = ->
|
|
|
|
utils.findIpAddrs().then (ipAddrs) ->
|
|
|
|
application.updateDeviceInfo(
|
|
|
|
ip_address: ipAddrs.join(' ')
|
|
|
|
)
|
|
|
|
console.log('Starting periodic check for IP addresses..')
|
2014-11-24 14:56:09 +00:00
|
|
|
setInterval(updateIpAddr, 30 * 1000) # Every 30s
|
2014-07-11 14:57:49 +00:00
|
|
|
updateIpAddr()
|
|
|
|
|
2014-10-01 17:26:32 +00:00
|
|
|
# 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
|
2014-12-23 17:52:23 +00:00
|
|
|
console.log('Updating supervisor version:', utils.supervisorVersion)
|
2014-10-08 18:19:27 +00:00
|
|
|
application.updateDeviceInfo(
|
2014-10-14 09:21:19 +00:00
|
|
|
supervisor_version: utils.supervisorVersion
|
2014-10-08 18:19:27 +00:00
|
|
|
)
|