balena-supervisor/src/app.coffee

55 lines
1.5 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'
console.log('Supervisor started..')
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 ->
api = require './api'
application = require './application'
supervisor = require './supervisor-update'
console.log('Starting OpenVPN..')
2014-04-27 21:41:59 +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.restart))
2014-03-19 19:54:39 +00:00
.catch (error) ->
console.error("Error starting apps:", error)
2014-03-19 19:54:39 +00:00
.then ->
console.log('Starting periodic check for updates..')
setInterval(->
application.update()
, 5 * 60 * 1000) # Every 5 mins
application.update()
console.log('Starting periodic check for supervisor updates..')
setInterval(->
supervisor.update()
, 5 * 60 * 1000) # Every 5 mins
supervisor.update()