forked from NodeBB/NodeBB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.js
More file actions
632 lines (548 loc) · 17.6 KB
/
Copy pathinstall.js
File metadata and controls
632 lines (548 loc) · 17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
'use strict';
const fs = require('fs');
const url = require('url');
const path = require('path');
const prompt = require('prompt');
const winston = require('winston');
const nconf = require('nconf');
const _ = require('lodash');
const utils = require('./utils');
const { paths } = require('./constants');
const install = module.exports;
const questions = {};
questions.main = [
{
name: 'url',
description: 'URL used to access this NodeBB',
default:
nconf.get('url') || '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: 'submitPluginUsage',
description: 'Would you like to submit anonymous plugin usage to nbbpm?',
default: 'yes',
},
{
name: 'database',
description: 'Which database to use',
default: nconf.get('database') || 'mongo',
},
];
questions.optional = [
{
name: 'port',
default: nconf.get('port') || 4567,
},
];
function checkSetupFlagEnv() {
let setupVal = install.values;
const envConfMap = {
CONFIG: 'config',
NODEBB_CONFIG: 'config',
NODEBB_URL: 'url',
NODEBB_PORT: 'port',
NODEBB_ADMIN_USERNAME: 'admin:username',
NODEBB_ADMIN_PASSWORD: 'admin:password',
NODEBB_ADMIN_EMAIL: 'admin:email',
NODEBB_DB: 'database',
NODEBB_DB_HOST: 'host',
NODEBB_DB_PORT: 'port',
NODEBB_DB_USER: 'username',
NODEBB_DB_PASSWORD: 'password',
NODEBB_DB_NAME: 'database',
NODEBB_DB_SSL: 'ssl',
};
// Set setup values from env vars (if set)
const envKeys = Object.keys(process.env);
if (Object.keys(envConfMap).some(key => envKeys.includes(key))) {
winston.info('[install/checkSetupFlagEnv] checking env vars for setup info...');
setupVal = setupVal || {};
Object.entries(process.env).forEach(([evName, evValue]) => { // get setup values from env
if (evName.startsWith('NODEBB_DB_')) {
setupVal[`${process.env.NODEBB_DB}:${envConfMap[evName]}`] = evValue;
} else if (evName.startsWith('NODEBB_')) {
setupVal[envConfMap[evName]] = evValue;
}
});
setupVal['admin:password:confirm'] = setupVal['admin:password'];
}
// try to get setup values from json, if successful this overwrites all values set by env
// TODO: better behaviour would be to support overrides per value, i.e. in order of priority (generic pattern):
// flag, env, config file, default
try {
if (nconf.get('setup')) {
const setupJSON = JSON.parse(nconf.get('setup'));
setupVal = { ...setupVal, ...setupJSON };
}
} catch (err) {
winston.error('[install/checkSetupFlagEnv] invalid json in nconf.get(\'setup\'), ignoring setup values from json');
}
if (setupVal && typeof setupVal === 'object') {
if (setupVal['admin:username'] && setupVal['admin:password'] && setupVal['admin:password:confirm'] && setupVal['admin:email']) {
install.values = setupVal;
} else {
winston.error('[install/checkSetupFlagEnv] 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');
}
process.exit();
}
} else if (nconf.get('database')) {
install.values = install.values || {};
install.values.database = nconf.get('database');
}
}
function checkCIFlag() {
let 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;
} else {
winston.error('[install/checkCIFlag] 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');
}
process.exit();
}
}
}
async function setupConfig() {
const configureDatabases = require('../install/databases');
// prompt prepends "prompt: " to questions, let's clear that.
prompt.start();
prompt.message = '';
prompt.delimiter = '';
prompt.colors = false;
let config = {};
if (install.values) {
// Use provided values, fall back to defaults
const redisQuestions = require('./database/redis').questions;
const mongoQuestions = require('./database/mongo').questions;
const postgresQuestions = require('./database/postgres').questions;
const allQuestions = [
...questions.main,
...questions.optional,
...redisQuestions,
...mongoQuestions,
...postgresQuestions,
];
allQuestions.forEach((question) => {
if (install.values.hasOwnProperty(question.name)) {
config[question.name] = install.values[question.name];
} else if (question.hasOwnProperty('default')) {
config[question.name] = question.default;
} else {
config[question.name] = undefined;
}
});
} else {
config = await prompt.get(questions.main);
}
await configureDatabases(config);
await completeConfigSetup(config);
}
async function completeConfigSetup(config) {
// Add CI object
if (install.ciVals) {
config.test_database = { ...install.ciVals };
}
// Add package_manager object if set
if (nconf.get('package_manager')) {
config.package_manager = nconf.get('package_manager');
}
nconf.overrides(config);
const db = require('./database');
await db.init();
if (db.hasOwnProperty('createIndices')) {
await db.createIndices();
}
// Sanity-check/fix url/port
if (!/^http(?:s)?:\/\//.test(config.url)) {
config.url = `http://${config.url}`;
}
// If port is explicitly passed via install vars, use it. Otherwise, glean from url if set.
const urlObj = url.parse(config.url);
if (urlObj.port && (!install.values || !install.values.hasOwnProperty('port'))) {
config.port = urlObj.port;
}
// Remove trailing slash from non-subfolder installs
if (urlObj.path === '/') {
urlObj.path = '';
urlObj.pathname = '';
}
config.url = url.format(urlObj);
// ref: https://github.com/indexzero/nconf/issues/300
delete config.type;
const meta = require('./meta');
await meta.configs.set('submitPluginUsage', config.submitPluginUsage === 'yes' ? 1 : 0);
delete config.submitPluginUsage;
await install.save(config);
}
async function setupDefaultConfigs() {
console.log('Populating database with default configs, if not already set...');
const meta = require('./meta');
const defaults = require(path.join(__dirname, '../', 'install/data/defaults.json'));
await meta.configs.setOnEmpty(defaults);
await meta.configs.init();
}
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-harmony';
console.log(`Enabling default theme: ${defaultTheme}`);
await meta.themes.set({
type: 'local',
id: defaultTheme,
});
}
async function createDefaultUserGroups() {
const groups = require('./groups');
async function createGroup(name) {
await groups.create({
name: name,
hidden: 1,
private: 1,
system: 1,
disableLeave: 1,
disableJoinRequests: 1,
});
}
const [verifiedExists, unverifiedExists, bannedExists] = await groups.exists([
'verified-users', 'unverified-users', 'banned-users',
]);
if (!verifiedExists) {
await createGroup('verified-users');
}
if (!unverifiedExists) {
await createGroup('unverified-users');
}
if (!bannedExists) {
await createGroup('banned-users');
}
}
async function createAdministrator() {
const Groups = require('./groups');
const memberCount = await Groups.getMemberCount('administrators');
if (memberCount > 0) {
console.log('Administrator found, skipping Admin setup');
return;
}
return await createAdmin();
}
async function createAdmin() {
const User = require('./user');
const Groups = require('./groups');
let password;
winston.warn('No administrators have been detected, running initial user setup\n');
let questions = [{
name: 'username',
description: 'Administrator username',
required: true,
type: 'string',
}, {
name: 'email',
description: 'Administrator email address',
pattern: /.+@.+/,
required: true,
}];
const passwordQuestions = [{
name: 'password',
description: 'Password',
required: true,
hidden: true,
type: 'string',
}, {
name: 'password:confirm',
description: 'Confirm Password',
required: true,
hidden: true,
type: 'string',
}];
async function success(results) {
if (!results) {
throw new Error('aborted');
}
if (results['password:confirm'] !== results.password) {
winston.warn('Passwords did not match, please try again');
return await retryPassword(results);
}
try {
User.isPasswordValid(results.password);
} catch (err) {
const [namespace, key] = err.message.slice(2, -2).split(':', 2);
if (namespace && key && err.message.startsWith('[[') && err.message.endsWith(']]')) {
const lang = require(path.join(__dirname, `../public/language/en-GB/${namespace}`));
if (lang && lang[key]) {
err.message = lang[key];
}
}
winston.warn(`Password error, please try again. ${err.message}`);
return await retryPassword(results);
}
const adminUid = await User.create({
username: results.username,
password: results.password,
email: results.email,
});
await Groups.join('administrators', adminUid);
await Groups.show('administrators');
await Groups.ownership.grant(adminUid, 'administrators');
return password ? results : undefined;
}
async function retryPassword(originalResults) {
// Ask only the password questions
const results = await prompt.get(passwordQuestions);
// Update the original data with newly collected password
originalResults.password = results.password;
originalResults['password:confirm'] = results['password:confirm'];
// Send back to success to handle
return await success(originalResults);
}
// Add the password questions
questions = questions.concat(passwordQuestions);
if (!install.values) {
const results = await prompt.get(questions);
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);
}
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);
}
async function createGlobalModeratorsGroup() {
const groups = require('./groups');
const exists = await groups.exists('Global Moderators');
if (exists) {
winston.info('Global Moderators group found, skipping creation!');
} else {
await groups.create({
name: 'Global Moderators',
userTitle: 'Global Moderator',
description: 'Forum wide moderators',
hidden: 0,
private: 1,
disableJoinRequests: 1,
});
}
await groups.show('Global Moderators');
}
async function giveGlobalPrivileges() {
const privileges = require('./privileges');
const defaultPrivileges = [
'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:local:login',
];
await privileges.global.give(defaultPrivileges, 'registered-users');
await privileges.global.give(defaultPrivileges.concat([
'groups:ban', 'groups:upload:post:file', 'groups:view:users:info',
]), 'Global Moderators');
await privileges.global.give(['groups:view:users', 'groups:view:tags', 'groups:view:groups'], 'guests');
await privileges.global.give(['groups:view:users', 'groups:view:tags', 'groups:view:groups'], 'spiders');
}
async function createCategories() {
const Categories = require('./categories');
const db = require('./database');
const cids = await db.getSortedSetRange('categories:cid', 0, -1);
if (Array.isArray(cids) && cids.length) {
console.log(`Categories OK. Found ${cids.length} categories.`);
return;
}
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);
}
}
async function createMenuItems() {
const db = require('./database');
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,
});
}
}
async function enableDefaultPlugins() {
console.log('Enabling default plugins');
let defaultEnabled = [
'nodebb-plugin-composer-default',
'nodebb-plugin-markdown',
'nodebb-plugin-mentions',
'nodebb-widget-essentials',
'nodebb-rewards-essentials',
'nodebb-plugin-emoji',
'nodebb-plugin-emoji-android',
];
let customDefaults = nconf.get('defaultplugins') || nconf.get('defaultPlugins');
winston.info(`[install/defaultPlugins] customDefaults ${String(customDefaults)}`);
if (customDefaults && customDefaults.length) {
try {
customDefaults = Array.isArray(customDefaults) ? customDefaults : JSON.parse(customDefaults);
defaultEnabled = defaultEnabled.concat(customDefaults);
} catch (e) {
// Invalid value received
winston.info('[install/enableDefaultPlugins] Invalid defaultPlugins value received. Ignoring.');
}
}
defaultEnabled = _.uniq(defaultEnabled);
winston.info('[install/enableDefaultPlugins] activating default plugins', defaultEnabled);
const db = require('./database');
const order = defaultEnabled.map((plugin, index) => index);
await db.sortedSetAdd('plugins:active', order, defaultEnabled);
}
async function setCopyrightWidget() {
const db = require('./database');
const [footerJSON, footer] = await Promise.all([
fs.promises.readFile(path.join(__dirname, '../', 'install/data/footer.json'), 'utf8'),
db.getObjectField('widgets:global', 'footer'),
]);
if (!footer && footerJSON) {
await db.setObjectField('widgets:global', 'sidebar-footer', footerJSON);
}
}
async function copyFavicon() {
const file = require('./file');
const pathToIco = path.join(nconf.get('upload_path'), 'system', 'favicon.ico');
const defaultIco = path.join(nconf.get('base_dir'), 'public', 'favicon.ico');
const targetExists = await file.exists(pathToIco);
const defaultExists = await file.exists(defaultIco);
if (defaultExists && !targetExists) {
try {
await fs.promises.copyFile(defaultIco, pathToIco);
} catch (err) {
winston.error(`Cannot copy favicon.ico\n${err.stack}`);
}
}
}
async function checkUpgrade() {
const upgrade = require('./upgrade');
try {
await upgrade.check();
} catch (err) {
if (err.message === 'schema-out-of-date') {
await upgrade.run();
return;
}
throw err;
}
}
async function installPlugins() {
const pluginInstall = require('./plugins');
const nbbVersion = require(paths.currentPackage).version;
await Promise.all((await pluginInstall.getActive()).map(async (id) => {
if (await pluginInstall.isInstalled(id)) return;
const version = await pluginInstall.suggest(id, nbbVersion);
await pluginInstall.toggleInstall(id, version.version);
}));
}
install.setup = async function () {
try {
checkSetupFlagEnv();
checkCIFlag();
await setupConfig();
await setupDefaultConfigs();
await enableDefaultTheme();
await createCategories();
await createDefaultUserGroups();
const adminInfo = await createAdministrator();
await createGlobalModeratorsGroup();
await giveGlobalPrivileges();
await createMenuItems();
await createWelcomePost();
await enableDefaultPlugins();
await setCopyrightWidget();
await copyFavicon();
if (nconf.get('plugins:autoinstall')) await installPlugins();
await checkUpgrade();
const data = {
...adminInfo,
};
return data;
} catch (err) {
if (err) {
winston.warn(`NodeBB Setup Aborted.\n ${err.stack}`);
process.exit(1);
}
}
};
install.save = async function (server_conf) {
let serverConfigPath = path.join(__dirname, '../config.json');
if (nconf.get('config')) {
serverConfigPath = path.resolve(__dirname, '../', nconf.get('config'));
}
let currentConfig = {};
try {
currentConfig = require(serverConfigPath);
} catch (err) {
if (err.code !== 'MODULE_NOT_FOUND') {
throw err;
}
}
await fs.promises.writeFile(serverConfigPath, JSON.stringify({ ...currentConfig, ...server_conf }, null, 4));
console.log('Configuration Saved OK');
nconf.file({
file: serverConfigPath,
});
};