balena-supervisor/app.coffee

136 lines
3.0 KiB
CoffeeScript
Raw Normal View History

fs = require('fs')
express = require('express')
2013-07-21 00:41:52 +00:00
async = require('async')
bootstrap = require('./bootstrap')
state = require('./state')
settings = require('./settings')
request = require('request')
Application = require('./application')
console.log('Supervisor started..')
hakiApp = null
2013-07-21 00:41:52 +00:00
tasks = [
2013-07-18 10:40:35 +00:00
(callback) ->
2013-07-21 00:41:52 +00:00
if state.get('virgin')
2013-07-21 13:32:46 +00:00
console.log('Device is virgin. Bootstrapping')
2013-07-21 00:41:52 +00:00
handler = (error) ->
if error
console.log('Bootstrapping failed with error', error)
console.log('Trying again in 10s')
setTimeout((-> bootstrap(handler)), 10000)
2013-07-21 00:41:52 +00:00
else
2013-07-21 13:32:46 +00:00
console.log('Bootstrapping successful')
2013-07-21 00:41:52 +00:00
state.set('virgin', false)
callback()
bootstrap(handler)
2013-07-19 00:10:43 +00:00
else
console.log("Device isn't a virgin")
2013-07-21 00:41:52 +00:00
callback()
(callback) ->
fs.writeFile('/sys/class/leds/led0/trigger', 'none', callback)
(callback) ->
hakiApp = new Application(state.get('gitUrl'), '/home/haki/hakiapp', 'haki')
hakiApp.on 'pre-init', ->
request(
uri: "#{settings.API_ENDPOINT}/ewa/device?$filter=uuid eq '#{state.get('uuid')}'"
method: 'PATCH'
json:
status: 'Initialising'
)
hakiApp.on 'post-init', ->
request(
uri: "#{settings.API_ENDPOINT}/ewa/device?$filter=uuid eq '#{state.get('uuid')}'"
method: 'PATCH'
json:
status: 'Idle'
)
hakiApp.on 'pre-update', ->
request(
uri: "#{settings.API_ENDPOINT}/ewa/device?$filter=uuid eq '#{state.get('uuid')}'"
method: 'PATCH'
json:
status: 'Updating'
)
hakiApp.on 'post-update', (hash) ->
request(
uri: "#{settings.API_ENDPOINT}/ewa/device?$filter=uuid eq '#{state.get('uuid')}'"
method: 'PATCH'
json:
status: 'Idle'
commit: state.get('gitHash')
)
hakiApp.on 'start', ->
request(
uri: "#{settings.API_ENDPOINT}/ewa/device?$filter=uuid eq '#{state.get('uuid')}'"
method: 'PATCH'
json:
status: 'Running'
)
hakiApp.on 'stop', ->
request(
uri: "#{settings.API_ENDPOINT}/ewa/device?$filter=uuid eq '#{state.get('uuid')}'"
method: 'PATCH'
json:
status: 'Idle'
)
if not state.get('appInitialised')
console.log('Initialising app..')
hakiApp.init((error) ->
if error then return callback(error)
state.set('appInitialised', true)
callback()
)
else
console.log('App already initialised')
callback()
(callback) ->
console.log('Fetching new code..')
hakiApp.update(callback)
(callback) ->
console.log('Starting the app..')
hakiApp.start(callback)
2013-07-19 00:10:43 +00:00
]
2013-07-21 00:41:52 +00:00
async.series(tasks, (error) ->
if error
2013-07-19 00:10:43 +00:00
console.error(error)
else
console.log('Everything is fine :)')
2013-07-19 00:10:43 +00:00
)
app = express()
app.post('/blink', (req, res) ->
ledState = 0
2013-07-21 00:41:52 +00:00
toggleLed = ->
ledState = (ledState + 1) % 2
fs.writeFileSync(settings.LED_FILE, ledState)
2013-07-21 00:41:52 +00:00
interval = setInterval(toggleLed, settings.BLINK_STEP)
setTimeout(->
clearInterval(interval)
fs.writeFileSync(settings.LED_FILE, 0)
2013-07-21 00:41:52 +00:00
res.send(200)
, 5000)
)
app.post('/update', (req, res) ->
2013-07-21 01:31:54 +00:00
hakiApp.update((error) ->
if error
res.send(500)
else
res.send(204)
)
)
app.listen(80)