2013-07-18 10:40:35 +00:00
|
|
|
fs = require('fs')
|
|
|
|
async = require('async')
|
|
|
|
request = require('request')
|
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 20:34:00 +00:00
|
|
|
HOME_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", {
|
|
|
|
user: config.id
|
|
|
|
}, (error, response, body) ->
|
|
|
|
if error
|
|
|
|
return callback(error)
|
|
|
|
|
|
|
|
try
|
2013-07-18 20:34:00 +00:00
|
|
|
if typeof body isnt 'object'
|
|
|
|
throw new Error(body)
|
|
|
|
|
2013-07-18 10:40:35 +00:00
|
|
|
body = JSON.parse(body)
|
|
|
|
catch error
|
|
|
|
callback(error)
|
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 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
|
|
|
|
|
|
|
stage1Tasks = [
|
|
|
|
(callback) -> async.waterfall(bootstrapTasks, callback)
|
|
|
|
(callback) -> exec('systemctl start openvpn@client', callback)
|
|
|
|
(callback) -> exec('systemctl enable openvpn@client', callback)
|
|
|
|
]
|
|
|
|
|
2013-07-18 20:34:00 +00:00
|
|
|
stage2Tasks = [
|
|
|
|
(callback) -> process.chdir("#{HOME_PATH}/hakiapp")
|
|
|
|
(callback) -> exec('npm install', callback)
|
|
|
|
(callback) -> exec('foreman start', callback)
|
|
|
|
]
|
|
|
|
|
2013-07-18 11:07:39 +00:00
|
|
|
async.series(stage1Tasks, ->
|
|
|
|
console.log('Bootstrapped')
|
2013-07-18 20:34:00 +00:00
|
|
|
|
|
|
|
async.doUntil(
|
|
|
|
-> fs.existsSync('hakiapp')
|
|
|
|
(callback) ->
|
|
|
|
process.chdir(HOME_PATH)
|
|
|
|
console.log('git clone')
|
|
|
|
exec("git clone #{state.giturl}")
|
|
|
|
setTimeout(callback, 1000)
|
|
|
|
(error) ->
|
|
|
|
if error?
|
|
|
|
console.error(error)
|
|
|
|
else
|
|
|
|
console.log('Initialized')
|
|
|
|
)
|