Files
NodeBB/src/install.js

621 lines
16 KiB
JavaScript
Raw Normal View History

2014-03-18 18:15:07 -04:00
'use strict';
2016-12-08 23:51:56 +03:00
var async = require('async');
var fs = require('fs');
var url = require('url');
2016-12-08 23:51:56 +03:00
var path = require('path');
var prompt = require('prompt');
var winston = require('winston');
var nconf = require('nconf');
var _ = require('lodash');
var utils = require('./utils.js');
var install = module.exports;
2016-12-08 23:51:56 +03:00
var questions = {};
2014-04-14 14:10:57 -04:00
questions.main = [
{
2014-11-29 21:54:58 -05:00
name: 'url',
2014-05-20 14:44:50 -04:00
description: 'URL used to access this NodeBB',
2017-02-18 01:19:20 -07:00
default:
nconf.get('url') || 'http://localhost:4567',
2014-04-14 14:10:57 -04:00
pattern: /^http(?:s)?:\/\//,
message: 'Base URL must begin with \'http://\' or \'https://\'',
},
{
name: 'secret',
description: 'Please enter a NodeBB secret',
2017-02-18 01:19:20 -07:00
default: nconf.get('secret') || utils.generateUUID(),
2014-04-14 14:10:57 -04:00
},
{
name: 'submitPluginUsage',
description: 'Would you like to submit anonymous plugin usage to nbbpm?',
default: 'yes',
},
2014-04-14 14:10:57 -04:00
{
name: 'database',
description: 'Which database to use',
2017-02-18 01:19:20 -07:00
default: nconf.get('database') || 'mongo',
2017-02-17 19:31:21 -07:00
},
2014-04-14 14:10:57 -04:00
];
2015-01-07 10:09:02 -05:00
questions.optional = [
{
name: 'port',
2017-02-17 19:31:21 -07:00
default: nconf.get('port') || 4567,
},
2015-01-07 10:09:02 -05:00
];
function checkSetupFlag(next) {
var setupVal = install.values;
2016-08-04 20:58:04 +03:00
2014-04-14 14:10:57 -04:00
try {
2015-04-23 17:03:31 -04:00
if (nconf.get('setup')) {
setupVal = JSON.parse(nconf.get('setup'));
}
} catch (err) {}
if (setupVal && typeof setupVal === 'object') {
2014-04-14 14:10:57 -04:00
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');
}
2014-04-14 14:10:57 -04:00
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');
}
2014-04-14 12:54:11 -04:00
2014-04-14 14:10:57 -04:00
process.exit();
}
2016-08-04 20:58:04 +03:00
} else if (nconf.get('database')) {
install.values = install.values || {};
install.values.database = nconf.get('database');
next();
2014-04-14 14:10:57 -04:00
} else {
next();
}
}
function checkCIFlag(next) {
2017-05-27 01:44:26 -04:00
var ciVals;
2014-04-14 14:10:57 -04:00
try {
ciVals = JSON.parse(nconf.get('ci'));
} catch (e) {
ciVals = undefined;
}
2014-04-14 14:10:57 -04:00
if (ciVals && ciVals instanceof Object) {
if (ciVals.hasOwnProperty('host') && ciVals.hasOwnProperty('port') && ciVals.hasOwnProperty('database')) {
install.ciVals = ciVals;
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');
}
2013-12-04 17:57:51 -05:00
2014-04-14 14:10:57 -04:00
process.exit();
}
} else {
next();
}
}
2013-12-04 17:57:51 -05:00
2014-04-14 14:10:57 -04:00
function setupConfig(next) {
2014-04-15 11:55:54 -04:00
var configureDatabases = require('../install/databases');
2014-04-14 14:10:57 -04:00
// prompt prepends "prompt: " to questions, let's clear that.
prompt.start();
prompt.message = '';
prompt.delimiter = '';
2014-12-28 22:32:02 -05:00
prompt.colors = false;
2013-09-07 15:49:23 -04:00
2016-12-08 23:51:56 +03:00
async.waterfall([
function (next) {
if (install.values) {
// Use provided values, fall back to defaults
var config = {};
var redisQuestions = require('./database/redis').questions;
var mongoQuestions = require('./database/mongo').questions;
var postgresQuestions = require('./database/postgres').questions;
var allQuestions = questions.main.concat(questions.optional).concat(redisQuestions).concat(mongoQuestions).concat(postgresQuestions);
2016-12-08 23:51:56 +03:00
allQuestions.forEach(function (question) {
if (install.values.hasOwnProperty(question.name)) {
config[question.name] = install.values[question.name];
} else if (question.hasOwnProperty('default')) {
config[question.name] = question.default;
} else {
config[question.name] = undefined;
}
2016-12-08 23:51:56 +03:00
});
setImmediate(next, null, config);
} else {
prompt.get(questions.main, next);
}
2016-12-08 23:51:56 +03:00
},
function (config, next) {
configureDatabases(config, next);
},
function (config, next) {
completeConfigSetup(config, next);
2017-02-17 19:31:21 -07:00
},
2016-12-08 23:51:56 +03:00
], next);
}
2016-12-08 23:51:56 +03:00
function completeConfigSetup(config, next) {
// Add CI object
if (install.ciVals) {
config.test_database = {};
for (var prop in install.ciVals) {
if (install.ciVals.hasOwnProperty(prop)) {
config.test_database[prop] = install.ciVals[prop];
}
}
2014-04-14 14:10:57 -04:00
}
// Add package_manager object if set
if (nconf.get('package_manager')) {
config.package_manager = nconf.get('package_manager');
}
2018-05-15 15:45:52 -04:00
nconf.overrides(config);
2016-12-08 23:51:56 +03:00
async.waterfall([
function (next) {
require('./database').init(next);
},
function (next) {
require('./database').createIndices(next);
2017-02-17 19:31:21 -07:00
},
2018-01-15 15:05:33 -05:00
function (next) {
// Sanity-check/fix url/port
if (!/^http(?:s)?:\/\//.test(config.url)) {
config.url = 'http://' + config.url;
}
var urlObj = url.parse(config.url);
if (urlObj.port) {
config.port = urlObj.port;
}
2018-05-24 16:01:04 -04:00
// Remove trailing slash from non-subfolder installs
if (urlObj.path === '/') {
urlObj.path = '';
urlObj.pathname = '';
}
config.url = url.format(urlObj);
// ref: https://github.com/indexzero/nconf/issues/300
delete config.type;
var meta = require('./meta');
meta.configs.set('submitPluginUsage', config.submitPluginUsage === 'yes' ? 1 : 0, function (err) {
next(err, config);
});
},
function (config, next) {
delete config.submitPluginUsage;
2018-01-15 15:05:33 -05:00
install.save(config, next);
},
2016-12-08 23:51:56 +03:00
], next);
}
2014-04-14 14:10:57 -04:00
function setupDefaultConfigs(next) {
console.log('Populating database with default configs, if not already set...');
2016-09-07 19:27:29 +03:00
var meta = require('./meta');
var defaults = require(path.join(__dirname, '../', 'install/data/defaults.json'));
2014-05-20 14:44:50 -04:00
2016-09-07 19:29:25 +03:00
meta.configs.setOnEmpty(defaults, function (err) {
2015-05-23 22:11:20 -04:00
if (err) {
return next(err);
}
2014-04-14 14:10:57 -04:00
2015-12-19 20:52:20 +02:00
meta.configs.init(next);
2015-05-23 22:11:20 -04:00
});
2014-06-29 14:29:32 -04:00
}
2014-04-14 14:10:57 -04:00
function enableDefaultTheme(next) {
2017-05-27 01:44:26 -04:00
var meta = require('./meta');
2014-04-14 14:10:57 -04:00
meta.configs.get('theme:id', function (err, id) {
2014-08-07 16:02:18 -04:00
if (err || id) {
console.log('Previous theme detected, skipping enabling default theme');
2014-08-07 16:02:18 -04:00
return next(err);
}
2015-09-11 18:48:29 -04:00
var defaultTheme = nconf.get('defaultTheme') || 'nodebb-theme-persona';
console.log('Enabling default theme: ' + defaultTheme);
2014-08-07 16:02:18 -04:00
meta.themes.set({
type: 'local',
2017-02-17 19:31:21 -07:00
id: defaultTheme,
2014-08-07 16:02:18 -04:00
}, next);
});
2014-04-14 14:10:57 -04:00
}
function createAdministrator(next) {
var Groups = require('./groups');
Groups.getMemberCount('administrators', function (err, memberCount) {
if (err) {
return next(err);
}
if (memberCount > 0) {
console.log('Administrator found, skipping Admin setup');
2014-04-14 14:10:57 -04:00
next();
} else {
2014-04-14 14:10:57 -04:00
createAdmin(next);
}
});
2014-04-14 14:10:57 -04:00
}
2014-04-14 14:10:57 -04:00
function createAdmin(callback) {
2016-12-13 01:18:42 +00:00
var User = require('./user');
2016-12-12 17:19:59 -08:00
var Groups = require('./groups');
var password;
2016-12-13 01:18:42 +00:00
var meta = require('./meta');
2013-09-07 15:49:23 -04:00
winston.warn('No administrators have been detected, running initial user setup\n');
var questions = [{
2017-02-17 20:20:42 -07:00
name: 'username',
description: 'Administrator username',
required: true,
type: 'string',
}, {
name: 'email',
description: 'Administrator email address',
pattern: /.+@.+/,
required: true,
}];
var passwordQuestions = [{
name: 'password',
description: 'Password',
required: true,
hidden: true,
type: 'string',
}, {
name: 'password:confirm',
description: 'Confirm Password',
required: true,
hidden: true,
type: 'string',
}];
function success(err, results) {
if (err) {
return callback(err);
}
if (!results) {
return callback(new Error('aborted'));
}
if (results['password:confirm'] !== results.password) {
2017-02-18 01:56:23 -07:00
winston.warn('Passwords did not match, please try again');
2017-02-17 20:20:42 -07:00
return retryPassword(results);
}
2017-02-18 01:27:46 -07:00
2017-02-17 20:20:42 -07:00
if (results.password.length < meta.config.minimumPasswordLength) {
2017-02-18 01:56:23 -07:00
winston.warn('Password too short, please try again');
2017-02-17 20:20:42 -07:00
return retryPassword(results);
}
2017-02-18 01:27:46 -07:00
2017-02-17 20:20:42 -07:00
var adminUid;
async.waterfall([
function (next) {
2017-02-18 12:30:49 -07:00
User.create({ username: results.username, password: results.password, email: results.email }, next);
2017-02-17 20:20:42 -07:00
},
function (uid, next) {
adminUid = uid;
Groups.join('administrators', uid, next);
},
function (next) {
Groups.show('administrators', next);
},
function (next) {
Groups.ownership.grant(adminUid, 'administrators', next);
},
], function (err) {
2016-08-16 19:46:59 +02:00
if (err) {
return callback(err);
}
2017-02-17 20:20:42 -07:00
callback(null, password ? results : undefined);
});
}
function retryPassword(originalResults) {
// Ask only the password questions
prompt.get(passwordQuestions, function (err, results) {
if (!results) {
return callback(new Error('aborted'));
}
2017-02-17 20:20:42 -07:00
// Update the original data with newly collected password
originalResults.password = results.password;
originalResults['password:confirm'] = results['password:confirm'];
// Send back to success to handle
success(err, originalResults);
});
}
// Add the password questions
questions = questions.concat(passwordQuestions);
if (!install.values) {
prompt.get(questions, success);
} else {
// If automated setup did not provide a user password, generate one, it will be shown to the user upon setup completion
if (!install.values.hasOwnProperty('admin:password') && !nconf.get('admin:password')) {
console.log('Password was not provided during automated setup, generating one...');
password = utils.generateUUID().slice(0, 8);
}
var results = {
username: install.values['admin:username'] || nconf.get('admin:username') || 'admin',
email: install.values['admin:email'] || nconf.get('admin:email') || '',
password: install.values['admin:password'] || nconf.get('admin:password') || password,
2017-02-17 19:31:21 -07:00
'password:confirm': install.values['admin:password:confirm'] || nconf.get('admin:password') || password,
};
success(null, results);
}
2014-04-14 14:10:57 -04:00
}
2016-01-25 13:36:10 +02:00
function createGlobalModeratorsGroup(next) {
var groups = require('./groups');
async.waterfall([
function (next) {
groups.exists('Global Moderators', next);
},
function (exists, next) {
if (exists) {
winston.info('Global Moderators group found, skipping creation!');
2016-03-09 22:45:28 +02:00
return next(null, null);
2016-01-25 13:36:10 +02:00
}
groups.create({
name: 'Global Moderators',
2016-02-05 18:34:58 +02:00
userTitle: 'Global Moderator',
2016-01-25 13:36:10 +02:00
description: 'Forum wide moderators',
hidden: 0,
private: 1,
2017-02-17 19:31:21 -07:00
disableJoinRequests: 1,
2016-01-25 13:36:10 +02:00
}, next);
},
function (groupData, next) {
groups.show('Global Moderators', next);
2017-02-17 19:31:21 -07:00
},
2016-01-25 13:36:10 +02:00
], next);
}
2017-12-20 15:19:22 -05:00
function giveGlobalPrivileges(next) {
var privileges = require('./privileges');
var defaultPrivileges = [
'chat', 'upload:post:image', 'signature', 'search:content',
'search:users', 'search:tags', 'view:users', 'view:tags', 'view:groups',
'local:login',
];
async.waterfall([
function (next) {
privileges.global.give(defaultPrivileges, 'registered-users', next);
},
function (next) {
privileges.global.give(defaultPrivileges.concat(['ban', 'upload:post:file', 'view:users:info']), 'Global Moderators', next);
},
function (next) {
privileges.global.give(['view:users', 'view:tags', 'view:groups'], 'guests', next);
},
function (next) {
privileges.global.give(['view:users', 'view:tags', 'view:groups'], 'spiders', next);
},
], next);
2017-12-20 15:19:22 -05:00
}
2014-04-14 14:10:57 -04:00
function createCategories(next) {
var Categories = require('./categories');
var db = require('./database');
db.getSortedSetRange('categories:cid', 0, -1, function (err, cids) {
if (err) {
return next(err);
}
if (Array.isArray(cids) && cids.length) {
console.log('Categories OK. Found ' + cids.length + ' categories.');
return next();
2014-04-14 14:10:57 -04:00
}
console.log('No categories found, populating instance with default categories');
fs.readFile(path.join(__dirname, '../', 'install/data/categories.json'), 'utf8', function (err, default_categories) {
if (err) {
return next(err);
}
default_categories = JSON.parse(default_categories);
async.eachSeries(default_categories, Categories.create, next);
});
2014-04-14 14:10:57 -04:00
});
}
2015-02-25 13:26:09 -05:00
function createMenuItems(next) {
2015-05-25 14:05:52 -04:00
var db = require('./database');
2015-02-25 13:26:09 -05:00
db.exists('navigation:enabled', function (err, exists) {
2015-05-25 14:05:52 -04:00
if (err || exists) {
return next(err);
}
2017-02-17 20:20:42 -07:00
var navigation = require('./navigation/admin');
var data = require('../install/data/navigation.json');
2015-05-25 14:05:52 -04:00
navigation.save(data, next);
});
2015-02-25 13:26:09 -05:00
}
function createWelcomePost(next) {
2017-02-17 20:20:42 -07:00
var db = require('./database');
var Topics = require('./topics');
async.parallel([
function (next) {
fs.readFile(path.join(__dirname, '../', 'install/data/welcome.md'), 'utf8', next);
},
function (next) {
2015-02-12 15:03:46 -05:00
db.getObjectField('global', 'topicCount', next);
2017-02-17 19:31:21 -07:00
},
], function (err, results) {
2015-11-25 12:20:37 -05:00
if (err) {
return next(err);
}
2017-02-17 20:20:42 -07:00
var content = results[0];
var numTopics = results[1];
2015-02-12 15:03:46 -05:00
if (!parseInt(numTopics, 10)) {
console.log('Creating welcome post!');
Topics.post({
uid: 1,
cid: 2,
title: 'Welcome to your NodeBB!',
content: content,
}, next);
} else {
next();
}
});
}
2014-04-14 14:10:57 -04:00
function enableDefaultPlugins(next) {
console.log('Enabling default plugins');
2014-04-14 14:10:57 -04:00
var defaultEnabled = [
2017-02-17 20:20:42 -07:00
'nodebb-plugin-composer-default',
'nodebb-plugin-markdown',
'nodebb-plugin-mentions',
'nodebb-widget-essentials',
'nodebb-rewards-essentials',
'nodebb-plugin-soundpack-default',
'nodebb-plugin-emoji',
'nodebb-plugin-emoji-android',
2017-02-17 20:20:42 -07:00
];
2017-10-16 13:51:47 -04:00
var customDefaults = nconf.get('defaultplugins') || nconf.get('defaultPlugins');
2015-09-02 18:17:58 -04:00
winston.info('[install/defaultPlugins] customDefaults', customDefaults);
if (customDefaults && customDefaults.length) {
try {
2018-03-22 18:33:35 -04:00
customDefaults = Array.isArray(customDefaults) ? customDefaults : JSON.parse(customDefaults);
defaultEnabled = defaultEnabled.concat(customDefaults);
} catch (e) {
// Invalid value received
2018-03-22 18:33:35 -04:00
winston.info('[install/enableDefaultPlugins] Invalid defaultPlugins value received. Ignoring.');
}
2015-08-13 12:47:59 -04:00
}
defaultEnabled = _.uniq(defaultEnabled);
2015-08-13 12:47:59 -04:00
2015-09-02 18:17:58 -04:00
winston.info('[install/enableDefaultPlugins] activating default plugins', defaultEnabled);
2015-08-13 12:49:15 -04:00
var db = require('./database');
2019-06-24 21:36:20 -04:00
var order = defaultEnabled.map((plugin, index) => index);
2015-02-23 14:57:22 -05:00
db.sortedSetAdd('plugins:active', order, defaultEnabled, next);
2014-04-14 14:10:57 -04:00
}
2014-04-15 02:02:03 -04:00
function setCopyrightWidget(next) {
2017-05-27 01:44:26 -04:00
var db = require('./database');
2015-02-12 15:03:46 -05:00
async.parallel({
footerJSON: function (next) {
fs.readFile(path.join(__dirname, '../', 'install/data/footer.json'), 'utf8', next);
2015-02-12 15:03:46 -05:00
},
footer: function (next) {
2015-02-12 15:03:46 -05:00
db.getObjectField('widgets:global', 'footer', next);
2017-02-17 19:31:21 -07:00
},
}, function (err, results) {
2015-02-12 15:03:46 -05:00
if (err) {
return next(err);
}
2015-04-23 17:03:31 -04:00
2015-02-12 15:03:46 -05:00
if (!results.footer && results.footerJSON) {
db.setObjectField('widgets:global', 'footer', results.footerJSON, next);
2015-02-12 15:03:46 -05:00
} else {
next();
}
2015-04-23 17:03:31 -04:00
});
2014-04-14 14:10:57 -04:00
}
install.setup = function (callback) {
2014-11-11 17:25:16 -05:00
async.series([
checkSetupFlag,
checkCIFlag,
setupConfig,
setupDefaultConfigs,
enableDefaultTheme,
createCategories,
createAdministrator,
2016-01-25 13:36:10 +02:00
createGlobalModeratorsGroup,
2017-12-20 15:19:22 -05:00
giveGlobalPrivileges,
2015-02-25 13:26:09 -05:00
createMenuItems,
2014-11-11 17:25:16 -05:00
createWelcomePost,
enableDefaultPlugins,
setCopyrightWidget,
2014-04-14 14:10:57 -04:00
function (next) {
2015-04-23 17:03:31 -04:00
var upgrade = require('./upgrade');
upgrade.check(function (err) {
if (err && err.message === 'schema-out-of-date') {
upgrade.run(next);
} else if (err) {
return next(err);
2017-02-17 22:11:35 -07:00
} else {
next();
}
});
2017-02-17 19:31:21 -07:00
},
], function (err, results) {
2014-04-14 14:10:57 -04:00
if (err) {
winston.warn('NodeBB Setup Aborted.\n ' + err.stack);
2018-01-15 15:05:33 -05:00
process.exit(1);
2014-04-14 14:10:57 -04:00
} else {
var data = {};
if (results[6]) {
2015-04-22 13:58:43 -04:00
data.username = results[6].username;
data.password = results[6].password;
}
callback(null, data);
2014-04-14 14:10:57 -04:00
}
});
};
install.save = function (server_conf, callback) {
2017-05-27 01:44:26 -04:00
var serverConfigPath = path.join(__dirname, '../config.json');
if (nconf.get('config')) {
serverConfigPath = path.resolve(__dirname, '../', nconf.get('config'));
}
fs.writeFile(serverConfigPath, JSON.stringify(server_conf, null, 4), function (err) {
if (err) {
winston.error('Error saving server configuration!', err);
return callback(err);
}
console.log('Configuration Saved OK');
nconf.file({
file: serverConfigPath,
});
callback();
});
};