scaffold: expanded v2 config checks

This commit is contained in:
Braydon Fuller 2016-05-23 11:27:54 -04:00
parent 73197fdc75
commit cd9bbc8661
2 changed files with 74 additions and 6 deletions

View File

@ -10,6 +10,48 @@ var shuttingDown = false;
log.debug = function() {}; log.debug = function() {};
/**
* Checks for configuration options from version 2. This includes an "address" and
* "db" service, or having "datadir" at the root of the config.
*/
function checkConfigVersion2(fullConfig) {
var datadirUndefined = _.isUndefined(fullConfig.datadir);
var addressDefined = (fullConfig.services.indexOf('address') >= 0);
var dbDefined = (fullConfig.services.indexOf('db') >= 0);
if (!datadirUndefined || addressDefined || dbDefined) {
console.warn('\nConfiguration file is not compatible with this version. \n' +
'A reindex for bitcoind is necessary for this upgrade with bitcoin.conf option "reindex=1". \n' +
'There are changes necessary in both bitcoin.conf and bitcore-node.json.' +
'To upgrade please see the details below and documentation at: \n' +
'https://github.com/bitpay/bitcore-node/blob/bitcoind/docs/upgrade.md \n');
if (!datadirUndefined) {
console.warn('Please remove "datadir" and add it to the config at ' + fullConfig.path + ' with:');
var missingConfig = {
servicesConfig: {
bitcoind: {
spawn: {
datadir: fullConfig.datadir,
exec: path.resolve(__dirname, '../../bin/bitcoind')
}
}
}
};
console.warn(JSON.stringify(missingConfig, null, 2) + '\n');
}
if (addressDefined || dbDefined) {
console.warn('Please remove "address" and/or "db" from "services" in: ' + fullConfig.path + '\n');
}
return true;
}
return false;
}
/** /**
* This function will instantiate and start a Node, requiring the necessary service * This function will instantiate and start a Node, requiring the necessary service
* modules, and registering event handlers. * modules, and registering event handlers.
@ -37,12 +79,8 @@ function start(options) {
fullConfig.path = path.resolve(options.path, './bitcore-node.json'); fullConfig.path = path.resolve(options.path, './bitcore-node.json');
if (fullConfig.datadir) { if (checkConfigVersion2(fullConfig)) {
throw new TypeError( process.exit(1);
'Configuration file (' + fullConfig.path + ') is not compatible with this version.' +
' Please see https://github.com/bitpay/bitcore-node/blob/bitcoind/docs/upgrade.md' +
' for upgrade details.'
);
} }
fullConfig.services = start.setupServices(require, servicesPath, options.config); fullConfig.services = start.setupServices(require, servicesPath, options.config);
@ -215,3 +253,4 @@ module.exports.registerExitHandlers = registerExitHandlers;
module.exports.exitHandler = exitHandler; module.exports.exitHandler = exitHandler;
module.exports.setupServices = setupServices; module.exports.setupServices = setupServices;
module.exports.cleanShutdown = cleanShutdown; module.exports.cleanShutdown = cleanShutdown;
module.exports.checkConfigVersion2 = checkConfigVersion2;

View File

@ -8,6 +8,35 @@ var proxyquire = require('proxyquire');
var start = require('../../lib/scaffold/start'); var start = require('../../lib/scaffold/start');
describe('#start', function() { describe('#start', function() {
describe('#checkConfigVersion2', function() {
var sandbox = sinon.sandbox.create();
beforeEach(function() {
sandbox.stub(console, 'warn');
});
afterEach(function() {
sandbox.restore();
});
it('will give true with "datadir" at root', function() {
var checkConfigVersion2 = proxyquire('../../lib/scaffold/start', {}).checkConfigVersion2;
var v2 = checkConfigVersion2({datadir: '/home/user/.bitcore/data', services: []});
v2.should.equal(true);
});
it('will give true with "address" service enabled', function() {
var checkConfigVersion2 = proxyquire('../../lib/scaffold/start', {}).checkConfigVersion2;
var v2 = checkConfigVersion2({services: ['address']});
v2.should.equal(true);
});
it('will give true with "db" service enabled', function() {
var checkConfigVersion2 = proxyquire('../../lib/scaffold/start', {}).checkConfigVersion2;
var v2 = checkConfigVersion2({services: ['db']});
v2.should.equal(true);
});
it('will give false without "datadir" at root and "address", "db" services disabled', function() {
var checkConfigVersion2 = proxyquire('../../lib/scaffold/start', {}).checkConfigVersion2;
var v2 = checkConfigVersion2({services: []});
v2.should.equal(false);
});
});
describe('#setupServices', function() { describe('#setupServices', function() {
var cwd = process.cwd(); var cwd = process.cwd();
var setupServices = proxyquire('../../lib/scaffold/start', {}).setupServices; var setupServices = proxyquire('../../lib/scaffold/start', {}).setupServices;