This commit is contained in:
barisusakli
2015-12-21 12:02:10 +02:00
parent 4bff714947
commit b3f63e0a0d
4 changed files with 78 additions and 192 deletions

View File

@@ -1,104 +1,82 @@
"use strict";
var async = require('async'),
prompt = require('prompt'),
nconf = require('nconf'),
winston = require('winston'),
var async = require('async');
var prompt = require('prompt');
var 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) {
return callback(new Error('aborted'));
}
var database = (config.redis || config.mongo) ? config.secondary_database : config.database;
function dbQuestionsSuccess(err, databaseConfig) {
if (!databaseConfig) {
return callback(new Error('aborted'));
}
// Translate redis properties into redis object
if(database === 'redis') {
config.redis = {
host: databaseConfig['redis:host'],
port: databaseConfig['redis:port'],
password: databaseConfig['redis:password'],
database: databaseConfig['redis:database']
};
if (config.redis.host.slice(0, 1) === '/') {
delete config.redis.port;
}
} else if (database === 'mongo') {
config.mongo = {
host: databaseConfig['mongo:host'],
port: databaseConfig['mongo:port'],
username: databaseConfig['mongo:username'],
password: databaseConfig['mongo:password'],
database: databaseConfig['mongo:database']
};
} else {
return callback(new Error('unknown database : ' + database));
}
var allQuestions = questions.redis.concat(questions.mongo);
for(var x=0;x<allQuestions.length;x++) {
delete config[allQuestions[x].name];
}
callback(err, config);
}
if(database === 'redis') {
if (config.database === 'redis') {
if (config['redis:host'] && config['redis:port']) {
dbQuestionsSuccess(null, config);
callback(null, config);
} else {
prompt.get(questions.redis, dbQuestionsSuccess);
prompt.get(questions.redis, callback);
}
} else if(database === 'mongo') {
} else if (config.database === 'mongo') {
if (config['mongo:host'] && config['mongo:port']) {
dbQuestionsSuccess(null, config);
callback(null, config);
} else {
prompt.get(questions.mongo, dbQuestionsSuccess);
prompt.get(questions.mongo, callback);
}
} else {
return callback(new Error('unknown database : ' + database));
return callback(new Error('unknown database : ' + config.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);
});
}
function saveDatabaseConfig(config, databaseConfig, callback) {
if (!databaseConfig) {
return callback(new Error('aborted'));
}
module.exports = function(err, config, databases, callback) {
var allowedDBs = Object.keys(databases);
// Translate redis properties into redis object
if (config.database === 'redis') {
config.redis = {
host: databaseConfig['redis:host'],
port: databaseConfig['redis:port'],
password: databaseConfig['redis:password'],
database: databaseConfig['redis:database']
};
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);
}
if (config.redis.host.slice(0, 1) === '/') {
delete config.redis.port;
}
], callback);
};
} else if (config.database === 'mongo') {
config.mongo = {
host: databaseConfig['mongo:host'],
port: databaseConfig['mongo:port'],
username: databaseConfig['mongo:username'],
password: databaseConfig['mongo:password'],
database: databaseConfig['mongo:database']
};
} else {
return callback(new Error('unknown database : ' + config.database));
}
var allQuestions = questions.redis.concat(questions.mongo);
for (var x=0; x<allQuestions.length; x++) {
delete config[allQuestions[x].name];
}
callback(null, config);
}