Files
NodeBB/src/install.js

567 lines
14 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 path = require('path');
var prompt = require('prompt');
var winston = require('winston');
var nconf = require('nconf');
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:
2014-11-29 21:54:58 -05:00
nconf.get('url') ||
(nconf.get('base_url') ? (nconf.get('base_url') + (nconf.get('use_port') ? ':' + nconf.get('port') : '')) : null) || // backwards compatibility (remove for v0.7.0)
'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: '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 allQuestions = questions.main.concat(questions.optional).concat(redisQuestions).concat(mongoQuestions);
allQuestions.forEach(function (question) {
config[question.name] = install.values[question.name] || question.default || 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-01-15 15:05:33 -05: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) {
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');
2018-02-28 17:38:31 -05:00
privileges.global.give(['chat', 'upload:post:image', 'signature'], 'registered-users', next);
2017-12-20 15:19:22 -05:00
}
2014-04-14 14:10:57 -04:00
function createCategories(next) {
var Categories = require('./categories');
2014-08-22 19:10:26 -04:00
Categories.getAllCategories(0, function (err, categoryData) {
if (err) {
return next(err);
}
if (Array.isArray(categoryData) && categoryData.length) {
console.log('Categories OK. Found ' + categoryData.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 = defaultEnabled.filter(function (plugin, index, array) {
2015-08-13 16:03:15 -04:00
return array.indexOf(plugin) === index;
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');
var order = defaultEnabled.map(function (plugin, index) {
2015-02-23 14:57:22 -05:00
return index;
});
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();
});
};