diff --git a/install/databases.js b/install/databases.js new file mode 100644 index 0000000000..17a0092d94 --- /dev/null +++ b/install/databases.js @@ -0,0 +1,112 @@ +"use strict"; + +var async = require('async'), + prompt = require('prompt'), + nconf = require('nconf'), + winston = require('winston'), + + questions = {}; + +// maybe this should go into install/database.js +function success(err, config, callback) { + if (!config) { + return callback(new Error('aborted')); + } + + var database = (config.redis || config.mongo || config.level) ? 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 if (database === 'level') { + config.level = { + database: databaseConfig['level:database'] + }; + } else { + return callback(new Error('unknown database : ' + database)); + } + + var allQuestions = questions.redis.concat(questions.mongo.concat(questions.level)); + for(var x=0;x 1) ? urlObject.pathname : '', - host = urlObject.host, - protocol = urlObject.protocol, - server_conf = config; - - server_conf.base_url = protocol + '//' + host; - server_conf.relative_path = relative_path; - - install.save(server_conf, function(err) { - if (err) { - return next(err); - } - - require('./database').init(next); - }); - } + var configureDatabases = require('./../install/databases'); // prompt prepends "prompt: " to questions, let's clear that. prompt.start(); @@ -274,25 +139,65 @@ function setupConfig(next) { 'default': nconf.get('secondary_database') || 'none' }, function(err, dbConfig) { config.secondary_database = dbConfig.secondary_database; - configureDatabases(err, config); + configureDatabases(err, config, ALLOWED_DATABASES, function(err, config) { + completeConfigSetup(err, config, next); + }); }); } else { - configureDatabases(err, config); + configureDatabases(err, config, ALLOWED_DATABASES, function(err, config) { + completeConfigSetup(err, config, next); + }); } }); } else { // Use provided values, fall back to defaults var config = {}, question, x, numQ, allQuestions = questions.main.concat(questions.redis).concat(questions.mongo.concat(questions.level)); + for(x=0,numQ=allQuestions.length;x 1) ? urlObject.pathname : '', + host = urlObject.host, + protocol = urlObject.protocol, + server_conf = config; + + server_conf.base_url = protocol + '//' + host; + server_conf.relative_path = relative_path; + + install.save(server_conf, function(err) { + if (err) { + return next(err); + } + + require('./database').init(next); + }); +} + function setupDefaultConfigs(next) { winston.info('Populating database with default configs, if not already set...'); var meta = require('./meta');