diff --git a/Dockerfile b/Dockerfile index 80dce7cfb5..8a5b7ae9bb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/src/install.js b/src/install.js index 3067a05ab5..f9f7912d9f 100644 --- a/src/install.js +++ b/src/install.js @@ -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();