mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-01-06 15:42:52 +01:00
Allow NodeBB setup with env vars (#9850)
* initial try [WIP] * typo; add test start script; initial Dockerfile mod with integrated setup [WIP] * minor fixes * add some winston debug... * typos * fix pass confirm setup * more fixes * fix entrypoint * cleanup * remove echo sensitive setupVal * remove obsolete code and comments * fix linting errors * Merge branch 'additional-fixes' * Merge branch 'pitaj-suggested-fixes' * Merge branch 'pitaj-fixes2' * merge checkSetup functions (env vars and flags) * comment (lint) * remove tab * finalise PR; tested ok locally (setup json overrides env vars)
This commit is contained in:
@@ -22,4 +22,4 @@ ENV NODE_ENV=production \
|
||||
|
||||
EXPOSE 4567
|
||||
|
||||
CMD node ./nodebb build ; node ./nodebb start
|
||||
CMD test -n "${SETUP}" && ./nodebb setup || node ./nodebb build; node ./nodebb start
|
||||
|
||||
@@ -46,22 +46,54 @@ questions.optional = [
|
||||
},
|
||||
];
|
||||
|
||||
function checkSetupFlag() {
|
||||
let setupVal = install.values;
|
||||
function checkSetupFlagEnv() {
|
||||
let setupVal = install.values || {};
|
||||
|
||||
const envConfMap = {
|
||||
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)
|
||||
winston.info('[install/checkSetupFlagEnv] checking env vars for setup info...');
|
||||
|
||||
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')) {
|
||||
setupVal = JSON.parse(nconf.get('setup'));
|
||||
const setupJSON = JSON.parse(nconf.get('setup'));
|
||||
setupVal = { ...setupVal, ...setupJSON };
|
||||
}
|
||||
} catch (err) {
|
||||
winston.error('Invalid json in nconf.get(\'setup\'), ignoring setup values');
|
||||
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('Required values are missing for automated setup:');
|
||||
winston.error('[install/checkSetupFlagEnv] required values are missing for automated setup:');
|
||||
if (!setupVal['admin:username']) {
|
||||
winston.error(' admin:username');
|
||||
}
|
||||
@@ -95,7 +127,7 @@ function checkCIFlag() {
|
||||
if (ciVals.hasOwnProperty('host') && ciVals.hasOwnProperty('port') && ciVals.hasOwnProperty('database')) {
|
||||
install.ciVals = ciVals;
|
||||
} else {
|
||||
winston.error('Required values are missing for automated CI integration:');
|
||||
winston.error('[install/checkCIFlag] required values are missing for automated CI integration:');
|
||||
if (!ciVals.hasOwnProperty('host')) {
|
||||
winston.error(' host');
|
||||
}
|
||||
@@ -521,7 +553,7 @@ async function checkUpgrade() {
|
||||
|
||||
install.setup = async function () {
|
||||
try {
|
||||
checkSetupFlag();
|
||||
checkSetupFlagEnv();
|
||||
checkCIFlag();
|
||||
await setupConfig();
|
||||
await setupDefaultConfigs();
|
||||
|
||||
Reference in New Issue
Block a user