2013-12-14 05:18:20 +00:00
|
|
|
Promise = require 'bluebird'
|
|
|
|
fs = Promise.promisifyAll(require('fs'))
|
|
|
|
os = require 'os'
|
|
|
|
api = require './api'
|
2013-12-17 00:33:42 +00:00
|
|
|
knex = require './db'
|
2013-12-14 05:18:20 +00:00
|
|
|
utils = require './utils'
|
2013-12-23 04:33:16 +00:00
|
|
|
Docker = require 'dockerode'
|
2013-12-14 05:18:20 +00:00
|
|
|
crypto = require 'crypto'
|
|
|
|
{spawn} = require 'child_process'
|
|
|
|
bootstrap = require './bootstrap'
|
2013-12-23 04:33:16 +00:00
|
|
|
application = require './application'
|
2013-12-14 05:18:20 +00:00
|
|
|
|
|
|
|
console.log('Supervisor started..')
|
|
|
|
|
2013-12-23 04:33:16 +00:00
|
|
|
# Connect to the host docker instance
|
|
|
|
docker = Promise.promisifyAll(new Docker(socketPath: '/hostrun/docker.sock'))
|
|
|
|
|
2013-12-14 05:18:20 +00:00
|
|
|
newUuid = utils.getDeviceUuid()
|
|
|
|
oldUuid = knex('config').select('value').where(key: 'uuid')
|
|
|
|
|
2013-12-17 06:04:53 +00:00
|
|
|
Promise.all([newUuid, oldUuid]).then(([newUuid, [oldUuid]]) ->
|
2013-12-23 04:33:16 +00:00
|
|
|
oldUuid = oldUuid?.value
|
2013-12-14 05:18:20 +00:00
|
|
|
if newUuid is oldUuid
|
|
|
|
return true
|
|
|
|
|
|
|
|
console.log('New device detected. Bootstrapping..')
|
|
|
|
return bootstrap(newUuid)
|
2013-12-17 06:04:53 +00:00
|
|
|
).then(->
|
2013-12-14 05:18:20 +00:00
|
|
|
console.log('Starting OpenVPN..')
|
|
|
|
openvpn = spawn('openvpn', ['client.conf'], cwd: '/supervisor/data')
|
|
|
|
|
|
|
|
# Prefix and log all OpenVPN output
|
|
|
|
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}"))
|
2013-12-14 05:18:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Prefix and log all OpenVPN output
|
|
|
|
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-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..')
|
|
|
|
knex('app').select().then((apps) ->
|
|
|
|
Promise.all(apps.map(application.start))
|
|
|
|
).catch((error) ->
|
|
|
|
console.log(error)
|
|
|
|
)
|
2013-12-14 05:18:20 +00:00
|
|
|
)
|