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'
|
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 20:34:00 +00:00
|
|
|
|
2013-07-18 10:40:35 +00:00
|
|
|
state.virgin = false
|
|
|
|
state.uuid = body.uuid
|
2013-07-18 20:34:00 +00:00
|
|
|
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)
|
2013-07-18 11:24:47 +00:00
|
|
|
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)
|
2013-07-18 11:07:39 +00:00
|
|
|
]
|
|
|
|
|
2013-07-19 00:10:43 +00:00
|
|
|
updateRepo = (callback) ->
|
|
|
|
tasks1 = [
|
|
|
|
(callback) -> setTimeout(callback, 3e5)
|
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-18 23:31:42 +00:00
|
|
|
(callback) -> exec('npm install', cwd: 'hakiapp', callback)
|
|
|
|
(callback) -> exec('foreman start', cwd: 'hakiapp', callback)
|
2013-07-19 00:10:43 +00:00
|
|
|
]
|
2013-07-18 20:34:00 +00:00
|
|
|
|
2013-07-19 00:10:43 +00:00
|
|
|
async.waterfall(tasks1, (error, hash) ->
|
|
|
|
if hash isnt state.githead
|
|
|
|
state.githead = hash
|
|
|
|
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 20:34:00 +00:00
|
|
|
|
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)
|
|
|
|
else
|
|
|
|
console.log('Bootstrapped')
|
|
|
|
)
|