balena-supervisor/app.coffee

113 lines
2.7 KiB
CoffeeScript
Raw Normal View History

2013-07-18 10:40:35 +00:00
fs = require('fs')
async = require('async')
request = require('request')
2013-07-19 00:45:02 +00:00
posix = require('posix')
2013-07-18 11:07:39 +00:00
{exec} = require('child_process')
2013-07-18 10:40:35 +00:00
API_ENDPOINT = 'http://paras.rulemotion.com:1337'
2013-07-18 23:31:42 +00:00
HAKI_PATH = '/home/haki'
POLLING_INTERVAL = 30000
2013-07-18 10:40:35 +00:00
try
2013-07-18 11:35:21 +00:00
state = require('./state.json')
2013-07-18 10:40:35 +00:00
catch e
console.error(e)
process.exit()
bootstrapTasks = [
2013-07-18 11:07:39 +00:00
# get config from extra partition
2013-07-18 10:40:35 +00:00
(callback) ->
try
callback(null, require('/mnt/config.json'))
catch error
callback(error)
2013-07-18 13:02:45 +00:00
# bootstrapping
2013-07-18 10:40:35 +00:00
(config, callback) ->
request.post("#{API_ENDPOINT}/associate", {
2013-07-18 23:31:42 +00:00
json:
user: config.id
2013-07-18 10:40:35 +00:00
}, (error, response, body) ->
if error
return callback(error)
2013-07-18 23:31:42 +00:00
if typeof body isnt 'object'
callback(body)
2013-07-18 10:40:35 +00:00
state.virgin = false
state.uuid = body.uuid
state.gitUrl = body.gitUrl
2013-07-18 10:40:35 +00:00
2013-07-18 23:31:42 +00:00
console.log state
2013-07-18 11:37:04 +00:00
fs.writeFileSync('state.json', JSON.stringify(state))
2013-07-18 10:40:35 +00:00
2013-07-18 11:07:39 +00:00
fs.writeFileSync('/etc/openvpn/ca.crt', body.ca)
fs.writeFileSync('/etc/openvpn/client.crt', body.cert)
fs.writeFileSync('/etc/openvpn/client.key', body.key)
fs.appendFileSync('/etc/openvpn/client.conf', "remote #{body.vpnhost} #{body.vpnport}")
2013-07-18 10:40:35 +00:00
callback(null)
)
]
2013-07-18 11:07:39 +00:00
2013-07-19 00:10:43 +00:00
setHakiEnv = (callback) ->
2013-07-19 00:45:02 +00:00
process.setuid(posix.getpwnam('haki').uid)
2013-07-19 00:10:43 +00:00
process.chdir(HAKI_PATH)
callback()
2013-07-18 11:07:39 +00:00
stage1Tasks = [
(callback) -> async.waterfall(bootstrapTasks, callback)
(callback) -> exec('systemctl start openvpn@client', callback)
(callback) -> exec('systemctl enable openvpn@client', callback)
2013-07-19 00:10:43 +00:00
setHakiEnv
(callback) -> fs.mkdir('hakiapp', callback)
2013-07-18 23:31:42 +00:00
(callback) -> exec('git init', cwd: 'hakiapp', callback)
(callback) -> exec("git remote add origin #{state.gitUrl}", cwd: 'hakiapp', callback)
(callback) -> console.log('Bootstrapped') ; callback()
2013-07-18 11:07:39 +00:00
]
2013-07-19 00:10:43 +00:00
updateRepo = (callback) ->
tasks1 = [
(callback) -> setTimeout(callback, POLLING_INTERVAL)
2013-07-18 23:31:42 +00:00
(callback) -> exec('git pull', cwd: 'hakiapp', callback)
2013-07-19 00:10:43 +00:00
(stdout, stderr, callback) -> exec('git rev-parse HEAD', cwd: 'hakiapp', callback)
(stdout, stderr, callback) -> callback(null, stdout.trim())
]
tasks2 = [
2013-07-19 14:40:53 +00:00
(callback) ->
if fs.existsSync('package.json')
exec('npm install', cwd: 'hakiapp', callback)
else
callback()
(callback) ->
if fs.existsSync('Procfile')
exec('foreman start', cwd: 'hakiapp', callback)
else
callback()
2013-07-19 00:10:43 +00:00
]
2013-07-19 00:10:43 +00:00
async.waterfall(tasks1, (error, hash) ->
if hash isnt state.gitHead
state.gitHead = hash
fs.writeFileSync('state.json', JSON.stringify(state))
2013-07-19 00:10:43 +00:00
async.series(tasks2, callback)
else
callback()
2013-07-19 00:33:20 +00:00
)
2013-07-19 00:10:43 +00:00
stage2Tasks = [
setHakiEnv
(callback) -> async.forever(updateRepo, callback)
]
2013-07-18 23:31:42 +00:00
if state.virgin
2013-07-19 00:10:43 +00:00
tasks = stage1Tasks.concat(stage2Tasks)
else
tasks = stage2Tasks
async.series(tasks, (error, results) ->
if (error)
console.error(error)
)