2016-07-15 20:59:39 +00:00
|
|
|
require('log-timestamp')
|
2015-01-14 17:01:27 +00:00
|
|
|
process.on 'uncaughtException', (e) ->
|
|
|
|
console.error('Got unhandled exception', e, e?.stack)
|
|
|
|
|
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'
|
2015-09-16 14:03:56 +00:00
|
|
|
request = require 'request'
|
2013-12-14 05:18:20 +00:00
|
|
|
|
2015-01-14 03:05:23 +00:00
|
|
|
knex.init.then ->
|
|
|
|
utils.mixpanelTrack('Supervisor start')
|
2014-08-14 21:32:03 +00:00
|
|
|
|
2015-01-14 03:05:23 +00:00
|
|
|
console.log('Starting connectivity check..')
|
|
|
|
utils.connectivityCheck()
|
2014-08-14 21:32:03 +00:00
|
|
|
|
2015-11-02 16:16:43 +00:00
|
|
|
Promise.join bootstrap.startBootstrapping(), utils.getOrGenerateSecret('api'), utils.getOrGenerateSecret('logsChannel'), (uuid, secret, logsChannel) ->
|
2015-01-14 03:05:23 +00:00
|
|
|
# Persist the uuid in subsequent metrics
|
|
|
|
utils.mixpanelProperties.uuid = uuid
|
2014-06-18 16:54:36 +00:00
|
|
|
|
2015-01-14 03:05:23 +00:00
|
|
|
api = require './api'
|
2015-10-28 23:02:00 +00:00
|
|
|
application = require('./application')(logsChannel)
|
2015-07-20 20:02:37 +00:00
|
|
|
device = require './device'
|
2015-02-04 13:14:30 +00:00
|
|
|
|
2015-09-01 17:11:46 +00:00
|
|
|
bootstrap.done
|
|
|
|
.then ->
|
2016-01-20 19:57:17 +00:00
|
|
|
device.getOSVersion()
|
|
|
|
.then (osVersion) ->
|
2015-09-01 22:28:37 +00:00
|
|
|
console.log('Starting API server..')
|
2016-05-18 17:12:35 +00:00
|
|
|
apiServer = api(application).listen(config.listenPort)
|
|
|
|
apiServer.timeout = config.apiTimeout
|
2015-09-01 17:13:01 +00:00
|
|
|
# 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
|
2016-01-20 19:57:17 +00:00
|
|
|
os_version: osVersion
|
2015-09-01 17:13:01 +00:00
|
|
|
supervisor_version: utils.supervisorVersion
|
|
|
|
provisioning_progress: null
|
|
|
|
provisioning_state: ''
|
|
|
|
download_progress: null
|
2015-10-28 23:02:00 +00:00
|
|
|
logs_channel: logsChannel
|
2015-09-01 17:13:01 +00:00
|
|
|
)
|
|
|
|
|
2015-01-14 03:05:23 +00:00
|
|
|
console.log('Starting Apps..')
|
2015-09-24 20:09:06 +00:00
|
|
|
application.initialize()
|
2014-05-10 16:27:25 +00:00
|
|
|
|
2015-01-14 03:05:23 +00:00
|
|
|
updateIpAddr = ->
|
2015-09-16 14:03:56 +00:00
|
|
|
callback = (error, response, body ) ->
|
2015-11-10 14:39:03 +00:00
|
|
|
if !error && response.statusCode == 200 && body.Data.IPAddresses?
|
2015-09-16 14:03:56 +00:00
|
|
|
device.updateState(
|
2015-09-18 16:59:25 +00:00
|
|
|
ip_address: body.Data.IPAddresses.join(' ')
|
2015-09-16 14:03:56 +00:00
|
|
|
)
|
2015-09-16 15:35:13 +00:00
|
|
|
request.get({ url: "#{config.gosuperAddress}/v1/ipaddr", json: true }, callback )
|
2015-09-16 14:03:56 +00:00
|
|
|
|
2015-01-14 03:05:23 +00:00
|
|
|
console.log('Starting periodic check for IP addresses..')
|
|
|
|
setInterval(updateIpAddr, 30 * 1000) # Every 30s
|
|
|
|
updateIpAddr()
|