2013-12-14 05:18:20 +00:00
|
|
|
Promise = require 'bluebird'
|
|
|
|
fs = Promise.promisifyAll(require('fs'))
|
|
|
|
os = require 'os'
|
2013-12-17 00:33:42 +00:00
|
|
|
knex = require './db'
|
2013-12-14 05:18:20 +00:00
|
|
|
utils = require './utils'
|
|
|
|
{spawn} = require 'child_process'
|
|
|
|
bootstrap = require './bootstrap'
|
|
|
|
|
|
|
|
console.log('Supervisor started..')
|
|
|
|
|
2014-05-10 16:27:25 +00:00
|
|
|
knex('config').select('value').where(key: 'uuid').then ([uuid]) ->
|
|
|
|
if not uuid?.value
|
|
|
|
console.log('New device detected. Bootstrapping..')
|
|
|
|
bootstrap()
|
2014-03-19 19:54:39 +00:00
|
|
|
.then ->
|
2014-05-10 16:27:25 +00:00
|
|
|
api = require './api'
|
|
|
|
application = require './application'
|
2014-06-16 20:25:06 +00:00
|
|
|
supervisor = require './supervisor-update'
|
2014-05-10 16:27:25 +00:00
|
|
|
|
2013-12-14 05:18:20 +00:00
|
|
|
console.log('Starting OpenVPN..')
|
2014-04-27 21:41:59 +00:00
|
|
|
openvpn = spawn('openvpn', ['client.conf'], cwd: '/data')
|
2013-12-14 05:18:20 +00:00
|
|
|
|
|
|
|
# Prefix and log all OpenVPN output
|
2014-03-19 19:54:39 +00:00
|
|
|
openvpn.stdout.on 'data', (data) ->
|
2013-12-14 05:18:20 +00:00
|
|
|
prefix = 'OPENVPN: '
|
2013-12-23 04:33:38 +00:00
|
|
|
console.log((prefix + data).trim().replace(/\n/gm, "\n#{prefix}"))
|
2013-12-14 05:18:20 +00:00
|
|
|
|
|
|
|
# Prefix and log all OpenVPN output
|
2014-03-19 19:54:39 +00:00
|
|
|
openvpn.stderr.on 'data', (data) ->
|
2013-12-14 05:18:20 +00:00
|
|
|
prefix = 'OPENVPN: '
|
2013-12-23 04:33:38 +00:00
|
|
|
console.log((prefix + data).trim().replace(/\n/gm, "\n#{prefix}"))
|
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-04-03 17:15:19 +00:00
|
|
|
Promise.all(apps.map(application.restart))
|
2014-03-19 19:54:39 +00:00
|
|
|
.catch (error) ->
|
2013-12-29 18:07:58 +00:00
|
|
|
console.error("Error starting apps:", error)
|
2014-03-19 19:54:39 +00:00
|
|
|
.then ->
|
2013-12-29 18:07:58 +00:00
|
|
|
console.log('Starting periodic check for updates..')
|
|
|
|
setInterval(->
|
|
|
|
application.update()
|
2014-05-04 20:22:03 +00:00
|
|
|
, 5 * 60 * 1000) # Every 5 mins
|
2013-12-29 18:07:58 +00:00
|
|
|
application.update()
|
2014-05-10 16:27:25 +00:00
|
|
|
|
2014-06-16 20:25:06 +00:00
|
|
|
console.log('Starting periodic check for supervisor updates..')
|
|
|
|
setInterval(->
|
|
|
|
supervisor.update()
|
|
|
|
, 5 * 60 * 1000) # Every 5 mins
|
|
|
|
supervisor.update()
|
|
|
|
|