Lots of logging and some bug fixing. Moved client.conf to client.conf.template

This commit is contained in:
Petros Angelatos 2013-07-21 02:30:12 +01:00
parent eee7df97eb
commit b7504843e9
3 changed files with 51 additions and 11 deletions

View File

@ -1,27 +1,56 @@
fs = require('fs')
express = require('express') express = require('express')
async = require('async') async = require('async')
bootstrap = require('./bootstrap') bootstrap = require('./bootstrap')
state = require('./state') state = require('./state')
settings = require('./settings') settings = require('./settings')
Application = require('./application')
console.log('Supervisor started..')
hakiApp = null
tasks = [ tasks = [
(callback) -> (callback) ->
if state.get('virgin') if state.get('virgin')
console.log('Device is virgin. Boostrapping')
handler = (error) -> handler = (error) ->
if error if error
setTimeout(-> bootstrap(handler), 10000) console.log('Bootstrapping failed with error', error)
console.log('Trying again in 10s')
setTimeout((-> bootstrap(handler)), 10000)
else else
state.set('virgin', false) state.set('virgin', false)
callback() callback()
bootstrap(handler) bootstrap(handler)
else else
console.log("Device isn't a virgin")
callback() callback()
# (callback) -> (callback) ->
hakiApp = new Application(state.get('gitUrl'), '/home/haki/hakiapp', 'haki')
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)
] ]
async.series(tasks, (error) -> async.series(tasks, (error) ->
if error if error
console.error(error) console.error(error)
else
console.log('Everything is fine :)')
) )
app = express() app = express()

View File

@ -1,16 +1,22 @@
settings = require('./settings') settings = require('./settings')
state = require('./state') state = require('./state')
{exec} = require('child_process') {exec} = require('child_process')
async = require('async')
request = require('request')
fs = require('fs')
bootstrapTasks = [ bootstrapTasks = [
# get config from extra partition # get config from extra partition
(callback) -> (callback) ->
console.log('Reading the user conf file')
try try
callback(null, require('/mnt/config.json')) callback(null, require('/mnt/config.json'))
catch error catch error
callback(error) callback(error)
# bootstrapping # bootstrapping
(config, callback) -> (config, callback) ->
console.log('Got user', config.username)
console.log('Posting to the API')
request.post("#{settings.API_ENDPOINT}/associate", { request.post("#{settings.API_ENDPOINT}/associate", {
json: json:
user: config.id user: config.id
@ -18,14 +24,14 @@ bootstrapTasks = [
if error or response.statusCode is 404 if error or response.statusCode is 404
return callback('Error associating with user') return callback('Error associating with user')
state.virgin = false state.set('virgin', false)
state.uuid = body.uuid state.set('uuid', body.uuid)
state.gitUrl = body.gitUrl state.set('gitUrl', body.gitUrl)
state.sync()
vpnConf = fs.readFileSync('/etc/openvpn/client.conf.template', 'utf8') vpnConf = fs.readFileSync('/etc/openvpn/client.conf.template', 'utf8')
vpnConf += "remote #{body.vpnhost} #{body.vpnport}") vpnConf += "remote #{body.vpnhost} #{body.vpnport}\n"
console.log('Configuring VPN')
fs.writeFileSync('/etc/openvpn/ca.crt', body.ca) fs.writeFileSync('/etc/openvpn/ca.crt', body.ca)
fs.writeFileSync('/etc/openvpn/client.crt', body.cert) fs.writeFileSync('/etc/openvpn/client.crt', body.cert)
fs.writeFileSync('/etc/openvpn/client.key', body.key) fs.writeFileSync('/etc/openvpn/client.key', body.key)
@ -33,8 +39,13 @@ bootstrapTasks = [
callback(null) callback(null)
) )
(callback) -> exec('systemctl start openvpn@client', callback) (callback) ->
(callback) -> exec('systemctl enable openvpn@client', callback) console.log('Starting VPN client..')
exec('systemctl start openvpn@client', callback)
(callback) ->
console.log('Enabling VPN client..')
exec('systemctl enable openvpn@client', callback)
] ]
module.exports = (callback) -> module.exports = (callback) ->
async.waterfall(bootstrapTasks, callback)

View File

@ -30,8 +30,8 @@ systemctl start haki
echo "/dev/mmcblk0p3 /mnt ext3 defaults 0 0" >> /etc/fstab echo "/dev/mmcblk0p3 /mnt ext3 defaults 0 0" >> /etc/fstab
mount /mnt mount /mnt
# initialize /etc/openvpn/client.conf # initialize /etc/openvpn/client.conf.template
sed -e 's,proto udp,;proto udp,' -e 's,;proto tcp,proto tcp,' -e 's,^remote.*,,' /usr/share/openvpn/examples/client.conf > /etc/openvpn/client.conf sed -e 's,proto udp,;proto udp,' -e 's,;proto tcp,proto tcp,' -e 's,^remote.*,,' /usr/share/openvpn/examples/client.conf > /etc/openvpn/client.conf.template
# ssh configuration # ssh configuration
if [ ! -d /home/haki/.ssh ] ; then sudo -u haki mkdir /home/haki/.ssh ; fi if [ ! -d /home/haki/.ssh ] ; then sudo -u haki mkdir /home/haki/.ssh ; fi