refactored out giant async.series

v1.18.x
psychobunny 11 years ago
parent be83157de2
commit 0a94cf7c95

@ -15,37 +15,44 @@ var async = require('async'),
var install = {}, var install = {},
questions = {}; questions = {};
questions.main = [{ questions.main = [
name: 'base_url', {
description: 'URL of this installation', name: 'base_url',
'default': nconf.get('base_url') || 'http://localhost', description: 'URL of this installation',
pattern: /^http(?:s)?:\/\//, 'default': nconf.get('base_url') || 'http://localhost',
message: 'Base URL must begin with \'http://\' or \'https://\'', pattern: /^http(?:s)?:\/\//,
}, { message: 'Base URL must begin with \'http://\' or \'https://\'',
name: 'port', },
description: 'Port number of your NodeBB', {
'default': nconf.get('port') || 4567, name: 'port',
pattern: /[0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5]/, description: 'Port number of your NodeBB',
message: 'Please enter a value betweeen 1 and 65535' 'default': nconf.get('port') || 4567,
}, { pattern: /[0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5]/,
name: 'use_port', message: 'Please enter a value betweeen 1 and 65535'
description: 'Use a port number to access NodeBB?', },
'default': (nconf.get('use_port') !== undefined ? (nconf.get('use_port') ? 'y' : 'n') : 'y'), {
pattern: /y[es]*|n[o]?/, name: 'use_port',
message: 'Please enter \'yes\' or \'no\'' description: 'Use a port number to access NodeBB?',
}, { 'default': (nconf.get('use_port') !== undefined ? (nconf.get('use_port') ? 'y' : 'n') : 'y'),
name: 'secret', pattern: /y[es]*|n[o]?/,
description: 'Please enter a NodeBB secret', message: 'Please enter \'yes\' or \'no\''
'default': nconf.get('secret') || utils.generateUUID() },
}, { {
name: 'bind_address', name: 'secret',
description: 'IP or Hostname to bind to', description: 'Please enter a NodeBB secret',
'default': nconf.get('bind_address') || '0.0.0.0' 'default': nconf.get('secret') || utils.generateUUID()
}, { },
name: 'database', {
description: 'Which database to use', name: 'bind_address',
'default': nconf.get('database') || 'redis' description: 'IP or Hostname to bind to',
}]; 'default': nconf.get('bind_address') || '0.0.0.0'
},
{
name: 'database',
description: 'Which database to use',
'default': nconf.get('database') || 'redis'
}
];
@ -53,359 +60,294 @@ ALLOWED_DATABASES.forEach(function(db) {
questions[db] = require('./database/' + db).questions; questions[db] = require('./database/' + db).questions;
}); });
install.setup = function (callback) { function checkSetupFlags (next) {
async.series([ // Check if the `--setup` flag contained values we can work with
function (next) { var setupVal;
// Check if the `--setup` flag contained values we can work with try {
var setupVal; setupVal = JSON.parse(nconf.get('setup'));
try { } catch (e) {
setupVal = JSON.parse(nconf.get('setup')); setupVal = undefined;
} catch (e) { }
setupVal = undefined;
}
if (setupVal && setupVal instanceof Object) {
if (setupVal['admin:username'] && setupVal['admin:password'] && setupVal['admin:password:confirm'] && setupVal['admin:email']) {
install.values = setupVal;
next();
} else {
winston.error('Required values are missing for automated setup:');
if (!setupVal['admin:username']) {
winston.error(' admin:username');
}
if (!setupVal['admin:password']) {
winston.error(' admin:password');
}
if (!setupVal['admin:password:confirm']) {
winston.error(' admin:password:confirm');
}
if (!setupVal['admin:email']) {
winston.error(' admin:email');
}
process.exit(); if (setupVal && setupVal instanceof Object) {
} if (setupVal['admin:username'] && setupVal['admin:password'] && setupVal['admin:password:confirm'] && setupVal['admin:email']) {
} else { install.values = setupVal;
next(); next();
} else {
winston.error('Required values are missing for automated setup:');
if (!setupVal['admin:username']) {
winston.error(' admin:username');
} }
}, if (!setupVal['admin:password']) {
function (next) { winston.error(' admin:password');
// Check if the `--ci` flag contained values we can work with }
var ciVals; if (!setupVal['admin:password:confirm']) {
try { winston.error(' admin:password:confirm');
ciVals = JSON.parse(nconf.get('ci')); }
} catch (e) { if (!setupVal['admin:email']) {
ciVals = undefined; winston.error(' admin:email');
} }
if (ciVals && ciVals instanceof Object) { process.exit();
if (ciVals.hasOwnProperty('host') && ciVals.hasOwnProperty('port') && ciVals.hasOwnProperty('database')) { }
install.ciVals = ciVals; } else {
next(); next();
} else { }
winston.error('Required values are missing for automated CI integration:'); }
if (!ciVals.hasOwnProperty('host')) {
winston.error(' host'); function checkCIFlags(next) {
} // Check if the `--ci` flag contained values we can work with
if (!ciVals.hasOwnProperty('port')) { var ciVals;
winston.error(' port'); try {
} ciVals = JSON.parse(nconf.get('ci'));
if (!ciVals.hasOwnProperty('database')) { } catch (e) {
winston.error(' database'); ciVals = undefined;
} }
process.exit(); if (ciVals && ciVals instanceof Object) {
} if (ciVals.hasOwnProperty('host') && ciVals.hasOwnProperty('port') && ciVals.hasOwnProperty('database')) {
} else { install.ciVals = ciVals;
next(); next();
} else {
winston.error('Required values are missing for automated CI integration:');
if (!ciVals.hasOwnProperty('host')) {
winston.error(' host');
}
if (!ciVals.hasOwnProperty('port')) {
winston.error(' port');
}
if (!ciVals.hasOwnProperty('database')) {
winston.error(' database');
} }
},
function (next) {
var success = function (err, config, callback) {
if (!config) {
return next(new Error('aborted'));
}
var database = (config.redis || config.mongo || config.level) ? config.secondary_database : config.database; process.exit();
}
} else {
next();
}
}
var dbQuestionsSuccess = function (err, databaseConfig) {
if (!databaseConfig) {
return next(new Error('aborted'));
}
// Translate redis properties into redis object function setupConfig(next) {
if(database === 'redis') { // maybe this should go into install/database.js
config.redis = { function success(err, config, callback) {
host: databaseConfig['redis:host'], if (!config) {
port: databaseConfig['redis:port'], return next(new Error('aborted'));
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 next(new Error('unknown database : ' + database));
}
var allQuestions = questions.redis.concat(questions.mongo.concat(questions.level)); var database = (config.redis || config.mongo || config.level) ? config.secondary_database : config.database;
for(var x=0;x<allQuestions.length;x++) {
delete config[allQuestions[x].name];
}
callback(err, config); function dbQuestionsSuccess(err, databaseConfig) {
}; if (!databaseConfig) {
return next(new Error('aborted'));
}
// Add CI object // Translate redis properties into redis object
if (install.ciVals) { if(database === 'redis') {
config.test_database = {}; config.redis = {
for(var prop in install.ciVals) { host: databaseConfig['redis:host'],
if (install.ciVals.hasOwnProperty(prop)) { port: databaseConfig['redis:port'],
config.test_database[prop] = install.ciVals[prop]; password: databaseConfig['redis:password'],
} database: databaseConfig['redis:database']
} };
}
if(database === 'redis') { if (config.redis.host.slice(0, 1) === '/') {
if (config['redis:host'] && config['redis:port']) { delete 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 if(database === 'level') {
if (config['level:database']) {
dbQuestionsSuccess(null, config);
} else {
prompt.get(questions.level, dbQuestionsSuccess);
}
} else {
return next(new Error('unknown database : ' + database));
} }
}; } else if (database === 'mongo') {
config.mongo = {
// prompt prepends "prompt: " to questions, let's clear that. host: databaseConfig['mongo:host'],
prompt.start(); port: databaseConfig['mongo:port'],
prompt.message = ''; username: databaseConfig['mongo:username'],
prompt.delimiter = ''; password: databaseConfig['mongo:password'],
database: databaseConfig['mongo:database']
if (!install.values) { };
prompt.get(questions.main, function(err, config) { } else if (database === 'level') {
if (nconf.get('advanced')) { config.level = {
prompt.get({ database: databaseConfig['level:database']
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);
});
} else {
configureDatabases(err, config);
}
});
} else { } else {
// Use provided values, fall back to defaults return next(new Error('unknown database : ' + database));
var config = {}, }
question, x, numQ, allQuestions = questions.main.concat(questions.redis).concat(questions.mongo.concat(questions.level));
for(x=0,numQ=allQuestions.length;x<numQ;x++) {
question = allQuestions[x];
config[question.name] = install.values[question.name] || question['default'] || '';
}
success(null, config, completeConfigSetup); var allQuestions = questions.redis.concat(questions.mongo.concat(questions.level));
for(var x=0;x<allQuestions.length;x++) {
delete config[allQuestions[x].name];
} }
function getSecondaryDatabaseModules(config, next) { callback(err, config);
prompt.get({ }
"name": "secondary_db_modules",
"description": "Which database modules should " + config.secondary_database + " store?", // Add CI object
"default": nconf.get('secondary_db_modules') || "hash, list, sets, sorted" if (install.ciVals) {
}, function(err, db) { config.test_database = {};
config.secondary_db_modules = db.secondary_db_modules; for(var prop in install.ciVals) {
success(err, config, next); if (install.ciVals.hasOwnProperty(prop)) {
}); config.test_database[prop] = install.ciVals[prop];
}
} }
}
function configureDatabases(err, config) { if(database === 'redis') {
async.waterfall([ if (config['redis:host'] && config['redis:port']) {
function(next) { dbQuestionsSuccess(null, config);
winston.info('Now configuring ' + config.database + ' database:'); } else {
success(err, config, next); prompt.get(questions.redis, dbQuestionsSuccess);
}, }
function(config, next) { } else if(database === 'mongo') {
winston.info('Now configuring ' + config.secondary_database + ' database:'); if (config['mongo:host'] && config['mongo:port']) {
if (config.secondary_database && ALLOWED_DATABASES.indexOf(config.secondary_database) !== -1) { dbQuestionsSuccess(null, config);
getSecondaryDatabaseModules(config, next); } else {
} else { prompt.get(questions.mongo, dbQuestionsSuccess);
next(err, config); }
} } else if(database === 'level') {
} if (config['level:database']) {
], completeConfigSetup); dbQuestionsSuccess(null, config);
} else {
prompt.get(questions.level, dbQuestionsSuccess);
} }
} else {
return next(new Error('unknown database : ' + database));
}
}
function completeConfigSetup(err, config) { function getSecondaryDatabaseModules(config, next) {
config.bcrypt_rounds = 12; prompt.get({
config.upload_path = '/public/uploads'; "name": "secondary_db_modules",
config.use_port = config.use_port.slice(0, 1) === 'y'; "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);
});
}
var urlObject = url.parse(config.base_url), function configureDatabases(err, config) {
relative_path = (urlObject.pathname && urlObject.pathname.length > 1) ? urlObject.pathname : '', async.waterfall([
host = urlObject.host, function(next) {
protocol = urlObject.protocol, winston.info('Now configuring ' + config.database + ' database:');
server_conf = config; success(err, config, next);
},
function(config, next) {
winston.info('Now configuring ' + config.secondary_database + ' database:');
if (config.secondary_database && ALLOWED_DATABASES.indexOf(config.secondary_database) !== -1) {
getSecondaryDatabaseModules(config, next);
} else {
next(err, config);
}
}
], completeConfigSetup);
}
server_conf.base_url = protocol + '//' + host; function completeConfigSetup(err, config) {
server_conf.relative_path = relative_path; config.bcrypt_rounds = 12;
config.upload_path = '/public/uploads';
config.use_port = config.use_port.slice(0, 1) === 'y';
install.save(server_conf, function(err) { var urlObject = url.parse(config.base_url),
if (err) { relative_path = (urlObject.pathname && urlObject.pathname.length > 1) ? urlObject.pathname : '',
return next(err); host = urlObject.host,
} protocol = urlObject.protocol,
server_conf = config;
require('./database').init(next); server_conf.base_url = protocol + '//' + host;
}); server_conf.relative_path = relative_path;
}
},
function (next) {
// Applying default database configs
winston.info('Populating database with default configs, if not already set...');
var meta = require('./meta');
fs.readFile(path.join(__dirname, '../', 'install/data/defaults.json'), function (err, defaults) { install.save(server_conf, function(err) {
async.each(defaults, function (configObj, next) { if (err) {
meta.configs.setOnEmpty(configObj.field, configObj.value, next); return next(err);
}, function (err) { }
meta.configs.init(next);
});
if (install.values) {
if (install.values['social:twitter:key'] && install.values['social:twitter:secret']) {
meta.configs.setOnEmpty('social:twitter:key', install.values['social:twitter:key']);
meta.configs.setOnEmpty('social:twitter:secret', install.values['social:twitter:secret']);
}
if (install.values['social:google:id'] && install.values['social:google:secret']) {
meta.configs.setOnEmpty('social:google:id', install.values['social:google:id']);
meta.configs.setOnEmpty('social:google:secret', install.values['social:google:secret']);
}
if (install.values['social:facebook:key'] && install.values['social:facebook:secret']) {
meta.configs.setOnEmpty('social:facebook:app_id', install.values['social:facebook:app_id']);
meta.configs.setOnEmpty('social:facebook:secret', install.values['social:facebook:secret']);
}
}
});
},
function(next) {
var meta = require('./meta');
winston.info('Enabling default theme: Lavender');
meta.themes.set({
type: 'local',
id: 'nodebb-theme-lavender'
}, next);
},
function (next) {
// Check if an administrator needs to be created
var Groups = require('./groups');
Groups.get('administrators', {}, function (err, groupObj) {
if (!err && groupObj && groupObj.memberCount > 0) {
winston.info('Administrator found, skipping Admin setup');
next();
} else {
install.createAdmin(next);
}
});
},
function (next) {
// Categories
var Categories = require('./categories');
Categories.getAllCategories(0, function (err, data) {
if (data.categories.length === 0) {
winston.warn('No categories found, populating instance with default categories');
fs.readFile(path.join(__dirname, '../', 'install/data/categories.json'), function (err, default_categories) {
default_categories = JSON.parse(default_categories);
async.eachSeries(default_categories, function (category, next) {
Categories.create(category, next);
}, function (err) {
if (!err) {
next();
} else {
winston.error('Could not set up categories');
}
});
});
} else {
winston.info('Categories OK. Found ' + data.categories.length + ' categories.');
next();
}
});
},
function (next) {
// Default plugins
var Plugins = require('./plugins');
winston.info('Enabling default plugins'); require('./database').init(next);
});
}
var defaultEnabled = [ // prompt prepends "prompt: " to questions, let's clear that.
'nodebb-plugin-markdown', 'nodebb-plugin-mentions', 'nodebb-widget-essentials' prompt.start();
]; prompt.message = '';
prompt.delimiter = '';
async.each(defaultEnabled, function (pluginId, next) { if (!install.values) {
Plugins.isActive(pluginId, function (err, active) { prompt.get(questions.main, function(err, config) {
if (!active) { if (nconf.get('advanced')) {
Plugins.toggleActive(pluginId, function () { prompt.get({
next(); name: 'secondary_database',
}); description: 'Select secondary database',
} else { 'default': nconf.get('secondary_database') || 'none'
next(); }, function(err, dbConfig) {
} config.secondary_database = dbConfig.secondary_database;
configureDatabases(err, config);
}); });
}, next); } else {
}, configureDatabases(err, config);
function (next) { }
var db = require('./database.js'); });
} 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<numQ;x++) {
question = allQuestions[x];
config[question.name] = install.values[question.name] || question['default'] || '';
}
db.init(function(err) { success(null, config, completeConfigSetup);
if (!err) { }
db.setObjectField('widgets:global', 'footer', "[{\"widget\":\"html\",\"data\":{\"html\":\"<footer id=\\\"footer\\\" class=\\\"container footer\\\">\\r\\n\\t<div class=\\\"copyright\\\">\\r\\n\\t\\tCopyright © 2014 <a target=\\\"_blank\\\" href=\\\"https://www.nodebb.com\\\">NodeBB Forums</a> | <a target=\\\"_blank\\\" href=\\\"//github.com/designcreateplay/NodeBB/graphs/contributors\\\">Contributors</a>\\r\\n\\t</div>\\r\\n</footer>\",\"title\":\"\",\"container\":\"\"}}]", next); }
}
}); function setupDefaultConfigs(next) {
}, // Applying default database configs
function (next) { winston.info('Populating database with default configs, if not already set...');
require('./upgrade').upgrade(next); var meta = require('./meta');
fs.readFile(path.join(__dirname, '../', 'install/data/defaults.json'), function (err, defaults) {
async.each(defaults, function (configObj, next) {
meta.configs.setOnEmpty(configObj.field, configObj.value, next);
}, function (err) {
meta.configs.init(next);
});
if (install.values) {
if (install.values['social:twitter:key'] && install.values['social:twitter:secret']) {
meta.configs.setOnEmpty('social:twitter:key', install.values['social:twitter:key']);
meta.configs.setOnEmpty('social:twitter:secret', install.values['social:twitter:secret']);
}
if (install.values['social:google:id'] && install.values['social:google:secret']) {
meta.configs.setOnEmpty('social:google:id', install.values['social:google:id']);
meta.configs.setOnEmpty('social:google:secret', install.values['social:google:secret']);
}
if (install.values['social:facebook:key'] && install.values['social:facebook:secret']) {
meta.configs.setOnEmpty('social:facebook:app_id', install.values['social:facebook:app_id']);
meta.configs.setOnEmpty('social:facebook:secret', install.values['social:facebook:secret']);
}
} }
], function (err) { });
if (err) { }
winston.warn('NodeBB Setup Aborted. ' + err.message);
process.exit(); function enableDefaultTheme(next) {
var meta = require('./meta');
winston.info('Enabling default theme: Lavender');
meta.themes.set({
type: 'local',
id: 'nodebb-theme-lavender'
}, next);
}
function createAdministrator(next) {
// Check if an administrator needs to be created
var Groups = require('./groups');
Groups.get('administrators', {}, function (err, groupObj) {
if (!err && groupObj && groupObj.memberCount > 0) {
winston.info('Administrator found, skipping Admin setup');
next();
} else { } else {
callback(); createAdmin(next);
} }
}); });
}; }
install.createAdmin = function(callback) { function createAdmin(callback) {
var User = require('./user'), var User = require('./user'),
Groups = require('./groups'); Groups = require('./groups');
@ -486,6 +428,82 @@ install.createAdmin = function(callback) {
success(null, results); success(null, results);
} }
}
function createCategories(next) {
// Categories
var Categories = require('./categories');
Categories.getAllCategories(0, function (err, data) {
if (data.categories.length === 0) {
winston.warn('No categories found, populating instance with default categories');
fs.readFile(path.join(__dirname, '../', 'install/data/categories.json'), function (err, default_categories) {
default_categories = JSON.parse(default_categories);
async.eachSeries(default_categories, function (category, next) {
Categories.create(category, next);
}, function (err) {
if (!err) {
next();
} else {
winston.error('Could not set up categories');
}
});
});
} else {
winston.info('Categories OK. Found ' + data.categories.length + ' categories.');
next();
}
});
}
function enableDefaultPlugins(next) {
// Default plugins
var Plugins = require('./plugins');
winston.info('Enabling default plugins');
var defaultEnabled = [
'nodebb-plugin-markdown', 'nodebb-plugin-mentions', 'nodebb-widget-essentials'
];
async.each(defaultEnabled, function (pluginId, next) {
Plugins.isActive(pluginId, function (err, active) {
if (!active) {
Plugins.toggleActive(pluginId, function () {
next();
});
} else {
next();
}
});
}, next);
}
function populateDatabase(next) {
var db = require('./database.js');
db.init(function(err) {
if (!err) {
db.setObjectField('widgets:global', 'footer', "[{\"widget\":\"html\",\"data\":{\"html\":\"<footer id=\\\"footer\\\" class=\\\"container footer\\\">\\r\\n\\t<div class=\\\"copyright\\\">\\r\\n\\t\\tCopyright © 2014 <a target=\\\"_blank\\\" href=\\\"https://www.nodebb.com\\\">NodeBB Forums</a> | <a target=\\\"_blank\\\" href=\\\"//github.com/designcreateplay/NodeBB/graphs/contributors\\\">Contributors</a>\\r\\n\\t</div>\\r\\n</footer>\",\"title\":\"\",\"container\":\"\"}}]", next);
}
});
}
install.setup = function (callback) {
async.series([checkSetupFlags, checkCIFlags, setupConfig, setupDefaultConfigs, enableDefaultTheme, createAdministrator, createCategories, enableDefaultPlugins, populateDatabase,
function (next) {
require('./upgrade').upgrade(next);
}
], function (err) {
if (err) {
winston.warn('NodeBB Setup Aborted. ' + err.message);
process.exit();
} else {
callback();
}
});
}; };
install.save = function (server_conf, callback) { install.save = function (server_conf, callback) {

Loading…
Cancel
Save