mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-31 19:15:58 +01:00
closes #3938
This commit is contained in:
@@ -1,26 +1,56 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
var async = require('async'),
|
var async = require('async');
|
||||||
prompt = require('prompt'),
|
var prompt = require('prompt');
|
||||||
nconf = require('nconf'),
|
var winston = require('winston');
|
||||||
winston = require('winston'),
|
|
||||||
|
|
||||||
questions = {};
|
var questions = {
|
||||||
|
redis: require('../src/database/redis').questions,
|
||||||
|
mongo: require('../src/database/mongo').questions
|
||||||
|
};
|
||||||
|
|
||||||
function success(err, config, callback) {
|
module.exports = function(config, callback) {
|
||||||
|
async.waterfall([
|
||||||
|
function (next) {
|
||||||
|
process.stdout.write('\n');
|
||||||
|
winston.info('Now configuring ' + config.database + ' database:');
|
||||||
|
getDatabaseConfig(config, next);
|
||||||
|
},
|
||||||
|
function (databaseConfig, next) {
|
||||||
|
saveDatabaseConfig(config, databaseConfig, next);
|
||||||
|
}
|
||||||
|
], callback);
|
||||||
|
};
|
||||||
|
|
||||||
|
function getDatabaseConfig(config, callback) {
|
||||||
if (!config) {
|
if (!config) {
|
||||||
return callback(new Error('aborted'));
|
return callback(new Error('aborted'));
|
||||||
}
|
}
|
||||||
|
|
||||||
var database = (config.redis || config.mongo) ? config.secondary_database : config.database;
|
if (config.database === 'redis') {
|
||||||
|
if (config['redis:host'] && config['redis:port']) {
|
||||||
|
callback(null, config);
|
||||||
|
} else {
|
||||||
|
prompt.get(questions.redis, callback);
|
||||||
|
}
|
||||||
|
} else if (config.database === 'mongo') {
|
||||||
|
if (config['mongo:host'] && config['mongo:port']) {
|
||||||
|
callback(null, config);
|
||||||
|
} else {
|
||||||
|
prompt.get(questions.mongo, callback);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return callback(new Error('unknown database : ' + config.database));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function dbQuestionsSuccess(err, databaseConfig) {
|
function saveDatabaseConfig(config, databaseConfig, callback) {
|
||||||
if (!databaseConfig) {
|
if (!databaseConfig) {
|
||||||
return callback(new Error('aborted'));
|
return callback(new Error('aborted'));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Translate redis properties into redis object
|
// Translate redis properties into redis object
|
||||||
if(database === 'redis') {
|
if (config.database === 'redis') {
|
||||||
config.redis = {
|
config.redis = {
|
||||||
host: databaseConfig['redis:host'],
|
host: databaseConfig['redis:host'],
|
||||||
port: databaseConfig['redis:port'],
|
port: databaseConfig['redis:port'],
|
||||||
@@ -31,7 +61,7 @@ function success(err, config, callback) {
|
|||||||
if (config.redis.host.slice(0, 1) === '/') {
|
if (config.redis.host.slice(0, 1) === '/') {
|
||||||
delete config.redis.port;
|
delete config.redis.port;
|
||||||
}
|
}
|
||||||
} else if (database === 'mongo') {
|
} else if (config.database === 'mongo') {
|
||||||
config.mongo = {
|
config.mongo = {
|
||||||
host: databaseConfig['mongo:host'],
|
host: databaseConfig['mongo:host'],
|
||||||
port: databaseConfig['mongo:port'],
|
port: databaseConfig['mongo:port'],
|
||||||
@@ -40,7 +70,7 @@ function success(err, config, callback) {
|
|||||||
database: databaseConfig['mongo:database']
|
database: databaseConfig['mongo:database']
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
return callback(new Error('unknown database : ' + database));
|
return callback(new Error('unknown database : ' + config.database));
|
||||||
}
|
}
|
||||||
|
|
||||||
var allQuestions = questions.redis.concat(questions.mongo);
|
var allQuestions = questions.redis.concat(questions.mongo);
|
||||||
@@ -48,57 +78,5 @@ function success(err, config, callback) {
|
|||||||
delete config[allQuestions[x].name];
|
delete config[allQuestions[x].name];
|
||||||
}
|
}
|
||||||
|
|
||||||
callback(err, config);
|
callback(null, config);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(database === 'redis') {
|
|
||||||
if (config['redis:host'] && config['redis:port']) {
|
|
||||||
dbQuestionsSuccess(null, config);
|
|
||||||
} else {
|
|
||||||
prompt.get(questions.redis, dbQuestionsSuccess);
|
|
||||||
}
|
|
||||||
} else if(database === 'mongo') {
|
|
||||||
if (config['mongo:host'] && config['mongo:port']) {
|
|
||||||
dbQuestionsSuccess(null, config);
|
|
||||||
} else {
|
|
||||||
prompt.get(questions.mongo, dbQuestionsSuccess);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return callback(new Error('unknown database : ' + database));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function getSecondaryDatabaseModules(config, next) {
|
|
||||||
prompt.get({
|
|
||||||
"name": "secondary_db_modules",
|
|
||||||
"description": "Which database modules should " + config.secondary_database + " store?",
|
|
||||||
"default": nconf.get('secondary_db_modules') || "hash, list, sets, sorted"
|
|
||||||
}, function(err, db) {
|
|
||||||
config.secondary_db_modules = db.secondary_db_modules;
|
|
||||||
success(err, config, next);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = function(err, config, databases, callback) {
|
|
||||||
var allowedDBs = Object.keys(databases);
|
|
||||||
|
|
||||||
allowedDBs.forEach(function(db) {
|
|
||||||
questions[db] = require('./../src/database/' + db).questions;
|
|
||||||
});
|
|
||||||
|
|
||||||
async.waterfall([
|
|
||||||
function(next) {
|
|
||||||
process.stdout.write('\n');
|
|
||||||
winston.info('Now configuring ' + config.database + ' database:');
|
|
||||||
success(err, config, next);
|
|
||||||
},
|
|
||||||
function(config, next) {
|
|
||||||
if (config.secondary_database && allowedDBs.indexOf(config.secondary_database) !== -1) {
|
|
||||||
winston.info('Now configuring ' + config.secondary_database + ' database:');
|
|
||||||
getSecondaryDatabaseModules(config, next);
|
|
||||||
} else {
|
|
||||||
next(err, config);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
], callback);
|
|
||||||
};
|
|
||||||
@@ -21,7 +21,9 @@
|
|||||||
"compression": "^1.1.0",
|
"compression": "^1.1.0",
|
||||||
"connect-ensure-login": "^0.1.1",
|
"connect-ensure-login": "^0.1.1",
|
||||||
"connect-flash": "^0.1.1",
|
"connect-flash": "^0.1.1",
|
||||||
|
"connect-mongo": "~0.8.2",
|
||||||
"connect-multiparty": "^2.0.0",
|
"connect-multiparty": "^2.0.0",
|
||||||
|
"connect-redis": "~2.0.0",
|
||||||
"cookie-parser": "^1.3.3",
|
"cookie-parser": "^1.3.3",
|
||||||
"cron": "^1.0.5",
|
"cron": "^1.0.5",
|
||||||
"csurf": "^1.6.1",
|
"csurf": "^1.6.1",
|
||||||
@@ -37,6 +39,7 @@
|
|||||||
"mime": "^1.3.4",
|
"mime": "^1.3.4",
|
||||||
"minimist": "^1.1.1",
|
"minimist": "^1.1.1",
|
||||||
"mkdirp": "~0.5.0",
|
"mkdirp": "~0.5.0",
|
||||||
|
"mongodb": "~2.0.0",
|
||||||
"morgan": "^1.3.2",
|
"morgan": "^1.3.2",
|
||||||
"nconf": "~0.8.2",
|
"nconf": "~0.8.2",
|
||||||
"nodebb-plugin-composer-default": "1.0.26",
|
"nodebb-plugin-composer-default": "1.0.26",
|
||||||
@@ -56,6 +59,7 @@
|
|||||||
"passport": "^0.3.0",
|
"passport": "^0.3.0",
|
||||||
"passport-local": "1.0.0",
|
"passport-local": "1.0.0",
|
||||||
"prompt": "^0.2.14",
|
"prompt": "^0.2.14",
|
||||||
|
"redis": "~2.4.2",
|
||||||
"request": "^2.44.0",
|
"request": "^2.44.0",
|
||||||
"rimraf": "~2.4.2",
|
"rimraf": "~2.4.2",
|
||||||
"rss": "^1.0.0",
|
"rss": "^1.0.0",
|
||||||
|
|||||||
@@ -1,56 +1,14 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
var nconf = require('nconf'),
|
var nconf = require('nconf');
|
||||||
primaryDBName = nconf.get('database'),
|
var databaseName = nconf.get('database');
|
||||||
secondaryDBName = nconf.get('secondary_database'),
|
var winston = require('winston');
|
||||||
secondaryModules = nconf.get('secondary_db_modules'),
|
|
||||||
winston = require('winston'),
|
|
||||||
async = require('async'),
|
|
||||||
|
|
||||||
ALLOWED_MODULES = ['hash', 'list', 'sets', 'sorted'];
|
if (!databaseName) {
|
||||||
|
|
||||||
if(!primaryDBName) {
|
|
||||||
winston.info('Database type not set! Run ./nodebb setup');
|
winston.info('Database type not set! Run ./nodebb setup');
|
||||||
process.exit();
|
process.exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
function setupSecondaryDB() {
|
var primaryDB = require('./database/' + databaseName);
|
||||||
var secondaryDB = require('./database/' + secondaryDBName);
|
|
||||||
|
|
||||||
secondaryModules = secondaryModules.split(/,\s*/);
|
|
||||||
|
|
||||||
for (var module in secondaryModules) {
|
|
||||||
if (secondaryModules.hasOwnProperty(module) && ALLOWED_MODULES.indexOf(module) !== -1) {
|
|
||||||
primaryDB[module] = secondaryDB[module];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var primaryDBinit = primaryDB.init,
|
|
||||||
primaryDBclose = primaryDB.close,
|
|
||||||
primaryDBhelpers = primaryDB.helpers;
|
|
||||||
|
|
||||||
primaryDB.init = function(callback) {
|
|
||||||
async.parallel([primaryDBinit, secondaryDB.init], function(err, results) {
|
|
||||||
callback(err);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
primaryDB.close = function(callback) {
|
|
||||||
async.parallel([primaryDBclose, secondaryDB.close], function(err, results) {
|
|
||||||
callback(err);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
primaryDB.helpers = {};
|
|
||||||
primaryDB.helpers[primaryDBName] = primaryDBhelpers[primaryDBName];
|
|
||||||
primaryDB.helpers[secondaryDBName] = secondaryDB.helpers[secondaryDBName];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
var primaryDB = require('./database/' + primaryDBName);
|
|
||||||
|
|
||||||
if (secondaryDBName && secondaryModules) {
|
|
||||||
setupSecondaryDB();
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = primaryDB;
|
module.exports = primaryDB;
|
||||||
@@ -6,16 +6,7 @@ var async = require('async'),
|
|||||||
prompt = require('prompt'),
|
prompt = require('prompt'),
|
||||||
winston = require('winston'),
|
winston = require('winston'),
|
||||||
nconf = require('nconf'),
|
nconf = require('nconf'),
|
||||||
utils = require('../public/src/utils.js'),
|
utils = require('../public/src/utils.js');
|
||||||
|
|
||||||
DATABASES = {
|
|
||||||
"redis": {
|
|
||||||
"dependencies": ["redis@~2.4.2", "connect-redis@~2.0.0"]
|
|
||||||
},
|
|
||||||
"mongo": {
|
|
||||||
"dependencies": ["mongodb@~2.0.0", "connect-mongo@~0.8.2"]
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
var install = {},
|
var install = {},
|
||||||
@@ -40,7 +31,7 @@ questions.main = [
|
|||||||
{
|
{
|
||||||
name: 'database',
|
name: 'database',
|
||||||
description: 'Which database to use',
|
description: 'Which database to use',
|
||||||
'default': nconf.get('database') || 'redis'
|
'default': nconf.get('database') || 'mongo'
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -145,23 +136,10 @@ function setupConfig(next) {
|
|||||||
process.exit();
|
process.exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nconf.get('advanced')) {
|
configureDatabases(config, function(err, config) {
|
||||||
prompt.get({
|
|
||||||
name: 'secondary_database',
|
|
||||||
description: 'Select secondary database',
|
|
||||||
'default': nconf.get('secondary_database') || 'none'
|
|
||||||
}, function(err, dbConfig) {
|
|
||||||
config.secondary_database = dbConfig.secondary_database;
|
|
||||||
configureDatabases(err, config, DATABASES, function(err, config) {
|
|
||||||
completeConfigSetup(err, config, next);
|
completeConfigSetup(err, config, next);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
configureDatabases(err, config, DATABASES, function(err, config) {
|
|
||||||
completeConfigSetup(err, config, next);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
// Use provided values, fall back to defaults
|
// Use provided values, fall back to defaults
|
||||||
var config = {},
|
var config = {},
|
||||||
@@ -169,12 +147,11 @@ function setupConfig(next) {
|
|||||||
mongoQuestions = require('./database/mongo').questions,
|
mongoQuestions = require('./database/mongo').questions,
|
||||||
question, x, numQ, allQuestions = questions.main.concat(questions.optional).concat(redisQuestions).concat(mongoQuestions);
|
question, x, numQ, allQuestions = questions.main.concat(questions.optional).concat(redisQuestions).concat(mongoQuestions);
|
||||||
|
|
||||||
for(x=0,numQ=allQuestions.length;x<numQ;x++) {
|
allQuestions.forEach(function (question) {
|
||||||
question = allQuestions[x];
|
|
||||||
config[question.name] = install.values[question.name] || question['default'] || undefined;
|
config[question.name] = install.values[question.name] || question['default'] || undefined;
|
||||||
}
|
});
|
||||||
|
|
||||||
configureDatabases(null, config, DATABASES, function(err, config) {
|
configureDatabases(config, function(err, config) {
|
||||||
completeConfigSetup(err, config, next);
|
completeConfigSetup(err, config, next);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -200,40 +177,10 @@ function completeConfigSetup(err, config, next) {
|
|||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
setupDatabase(config, next);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function setupDatabase(server_conf, next) {
|
|
||||||
install.installDbDependencies(server_conf, function(err) {
|
|
||||||
if (err) {
|
|
||||||
return next(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
require('./database').init(next);
|
require('./database').init(next);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
install.installDbDependencies = function(server_conf, next) {
|
|
||||||
var npm = require('npm'),
|
|
||||||
packages = [];
|
|
||||||
|
|
||||||
npm.load({}, function(err) {
|
|
||||||
if (err) {
|
|
||||||
return next(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
npm.config.set('spin', false);
|
|
||||||
|
|
||||||
packages = packages.concat(DATABASES[server_conf.database].dependencies);
|
|
||||||
if (server_conf.secondary_database) {
|
|
||||||
packages = packages.concat(DATABASES[server_conf.secondary_database].dependencies);
|
|
||||||
}
|
|
||||||
|
|
||||||
npm.commands.install(packages, next);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
function setupDefaultConfigs(next) {
|
function setupDefaultConfigs(next) {
|
||||||
process.stdout.write('Populating database with default configs, if not already set...\n');
|
process.stdout.write('Populating database with default configs, if not already set...\n');
|
||||||
var meta = require('./meta'),
|
var meta = require('./meta'),
|
||||||
@@ -457,7 +404,6 @@ function createWelcomePost(next) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function enableDefaultPlugins(next) {
|
function enableDefaultPlugins(next) {
|
||||||
var Plugins = require('./plugins');
|
|
||||||
|
|
||||||
process.stdout.write('Enabling default plugins\n');
|
process.stdout.write('Enabling default plugins\n');
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user