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'
|
|
|
|
|
|
|
|
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 11:07:39 +00:00
|
|
|
# bootstraping
|
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
|
|
|
|
body = JSON.parse(body)
|
|
|
|
catch error
|
|
|
|
callback(error)
|
|
|
|
|
|
|
|
state.virgin = false
|
|
|
|
state.uuid = body.uuid
|
|
|
|
|
|
|
|
fs.writeFileSync('state.json', JSON.strigify(state))
|
|
|
|
|
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)
|
|
|
|
]
|
|
|
|
|
|
|
|
async.series(stage1Tasks, ->
|
|
|
|
console.log('Bootstrapped')
|
|
|
|
)
|