Files
NodeBB/src/install.js

561 lines
14 KiB
JavaScript
Raw Normal View History

2014-03-18 18:15:07 -04:00
'use strict';
2013-09-17 13:09:37 -04:00
var async = require('async'),
fs = require('fs'),
path = require('path'),
prompt = require('prompt'),
winston = require('winston'),
nconf = require('nconf'),
2015-12-21 12:02:10 +02:00
utils = require('../public/src/utils.js');
var install = {},
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',
2014-11-29 21:54:58 -05:00
'default':
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',
'default': nconf.get('secret') || utils.generateUUID()
},
{
name: 'database',
description: 'Which database to use',
2015-12-21 12:02:10 +02:00
'default': nconf.get('database') || 'mongo'
2014-04-14 14:10:57 -04:00
}
];
2015-01-07 10:09:02 -05:00
questions.optional = [
{
name: 'port',
default: nconf.get('port') || 4567
2015-01-07 10:09:02 -05:00
}
];
function checkSetupFlag(next) {
var envSetupKeys = ['database'],
setupVal;
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) {
2014-04-14 14:10:57 -04:00
setupVal = undefined;
}
2014-04-14 14:10:57 -04:00
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');
}
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();
}
} else if (envSetupKeys.every(function(key) {
return nconf.stores.env.store.hasOwnProperty(key);
})) {
install.values = envSetupKeys.reduce(function(config, key) {
config[key] = nconf.stores.env.store[key];
return config;
}, {});
next();
2014-04-14 14:10:57 -04:00
} else {
next();
}
}
function checkCIFlag(next) {
2014-04-14 14:10:57 -04:00
var ciVals;
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
2014-04-14 14:10:57 -04:00
if (!install.values) {
prompt.get(questions.main, function(err, config) {
if (err) {
process.stdout.write('\n\n');
winston.warn('NodeBB setup ' + err.message);
process.exit();
}
2015-12-21 12:02:10 +02:00
configureDatabases(config, function(err, config) {
completeConfigSetup(err, config, next);
});
2014-04-14 14:10:57 -04:00
});
} else {
// Use provided values, fall back to defaults
var config = {},
2014-04-14 15:26:42 -04:00
redisQuestions = require('./database/redis').questions,
mongoQuestions = require('./database/mongo').questions,
2015-12-21 12:19:12 +02:00
allQuestions = questions.main.concat(questions.optional).concat(redisQuestions).concat(mongoQuestions);
2015-12-21 12:02:10 +02:00
allQuestions.forEach(function (question) {
2015-01-07 10:09:02 -05:00
config[question.name] = install.values[question.name] || question['default'] || undefined;
2015-12-21 12:02:10 +02:00
});
2013-11-21 17:41:27 -05:00
2015-12-21 12:02:10 +02:00
configureDatabases(config, function(err, config) {
completeConfigSetup(err, config, next);
});
}
}
function completeConfigSetup(err, config, next) {
2014-07-31 08:23:07 -04:00
if (err) {
return next(err);
}
2014-11-29 21:54:58 -05:00
// 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
}
2014-11-29 21:54:58 -05:00
install.save(config, function(err) {
if (err) {
return next(err);
}
require('./database').init(next);
});
}
2014-04-14 14:10:57 -04:00
function setupDefaultConfigs(next) {
process.stdout.write('Populating database with default configs, if not already set...\n');
var meta = require('./meta'),
defaults = require(path.join(__dirname, '../', 'install/data/defaults.json'));
2014-05-20 14:44:50 -04:00
2015-05-23 22:11:20 -04:00
async.each(Object.keys(defaults), function (key, next) {
meta.configs.setOnEmpty(key, defaults[key], next);
2014-05-05 15:19:37 -04:00
}, 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) {
var meta = require('./meta');
2014-04-14 14:10:57 -04:00
2014-08-07 16:02:18 -04:00
meta.configs.get('theme:id', function(err, id) {
if (err || id) {
process.stdout.write('Previous theme detected, skipping enabling default theme\n');
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';
process.stdout.write('Enabling default theme: ' + defaultTheme + '\n');
2014-08-07 16:02:18 -04:00
meta.themes.set({
type: 'local',
2015-09-11 18:48:29 -04: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) {
process.stdout.write('Administrator found, skipping Admin setup\n');
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) {
var User = require('./user'),
Groups = require('./groups'),
password;
2013-09-07 15:49:23 -04:00
winston.warn('No administrators have been detected, running initial user setup\n');
var questions = [{
name: 'username',
description: 'Administrator username',
required: true,
type: 'string'
}, {
name: 'email',
description: 'Administrator email address',
pattern: /.+@.+/,
required: true
}],
passwordQuestions = [{
name: 'password',
description: 'Password',
required: true,
hidden: true,
type: 'string'
}, {
name: 'password:confirm',
description: 'Confirm Password',
required: true,
hidden: true,
type: 'string'
}],
success = function(err, results) {
if (!results) {
return callback(new Error('aborted'));
}
if (results['password:confirm'] !== results.password) {
winston.warn("Passwords did not match, please try again");
return retryPassword(results);
2014-02-14 11:49:16 -05:00
}
2015-09-10 18:13:26 -04:00
var adminUid;
2015-06-15 16:55:29 -04:00
async.waterfall([
function(next) {
User.create({username: results.username, password: results.password, email: results.email}, next);
},
function(uid, next) {
2015-09-10 18:13:26 -04:00
adminUid = uid;
2015-06-15 16:55:29 -04:00
Groups.join('administrators', uid, next);
},
function(next) {
Groups.show('administrators', next);
2015-09-10 18:13:26 -04:00
},
function(next) {
Groups.ownership.grant(adminUid, 'administrators', next);
2015-06-15 16:55:29 -04:00
}
], function(err) {
2014-03-18 18:15:07 -04:00
if (err) {
2015-06-15 16:55:29 -04:00
return callback(err);
}
2015-06-15 16:55:29 -04:00
callback(null, password ? results : undefined);
});
},
retryPassword = function (originalResults) {
// Ask only the password questions
prompt.get(passwordQuestions, function (err, results) {
if (!results) {
return callback(new Error('aborted'));
}
2013-09-10 16:26:26 -04:00
// Update the original data with newly collected password
originalResults.password = results.password;
originalResults['password:confirm'] = results['password:confirm'];
2013-09-10 16:26:26 -04:00
// 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')) {
2015-04-22 13:58:43 -04:00
process.stdout.write('Password was not provided during automated setup, generating one...\n');
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,
'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,
disableJoinRequests: 1
}, next);
},
function (groupData, next) {
groups.show('Global Moderators', next);
}
], next);
}
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) {
process.stdout.write('Categories OK. Found ' + categoryData.length + ' categories.\n');
return next();
2014-04-14 14:10:57 -04:00
}
process.stdout.write('No categories found, populating instance with default categories\n');
fs.readFile(path.join(__dirname, '../', 'install/data/categories.json'), 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
2015-05-25 14:05:52 -04:00
db.exists('navigation:enabled', function(err, exists) {
if (err || exists) {
return next(err);
}
var navigation = require('./navigation/admin'),
data = require('../install/data/navigation.json');
navigation.save(data, next);
});
2015-02-25 13:26:09 -05:00
}
function createWelcomePost(next) {
var db = require('./database'),
Topics = require('./topics');
async.parallel([
function(next) {
fs.readFile(path.join(__dirname, '../', 'install/data/welcome.md'), next);
},
function(next) {
2015-02-12 15:03:46 -05:00
db.getObjectField('global', 'topicCount', next);
}
], function(err, results) {
2015-11-25 12:20:37 -05:00
if (err) {
return next(err);
}
var content = results[0],
numTopics = results[1];
2015-02-12 15:03:46 -05:00
if (!parseInt(numTopics, 10)) {
2015-11-25 12:20:37 -05:00
process.stdout.write('Creating welcome post!\n');
Topics.post({
uid: 1,
cid: 2,
title: 'Welcome to your NodeBB!',
content: content.toString()
}, next);
} else {
next();
}
});
}
2014-04-14 14:10:57 -04:00
function enableDefaultPlugins(next) {
process.stdout.write('Enabling default plugins\n');
2014-04-14 14:10:57 -04:00
var defaultEnabled = [
'nodebb-plugin-composer-default',
'nodebb-plugin-markdown',
'nodebb-plugin-mentions',
'nodebb-widget-essentials',
'nodebb-rewards-essentials',
'nodebb-plugin-soundpack-default',
2016-04-05 21:01:13 +03:00
'nodebb-plugin-emoji-extended',
'nodebb-plugin-emoji-apple'
],
customDefaults = nconf.get('defaultPlugins');
2015-09-02 18:17:58 -04:00
winston.info('[install/defaultPlugins] customDefaults', customDefaults);
if (customDefaults && customDefaults.length) {
try {
customDefaults = JSON.parse(customDefaults);
defaultEnabled = defaultEnabled.concat(customDefaults);
} catch (e) {
// Invalid value received
winston.warn('[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');
2015-02-23 14:57:22 -05:00
var order = defaultEnabled.map(function(plugin, index) {
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) {
2014-09-29 18:36:14 -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'), next);
},
footer: function(next) {
db.getObjectField('widgets:global', 'footer', next);
2014-04-14 14:10:57 -04:00
}
2015-02-12 15:03:46 -05:00
}, function(err, results) {
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) {
2015-04-23 17:03:31 -04:00
db.setObjectField('widgets:global', 'footer', results.footerJSON.toString(), 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) {
2015-04-23 17:03:31 -04:00
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,
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, uptodate) {
if (err) {
return next(err);
}
if (!uptodate) { upgrade.upgrade(next); }
else { next(); }
});
2014-04-14 14:10:57 -04:00
}
], function (err, results) {
2014-04-14 14:10:57 -04:00
if (err) {
winston.warn('NodeBB Setup Aborted.\n ' + err.stack);
2014-04-14 14:10:57 -04:00
process.exit();
} 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) {
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.message);
return callback(err);
}
process.stdout.write('Configuration Saved OK\n');
nconf.file({
file: path.join(__dirname, '..', 'config.json')
});
callback();
});
};
2014-04-10 20:31:57 +01:00
module.exports = install;