mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-02-28 01:21:13 +01:00
refactor: rewrite src/install with async/await
This commit is contained in:
@@ -1,58 +1,51 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const prompt = require('prompt');
|
||||||
var prompt = require('prompt');
|
const winston = require('winston');
|
||||||
var winston = require('winston');
|
|
||||||
|
|
||||||
var questions = {
|
const util = require('util');
|
||||||
|
|
||||||
|
const promptGet = util.promisify((schema, callback) => prompt.get(schema, callback));
|
||||||
|
|
||||||
|
const questions = {
|
||||||
redis: require('../src/database/redis').questions,
|
redis: require('../src/database/redis').questions,
|
||||||
mongo: require('../src/database/mongo').questions,
|
mongo: require('../src/database/mongo').questions,
|
||||||
postgres: require('../src/database/postgres').questions,
|
postgres: require('../src/database/postgres').questions,
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = function (config, callback) {
|
module.exports = async function (config) {
|
||||||
async.waterfall([
|
winston.info('\nNow configuring ' + config.database + ' database:');
|
||||||
function (next) {
|
const databaseConfig = await getDatabaseConfig(config);
|
||||||
winston.info('\nNow configuring ' + config.database + ' database:');
|
return saveDatabaseConfig(config, databaseConfig);
|
||||||
getDatabaseConfig(config, next);
|
|
||||||
},
|
|
||||||
function (databaseConfig, next) {
|
|
||||||
saveDatabaseConfig(config, databaseConfig, next);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function getDatabaseConfig(config, callback) {
|
async function getDatabaseConfig(config) {
|
||||||
if (!config) {
|
if (!config) {
|
||||||
return callback(new Error('aborted'));
|
throw new Error('invalid config, aborted');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config.database === 'redis') {
|
if (config.database === 'redis') {
|
||||||
if (config['redis:host'] && config['redis:port']) {
|
if (config['redis:host'] && config['redis:port']) {
|
||||||
callback(null, config);
|
return config;
|
||||||
} else {
|
|
||||||
prompt.get(questions.redis, callback);
|
|
||||||
}
|
}
|
||||||
|
return await promptGet(questions.redis);
|
||||||
} else if (config.database === 'mongo') {
|
} else if (config.database === 'mongo') {
|
||||||
if ((config['mongo:host'] && config['mongo:port']) || config['mongo:uri']) {
|
if ((config['mongo:host'] && config['mongo:port']) || config['mongo:uri']) {
|
||||||
callback(null, config);
|
return config;
|
||||||
} else {
|
|
||||||
prompt.get(questions.mongo, callback);
|
|
||||||
}
|
}
|
||||||
|
return await promptGet(questions.mongo);
|
||||||
} else if (config.database === 'postgres') {
|
} else if (config.database === 'postgres') {
|
||||||
if (config['postgres:host'] && config['postgres:port']) {
|
if (config['postgres:host'] && config['postgres:port']) {
|
||||||
callback(null, config);
|
return config;
|
||||||
} else {
|
|
||||||
prompt.get(questions.postgres, callback);
|
|
||||||
}
|
}
|
||||||
} else {
|
return await promptGet(questions.postgres);
|
||||||
return callback(new Error('unknown database : ' + config.database));
|
|
||||||
}
|
}
|
||||||
|
throw new Error('unknown database : ' + config.database);
|
||||||
}
|
}
|
||||||
|
|
||||||
function saveDatabaseConfig(config, databaseConfig, callback) {
|
function saveDatabaseConfig(config, databaseConfig) {
|
||||||
if (!databaseConfig) {
|
if (!databaseConfig) {
|
||||||
return callback(new Error('aborted'));
|
throw new Error('invalid config, aborted');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Translate redis properties into redis object
|
// Translate redis properties into redis object
|
||||||
@@ -86,13 +79,13 @@ function saveDatabaseConfig(config, databaseConfig, callback) {
|
|||||||
ssl: databaseConfig['postgres:ssl'],
|
ssl: databaseConfig['postgres:ssl'],
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
return callback(new Error('unknown database : ' + config.database));
|
throw new Error('unknown database : ' + config.database);
|
||||||
}
|
}
|
||||||
|
|
||||||
var allQuestions = questions.redis.concat(questions.mongo).concat(questions.postgres);
|
const allQuestions = questions.redis.concat(questions.mongo).concat(questions.postgres);
|
||||||
for (var x = 0; x < allQuestions.length; x += 1) {
|
for (var x = 0; x < allQuestions.length; x += 1) {
|
||||||
delete config[allQuestions[x].name];
|
delete config[allQuestions[x].name];
|
||||||
}
|
}
|
||||||
|
|
||||||
callback(null, config);
|
return config;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,7 +23,9 @@ function setup(initConfig) {
|
|||||||
install.values = initConfig;
|
install.values = initConfig;
|
||||||
|
|
||||||
async.series([
|
async.series([
|
||||||
install.setup,
|
async function () {
|
||||||
|
return await install.setup();
|
||||||
|
},
|
||||||
function (next) {
|
function (next) {
|
||||||
var configFile = paths.config;
|
var configFile = paths.config;
|
||||||
if (nconf.get('config')) {
|
if (nconf.get('config')) {
|
||||||
|
|||||||
629
src/install.js
629
src/install.js
@@ -1,18 +1,20 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const fs = require('fs');
|
||||||
var fs = require('fs');
|
const url = require('url');
|
||||||
var url = require('url');
|
const path = require('path');
|
||||||
var path = require('path');
|
const prompt = require('prompt');
|
||||||
var prompt = require('prompt');
|
const winston = require('winston');
|
||||||
var winston = require('winston');
|
const nconf = require('nconf');
|
||||||
var nconf = require('nconf');
|
const _ = require('lodash');
|
||||||
var _ = require('lodash');
|
const util = require('util');
|
||||||
|
|
||||||
var utils = require('./utils.js');
|
const promptGet = util.promisify((schema, callback) => prompt.get(schema, callback));
|
||||||
|
|
||||||
var install = module.exports;
|
const utils = require('./utils.js');
|
||||||
var questions = {};
|
|
||||||
|
const install = module.exports;
|
||||||
|
const questions = {};
|
||||||
|
|
||||||
questions.main = [
|
questions.main = [
|
||||||
{
|
{
|
||||||
@@ -47,8 +49,8 @@ questions.optional = [
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
function checkSetupFlag(next) {
|
function checkSetupFlag() {
|
||||||
var setupVal = install.values;
|
let setupVal = install.values;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (nconf.get('setup')) {
|
if (nconf.get('setup')) {
|
||||||
@@ -61,7 +63,6 @@ function checkSetupFlag(next) {
|
|||||||
if (setupVal && typeof setupVal === 'object') {
|
if (setupVal && typeof setupVal === 'object') {
|
||||||
if (setupVal['admin:username'] && setupVal['admin:password'] && setupVal['admin:password:confirm'] && setupVal['admin:email']) {
|
if (setupVal['admin:username'] && setupVal['admin:password'] && setupVal['admin:password:confirm'] && setupVal['admin:email']) {
|
||||||
install.values = setupVal;
|
install.values = setupVal;
|
||||||
next();
|
|
||||||
} else {
|
} else {
|
||||||
winston.error('Required values are missing for automated setup:');
|
winston.error('Required values are missing for automated setup:');
|
||||||
if (!setupVal['admin:username']) {
|
if (!setupVal['admin:username']) {
|
||||||
@@ -82,13 +83,10 @@ function checkSetupFlag(next) {
|
|||||||
} else if (nconf.get('database')) {
|
} else if (nconf.get('database')) {
|
||||||
install.values = install.values || {};
|
install.values = install.values || {};
|
||||||
install.values.database = nconf.get('database');
|
install.values.database = nconf.get('database');
|
||||||
next();
|
|
||||||
} else {
|
|
||||||
next();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkCIFlag(next) {
|
function checkCIFlag() {
|
||||||
var ciVals;
|
var ciVals;
|
||||||
try {
|
try {
|
||||||
ciVals = JSON.parse(nconf.get('ci'));
|
ciVals = JSON.parse(nconf.get('ci'));
|
||||||
@@ -99,7 +97,6 @@ function checkCIFlag(next) {
|
|||||||
if (ciVals && ciVals instanceof Object) {
|
if (ciVals && ciVals instanceof Object) {
|
||||||
if (ciVals.hasOwnProperty('host') && ciVals.hasOwnProperty('port') && ciVals.hasOwnProperty('database')) {
|
if (ciVals.hasOwnProperty('host') && ciVals.hasOwnProperty('port') && ciVals.hasOwnProperty('database')) {
|
||||||
install.ciVals = ciVals;
|
install.ciVals = ciVals;
|
||||||
next();
|
|
||||||
} else {
|
} else {
|
||||||
winston.error('Required values are missing for automated CI integration:');
|
winston.error('Required values are missing for automated CI integration:');
|
||||||
if (!ciVals.hasOwnProperty('host')) {
|
if (!ciVals.hasOwnProperty('host')) {
|
||||||
@@ -114,54 +111,43 @@ function checkCIFlag(next) {
|
|||||||
|
|
||||||
process.exit();
|
process.exit();
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
next();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function setupConfig(next) {
|
async function setupConfig() {
|
||||||
var configureDatabases = require('../install/databases');
|
const configureDatabases = require('../install/databases');
|
||||||
|
|
||||||
// prompt prepends "prompt: " to questions, let's clear that.
|
// prompt prepends "prompt: " to questions, let's clear that.
|
||||||
prompt.start();
|
prompt.start();
|
||||||
prompt.message = '';
|
prompt.message = '';
|
||||||
prompt.delimiter = '';
|
prompt.delimiter = '';
|
||||||
prompt.colors = false;
|
prompt.colors = false;
|
||||||
|
let config = {};
|
||||||
|
|
||||||
async.waterfall([
|
if (install.values) {
|
||||||
function (next) {
|
// Use provided values, fall back to defaults
|
||||||
if (install.values) {
|
const redisQuestions = require('./database/redis').questions;
|
||||||
// Use provided values, fall back to defaults
|
const mongoQuestions = require('./database/mongo').questions;
|
||||||
var config = {};
|
const postgresQuestions = require('./database/postgres').questions;
|
||||||
var redisQuestions = require('./database/redis').questions;
|
const allQuestions = questions.main.concat(questions.optional).concat(redisQuestions).concat(mongoQuestions).concat(postgresQuestions);
|
||||||
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);
|
|
||||||
|
|
||||||
allQuestions.forEach(function (question) {
|
allQuestions.forEach(function (question) {
|
||||||
if (install.values.hasOwnProperty(question.name)) {
|
if (install.values.hasOwnProperty(question.name)) {
|
||||||
config[question.name] = install.values[question.name];
|
config[question.name] = install.values[question.name];
|
||||||
} else if (question.hasOwnProperty('default')) {
|
} else if (question.hasOwnProperty('default')) {
|
||||||
config[question.name] = question.default;
|
config[question.name] = question.default;
|
||||||
} else {
|
|
||||||
config[question.name] = undefined;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
setImmediate(next, null, config);
|
|
||||||
} else {
|
} else {
|
||||||
prompt.get(questions.main, next);
|
config[question.name] = undefined;
|
||||||
}
|
}
|
||||||
},
|
});
|
||||||
function (config, next) {
|
} else {
|
||||||
configureDatabases(config, next);
|
config = await promptGet(questions.main);
|
||||||
},
|
}
|
||||||
function (config, next) {
|
await configureDatabases(config);
|
||||||
completeConfigSetup(config, next);
|
await completeConfigSetup(config);
|
||||||
},
|
|
||||||
], next);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function completeConfigSetup(config, next) {
|
async function completeConfigSetup(config) {
|
||||||
// Add CI object
|
// Add CI object
|
||||||
if (install.ciVals) {
|
if (install.ciVals) {
|
||||||
config.test_database = {};
|
config.test_database = {};
|
||||||
@@ -177,101 +163,81 @@ function completeConfigSetup(config, next) {
|
|||||||
config.package_manager = nconf.get('package_manager');
|
config.package_manager = nconf.get('package_manager');
|
||||||
}
|
}
|
||||||
nconf.overrides(config);
|
nconf.overrides(config);
|
||||||
async.waterfall([
|
const db = require('./database');
|
||||||
function (next) {
|
await db.init();
|
||||||
require('./database').init(next);
|
await db.createIndices();
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
require('./database').createIndices(next);
|
|
||||||
},
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove trailing slash from non-subfolder installs
|
// Sanity-check/fix url/port
|
||||||
if (urlObj.path === '/') {
|
if (!/^http(?:s)?:\/\//.test(config.url)) {
|
||||||
urlObj.path = '';
|
config.url = 'http://' + config.url;
|
||||||
urlObj.pathname = '';
|
}
|
||||||
}
|
var urlObj = url.parse(config.url);
|
||||||
|
if (urlObj.port) {
|
||||||
|
config.port = urlObj.port;
|
||||||
|
}
|
||||||
|
|
||||||
config.url = url.format(urlObj);
|
// Remove trailing slash from non-subfolder installs
|
||||||
|
if (urlObj.path === '/') {
|
||||||
|
urlObj.path = '';
|
||||||
|
urlObj.pathname = '';
|
||||||
|
}
|
||||||
|
|
||||||
// ref: https://github.com/indexzero/nconf/issues/300
|
config.url = url.format(urlObj);
|
||||||
delete config.type;
|
|
||||||
|
|
||||||
var meta = require('./meta');
|
// ref: https://github.com/indexzero/nconf/issues/300
|
||||||
meta.configs.set('submitPluginUsage', config.submitPluginUsage === 'yes' ? 1 : 0, function (err) {
|
delete config.type;
|
||||||
next(err, config);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
function (config, next) {
|
|
||||||
delete config.submitPluginUsage;
|
|
||||||
|
|
||||||
install.save(config, next);
|
const meta = require('./meta');
|
||||||
},
|
await meta.configs.set('submitPluginUsage', config.submitPluginUsage === 'yes' ? 1 : 0);
|
||||||
], next);
|
delete config.submitPluginUsage;
|
||||||
|
|
||||||
|
await install.save(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
function setupDefaultConfigs(next) {
|
async function setupDefaultConfigs() {
|
||||||
console.log('Populating database with default configs, if not already set...');
|
console.log('Populating database with default configs, if not already set...');
|
||||||
var meta = require('./meta');
|
const meta = require('./meta');
|
||||||
var defaults = require(path.join(__dirname, '../', 'install/data/defaults.json'));
|
const defaults = require(path.join(__dirname, '../', 'install/data/defaults.json'));
|
||||||
|
|
||||||
meta.configs.setOnEmpty(defaults, function (err) {
|
await meta.configs.setOnEmpty(defaults);
|
||||||
if (err) {
|
await meta.configs.init();
|
||||||
return next(err);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
meta.configs.init(next);
|
async function enableDefaultTheme() {
|
||||||
|
const meta = require('./meta');
|
||||||
|
|
||||||
|
const id = await meta.configs.get('theme:id');
|
||||||
|
if (id) {
|
||||||
|
console.log('Previous theme detected, skipping enabling default theme');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const defaultTheme = nconf.get('defaultTheme') || 'nodebb-theme-persona';
|
||||||
|
console.log('Enabling default theme: ' + defaultTheme);
|
||||||
|
await meta.themes.set({
|
||||||
|
type: 'local',
|
||||||
|
id: defaultTheme,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function enableDefaultTheme(next) {
|
async function createAdministrator() {
|
||||||
var meta = require('./meta');
|
const Groups = require('./groups');
|
||||||
|
const memberCount = await Groups.getMemberCount('administrators');
|
||||||
meta.configs.get('theme:id', function (err, id) {
|
if (memberCount > 0) {
|
||||||
if (err || id) {
|
console.log('Administrator found, skipping Admin setup');
|
||||||
console.log('Previous theme detected, skipping enabling default theme');
|
return;
|
||||||
return next(err);
|
}
|
||||||
}
|
return await createAdmin();
|
||||||
var defaultTheme = nconf.get('defaultTheme') || 'nodebb-theme-persona';
|
|
||||||
console.log('Enabling default theme: ' + defaultTheme);
|
|
||||||
meta.themes.set({
|
|
||||||
type: 'local',
|
|
||||||
id: defaultTheme,
|
|
||||||
}, next);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function createAdministrator(next) {
|
async function createAdmin() {
|
||||||
var Groups = require('./groups');
|
const User = require('./user');
|
||||||
Groups.getMemberCount('administrators', function (err, memberCount) {
|
const Groups = require('./groups');
|
||||||
if (err) {
|
let password;
|
||||||
return next(err);
|
|
||||||
}
|
|
||||||
if (memberCount > 0) {
|
|
||||||
console.log('Administrator found, skipping Admin setup');
|
|
||||||
next();
|
|
||||||
} else {
|
|
||||||
createAdmin(next);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function createAdmin(callback) {
|
|
||||||
var User = require('./user');
|
|
||||||
var Groups = require('./groups');
|
|
||||||
var password;
|
|
||||||
|
|
||||||
winston.warn('No administrators have been detected, running initial user setup\n');
|
winston.warn('No administrators have been detected, running initial user setup\n');
|
||||||
|
|
||||||
var questions = [{
|
let questions = [{
|
||||||
name: 'username',
|
name: 'username',
|
||||||
description: 'Administrator username',
|
description: 'Administrator username',
|
||||||
required: true,
|
required: true,
|
||||||
@@ -282,7 +248,7 @@ function createAdmin(callback) {
|
|||||||
pattern: /.+@.+/,
|
pattern: /.+@.+/,
|
||||||
required: true,
|
required: true,
|
||||||
}];
|
}];
|
||||||
var passwordQuestions = [{
|
const passwordQuestions = [{
|
||||||
name: 'password',
|
name: 'password',
|
||||||
description: 'Password',
|
description: 'Password',
|
||||||
required: true,
|
required: true,
|
||||||
@@ -295,213 +261,160 @@ function createAdmin(callback) {
|
|||||||
hidden: true,
|
hidden: true,
|
||||||
type: 'string',
|
type: 'string',
|
||||||
}];
|
}];
|
||||||
function success(err, results) {
|
|
||||||
if (err) {
|
async function success(results) {
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
if (!results) {
|
if (!results) {
|
||||||
return callback(new Error('aborted'));
|
throw new Error('aborted');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (results['password:confirm'] !== results.password) {
|
if (results['password:confirm'] !== results.password) {
|
||||||
winston.warn('Passwords did not match, please try again');
|
winston.warn('Passwords did not match, please try again');
|
||||||
return retryPassword(results);
|
return await retryPassword(results);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
User.isPasswordValid(results.password);
|
User.isPasswordValid(results.password);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
winston.warn('Password error, please try again. ' + err.message);
|
winston.warn('Password error, please try again. ' + err.message);
|
||||||
return retryPassword(results);
|
return await retryPassword(results);
|
||||||
}
|
}
|
||||||
|
|
||||||
var adminUid;
|
const adminUid = await User.create({
|
||||||
async.waterfall([
|
username: results.username,
|
||||||
function (next) {
|
password: results.password,
|
||||||
User.create({ username: results.username, password: results.password, email: results.email }, next);
|
email: results.email,
|
||||||
},
|
|
||||||
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) {
|
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
callback(null, password ? results : undefined);
|
|
||||||
});
|
});
|
||||||
|
await Groups.join('administrators', adminUid);
|
||||||
|
await Groups.show('administrators');
|
||||||
|
await Groups.ownership.grant(adminUid, 'administrators');
|
||||||
|
|
||||||
|
return password ? results : undefined;
|
||||||
}
|
}
|
||||||
function retryPassword(originalResults) {
|
|
||||||
|
async function retryPassword(originalResults) {
|
||||||
// Ask only the password questions
|
// Ask only the password questions
|
||||||
prompt.get(passwordQuestions, function (err, results) {
|
const results = await promptGet(passwordQuestions);
|
||||||
if (!results) {
|
|
||||||
return callback(new Error('aborted'));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update the original data with newly collected password
|
// Update the original data with newly collected password
|
||||||
originalResults.password = results.password;
|
originalResults.password = results.password;
|
||||||
originalResults['password:confirm'] = results['password:confirm'];
|
originalResults['password:confirm'] = results['password:confirm'];
|
||||||
|
|
||||||
// Send back to success to handle
|
// Send back to success to handle
|
||||||
success(err, originalResults);
|
return await success(originalResults);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the password questions
|
// Add the password questions
|
||||||
questions = questions.concat(passwordQuestions);
|
questions = questions.concat(passwordQuestions);
|
||||||
|
|
||||||
if (!install.values) {
|
if (!install.values) {
|
||||||
prompt.get(questions, success);
|
const results = await promptGet(questions);
|
||||||
} else {
|
return await success(results);
|
||||||
// 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,
|
|
||||||
'password:confirm': install.values['admin:password:confirm'] || nconf.get('admin:password') || password,
|
|
||||||
};
|
|
||||||
|
|
||||||
success(null, results);
|
|
||||||
}
|
}
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
const 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,
|
||||||
|
};
|
||||||
|
|
||||||
|
return await success(results);
|
||||||
}
|
}
|
||||||
|
|
||||||
function createGlobalModeratorsGroup(next) {
|
async function createGlobalModeratorsGroup() {
|
||||||
var groups = require('./groups');
|
const groups = require('./groups');
|
||||||
async.waterfall([
|
const exists = await groups.exists('Global Moderators');
|
||||||
function (next) {
|
if (exists) {
|
||||||
groups.exists('Global Moderators', next);
|
winston.info('Global Moderators group found, skipping creation!');
|
||||||
},
|
} else {
|
||||||
function (exists, next) {
|
await groups.create({
|
||||||
if (exists) {
|
name: 'Global Moderators',
|
||||||
winston.info('Global Moderators group found, skipping creation!');
|
userTitle: 'Global Moderator',
|
||||||
return next(null, null);
|
description: 'Forum wide moderators',
|
||||||
}
|
hidden: 0,
|
||||||
groups.create({
|
private: 1,
|
||||||
name: 'Global Moderators',
|
disableJoinRequests: 1,
|
||||||
userTitle: 'Global Moderator',
|
});
|
||||||
description: 'Forum wide moderators',
|
}
|
||||||
hidden: 0,
|
await groups.show('Global Moderators');
|
||||||
private: 1,
|
|
||||||
disableJoinRequests: 1,
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
function (groupData, next) {
|
|
||||||
groups.show('Global Moderators', next);
|
|
||||||
},
|
|
||||||
], next);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function giveGlobalPrivileges(next) {
|
async function giveGlobalPrivileges() {
|
||||||
var privileges = require('./privileges');
|
const privileges = require('./privileges');
|
||||||
var defaultPrivileges = [
|
const defaultPrivileges = [
|
||||||
'groups:chat', 'groups:upload:post:image', 'groups:signature', 'groups:search:content',
|
'groups:chat', 'groups:upload:post:image', 'groups:signature', 'groups:search:content',
|
||||||
'groups:search:users', 'groups:search:tags', 'groups:view:users', 'groups:view:tags', 'groups:view:groups',
|
'groups:search:users', 'groups:search:tags', 'groups:view:users', 'groups:view:tags', 'groups:view:groups',
|
||||||
'groups:local:login',
|
'groups:local:login',
|
||||||
];
|
];
|
||||||
async.waterfall([
|
await privileges.global.give(defaultPrivileges, 'registered-users');
|
||||||
function (next) {
|
await privileges.global.give(defaultPrivileges.concat([
|
||||||
privileges.global.give(defaultPrivileges, 'registered-users', next);
|
'groups:ban', 'groups:upload:post:file', 'groups:view:users:info',
|
||||||
},
|
]), 'Global Moderators');
|
||||||
function (next) {
|
await privileges.global.give(['groups:view:users', 'groups:view:tags', 'groups:view:groups'], 'guests');
|
||||||
privileges.global.give(defaultPrivileges.concat(['groups:ban', 'groups:upload:post:file', 'groups:view:users:info']), 'Global Moderators', next);
|
await privileges.global.give(['groups:view:users', 'groups:view:tags', 'groups:view:groups'], 'spiders');
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
privileges.global.give(['groups:view:users', 'groups:view:tags', 'groups:view:groups'], 'guests', next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
privileges.global.give(['groups:view:users', 'groups:view:tags', 'groups:view:groups'], 'spiders', next);
|
|
||||||
},
|
|
||||||
], next);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function createCategories(next) {
|
async function createCategories() {
|
||||||
var Categories = require('./categories');
|
const Categories = require('./categories');
|
||||||
var db = require('./database');
|
const db = require('./database');
|
||||||
db.getSortedSetRange('categories:cid', 0, -1, function (err, cids) {
|
const cids = await db.getSortedSetRange('categories:cid', 0, -1);
|
||||||
if (err) {
|
if (Array.isArray(cids) && cids.length) {
|
||||||
return next(err);
|
console.log('Categories OK. Found ' + cids.length + ' categories.');
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (Array.isArray(cids) && cids.length) {
|
console.log('No categories found, populating instance with default categories');
|
||||||
console.log('Categories OK. Found ' + cids.length + ' categories.');
|
|
||||||
return next();
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log('No categories found, populating instance with default categories');
|
const default_categories = JSON.parse(
|
||||||
|
await fs.promises.readFile(path.join(__dirname, '../', 'install/data/categories.json'), 'utf8')
|
||||||
|
);
|
||||||
|
for (const categoryData of default_categories) {
|
||||||
|
// eslint-disable-next-line no-await-in-loop
|
||||||
|
await Categories.create(categoryData);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fs.readFile(path.join(__dirname, '../', 'install/data/categories.json'), 'utf8', function (err, default_categories) {
|
async function createMenuItems() {
|
||||||
if (err) {
|
const db = require('./database');
|
||||||
return next(err);
|
|
||||||
}
|
|
||||||
default_categories = JSON.parse(default_categories);
|
|
||||||
|
|
||||||
async.eachSeries(default_categories, Categories.create, next);
|
const exists = await db.exists('navigation:enabled');
|
||||||
|
if (exists) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const navigation = require('./navigation/admin');
|
||||||
|
const data = require('../install/data/navigation.json');
|
||||||
|
await navigation.save(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function createWelcomePost() {
|
||||||
|
const db = require('./database');
|
||||||
|
const Topics = require('./topics');
|
||||||
|
|
||||||
|
const [content, numTopics] = await Promise.all([
|
||||||
|
fs.promises.readFile(path.join(__dirname, '../', 'install/data/welcome.md'), 'utf8'),
|
||||||
|
db.getObjectField('global', 'topicCount'),
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (!parseInt(numTopics, 10)) {
|
||||||
|
console.log('Creating welcome post!');
|
||||||
|
await Topics.post({
|
||||||
|
uid: 1,
|
||||||
|
cid: 2,
|
||||||
|
title: 'Welcome to your NodeBB!',
|
||||||
|
content: content,
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function createMenuItems(next) {
|
async function enableDefaultPlugins() {
|
||||||
var db = require('./database');
|
|
||||||
|
|
||||||
db.exists('navigation:enabled', function (err, exists) {
|
|
||||||
if (err || exists) {
|
|
||||||
return next(err);
|
|
||||||
}
|
|
||||||
var navigation = require('./navigation/admin');
|
|
||||||
var data = require('../install/data/navigation.json');
|
|
||||||
|
|
||||||
navigation.save(data, next);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function createWelcomePost(next) {
|
|
||||||
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) {
|
|
||||||
db.getObjectField('global', 'topicCount', next);
|
|
||||||
},
|
|
||||||
], function (err, results) {
|
|
||||||
if (err) {
|
|
||||||
return next(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
var content = results[0];
|
|
||||||
var numTopics = results[1];
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function enableDefaultPlugins(next) {
|
|
||||||
console.log('Enabling default plugins');
|
console.log('Enabling default plugins');
|
||||||
|
|
||||||
var defaultEnabled = [
|
let defaultEnabled = [
|
||||||
'nodebb-plugin-composer-default',
|
'nodebb-plugin-composer-default',
|
||||||
'nodebb-plugin-markdown',
|
'nodebb-plugin-markdown',
|
||||||
'nodebb-plugin-mentions',
|
'nodebb-plugin-mentions',
|
||||||
@@ -511,7 +424,7 @@ function enableDefaultPlugins(next) {
|
|||||||
'nodebb-plugin-emoji',
|
'nodebb-plugin-emoji',
|
||||||
'nodebb-plugin-emoji-android',
|
'nodebb-plugin-emoji-android',
|
||||||
];
|
];
|
||||||
var customDefaults = nconf.get('defaultplugins') || nconf.get('defaultPlugins');
|
let customDefaults = nconf.get('defaultplugins') || nconf.get('defaultPlugins');
|
||||||
|
|
||||||
winston.info('[install/defaultPlugins] customDefaults', customDefaults);
|
winston.info('[install/defaultPlugins] customDefaults', customDefaults);
|
||||||
|
|
||||||
@@ -529,95 +442,75 @@ function enableDefaultPlugins(next) {
|
|||||||
|
|
||||||
winston.info('[install/enableDefaultPlugins] activating default plugins', defaultEnabled);
|
winston.info('[install/enableDefaultPlugins] activating default plugins', defaultEnabled);
|
||||||
|
|
||||||
var db = require('./database');
|
const db = require('./database');
|
||||||
var order = defaultEnabled.map((plugin, index) => index);
|
const order = defaultEnabled.map((plugin, index) => index);
|
||||||
db.sortedSetAdd('plugins:active', order, defaultEnabled, next);
|
await db.sortedSetAdd('plugins:active', order, defaultEnabled);
|
||||||
}
|
}
|
||||||
|
|
||||||
function setCopyrightWidget(next) {
|
async function setCopyrightWidget() {
|
||||||
var db = require('./database');
|
const db = require('./database');
|
||||||
async.parallel({
|
const [footerJSON, footer] = await Promise.all([
|
||||||
footerJSON: function (next) {
|
fs.promises.readFile(path.join(__dirname, '../', 'install/data/footer.json'), 'utf8'),
|
||||||
fs.readFile(path.join(__dirname, '../', 'install/data/footer.json'), 'utf8', next);
|
db.getObjectField('widgets:global', 'footer'),
|
||||||
},
|
]);
|
||||||
footer: function (next) {
|
|
||||||
db.getObjectField('widgets:global', 'footer', next);
|
|
||||||
},
|
|
||||||
}, function (err, results) {
|
|
||||||
if (err) {
|
|
||||||
return next(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!results.footer && results.footerJSON) {
|
if (!footer && footerJSON) {
|
||||||
db.setObjectField('widgets:global', 'footer', results.footerJSON, next);
|
await db.setObjectField('widgets:global', 'footer', footerJSON);
|
||||||
} else {
|
}
|
||||||
next();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
install.setup = function (callback) {
|
async function checkUpgrade() {
|
||||||
async.series([
|
const upgrade = require('./upgrade');
|
||||||
checkSetupFlag,
|
try {
|
||||||
checkCIFlag,
|
await upgrade.check();
|
||||||
setupConfig,
|
} catch (err) {
|
||||||
setupDefaultConfigs,
|
if (err.message === 'schema-out-of-date') {
|
||||||
enableDefaultTheme,
|
await upgrade.run();
|
||||||
createCategories,
|
return;
|
||||||
createAdministrator,
|
}
|
||||||
createGlobalModeratorsGroup,
|
throw err;
|
||||||
giveGlobalPrivileges,
|
}
|
||||||
createMenuItems,
|
}
|
||||||
createWelcomePost,
|
|
||||||
enableDefaultPlugins,
|
install.setup = async function () {
|
||||||
setCopyrightWidget,
|
try {
|
||||||
function (next) {
|
checkSetupFlag();
|
||||||
var upgrade = require('./upgrade');
|
checkCIFlag();
|
||||||
upgrade.check(function (err) {
|
await setupConfig();
|
||||||
if (err && err.message === 'schema-out-of-date') {
|
await setupDefaultConfigs();
|
||||||
upgrade.run(next);
|
await enableDefaultTheme();
|
||||||
} else if (err) {
|
await createCategories();
|
||||||
return next(err);
|
const adminInfo = await createAdministrator();
|
||||||
} else {
|
await createGlobalModeratorsGroup();
|
||||||
next();
|
await giveGlobalPrivileges();
|
||||||
}
|
await createMenuItems();
|
||||||
});
|
await createWelcomePost();
|
||||||
},
|
await enableDefaultPlugins();
|
||||||
], function (err, results) {
|
await setCopyrightWidget();
|
||||||
|
await checkUpgrade();
|
||||||
|
|
||||||
|
const data = {
|
||||||
|
...adminInfo,
|
||||||
|
};
|
||||||
|
return data;
|
||||||
|
} catch (err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
winston.warn('NodeBB Setup Aborted.\n ' + err.stack);
|
winston.warn('NodeBB Setup Aborted.\n ' + err.stack);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
} else {
|
|
||||||
var data = {};
|
|
||||||
if (results[6]) {
|
|
||||||
data.username = results[6].username;
|
|
||||||
data.password = results[6].password;
|
|
||||||
}
|
|
||||||
|
|
||||||
callback(null, data);
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
install.save = function (server_conf, callback) {
|
install.save = async function (server_conf) {
|
||||||
var serverConfigPath = path.join(__dirname, '../config.json');
|
let serverConfigPath = path.join(__dirname, '../config.json');
|
||||||
|
|
||||||
if (nconf.get('config')) {
|
if (nconf.get('config')) {
|
||||||
serverConfigPath = path.resolve(__dirname, '../', nconf.get('config'));
|
serverConfigPath = path.resolve(__dirname, '../', nconf.get('config'));
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.writeFile(serverConfigPath, JSON.stringify(server_conf, null, 4), function (err) {
|
await fs.promises.writeFile(serverConfigPath, JSON.stringify(server_conf, null, 4));
|
||||||
if (err) {
|
console.log('Configuration Saved OK');
|
||||||
winston.error('Error saving server configuration!', err.stack);
|
nconf.file({
|
||||||
return callback(err);
|
file: serverConfigPath,
|
||||||
}
|
|
||||||
|
|
||||||
console.log('Configuration Saved OK');
|
|
||||||
|
|
||||||
nconf.file({
|
|
||||||
file: serverConfigPath,
|
|
||||||
});
|
|
||||||
|
|
||||||
callback();
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user