|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"use strict"; |
|
|
|
|
|
var cwd = process.cwd(); |
|
|
|
function usage() { |
|
console.log('ERROR -- missing config file'); |
|
console.log('Usage: node standalone.norunner.js'); |
|
process.exit(1); |
|
} |
|
|
|
var configMaster = require(cwd + '/configs/bot').config; |
|
if (configMaster === undefined) { |
|
console.log('ERROR Invalid master configuration file', process.argv[2]); |
|
process.exit(2); |
|
} |
|
|
|
var NODE_EXIT_MASTERDISCONNECT = 3; |
|
|
|
var nodeCiBot = require(cwd + '/src/bot'); |
|
var master = require(cwd + '/src/master'); |
|
var service = require(cwd + '/src/service'); |
|
var masterNetRunner = require(cwd + '/src/masterNetRunner.ws'); |
|
var runners = {}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var wsActions = {}; |
|
|
|
function register(action, handler) { |
|
wsActions[action] = handler; |
|
} |
|
|
|
function wsSend(runnerId, action, params) { |
|
console.log('wsSend', runnerId, action, params) |
|
runners[runnerId].ws.send(JSON.stringify({ |
|
action: action, |
|
params: params |
|
})); |
|
} |
|
|
|
|
|
|
|
register('runnerConfig', function(ws, config) { |
|
console.log('RUNNER node connected:)'); |
|
if (isSystemRunning()) { |
|
console.log('System is already running'); |
|
wsSend(ws, 'connectionRefused'); |
|
return; |
|
} |
|
|
|
ws.name = config.name; |
|
ws.runner = true; |
|
runners[config.name] = { |
|
config: config, |
|
instance: masterNetRunner, |
|
ws: ws |
|
}; |
|
isSystemReady(); |
|
}); |
|
|
|
register('runnerRunJobEnd', function(ws, msg) { |
|
master.runnerRunJobEnd(msg); |
|
}); |
|
|
|
register('runnerSyncProjectEnd', function(ws, state) { |
|
master.runnerSyncProjectEnd(state); |
|
}); |
|
|
|
function isSystemRunning() { |
|
if (Object.keys(runners).length === 1) |
|
return true; |
|
return false; |
|
} |
|
|
|
function isSystemReady() { |
|
if (Object.keys(runners).length === 1) { |
|
console.log() |
|
var err = masterNetRunner.setConfig({ |
|
wsSend: wsSend, |
|
runners: runners |
|
}); |
|
if (err) { |
|
console.log('Unable to set masterNetRunner config - ', err); |
|
process.exit(1); |
|
} |
|
err = service.setConfig({ |
|
master: master |
|
}); |
|
if (err) { |
|
console.log('Unable to set service config - ', err); |
|
process.exit(1); |
|
} |
|
err = master.setConfig({ |
|
master: master, |
|
runners: runners, |
|
user: nodeCiBot, |
|
service: service |
|
}); |
|
if (err) { |
|
console.log('Unable to set masterconfig - ', err); |
|
process.exit(1); |
|
} |
|
err = nodeCiBot.setConfig({ |
|
master: master, |
|
configWeb: configMaster |
|
}); |
|
if (err) { |
|
console.log('Unable to set nodeCiBot - ', err); |
|
process.exit(1); |
|
} |
|
} |
|
} |
|
|
|
var WebSocketServer = require('ws').Server; |
|
var wss = new WebSocketServer({ |
|
port: configMaster.port |
|
}); |
|
|
|
wss.on('connection', function connection(ws) { |
|
console.log('New connection received'); |
|
ws.on('message', function(msg, flags) { |
|
var data = JSON.parse(msg); |
|
if (wsActions[data.action] === undefined) { |
|
console.log('received ', data, ' unknown action'); |
|
return; |
|
} |
|
wsActions[data.action](ws, data.params); |
|
}); |
|
ws.on('close', function close() { |
|
console.log('One Runner disconnected !', ws.name, ws.runner); |
|
}); |
|
}); |
|
|