Commit 99bb08b4 authored by Cédric Moreau's avatar Cédric Moreau

Fix: 'sync' command no more require configuration to exist

parent cf1d5840
...@@ -21,7 +21,6 @@ function keys (val) { ...@@ -21,7 +21,6 @@ function keys (val) {
} }
// Constants // Constants
var LISTEN_HTTP = true;
var DO_NOT_LISTEN_HTTP = false; var DO_NOT_LISTEN_HTTP = false;
program program
...@@ -64,7 +63,7 @@ program ...@@ -64,7 +63,7 @@ program
type: "console" type: "console"
}] }]
}); });
service(DO_NOT_LISTEN_HTTP, ucoin.createPeerServer, function (server, conf) { service(ucoin.createPeerServer, function (server, conf) {
// Only show message "Saved" // Only show message "Saved"
require('log4js').configure({ require('log4js').configure({
"appenders": [{ "appenders": [{
...@@ -73,6 +72,9 @@ program ...@@ -73,6 +72,9 @@ program
}] }]
}); });
async.waterfall([ async.waterfall([
function (next){
server.start(DO_NOT_LISTEN_HTTP, next);
},
function (next){ function (next){
server.on('peerInited', next); server.on('peerInited', next);
}, },
...@@ -127,7 +129,7 @@ program ...@@ -127,7 +129,7 @@ program
}, },
function (next) { function (next) {
// Check config // Check config
service(DO_NOT_LISTEN_HTTP, ucoin.createPeerServer, function (key, server, conf) { service(ucoin.createPeerServer, function (key, server, conf) {
next(); next();
})(null, null); })(null, null);
} }
...@@ -142,15 +144,12 @@ program ...@@ -142,15 +144,12 @@ program
program program
.command('sync [host] [port]') .command('sync [host] [port]')
.description('Tries to synchronise data with remote uCoin node') .description('Tries to synchronise data with remote uCoin node')
.action(service(DO_NOT_LISTEN_HTTP, ucoin.createPeerServer, function (host, port, server, conf) { .action(service(ucoin.createPeerServer, function (host, port, server, conf) {
// Stop keyblock generation // Stop keyblock generation
conf.participate = false; conf.participate = false;
async.series([ async.series([
function (next) {
server.once('peerInited', next);
},
function (next){ function (next){
// Synchronize // Synchronize
var Synchroniser = require('../app/lib/sync'); var Synchroniser = require('../app/lib/sync');
...@@ -169,7 +168,7 @@ program ...@@ -169,7 +168,7 @@ program
program program
.command('allow-key [key]') .command('allow-key [key]')
.description('Add given key to authorized keys of this node') .description('Add given key to authorized keys of this node')
.action(service(DO_NOT_LISTEN_HTTP, ucoin.createHDCServer, function (key, server, conf) { .action(service(ucoin.createWOTServer, function (key, server, conf) {
key = key || ""; key = key || "";
key = key.toUpperCase(); key = key.toUpperCase();
async.waterfall([ async.waterfall([
...@@ -199,14 +198,14 @@ program ...@@ -199,14 +198,14 @@ program
program program
.command('manage-key [key]') .command('manage-key [key]')
.description('Add given key to stack of managed keys of this node') .description('Add given key to stack of managed keys of this node')
.action(service(DO_NOT_LISTEN_HTTP, ucoin.createHDCServer, function (key, server, conf) { .action(service(ucoin.createWOTServer, function (key, server, conf) {
handleKey(server, key, true, 'Key %s is now managed'); handleKey(server, key, true, 'Key %s is now managed');
})); }));
program program
.command('forget-key [key]') .command('forget-key [key]')
.description('Remove given key of the managed keys\' stack of this node') .description('Remove given key of the managed keys\' stack of this node')
.action(service(DO_NOT_LISTEN_HTTP, ucoin.createHDCServer, function (key, server, conf) { .action(service(ucoin.createWOTServer, function (key, server, conf) {
handleKey(server, key, false, 'Key %s no more managed from now'); handleKey(server, key, false, 'Key %s no more managed from now');
})); }));
...@@ -235,32 +234,32 @@ function handleKey (server, key, isManaged, message) { ...@@ -235,32 +234,32 @@ function handleKey (server, key, isManaged, message) {
program program
.command('gen-next [host] [port] [difficulty]') .command('gen-next [host] [port] [difficulty]')
.description('Tries to generate the next keyblock of the keychain, with both updates & newcomers') .description('Tries to generate the next keyblock of the keychain, with both updates & newcomers')
.action(service(DO_NOT_LISTEN_HTTP, ucoin.createWOTServer, generateAndSend("generateNext"))); .action(service(ucoin.createWOTServer, generateAndSend("generateNext")));
program program
.command('gen-empty-next [host] [port] [difficulty]') .command('gen-empty-next [host] [port] [difficulty]')
.description('Tries to generate the next keyblock of the keychain without any changes') .description('Tries to generate the next keyblock of the keychain without any changes')
.action(service(DO_NOT_LISTEN_HTTP, ucoin.createWOTServer, generateAndSend("generateEmptyNext"))); .action(service(ucoin.createWOTServer, generateAndSend("generateEmptyNext")));
program program
.command('gen-updates [host] [port] [difficulty]') .command('gen-updates [host] [port] [difficulty]')
.description('Tries to generate an update (#1+) keyblock, containing only update changes') .description('Tries to generate an update (#1+) keyblock, containing only update changes')
.action(service(DO_NOT_LISTEN_HTTP, ucoin.createWOTServer, generateAndSend("generateUpdates"))); .action(service(ucoin.createWOTServer, generateAndSend("generateUpdates")));
program program
.command('gen-newcomers [host] [port] [difficulty]') .command('gen-newcomers [host] [port] [difficulty]')
.description('Tries to generate a newcomers (#0+) keyblock, containing only newcomers changes') .description('Tries to generate a newcomers (#0+) keyblock, containing only newcomers changes')
.action(service(DO_NOT_LISTEN_HTTP, ucoin.createWOTServer, generateAndSend("generateNewcomers"))); .action(service(ucoin.createWOTServer, generateAndSend("generateNewcomers")));
program program
.command('gen-newcomers-auto [host] [port] [difficulty]') .command('gen-newcomers-auto [host] [port] [difficulty]')
.description('Tries to generate a newcomers (#0+) keyblock, containing only newcomers changes') .description('Tries to generate a newcomers (#0+) keyblock, containing only newcomers changes')
.action(service(DO_NOT_LISTEN_HTTP, ucoin.createWOTServer, generateAndSend("generateNewcomersAuto"))); .action(service(ucoin.createWOTServer, generateAndSend("generateNewcomersAuto")));
program program
.command('compute-distances') .command('compute-distances')
.description('Recompute distance between each member and the whole WoT and mark outdistanced ones as kicked') .description('Recompute distance between each member and the whole WoT and mark outdistanced ones as kicked')
.action(service(DO_NOT_LISTEN_HTTP, ucoin.createWOTServer, function (server, conf) { .action(service(ucoin.createWOTServer, function (server, conf) {
server.KeychainService.computeDistances(function (err) { server.KeychainService.computeDistances(function (err) {
if (err) logger.error(err); if (err) logger.error(err);
server.disconnect(); server.disconnect();
...@@ -271,7 +270,7 @@ program ...@@ -271,7 +270,7 @@ program
program program
.command('show-keychain') .command('show-keychain')
.description('Recompute distance between each member and the whole WoT and mark outdistanced ones as kicked') .description('Recompute distance between each member and the whole WoT and mark outdistanced ones as kicked')
.action(service(DO_NOT_LISTEN_HTTP, ucoin.createWOTServer, function (server, conf) { .action(service(ucoin.createWOTServer, function (server, conf) {
server.KeychainService.showKeychain(function (err) { server.KeychainService.showKeychain(function (err) {
if (err) logger.error(err); if (err) logger.error(err);
server.disconnect(); server.disconnect();
...@@ -282,7 +281,7 @@ program ...@@ -282,7 +281,7 @@ program
program program
.command('compute-certifications') .command('compute-certifications')
.description('Recompute available certifications for next blocks') .description('Recompute available certifications for next blocks')
.action(service(DO_NOT_LISTEN_HTTP, ucoin.createWOTServer, function (server, conf) { .action(service(ucoin.createWOTServer, function (server, conf) {
server.KeychainService.updateCertifications(function (err) { server.KeychainService.updateCertifications(function (err) {
if (err) logger.error(err); if (err) logger.error(err);
server.disconnect(); server.disconnect();
...@@ -360,7 +359,7 @@ program ...@@ -360,7 +359,7 @@ program
program program
.command('config') .command('config')
.description('Register configuration in database') .description('Register configuration in database')
.action(service(false, function (server, conf) { .action(service(function (server, conf) {
conf.save(function (err) { conf.save(function (err) {
if(err){ if(err){
logger.error("Configuration could not be saved: " + err); logger.error("Configuration could not be saved: " + err);
...@@ -408,17 +407,17 @@ program ...@@ -408,17 +407,17 @@ program
program program
.command('start') .command('start')
.description('Start uCoin server using given --currency') .description('Start uCoin server using given --currency')
.action(service(LISTEN_HTTP, ucoin.createPeerServer, function (server, conf) { .action(service(ucoin.createPeerServer, function (server, conf) {
server server
.pipe(router(server.PeeringService.cert.fingerprint, server.conn)) .pipe(router(server.PeeringService.cert.fingerprint, server.conn))
.pipe(multicaster()); .pipe(multicaster());
// Launching server // Launching server
server.on('BMALoaded', function (err, app) { server.start(function (err, app) {
if(err){ if(err){
console.error(err); logger.error(err);
this.disconnect(); server.disconnect();
process.exit(); process.exit();
return; return;
} }
...@@ -523,20 +522,16 @@ function connect(callback) { ...@@ -523,20 +522,16 @@ function connect(callback) {
}; };
} }
function service(listenHTTP, serverFactory, callback) { function service(serverFactory, callback) {
if (arguments.length == 1) { if (arguments.length == 1) {
callback = listenHTTP;
listenHTTP = false;
serverFactory = ucoin.createWOTServer;
} else if (arguments.length == 2) {
callback = serverFactory; callback = serverFactory;
serverFactory = ucoin.createHDCServer; serverFactory = ucoin.createWOTServer;
} }
return function () { return function () {
var cbArgs = arguments; var cbArgs = arguments;
var dbName = program.mdb || "ucoin_default"; var dbName = program.mdb || "ucoin_default";
var server = serverFactory({ name: dbName, host: program.mhost, port: program.mport, listenBMA: listenHTTP }, overrideConf({})); var server = serverFactory({ name: dbName, host: program.mhost, port: program.mport }, overrideConf({}));
// Connecting to DB // Connecting to DB
server.on('services', function (err) { server.on('services', function (err) {
......
...@@ -91,15 +91,7 @@ function PeerServer (dbConf, overrideConf, interceptors, onInit) { ...@@ -91,15 +91,7 @@ function PeerServer (dbConf, overrideConf, interceptors, onInit) {
} }
]; ];
var initFunctions = [ var initFunctions = onInit || [];
function (done) {
that.initPeer(that.conn, that.conf, done);
},
function (done) {
that.emit('peerInited');
done();
}
].concat(onInit || []);
WOT.call(this, dbConf, overrideConf, selfInterceptors.concat(interceptors || []), initFunctions); WOT.call(this, dbConf, overrideConf, selfInterceptors.concat(interceptors || []), initFunctions);
...@@ -130,12 +122,18 @@ function PeerServer (dbConf, overrideConf, interceptors, onInit) { ...@@ -130,12 +122,18 @@ function PeerServer (dbConf, overrideConf, interceptors, onInit) {
}, function (err) { }, function (err) {
next(err); next(err);
}); });
}
], done);
};
this._start = function (done) {
async.waterfall([
function (next) {
that.initPeer(that.conn, that.conf, next);
}, },
function (next){ function (next) {
that.checkConfig(next); that.emit('peerInited');
}, next();
function (next){
that.createSignFunction(that.conf, next);
} }
], done); ], done);
}; };
...@@ -189,6 +187,12 @@ function PeerServer (dbConf, overrideConf, interceptors, onInit) { ...@@ -189,6 +187,12 @@ function PeerServer (dbConf, overrideConf, interceptors, onInit) {
this.initPeer = function (conn, conf, done) { this.initPeer = function (conn, conf, done) {
async.waterfall([ async.waterfall([
function (next){
that.checkConfig(next);
},
function (next){
that.createSignFunction(that.conf, next);
},
function (next){ function (next){
// Add selfkey as managed // Add selfkey as managed
conn.model('Key').setManaged(that.PeeringService.cert.fingerprint, true, next); conn.model('Key').setManaged(that.PeeringService.cert.fingerprint, true, next);
......
...@@ -35,14 +35,6 @@ function Server (dbConf, overrideConf, interceptors, onInit) { ...@@ -35,14 +35,6 @@ function Server (dbConf, overrideConf, interceptors, onInit) {
that.emit('services', err); that.emit('services', err);
done(err); done(err);
}); });
},
function (done) {
if (dbConf.listenBMA) {
listenBMA(function (err, app) {
that.emit('BMALoaded', err, app);
done();
});
} else done();
} }
]; ];
...@@ -82,10 +74,9 @@ function Server (dbConf, overrideConf, interceptors, onInit) { ...@@ -82,10 +74,9 @@ function Server (dbConf, overrideConf, interceptors, onInit) {
], function (err, res) { ], function (err, res) {
if (err){ if (err){
switch (err) { switch (err) {
case constants.ERROR.PUBKEY.ALREADY_UPDATED: msg = 'Key already up-to-date'; case constants.ERROR.PUBKEY.ALREADY_UPDATED: err = 'Key already up-to-date'; break;
default: msg = err.toString();
} }
logger.debug(msg); logger.debug(err);
} }
if (res != null && res != undefined) { if (res != null && res != undefined) {
that.push(res); that.push(res);
...@@ -160,6 +151,33 @@ function Server (dbConf, overrideConf, interceptors, onInit) { ...@@ -160,6 +151,33 @@ function Server (dbConf, overrideConf, interceptors, onInit) {
} }
}; };
this.start = function (doListenHTTP, done) {
if (arguments.length == 1) {
done = doListenHTTP;
doListenHTTP = true;
}
async.waterfall([
function (next){
that._start(next);
},
function (next) {
if (!doListenHTTP) {
next();
return;
}
listenBMA(function (err, app) {
that.emit('BMALoaded', err, app);
next();
});
}
], done);
};
this._start = function (done) {
// Method to override
done();
};
this.reset = function(done) { this.reset = function(done) {
async.waterfall([ async.waterfall([
function (next){ function (next){
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment