mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-07 14:35:47 +01:00
refactored out giant async.series
This commit is contained in:
274
src/install.js
274
src/install.js
@@ -15,37 +15,44 @@ var async = require('async'),
|
|||||||
var install = {},
|
var install = {},
|
||||||
questions = {};
|
questions = {};
|
||||||
|
|
||||||
questions.main = [{
|
questions.main = [
|
||||||
|
{
|
||||||
name: 'base_url',
|
name: 'base_url',
|
||||||
description: 'URL of this installation',
|
description: 'URL of this installation',
|
||||||
'default': nconf.get('base_url') || 'http://localhost',
|
'default': nconf.get('base_url') || 'http://localhost',
|
||||||
pattern: /^http(?:s)?:\/\//,
|
pattern: /^http(?:s)?:\/\//,
|
||||||
message: 'Base URL must begin with \'http://\' or \'https://\'',
|
message: 'Base URL must begin with \'http://\' or \'https://\'',
|
||||||
}, {
|
},
|
||||||
|
{
|
||||||
name: 'port',
|
name: 'port',
|
||||||
description: 'Port number of your NodeBB',
|
description: 'Port number of your NodeBB',
|
||||||
'default': nconf.get('port') || 4567,
|
'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]/,
|
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]/,
|
||||||
message: 'Please enter a value betweeen 1 and 65535'
|
message: 'Please enter a value betweeen 1 and 65535'
|
||||||
}, {
|
},
|
||||||
|
{
|
||||||
name: 'use_port',
|
name: 'use_port',
|
||||||
description: 'Use a port number to access NodeBB?',
|
description: 'Use a port number to access NodeBB?',
|
||||||
'default': (nconf.get('use_port') !== undefined ? (nconf.get('use_port') ? 'y' : 'n') : 'y'),
|
'default': (nconf.get('use_port') !== undefined ? (nconf.get('use_port') ? 'y' : 'n') : 'y'),
|
||||||
pattern: /y[es]*|n[o]?/,
|
pattern: /y[es]*|n[o]?/,
|
||||||
message: 'Please enter \'yes\' or \'no\''
|
message: 'Please enter \'yes\' or \'no\''
|
||||||
}, {
|
},
|
||||||
|
{
|
||||||
name: 'secret',
|
name: 'secret',
|
||||||
description: 'Please enter a NodeBB secret',
|
description: 'Please enter a NodeBB secret',
|
||||||
'default': nconf.get('secret') || utils.generateUUID()
|
'default': nconf.get('secret') || utils.generateUUID()
|
||||||
}, {
|
},
|
||||||
|
{
|
||||||
name: 'bind_address',
|
name: 'bind_address',
|
||||||
description: 'IP or Hostname to bind to',
|
description: 'IP or Hostname to bind to',
|
||||||
'default': nconf.get('bind_address') || '0.0.0.0'
|
'default': nconf.get('bind_address') || '0.0.0.0'
|
||||||
}, {
|
},
|
||||||
|
{
|
||||||
name: 'database',
|
name: 'database',
|
||||||
description: 'Which database to use',
|
description: 'Which database to use',
|
||||||
'default': nconf.get('database') || 'redis'
|
'default': nconf.get('database') || 'redis'
|
||||||
}];
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -53,9 +60,7 @@ 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([
|
|
||||||
function (next) {
|
|
||||||
// Check if the `--setup` flag contained values we can work with
|
// Check if the `--setup` flag contained values we can work with
|
||||||
var setupVal;
|
var setupVal;
|
||||||
try {
|
try {
|
||||||
@@ -88,8 +93,9 @@ install.setup = function (callback) {
|
|||||||
} else {
|
} else {
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
function (next) {
|
|
||||||
|
function checkCIFlags(next) {
|
||||||
// Check if the `--ci` flag contained values we can work with
|
// Check if the `--ci` flag contained values we can work with
|
||||||
var ciVals;
|
var ciVals;
|
||||||
try {
|
try {
|
||||||
@@ -119,16 +125,19 @@ install.setup = function (callback) {
|
|||||||
} else {
|
} else {
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
function (next) {
|
|
||||||
var success = function (err, config, callback) {
|
|
||||||
|
function setupConfig(next) {
|
||||||
|
// maybe this should go into install/database.js
|
||||||
|
function success(err, config, callback) {
|
||||||
if (!config) {
|
if (!config) {
|
||||||
return next(new Error('aborted'));
|
return next(new Error('aborted'));
|
||||||
}
|
}
|
||||||
|
|
||||||
var database = (config.redis || config.mongo || config.level) ? config.secondary_database : config.database;
|
var database = (config.redis || config.mongo || config.level) ? config.secondary_database : config.database;
|
||||||
|
|
||||||
var dbQuestionsSuccess = function (err, databaseConfig) {
|
function dbQuestionsSuccess(err, databaseConfig) {
|
||||||
if (!databaseConfig) {
|
if (!databaseConfig) {
|
||||||
return next(new Error('aborted'));
|
return next(new Error('aborted'));
|
||||||
}
|
}
|
||||||
@@ -167,7 +176,7 @@ install.setup = function (callback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
callback(err, config);
|
callback(err, config);
|
||||||
};
|
}
|
||||||
|
|
||||||
// Add CI object
|
// Add CI object
|
||||||
if (install.ciVals) {
|
if (install.ciVals) {
|
||||||
@@ -200,38 +209,6 @@ install.setup = function (callback) {
|
|||||||
} else {
|
} else {
|
||||||
return next(new Error('unknown database : ' + database));
|
return next(new Error('unknown database : ' + database));
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
// prompt prepends "prompt: " to questions, let's clear that.
|
|
||||||
prompt.start();
|
|
||||||
prompt.message = '';
|
|
||||||
prompt.delimiter = '';
|
|
||||||
|
|
||||||
if (!install.values) {
|
|
||||||
prompt.get(questions.main, function(err, config) {
|
|
||||||
if (nconf.get('advanced')) {
|
|
||||||
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);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
configureDatabases(err, config);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} 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'] || '';
|
|
||||||
}
|
|
||||||
|
|
||||||
success(null, config, completeConfigSetup);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSecondaryDatabaseModules(config, next) {
|
function getSecondaryDatabaseModules(config, next) {
|
||||||
@@ -284,8 +261,41 @@ install.setup = function (callback) {
|
|||||||
require('./database').init(next);
|
require('./database').init(next);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
|
||||||
function (next) {
|
// prompt prepends "prompt: " to questions, let's clear that.
|
||||||
|
prompt.start();
|
||||||
|
prompt.message = '';
|
||||||
|
prompt.delimiter = '';
|
||||||
|
|
||||||
|
if (!install.values) {
|
||||||
|
prompt.get(questions.main, function(err, config) {
|
||||||
|
if (nconf.get('advanced')) {
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
configureDatabases(err, config);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} 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'] || '';
|
||||||
|
}
|
||||||
|
|
||||||
|
success(null, config, completeConfigSetup);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function setupDefaultConfigs(next) {
|
||||||
// Applying default database configs
|
// Applying default database configs
|
||||||
winston.info('Populating database with default configs, if not already set...');
|
winston.info('Populating database with default configs, if not already set...');
|
||||||
var meta = require('./meta');
|
var meta = require('./meta');
|
||||||
@@ -312,8 +322,9 @@ install.setup = function (callback) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
}
|
||||||
function(next) {
|
|
||||||
|
function enableDefaultTheme(next) {
|
||||||
var meta = require('./meta');
|
var meta = require('./meta');
|
||||||
winston.info('Enabling default theme: Lavender');
|
winston.info('Enabling default theme: Lavender');
|
||||||
|
|
||||||
@@ -321,8 +332,9 @@ install.setup = function (callback) {
|
|||||||
type: 'local',
|
type: 'local',
|
||||||
id: 'nodebb-theme-lavender'
|
id: 'nodebb-theme-lavender'
|
||||||
}, next);
|
}, next);
|
||||||
},
|
}
|
||||||
function (next) {
|
|
||||||
|
function createAdministrator(next) {
|
||||||
// Check if an administrator needs to be created
|
// Check if an administrator needs to be created
|
||||||
var Groups = require('./groups');
|
var Groups = require('./groups');
|
||||||
Groups.get('administrators', {}, function (err, groupObj) {
|
Groups.get('administrators', {}, function (err, groupObj) {
|
||||||
@@ -330,82 +342,12 @@ install.setup = function (callback) {
|
|||||||
winston.info('Administrator found, skipping Admin setup');
|
winston.info('Administrator found, skipping Admin setup');
|
||||||
next();
|
next();
|
||||||
} else {
|
} else {
|
||||||
install.createAdmin(next);
|
createAdmin(next);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
}
|
||||||
function (next) {
|
|
||||||
// Categories
|
|
||||||
var Categories = require('./categories');
|
|
||||||
|
|
||||||
Categories.getAllCategories(0, function (err, data) {
|
function createAdmin(callback) {
|
||||||
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');
|
|
||||||
|
|
||||||
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 (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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
require('./upgrade').upgrade(next);
|
|
||||||
}
|
|
||||||
], function (err) {
|
|
||||||
if (err) {
|
|
||||||
winston.warn('NodeBB Setup Aborted. ' + err.message);
|
|
||||||
process.exit();
|
|
||||||
} else {
|
|
||||||
callback();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
install.createAdmin = function(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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user