You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/install.js

592 lines
14 KiB
JavaScript

11 years ago
'use strict';
var async = require('async'),
fs = require('fs'),
path = require('path'),
prompt = require('prompt'),
winston = require('winston'),
nconf = require('nconf'),
utils = require('../public/src/utils.js'),
DATABASES = {
"redis": {
11 years ago
"dependencies": ["redis@~0.10.1", "connect-redis@~2.0.0"]
},
"mongo": {
"dependencies": ["mongodb@~2.0.0", "connect-mongo"]
}
};
var install = {},
questions = {};
questions.main = [
{
10 years ago
name: 'url',
11 years ago
description: 'URL used to access this NodeBB',
10 years ago
'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',
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',
'default': nconf.get('database') || 'redis'
}
];
10 years ago
questions.optional = [
{
name: 'port',
default: nconf.get('port') || 4567
10 years ago
}
];
function checkSetupFlag(next) {
var envSetupKeys = ['database'],
setupVal;
try {
10 years ago
if (nconf.get('setup')) {
setupVal = JSON.parse(nconf.get('setup'));
}
} catch (err) {
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');
}
11 years ago
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();
} else {
next();
}
}
function checkCIFlag(next) {
var ciVals;
try {
ciVals = JSON.parse(nconf.get('ci'));
} catch (e) {
ciVals = undefined;
}
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');
}
11 years ago
process.exit();
}
} else {
next();
}
}
11 years ago
function setupConfig(next) {
11 years ago
var configureDatabases = require('../install/databases');
// prompt prepends "prompt: " to questions, let's clear that.
prompt.start();
prompt.message = '';
prompt.delimiter = '';
prompt.colors = false;
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();
}
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, DATABASES, function(err, config) {
completeConfigSetup(err, config, next);
});
});
} else {
configureDatabases(err, config, DATABASES, function(err, config) {
completeConfigSetup(err, config, next);
});
}
});
} else {
// Use provided values, fall back to defaults
var config = {},
redisQuestions = require('./database/redis').questions,
mongoQuestions = require('./database/mongo').questions,
10 years ago
question, x, numQ, allQuestions = questions.main.concat(questions.optional).concat(redisQuestions).concat(mongoQuestions);
for(x=0,numQ=allQuestions.length;x<numQ;x++) {
question = allQuestions[x];
10 years ago
config[question.name] = install.values[question.name] || question['default'] || undefined;
}
11 years ago
configureDatabases(null, config, DATABASES, function(err, config) {
completeConfigSetup(err, config, next);
});
}
}
function completeConfigSetup(err, config, next) {
11 years ago
if (err) {
return next(err);
}
10 years ago
// 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];
}
}
}
10 years ago
install.save(config, function(err) {
if (err) {
return next(err);
}
10 years ago
setupDatabase(config, next);
11 years ago
});
}
function setupDatabase(server_conf, next) {
install.installDbDependencies(server_conf, function(err) {
if (err) {
return next(err);
}
require('./database').init(next);
});
}
install.installDbDependencies = function(server_conf, next) {
11 years ago
var npm = require('npm'),
packages = [];
npm.load({}, function(err) {
11 years ago
if (err) {
return next(err);
11 years ago
}
npm.config.set('spin', false);
packages = packages.concat(DATABASES[server_conf.database].dependencies);
if (server_conf.secondary_database) {
packages = packages.concat(DATABASES[server_conf.secondary_database].dependencies);
11 years ago
}
npm.commands.install(packages, next);
});
};
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'));
11 years ago
10 years ago
async.each(Object.keys(defaults), function (key, next) {
meta.configs.setOnEmpty(key, defaults[key], next);
11 years ago
}, function (err) {
10 years ago
if (err) {
return next(err);
}
10 years ago
if (install.values) {
async.parallel([
async.apply(setIfPaired, 'social:twitter:key', 'social:twitter:secret'),
async.apply(setIfPaired, 'social:google:id', 'social:google:secret'),
async.apply(setIfPaired, 'social:facebook:app_id', 'social:facebook:secret')
], function(err) {
if (err) {
return next(err);
}
meta.configs.init(next);
});
} else {
meta.configs.init(next);
}
});
11 years ago
}
10 years ago
function setIfPaired(key1, key2, callback) {
var meta = require('./meta');
11 years ago
if (install.values[key1] && install.values[key2]) {
10 years ago
async.parallel([
async.apply(meta.configs.setOnEmpty, key1, install.values[key1]),
async.apply(meta.configs.setOnEmpty, key2, install.values[key2])
], callback);
} else {
callback();
11 years ago
}
}
function enableDefaultTheme(next) {
var meta = require('./meta');
11 years ago
meta.configs.get('theme:id', function(err, id) {
if (err || id) {
process.stdout.write('Previous theme detected, skipping enabling default theme\n');
11 years ago
return next(err);
}
process.stdout.write('Enabling default theme: Persona\n');
11 years ago
meta.themes.set({
type: 'local',
id: 'nodebb-theme-persona'
11 years ago
}, next);
});
}
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');
next();
} else {
createAdmin(next);
}
});
}
function createAdmin(callback) {
var User = require('./user'),
Groups = require('./groups'),
password;
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);
11 years ago
}
10 years ago
async.waterfall([
function(next) {
User.create({username: results.username, password: results.password, email: results.email}, next);
},
function(uid, next) {
Groups.join('administrators', uid, next);
},
function(next) {
Groups.show('administrators', next);
}
], function(err) {
11 years ago
if (err) {
10 years ago
return callback(err);
}
10 years ago
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'));
}
12 years ago
// Update the original data with newly collected password
originalResults.password = results.password;
originalResults['password:confirm'] = results['password:confirm'];
12 years ago
// 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')) {
10 years ago
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);
}
}
function createCategories(next) {
var Categories = require('./categories');
11 years ago
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();
}
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);
});
});
}
function createMenuItems(next) {
10 years ago
var db = require('./database');
10 years ago
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);
});
}
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) {
10 years ago
db.getObjectField('global', 'topicCount', next);
}
], function(err, results) {
var content = results[0],
numTopics = results[1];
10 years ago
if (!parseInt(numTopics, 10)) {
Topics.post({
uid: 1,
cid: 2,
title: 'Welcome to your NodeBB!',
content: content.toString()
}, next);
} else {
next();
}
});
}
function enableDefaultPlugins(next) {
var Plugins = require('./plugins');
process.stdout.write('Enabling default plugins\n');
var defaultEnabled = [
'nodebb-plugin-composer-default',
'nodebb-plugin-markdown',
'nodebb-plugin-mentions',
'nodebb-widget-essentials',
'nodebb-rewards-essentials',
'nodebb-plugin-soundpack-default',
'nodebb-plugin-emoji-extended'
];
if (Array.isArray(nconf.get('defaultPlugins'))) {
defaultEnabled = defaultEnabled.concat(nconf.get('defaultPlugins'));
}
defaultEnabled = defaultEnabled.filter(function(plugin, index, array) {
return array.indexOf(plugin) === -1;
});
10 years ago
var db = require('./database');
var order = defaultEnabled.map(function(plugin, index) {
return index;
});
db.sortedSetAdd('plugins:active', order, defaultEnabled, next);
}
function setCopyrightWidget(next) {
10 years ago
var db = require('./database');
10 years ago
async.parallel({
footerJSON: function(next) {
fs.readFile(path.join(__dirname, '../', 'install/data/footer.json'), next);
},
footer: function(next) {
db.getObjectField('widgets:global', 'footer', next);
}
10 years ago
}, function(err, results) {
if (err) {
return next(err);
}
10 years ago
10 years ago
if (!results.footer && results.footerJSON) {
10 years ago
db.setObjectField('widgets:global', 'footer', results.footerJSON.toString(), next);
10 years ago
} else {
next();
}
10 years ago
});
}
install.setup = function (callback) {
10 years ago
async.series([
checkSetupFlag,
checkCIFlag,
setupConfig,
setupDefaultConfigs,
enableDefaultTheme,
createCategories,
createAdministrator,
createMenuItems,
createWelcomePost,
enableDefaultPlugins,
setCopyrightWidget,
function (next) {
10 years ago
var upgrade = require('./upgrade');
upgrade.check(function(err, uptodate) {
if (err) {
return next(err);
}
if (!uptodate) { upgrade.upgrade(next); }
else { next(); }
});
}
], function (err, results) {
if (err) {
winston.warn('NodeBB Setup Aborted.\n ' + err.stack);
process.exit();
} else {
var data = {};
if (results[6]) {
10 years ago
data.username = results[6].username;
data.password = results[6].password;
}
callback(null, data);
}
});
};
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();
});
};
module.exports = install;