Files
NodeBB/src/install.js

550 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'),
2014-04-01 11:19:12 -04:00
utils = require('../public/src/utils.js'),
DATABASES = {
"redis": {
2014-07-02 14:07:08 -04:00
"dependencies": ["redis@~0.10.1", "connect-redis@~2.0.0"]
},
"mongo": {
"dependencies": ["mongodb@~2.0.0", "connect-mongo"]
}
};
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',
'default': nconf.get('database') || 'redis'
}
];
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();
}
2014-04-14 14:10:57 -04:00
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);
});
});
2014-04-14 14:10:57 -04:00
} else {
configureDatabases(err, config, DATABASES, 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-01-07 10:09:02 -05:00
question, x, numQ, allQuestions = questions.main.concat(questions.optional).concat(redisQuestions).concat(mongoQuestions);
2014-04-14 14:10:57 -04:00
for(x=0,numQ=allQuestions.length;x<numQ;x++) {
question = allQuestions[x];
2015-01-07 10:09:02 -05:00
config[question.name] = install.values[question.name] || question['default'] || undefined;
2014-04-14 14:10:57 -04:00
}
2013-11-21 17:41:27 -05:00
configureDatabases(null, config, DATABASES, 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);
}
2014-11-29 21:54:58 -05:00
setupDatabase(config, next);
2014-04-15 11:55:54 -04:00
});
}
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) {
2014-04-15 11:55:54 -04:00
var npm = require('npm'),
packages = [];
2014-04-15 12:14:16 -04:00
npm.load({}, function(err) {
2014-04-15 11:55:54 -04:00
if (err) {
2014-11-04 17:57:31 -05:00
return next(err);
2014-04-15 11:55:54 -04:00
}
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);
2014-04-15 11:55:54 -04:00
}
npm.commands.install(packages, 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
2014-05-05 15:19:37 -04:00
async.each(defaults, function (configObj, next) {
meta.configs.setOnEmpty(configObj.field, configObj.value, next);
}, function (err) {
meta.configs.init(next);
});
2014-04-14 14:10:57 -04:00
2014-05-05 15:19:37 -04:00
if (install.values) {
setIfPaired('social:twitter:key', 'social:twitter:secret');
setIfPaired('social:google:id', 'social:google:secret');
setIfPaired('social:facebook:app_id', 'social:facebook:secret');
2014-06-29 14:29:32 -04:00
}
}
function setIfPaired(key1, key2) {
var meta = require('./meta');
2014-06-29 14:29:32 -04:00
if (install.values[key1] && install.values[key2]) {
meta.configs.setOnEmpty(key1, install.values[key1]);
meta.configs.setOnEmpty(key2, install.values[key2]);
2014-05-05 15:19:37 -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);
}
process.stdout.write('Enabling default theme: Lavender\n');
2014-08-07 16:02:18 -04:00
meta.themes.set({
type: 'local',
id: 'nodebb-theme-lavender'
}, next);
});
2014-04-14 14:10:57 -04:00
}
function createAdministrator(next) {
var Groups = require('./groups');
Groups.get('administrators', {}, function (err, groupObj) {
if (!err && groupObj && groupObj.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
}
User.create({username: results.username, password: results.password, email: results.email}, function (err, uid) {
2014-03-18 18:15:07 -04:00
if (err) {
winston.warn(err.message + ' Please try again.');
return callback(new Error('invalid-values'));
}
2014-03-18 18:15:07 -04:00
Groups.join('administrators', uid, function(err) {
callback(err, 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
}
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) {
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) {
2015-02-12 15:03:46 -05:00
db.getObjectField('global', 'topicCount', next);
}
], function(err, results) {
var content = results[0],
numTopics = results[1];
2015-02-12 15:03:46 -05:00
if (!parseInt(numTopics, 10)) {
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) {
var Plugins = require('./plugins');
process.stdout.write('Enabling default plugins\n');
2014-04-14 14:10:57 -04:00
var defaultEnabled = [
'nodebb-plugin-markdown',
'nodebb-plugin-mentions',
'nodebb-widget-essentials',
2015-02-20 16:09:20 -05:00
'nodebb-rewards-essentials',
'nodebb-plugin-soundpack-default'
2014-04-14 14:10:57 -04:00
];
2014-09-29 18:36:14 -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,
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;