mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-30 18:46:01 +01:00
* use yarn and debian slim build * feat: update Dockerfile to use multistage builds * Create main.yml * remove some useless things from docker context and assume yarn by default * remove all dotfiles in docker context * no need for extra build tools, complain to the module author if there is no alpine build (cherry picked from commit 90516a3c8399e74c38be7115edb39411ba0d86b9) * specify the config file location instead of creating it (cherry picked from commit 38e4295d70682f1049fe671ade96eeccd669d908) * set explicit config path (cherry picked from commit 8dcc6f249d099cb8939a95511ec13702491958bc) * fix docker-compose example to use the exposed volumes * dockerfile: upgrade alpine to 3.16 * dockerignore: add more ignorable entries * docker-compose: change the way the docker startup process works * install: pass config path to child process as well Signed-off-by: steve <29133953+stevefan1999-personal@users.noreply.github.com> * setup: move config file resolution up before setup This fixes issue with different config file location, which will otherwise default on 'config.json', which means the config save won't save to the file we specified Signed-off-by: steve <29133953+stevefan1999-personal@users.noreply.github.com> * docker-entrypoint: don't fix CONFIG_DIR location but fix default location Signed-off-by: steve <29133953+stevefan1999-personal@users.noreply.github.com> * docker-entrypoint: handle missing config file logic Signed-off-by: steve <29133953+stevefan1999-personal@users.noreply.github.com> * README: add simple notice on how to use it Signed-off-by: steve <29133953+stevefan1999-personal@users.noreply.github.com> * add missing semicolons Signed-off-by: steve <29133953+stevefan1999-personal@users.noreply.github.com> * docker-compose: remove multi override, use one big profile instead However, Docker Compose doesn't support profile-based dependency and this would probably means we have less guarantee about the liveness of the database. But since this is just a sample configuration it should be fine Signed-off-by: steve <29133953+stevefan1999-personal@users.noreply.github.com> * workflows: remove main.yml, add platforms to buildx matrix in docker.yml Signed-off-by: steve <29133953+stevefan1999-personal@users.noreply.github.com> * workflows: set docker buildx to build for amd64 and arm64 only Signed-off-by: steve <29133953+stevefan1999-personal@users.noreply.github.com> * docker-entrypoint: don't force build everytime before start Signed-off-by: steve <29133953+stevefan1999-personal@users.noreply.github.com> * docker-entrypoint: implement init verb This would allow you to change between "setup" (automated setup using environmental variables which is the current preferred way to run containerized NodeBB) or "install" (web install that guides user to fill in connection information, which is similar to WordPress) Signed-off-by: steve <29133953+stevefan1999-personal@users.noreply.github.com> * README: mention caveat with MongoDB Signed-off-by: steve <29133953+stevefan1999-personal@users.noreply.github.com> * README: add Docker section placeholder for doc migration Signed-off-by: steve <29133953+stevefan1999-personal@users.noreply.github.com> * docker-entrypoint: add SETUP variable support Signed-off-by: steve <29133953+stevefan1999-personal@users.noreply.github.com> * docker-compose: add force flag to ln on setup Signed-off-by: steve <29133953+stevefan1999-personal@users.noreply.github.com> * docker-compose: fix permission issue; docker-compose: fast exit if still no permission on config dir Signed-off-by: steve <29133953+stevefan1999-personal@users.noreply.github.com> * fix: remove redundant FROM * docs: remove docker stuff (in favour of docs entry, nodebb/docs#78) but add link to cloud install docs * fix: correctly check if directory is writable * fix: ignore .docker directory * fix: multi-arch docker builds and chown performance * chore: bump database image versions * fix: move from alpine to slim image * fix: use omit=dev instead of only=prod * feat: move entrypoint to install directory * feat: initialize mongodb user * feat: use separate rebuild stage * fix: disable eslint for mongodb script * fix: remove node_modules bind mount bind mounts don't save data from container, resulting in a LOONG startup * feat: prepopulate database defaults for installation * feat: enable persistence in redis container * docs: add some comments to the compose file --------- Signed-off-by: steve <29133953+stevefan1999-personal@users.noreply.github.com> Co-authored-by: Steve Fan <29133953+stevefan1999-personal@users.noreply.github.com> Co-authored-by: Steve Fan <19037626d@connect.polyu.hk> Co-authored-by: Julian Lam <julian@nodebb.org>
320 lines
8.6 KiB
JavaScript
320 lines
8.6 KiB
JavaScript
'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,
|
|
CONFIG: nconf.get('config'),
|
|
NODEBB_CONFIG: nconf.get('config'),
|
|
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,
|
|
});
|
|
}
|