diff --git a/public/503.html b/public/503.html index 68c9386146..43d1e648d9 100644 --- a/public/503.html +++ b/public/503.html @@ -6,7 +6,6 @@ body { background: #00A9EA; color: white; - /* see public/less/admin/vars.less for documentation on system font family */ font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", Helvetica, Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; text-align: center; -webkit-transform-style: preserve-3d; diff --git a/public/scss/install.scss b/public/scss/install.scss index 831d2a84e1..df01ae3e0e 100644 --- a/public/scss/install.scss +++ b/public/scss/install.scss @@ -1,5 +1,3 @@ -@import "./admin/vars"; - .hidden, .hide { display: none!important; } @@ -49,19 +47,11 @@ } } -.btn, .form-control, .navbar { - border-radius: 0; -} - .container { font-size: 18px; margin-bottom: 100px; } -body, small, p, div { - font-family: $font-family-sans-serif; -} - .input-row { margin-bottom: 20px; @@ -69,14 +59,6 @@ body, small, p, div { margin-bottom: 5px; } - .help-text { - pointer-events: none; - line-height: 20px; - color: #888; - font-size: 85%; - display: none; - } - .input-field { border-right: 5px solid #FFF; } diff --git a/public/src/installer/install.js b/public/src/installer/install.js index cb027f4ce6..8aa00c02d0 100644 --- a/public/src/installer/install.js +++ b/public/src/installer/install.js @@ -14,12 +14,38 @@ $('document').ready(function () { activate('database', $('[name="database"]')); - if ($('#database-error').length) { - $('[name="database"]').parents('.input-row').addClass('error'); - $('html, body').animate({ - scrollTop: ($('#database-error').offset().top + 100) + 'px', - }, 400); - } + $('#test-database').on('click', function () { + const conf = {}; + $('#database-config input[name]').each((i, el) => { + conf[$(el).attr('name')] = $(el).val(); + }); + $('#test-database-spinner').removeClass('hidden'); + $('#database-success').addClass('hidden'); + $('#database-error').addClass('hidden'); + $('#database-full').addClass('hidden'); + const qs = new URLSearchParams(conf).toString(); + $.ajax({ + url: `/testdb?${qs}`, + success: function (res) { + $('#test-database-spinner').addClass('hidden'); + if (res.success) { + $('#database-success').removeClass('hidden'); + if (res.dbfull) { + $('#database-full').removeClass('hidden') + .text('Found existing install in this database!'); + } + } else if (res.error) { + $('#database-error').removeClass('hidden').text(res.error); + } + }, + error: function (jqXHR, textStatus) { + $('#test-database-spinner').addClass('hidden'); + $('#database-error').removeClass('hidden').text(textStatus); + }, + }); + + return false; + }); function checkIfReady() { let successCount = 0; @@ -127,6 +153,9 @@ $('document').ready(function () { function switchDatabase(field) { $('#database-config').html($('[data-database="' + field + '"]').html()); + $('#database-success').addClass('hidden'); + $('#database-error').addClass('hidden'); + $('#database-full').addClass('hidden'); } switch (type) { diff --git a/src/database/mongo.js b/src/database/mongo.js index 5ecf81d285..02bc79d7d3 100644 --- a/src/database/mongo.js +++ b/src/database/mongo.js @@ -59,8 +59,8 @@ mongoModule.questions = [ }, ]; -mongoModule.init = async function () { - client = await connection.connect(nconf.get('mongo')); +mongoModule.init = async function (opts) { + client = await connection.connect(opts || nconf.get('mongo')); mongoModule.client = client.db(); }; @@ -175,6 +175,9 @@ async function getCollectionStats(db) { mongoModule.close = async function () { await client.close(); + if (mongoModule.objectCache) { + mongoModule.objectCache.reset(); + } }; require('./mongo/main')(mongoModule); diff --git a/src/database/postgres.js b/src/database/postgres.js index f09041c7c3..ee36941a17 100644 --- a/src/database/postgres.js +++ b/src/database/postgres.js @@ -45,9 +45,9 @@ postgresModule.questions = [ }, ]; -postgresModule.init = async function () { +postgresModule.init = async function (opts) { const { Pool } = require('pg'); - const connOptions = connection.getConnectionOptions(); + const connOptions = connection.getConnectionOptions(opts); const pool = new Pool(connOptions); postgresModule.pool = pool; postgresModule.client = pool; diff --git a/src/database/redis.js b/src/database/redis.js index 703b94905a..22d13cc9e4 100644 --- a/src/database/redis.js +++ b/src/database/redis.js @@ -33,8 +33,8 @@ redisModule.questions = [ ]; -redisModule.init = async function () { - redisModule.client = await connection.connect(nconf.get('redis')); +redisModule.init = async function (opts) { + redisModule.client = await connection.connect(opts || nconf.get('redis')); }; redisModule.createSessionStore = async function (options) { @@ -62,6 +62,9 @@ redisModule.checkCompatibilityVersion = function (version, callback) { redisModule.close = async function () { await redisModule.client.quit(); + if (redisModule.objectCache) { + redisModule.objectCache.reset(); + } }; redisModule.info = async function (cxn) { diff --git a/src/views/install/index.tpl b/src/views/install/index.tpl index bc8a60abd2..f41400f734 100644 --- a/src/views/install/index.tpl +++ b/src/views/install/index.tpl @@ -87,12 +87,10 @@ - {{{ if error }}} - - {{{ end }}} + {{{ if !skipDatabaseSetup }}} -
+

Configure your database


@@ -113,8 +111,22 @@
{{{ end }}} - - +
+
+
+ {{{ if !skipDatabaseSetup }}} + + {{{ end }}} + +
+ + + +
+
{{{ end }}} diff --git a/web.js b/web.js new file mode 100644 index 0000000000..1f5a846a30 --- /dev/null +++ b/web.js @@ -0,0 +1,317 @@ +'use strict'; + +const winston = require('winston'); +const express = require('express'); +const bodyParser = require('body-parser'); +const fs = require('fs'); +const path = require('path'); +const childProcess = require('child_process'); + +const webpack = require('webpack'); +const nconf = require('nconf'); + +const Benchpress = require('benchpressjs'); +const { mkdirp } = require('mkdirp'); +const { paths } = require('../src/constants'); +const sass = require('../src/utils').getSass(); + +const app = express(); +let server; + +const formats = [ + winston.format.colorize(), +]; + +const timestampFormat = winston.format((info) => { + const dateString = `${new Date().toISOString()} [${global.process.pid}]`; + info.level = `${dateString} - ${info.level}`; + return info; +}); +formats.push(timestampFormat()); +formats.push(winston.format.splat()); +formats.push(winston.format.simple()); + +winston.configure({ + level: 'verbose', + format: winston.format.combine.apply(null, formats), + transports: [ + new winston.transports.Console({ + handleExceptions: true, + }), + new winston.transports.File({ + filename: 'logs/webinstall.log', + handleExceptions: true, + }), + ], +}); + +const web = module.exports; +let installing = false; +let success = false; +let error = false; +let launchUrl; +let timeStart = 0; +const totalTime = 1000 * 60 * 3; + + +const viewsDir = path.join(paths.baseDir, 'build/public/templates'); + +web.install = async function (port) { + port = port || 4567; + winston.info(`Launching web installer on port ${port}`); + + app.use(express.static('public', {})); + app.use('/assets', express.static(path.join(__dirname, '../build/public'), {})); + + app.engine('tpl', (filepath, options, callback) => { + filepath = filepath.replace(/\.tpl$/, '.js'); + + Benchpress.__express(filepath, options, callback); + }); + app.set('view engine', 'tpl'); + app.set('views', viewsDir); + app.use(bodyParser.urlencoded({ + extended: true, + })); + try { + await Promise.all([ + compileTemplate(), + compileSass(), + runWebpack(), + copyCSS(), + loadDefaults(), + ]); + setupRoutes(); + launchExpress(port); + } catch (err) { + winston.error(err.stack); + } +}; + +async function runWebpack() { + const util = require('util'); + const webpackCfg = require('../webpack.installer'); + const compiler = webpack(webpackCfg); + const webpackRun = util.promisify(compiler.run).bind(compiler); + await webpackRun(); +} + +function launchExpress(port) { + server = app.listen(port, () => { + winston.info('Web installer listening on http://%s:%s', '0.0.0.0', port); + }); +} + +function setupRoutes() { + app.get('/', welcome); + app.post('/', install); + app.get('/testdb', testDatabase); + app.get('/ping', ping); + app.get('/sping', ping); +} + +async function testDatabase(req, res) { + let db; + try { + const keys = Object.keys(req.query); + const dbName = keys[0].split(':')[0]; + db = require(`../src/database/${dbName}`); + + const opts = {}; + keys.forEach((key) => { + opts[key.replace(`${dbName}:`, '')] = req.query[key]; + }); + + await db.init(opts); + const global = await db.getObject('global'); + await db.close(); + res.json({ success: 1, dbfull: !!global }); + } catch (err) { + res.json({ error: err.stack }); + } +} + +function ping(req, res) { + res.status(200).send(req.path === '/sping' ? 'healthy' : '200'); +} + +function welcome(req, res) { + const dbs = ['mongo', 'redis', 'postgres']; + const databases = dbs.map((databaseName) => { + const questions = require(`../src/database/${databaseName}`).questions.filter(question => question && !question.hideOnWebInstall); + + return { + name: databaseName, + questions: questions, + }; + }); + + const defaults = require('./data/defaults.json'); + res.render('install/index', { + url: nconf.get('url') || (`${req.protocol}://${req.get('host')}`), + launchUrl: launchUrl, + skipGeneralSetup: !!nconf.get('url'), + databases: databases, + skipDatabaseSetup: !!nconf.get('database'), + error: error, + success: success, + values: req.body, + minimumPasswordLength: defaults.minimumPasswordLength, + minimumPasswordStrength: defaults.minimumPasswordStrength, + installing: installing, + percentInstalled: installing ? ((Date.now() - timeStart) / totalTime * 100).toFixed(2) : 0, + }); +} + +function install(req, res) { + if (installing) { + return welcome(req, res); + } + timeStart = Date.now(); + req.setTimeout(0); + installing = true; + + const database = nconf.get('database') || req.body.database || 'mongo'; + const setupEnvVars = { + ...process.env, + NODEBB_URL: nconf.get('url') || req.body.url || (`${req.protocol}://${req.get('host')}`), + NODEBB_PORT: nconf.get('port') || 4567, + NODEBB_ADMIN_USERNAME: nconf.get('admin:username') || req.body['admin:username'], + NODEBB_ADMIN_PASSWORD: nconf.get('admin:password') || req.body['admin:password'], + NODEBB_ADMIN_EMAIL: nconf.get('admin:email') || req.body['admin:email'], + NODEBB_DB: database, + NODEBB_DB_HOST: nconf.get(`${database}:host`) || req.body[`${database}:host`], + NODEBB_DB_PORT: nconf.get(`${database}:port`) || req.body[`${database}:port`], + NODEBB_DB_USER: nconf.get(`${database}:username`) || req.body[`${database}:username`], + NODEBB_DB_PASSWORD: nconf.get(`${database}:password`) || req.body[`${database}:password`], + NODEBB_DB_NAME: nconf.get(`${database}:database`) || req.body[`${database}:database`], + NODEBB_DB_SSL: nconf.get(`${database}:ssl`) || req.body[`${database}:ssl`], + defaultPlugins: JSON.stringify(nconf.get('defaultplugins') || nconf.get('defaultPlugins') || []), + }; + + winston.info('Starting setup process'); + launchUrl = setupEnvVars.NODEBB_URL; + + const child = require('child_process').fork('app', ['--setup'], { + env: setupEnvVars, + }); + child.on('error', (err) => { + error = true; + success = false; + winston.error(err.stack); + }); + child.on('close', (data) => { + success = data === 0; + error = data !== 0; + launch(); + }); + welcome(req, res); +} + +async function launch() { + try { + server.close(); + let child; + + if (!nconf.get('launchCmd')) { + child = childProcess.spawn('node', ['loader.js'], { + detached: true, + stdio: ['ignore', 'ignore', 'ignore'], + }); + + console.log('\nStarting NodeBB'); + console.log(' "./nodebb stop" to stop the NodeBB server'); + console.log(' "./nodebb log" to view server output'); + console.log(' "./nodebb restart" to restart NodeBB'); + } else { + // Use launchCmd instead, if specified + child = childProcess.exec(nconf.get('launchCmd'), { + detached: true, + stdio: ['ignore', 'ignore', 'ignore'], + }); + } + + const filesToDelete = [ + path.join(__dirname, '../public', 'installer.css'), + path.join(__dirname, '../public', 'bootstrap.min.css'), + path.join(__dirname, '../build/public', 'installer.min.js'), + ]; + try { + await Promise.all( + filesToDelete.map( + filename => fs.promises.unlink(filename) + ) + ); + } catch (err) { + console.log(err.stack); + } + + child.unref(); + process.exit(0); + } catch (err) { + winston.error(err.stack); + throw err; + } +} + +// this is necessary because otherwise the compiled templates won't be available on a clean install +async function compileTemplate() { + const sourceFile = path.join(__dirname, '../src/views/install/index.tpl'); + const destTpl = path.join(viewsDir, 'install/index.tpl'); + const destJs = path.join(viewsDir, 'install/index.js'); + + const source = await fs.promises.readFile(sourceFile, 'utf8'); + + const [compiled] = await Promise.all([ + Benchpress.precompile(source, { filename: 'install/index.tpl' }), + mkdirp(path.dirname(destJs)), + ]); + + await Promise.all([ + fs.promises.writeFile(destJs, compiled), + fs.promises.writeFile(destTpl, source), + ]); +} + +async function compileSass() { + try { + const installSrc = path.join(__dirname, '../public/scss/install.scss'); + const style = await fs.promises.readFile(installSrc); + const scssOutput = sass.compileString(String(style), { + loadPaths: [ + path.join(__dirname, '../public/scss'), + ], + }); + + await fs.promises.writeFile(path.join(__dirname, '../public/installer.css'), scssOutput.css.toString()); + } catch (err) { + winston.error(`Unable to compile SASS: \n${err.stack}`); + throw err; + } +} + +async function copyCSS() { + await fs.promises.copyFile( + path.join(__dirname, '../node_modules/bootstrap/dist/css/bootstrap.min.css'), + path.join(__dirname, '../public/bootstrap.min.css'), + ); +} + +async function loadDefaults() { + const setupDefaultsPath = path.join(__dirname, '../setup.json'); + try { + // eslint-disable-next-line no-bitwise + await fs.promises.access(setupDefaultsPath, fs.constants.F_OK | fs.constants.R_OK); + } catch (err) { + // setup.json not found or inaccessible, proceed with no defaults + if (err.code !== 'ENOENT') { + throw err; + } + + return; + } + winston.info('[installer] Found setup.json, populating default values'); + nconf.file({ + file: setupDefaultsPath, + }); +}