initializing the git repo...

This commit is contained in:
Spyros Ligouras 2013-07-19 02:31:42 +03:00
parent 6ef6a77036
commit 00f121371b

View File

@ -1,10 +1,11 @@
fs = require('fs') fs = require('fs')
async = require('async') async = require('async')
request = require('request') request = require('request')
getuid = require('getuid')
{exec} = require('child_process') {exec} = require('child_process')
API_ENDPOINT = 'http://paras.rulemotion.com:1337' API_ENDPOINT = 'http://paras.rulemotion.com:1337'
HOME_PATH = '/home/haki' HAKI_PATH = '/home/haki'
try try
state = require('./state.json') state = require('./state.json')
@ -22,23 +23,21 @@ bootstrapTasks = [
# bootstrapping # bootstrapping
(config, callback) -> (config, callback) ->
request.post("#{API_ENDPOINT}/associate", { request.post("#{API_ENDPOINT}/associate", {
user: config.id json:
user: config.id
}, (error, response, body) -> }, (error, response, body) ->
if error if error
return callback(error) return callback(error)
try if typeof body isnt 'object'
if typeof body isnt 'object' callback(body)
throw new Error(body)
body = JSON.parse(body)
catch error
callback(error)
state.virgin = false state.virgin = false
state.uuid = body.uuid state.uuid = body.uuid
state.giturl = body.giturl state.giturl = body.giturl
console.log state
fs.writeFileSync('state.json', JSON.stringify(state)) fs.writeFileSync('state.json', JSON.stringify(state))
fs.writeFileSync('/etc/openvpn/ca.crt', body.ca) fs.writeFileSync('/etc/openvpn/ca.crt', body.ca)
@ -54,27 +53,31 @@ stage1Tasks = [
(callback) -> async.waterfall(bootstrapTasks, callback) (callback) -> async.waterfall(bootstrapTasks, callback)
(callback) -> exec('systemctl start openvpn@client', callback) (callback) -> exec('systemctl start openvpn@client', callback)
(callback) -> exec('systemctl enable openvpn@client', callback) (callback) -> exec('systemctl enable openvpn@client', callback)
(callback) ->
process.setuid(getuid('haki'))
process.chdir(HAKI_PATH)
fs.mkdir('hakiapp', callback)
(callback) -> exec('git init', cwd: 'hakiapp', callback)
(callback) -> exec("git remote add origin #{state.giturl}", cwd: 'hakiapp', callback)
] ]
stage2Tasks = [ stage2Tasks = [
(callback) -> process.chdir("#{HOME_PATH}/hakiapp") (callback) ->
(callback) -> exec('npm install', callback) process.setuid(getuid('haki'))
(callback) -> exec('foreman start', callback) process.chdir(HAKI_PATH)
fs.mkdir('hakiapp', callback)
(callback) -> async.forever([
(callback) -> exec('git pull', cwd: 'hakiapp', callback)
(callback) -> exec('npm install', cwd: 'hakiapp', callback)
(callback) -> exec('foreman start', cwd: 'hakiapp', callback)
])
] ]
async.series(stage1Tasks, ->
console.log('Bootstrapped')
async.doUntil( if state.virgin
-> fs.existsSync('hakiapp') async.series(stage1Tasks, (error, results) ->
(callback) -> if (error)
process.chdir(HOME_PATH) console.error(error)
console.log('git clone') else
exec("git clone #{state.giturl}") console.log('Bootstrapped')
setTimeout(callback, 1000)
(error) ->
if error?
console.error(error)
else
console.log('Initialized')
) )