From 23288514718e66d3d698250528c7cc1c82191aa1 Mon Sep 17 00:00:00 2001 From: Okabe Rintaro Date: Wed, 29 Jun 2022 17:25:37 +0300 Subject: [PATCH] remove separate setup-pq task, move its logic to update-env-php task, add setup for admin uri in env.php --- .../lib/tasks/magento/setup-magento/index.js | 6 -- .../magento/setup-magento/migrate-database.js | 5 ++ .../lib/tasks/php/update-env-php.js | 34 ++++++++- .../lib/tasks/php/update-env.php | 30 +++++--- .../lib/tasks/theme/link-theme.js | 2 - .../lib/tasks/theme/setup-persisted-query.js | 70 ------------------- 6 files changed, 60 insertions(+), 87 deletions(-) delete mode 100644 build-packages/magento-scripts/lib/tasks/theme/setup-persisted-query.js diff --git a/build-packages/magento-scripts/lib/tasks/magento/setup-magento/index.js b/build-packages/magento-scripts/lib/tasks/magento/setup-magento/index.js index 6d58960e..b8f92a12 100644 --- a/build-packages/magento-scripts/lib/tasks/magento/setup-magento/index.js +++ b/build-packages/magento-scripts/lib/tasks/magento/setup-magento/index.js @@ -7,12 +7,10 @@ const setBaseUrl = require('./set-base-url'); const disableMaintenanceMode = require('./disable-maintenance-mode'); const disable2fa = require('./disable-2fa'); const setUrlRewrite = require('./set-url-rewrite'); -const updateEnvPHP = require('../../php/update-env-php'); const increaseAdminSessionLifetime = require('./increase-admin-session-lifetime'); const magentoTask = require('../../../util/magento-task'); const urnHighlighter = require('./urn-highlighter'); const waitingForVarnish = require('./waiting-for-varnish'); -const setupPersistedQuery = require('../../theme/setup-persisted-query'); const adjustFullPageCache = require('./adjust-full-page-cache'); /** @@ -28,8 +26,6 @@ const setupMagento = (options = {}) => ({ return task.newListr([ flushRedisConfig(), waitingForRedis(), - updateEnvPHP(), - setupPersistedQuery(), migrateDatabase({ onlyInstallMagento: true }) ]); } @@ -37,8 +33,6 @@ const setupMagento = (options = {}) => ({ return task.newListr([ flushRedisConfig(), waitingForRedis(), - updateEnvPHP(), - setupPersistedQuery(), migrateDatabase(), { title: 'Configuring Magento settings', diff --git a/build-packages/magento-scripts/lib/tasks/magento/setup-magento/migrate-database.js b/build-packages/magento-scripts/lib/tasks/magento/setup-magento/migrate-database.js index 2e7d706f..27c6ee67 100644 --- a/build-packages/magento-scripts/lib/tasks/magento/setup-magento/migrate-database.js +++ b/build-packages/magento-scripts/lib/tasks/magento/setup-magento/migrate-database.js @@ -7,6 +7,7 @@ const installMagento = require('./install-magento'); const upgradeMagento = require('./upgrade-magento'); const varnishConfigSetup = require('./varnish-config'); const pathExists = require('../../../util/path-exists'); +const updateEnvPHP = require('../../php/update-env-php'); /** * @param {Object} [options] @@ -41,6 +42,7 @@ const migrateDatabase = (options = {}) => ({ return task.newListr([ installMagento({ isDbEmpty: true }), + updateEnvPHP(), varnishConfigSetup(), configureElasticsearch(), upgradeMagento(), @@ -65,6 +67,7 @@ const migrateDatabase = (options = {}) => ({ ctx.isSetupUpgradeNeeded = false; // no setup is needed, but still to be sure configure ES return task.newListr([ + updateEnvPHP(), varnishConfigSetup(), configureElasticsearch() ], { @@ -86,6 +89,7 @@ const migrateDatabase = (options = {}) => ({ return task.newListr([ installMagentoProject(), + updateEnvPHP(), varnishConfigSetup(), configureElasticsearch(), upgradeMagento(), @@ -101,6 +105,7 @@ const migrateDatabase = (options = {}) => ({ } case 2: { return task.newListr([ + updateEnvPHP(), varnishConfigSetup(), configureElasticsearch(), upgradeMagento() diff --git a/build-packages/magento-scripts/lib/tasks/php/update-env-php.js b/build-packages/magento-scripts/lib/tasks/php/update-env-php.js index f020f417..29e3edfa 100644 --- a/build-packages/magento-scripts/lib/tasks/php/update-env-php.js +++ b/build-packages/magento-scripts/lib/tasks/php/update-env-php.js @@ -1,7 +1,10 @@ const path = require('path'); +const envPhpToJson = require('../../util/env-php-json'); +const getJsonfileData = require('../../util/get-jsonfile-data'); const pathExists = require('../../util/path-exists'); const phpTask = require('../../util/php-task'); +const composerLockPath = path.join(process.cwd(), 'composer.lock'); /** * @type {() => import('listr2').ListrTask} */ @@ -21,6 +24,32 @@ const updateEnvPHP = () => ({ ? ctx.cachedPorts.varnish : ctx.cachedPorts; + let SETUP_PQ = '1'; + + if (await pathExists(composerLockPath)) { + const composerLockData = await getJsonfileData(composerLockPath); + + if (composerLockData.packages.some(({ name }) => name === 'scandipwa/persisted-query')) { + if (typeof ctx.CSAThemeInstalled !== 'boolean') { + ctx.CSAThemeInstalled = true; + } + + const envPhp = await envPhpToJson(process.cwd(), { magentoVersion: ctx.magentoVersion }); + + const persistedQueryConfig = envPhp.cache && envPhp.cache['persisted-query']; + + if ( + persistedQueryConfig + && persistedQueryConfig.redis + && persistedQueryConfig.redis.port === `${ ctx.ports.redis }` + && persistedQueryConfig.redis.host === 'localhost' + ) { + SETUP_PQ = ''; + return; + } + } + } + return task.newListr( phpTask(`-f ${ path.join(__dirname, 'update-env.php') }`, { noTitle: true, @@ -28,7 +57,10 @@ const updateEnvPHP = () => ({ USE_VARNISH: useVarnish, VARNISH_PORT: `${ varnishPort }`, VARNISH_HOST: varnishHost, - PREVIOUS_VARNISH_PORT: `${ previousVarnishPort }` + PREVIOUS_VARNISH_PORT: `${ previousVarnishPort }`, + SETUP_PQ, + REDIS_PORT: ctx.ports.redis, + ADMIN_URI: ctx.config.overridenConfiguration.magento.adminuri } }) ); diff --git a/build-packages/magento-scripts/lib/tasks/php/update-env.php b/build-packages/magento-scripts/lib/tasks/php/update-env.php index 2cd2a66c..00e6dc5e 100644 --- a/build-packages/magento-scripts/lib/tasks/php/update-env.php +++ b/build-packages/magento-scripts/lib/tasks/php/update-env.php @@ -78,6 +78,9 @@ public function loadPortConfig() public function modifyConfig() { + // set admin uri + $this->config['backend']['frontName'] = getenv('ADMIN_URI'); + // update mysql config if (isset($this->config['db']['connection']['default'])) { $conn = &$this->config['db']['connection']['default']; @@ -120,19 +123,30 @@ public function modifyConfig() } // update persisted query redis config - if (isset($this->config['cache']['persisted-query'])) { - $persistedQuery = &$this->config['cache']['persisted-query']; + if (getenv('SETUP_PQ') == '1') { + $cacheConfig = &$this->config['cache']; + $redisPort = getenv('REDIS_PORT'); - if (isset($persistedQuery['redis'])) { - if ($persistedQuery['redis']['port'] !== strval($this->portConfig['redis'])) { - $persistedQuery['redis']['port'] = strval($this->portConfig['redis']); - } - if ($persistedQuery['redis']['host'] !== 'localhost') { - $persistedQuery['redis']['host'] = 'localhost'; + if (isset($cacheConfig) && isset($cacheConfig['persisted-query']) && isset($cacheConfig['persisted-query']['redis']) && $cacheConfig['persisted-query']['redis']['port'] != $redisPort) { + $cacheConfig['persisted-query']['redis']['port'] = $redisPort; + } else { + if (!isset($cacheConfig)) { + $this->config['cache'] = []; } + $this->config['cache']['persisted-query'] = [ + 'redis' => [ + 'host' => 'localhost', + 'port' => $redisPort, + 'database' => '5', + 'scheme' => 'tcp' + ] + ]; } + } else { + unset($this->config['cache']['persisted-query']); } + // set varnish config $httpCacheHosts = &$this->config['http_cache_hosts']; $httpCacheHosts = []; diff --git a/build-packages/magento-scripts/lib/tasks/theme/link-theme.js b/build-packages/magento-scripts/lib/tasks/theme/link-theme.js index 712aa61f..9e38d450 100644 --- a/build-packages/magento-scripts/lib/tasks/theme/link-theme.js +++ b/build-packages/magento-scripts/lib/tasks/theme/link-theme.js @@ -4,7 +4,6 @@ const adjustFullPageCache = require('../magento/setup-magento/adjust-full-page-c const disablePageBuilder = require('../magento/setup-magento/disable-page-builder'); const buildTheme = require('./build-theme'); const upgradeMagento = require('../magento/setup-magento/upgrade-magento'); -const setupPersistedQuery = require('./setup-persisted-query'); const updateEnvPHP = require('../php/update-env-php'); const semver = require('semver'); @@ -51,7 +50,6 @@ const linkTheme = () => ({ symlinkTheme(theme), installTheme(theme), updateEnvPHP(), - setupPersistedQuery(), upgradeMagento(), adjustFullPageCache(), ...(isPageBuilderInstalled && Number(isPagebuilderEnabled) ? [disablePageBuilder()] : []), diff --git a/build-packages/magento-scripts/lib/tasks/theme/setup-persisted-query.js b/build-packages/magento-scripts/lib/tasks/theme/setup-persisted-query.js deleted file mode 100644 index 95d2d813..00000000 --- a/build-packages/magento-scripts/lib/tasks/theme/setup-persisted-query.js +++ /dev/null @@ -1,70 +0,0 @@ -const path = require('path'); -const UnknownError = require('../../errors/unknown-error'); -const envPhpToJson = require('../../util/env-php-json'); -const getJsonfileData = require('../../util/get-jsonfile-data'); -const pathExists = require('../../util/path-exists'); -const runMagentoCommand = require('../../util/run-magento'); - -const composerLockPath = path.join(process.cwd(), 'composer.lock'); - -/** - * TODO move this block inside theme folder as post installation command - * @type {() => import('listr2').ListrTask} - */ -const persistedQuerySetup = () => ({ - title: 'Setting up Redis configuration for persisted queries', - task: async (ctx, task) => { - const { ports, magentoVersion, verbose = false } = ctx; - if (!await pathExists(composerLockPath)) { - task.skip('composer.lock does not exist'); - return; - } - const composerLockData = await getJsonfileData(composerLockPath); - - if (!composerLockData.packages.some(({ name }) => name === 'scandipwa/persisted-query')) { - /** - * No persisted query package available, skipping its setup - */ - task.skip(); - return; - } - ctx.CSAThemeInstalled = true; - - const envPhp = await envPhpToJson(process.cwd(), { magentoVersion }); - - const persistedQueryConfig = envPhp.cache && envPhp.cache['persisted-query']; - - if ( - persistedQueryConfig - && persistedQueryConfig.redis - && persistedQueryConfig.redis.port === `${ ports.redis }` - && persistedQueryConfig.redis.host === 'localhost' - ) { - task.skip(); - return; - } - - task.output = 'Setting up persisted query...'; - - try { - await runMagentoCommand(`setup:config:set \ - --pq-host=localhost \ - --pq-port=${ports.redis} \ - --pq-database=5 \ - --pq-scheme=tcp \ - -n`, { - callback: !verbose ? undefined : (t) => { - task.output = t; - }, - magentoVersion - }); - } catch (e) { - throw new UnknownError( - `Unexpected error while setting redis for pq!. - See ERROR log below.\n\n${e}` - ); - } - } -}); - -module.exports = persistedQuerySetup;