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')
async = require('async')
bootstrap = require('./bootstrap')
state = require('./state')
settings = require('./settings')
Application = require('./application')
console.log('Supervisor started..')
hakiApp = null
tasks = [
(callback) ->
if state.get('virgin')
console.log('Device is virgin. Boostrapping')
handler = (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
state.set('virgin', false)
callback()
bootstrap(handler)
else
console.log("Device isn't a virgin")
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) ->
if error
console.error(error)
else
console.log('Everything is fine :)')
)
app = express()

View File

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

View File

@ -30,8 +30,8 @@ systemctl start haki
echo "/dev/mmcblk0p3 /mnt ext3 defaults 0 0" >> /etc/fstab
mount /mnt
# initialize /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
# 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.template
# ssh configuration
if [ ! -d /home/haki/.ssh ] ; then sudo -u haki mkdir /home/haki/.ssh ; fi