Skip to content

Commit f5a000b

Browse files
authored
MCLOUD-5991: Restore additional data in env.php if this file was removed (magento#732)
1 parent e62c3b5 commit f5a000b

File tree

13 files changed

+617
-21
lines changed

13 files changed

+617
-21
lines changed

scenario/deploy.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<arguments>
66
<argument name="logger" xsi:type="object">Psr\Log\LoggerInterface</argument>
77
<argument name="steps" xsi:type="array">
8+
<item name="check-state" xsi:type="object" priority="50">Magento\MagentoCloud\Step\Deploy\PreDeploy\CheckState</item>
89
<item name="cache" xsi:type="object" priority="100">Magento\MagentoCloud\Step\Deploy\PreDeploy\ConfigUpdate\Cache</item>
910
<item name="clean-static-content" xsi:type="object" priority="200">Magento\MagentoCloud\Step\Deploy\PreDeploy\CleanStaticContent</item>
1011
<item name="clean-view-preprocessed" xsi:type="object" priority="300">Magento\MagentoCloud\Step\Deploy\PreDeploy\CleanViewPreprocessed</item>
@@ -58,6 +59,7 @@
5859
<arguments>
5960
<argument name="logger" xsi:type="object">Psr\Log\LoggerInterface</argument>
6061
<argument name="state" xsi:type="object">Magento\MagentoCloud\Config\State</argument>
62+
<argument name="flagManager" xsi:type="object">Magento\MagentoCloud\Filesystem\Flag\Manager</argument>
6163
<argument name="installSteps" xsi:type="array">
6264
<item name="cleanup-db-config" xsi:type="object" priority="50">Magento\MagentoCloud\Step\Deploy\InstallUpdate\Install\CleanupDbConfig</item>
6365
<item name="setup" xsi:type="object" priority="100">Magento\MagentoCloud\Step\Deploy\InstallUpdate\Install\Setup</item>
@@ -91,6 +93,7 @@
9193
<item name="set-admin-url" xsi:type="object" priority="1000">Magento\MagentoCloud\Step\Deploy\InstallUpdate\Update\SetAdminUrl</item>
9294
<item name="setup" xsi:type="object" priority="1100">Magento\MagentoCloud\Step\Deploy\InstallUpdate\Update\Setup</item>
9395
<item name="split-db" xsi:type="object" priority="1200">Magento\MagentoCloud\Step\Deploy\SplitDbConnection</item>
96+
<item name="cache-type" xsi:type="object" priority="1300">Magento\MagentoCloud\Step\Deploy\InstallUpdate\ConfigUpdate\CacheType</item>
9497
</argument>
9598
</arguments>
9699
</step>

src/App/Error.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ class Error
6666
public const DEPLOY_CACHE_FLUSH_COMMAND_FAILED = 127;
6767
public const DEPLOY_MAINTENANCE_MODE_DISABLING_FAILED = 128;
6868
public const DEPLOY_UNABLE_TO_READ_RESET_PASSWORD_TMPL = 129;
69+
public const DEPLOY_CACHE_ENABLE_FAILED = 130;
70+
public const DEPLOY_CRYPT_KEY_IS_ABSENT = 131;
6971

7072
public const PD_DEPLOY_IS_FAILED = 201;
7173
public const PD_ENV_PHP_IS_NOT_WRITABLE = 202;

src/Config/State.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\MagentoCloud\Config;
99

10+
use Magento\MagentoCloud\App\Error;
1011
use Magento\MagentoCloud\App\GenericException;
1112
use Magento\MagentoCloud\Config\Magento\Env\ReaderInterface;
1213
use Magento\MagentoCloud\Config\Magento\Env\WriterInterface;
@@ -38,22 +39,30 @@ class State
3839
*/
3940
private $writer;
4041

42+
/**
43+
* @var Environment
44+
*/
45+
private $environment;
46+
4147
/**
4248
* @param LoggerInterface $logger
4349
* @param ConnectionInterface $connection
4450
* @param ReaderInterface $reader
4551
* @param WriterInterface $writer
52+
* @param Environment $environment
4653
*/
4754
public function __construct(
4855
LoggerInterface $logger,
4956
ConnectionInterface $connection,
5057
ReaderInterface $reader,
51-
WriterInterface $writer
58+
WriterInterface $writer,
59+
Environment $environment
5260
) {
5361
$this->logger = $logger;
5462
$this->connection = $connection;
5563
$this->reader = $reader;
5664
$this->writer = $writer;
65+
$this->environment = $environment;
5766
}
5867

5968
/**
@@ -83,6 +92,10 @@ public function isInstalled(): bool
8392
}
8493

8594
$data = $this->reader->read();
95+
if (empty($data['crypt']['key']) && empty($this->environment->getCryptKey())) {
96+
throw new GenericException('Missing crypt key for upgrading Magento', Error::DEPLOY_CRYPT_KEY_IS_ABSENT);
97+
}
98+
8699
if (isset($data['install']['date'])) {
87100
$this->logger->info('Magento was installed on ' . $data['install']['date']);
88101

src/Filesystem/Flag/Manager.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ class Manager
3737
*/
3838
public const FLAG_IGNORE_SPLIT_DB = 'ignore_split_db';
3939

40+
/**
41+
* Used to mark that env.php file does not exist at the beginning of deployment
42+
*/
43+
public const FLAG_ENV_FILE_ABSENCE = 'env_file_absence';
44+
4045
/**
4146
* @var LoggerInterface
4247
*/

src/Filesystem/Flag/Pool.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class Pool
2020
Manager::FLAG_STATIC_CONTENT_DEPLOY_IN_BUILD => '.static_content_deploy',
2121
Manager::FLAG_DEPLOY_HOOK_IS_FAILED => 'var/.deploy_is_failed',
2222
Manager::FLAG_IGNORE_SPLIT_DB => 'var/.ignore_split_db',
23+
Manager::FLAG_ENV_FILE_ABSENCE => 'var/.env_file_absence',
2324
];
2425

2526
/**

src/Step/Deploy/InstallUpdate.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\MagentoCloud\App\GenericException;
1111
use Magento\MagentoCloud\Config\State;
12+
use Magento\MagentoCloud\Filesystem\Flag\Manager as FlagManager;
1213
use Magento\MagentoCloud\Step\StepException;
1314
use Magento\MagentoCloud\Step\StepInterface;
1415
use Psr\Log\LoggerInterface;
@@ -28,6 +29,11 @@ class InstallUpdate implements StepInterface
2829
*/
2930
private $deployConfig;
3031

32+
/**
33+
* @var FlagManager
34+
*/
35+
private $flagManager;
36+
3137
/**
3238
* @var StepInterface[]
3339
*/
@@ -41,17 +47,20 @@ class InstallUpdate implements StepInterface
4147
/**
4248
* @param LoggerInterface $logger
4349
* @param State $state
50+
* @param FlagManager $flagManager
4451
* @param StepInterface[] $installSteps
4552
* @param StepInterface[] $updateSteps
4653
*/
4754
public function __construct(
4855
LoggerInterface $logger,
4956
State $state,
57+
FlagManager $flagManager,
5058
array $installSteps,
5159
array $updateSteps
5260
) {
5361
$this->logger = $logger;
5462
$this->deployConfig = $state;
63+
$this->flagManager = $flagManager;
5564
$this->installSteps = $installSteps;
5665
$this->updateSteps = $updateSteps;
5766
}
@@ -68,6 +77,12 @@ public function execute()
6877

6978
$this->logger->notice('End of install.');
7079
} else {
80+
if ($this->flagManager->exists(FlagManager::FLAG_ENV_FILE_ABSENCE)) {
81+
$this->logger->warning('Magento state indicated as installed'
82+
. ' but configuration file app/etc/env.php was empty or did not exist.'
83+
. ' Required data will be restored from environment configurations'
84+
. ' and from .magento.env.yaml file.');
85+
}
7186
$this->logger->notice('Starting update.');
7287

7388
foreach ($this->updateSteps as $step) {
@@ -76,6 +91,7 @@ public function execute()
7691

7792
$this->logger->notice('End of update.');
7893
}
94+
$this->flagManager->delete(FlagManager::FLAG_ENV_FILE_ABSENCE);
7995
} catch (GenericException $exception) {
8096
throw new StepException($exception->getMessage(), $exception->getCode(), $exception);
8197
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MagentoCloud\Step\Deploy\InstallUpdate\ConfigUpdate;
9+
10+
use Magento\MagentoCloud\App\Error;
11+
use Magento\MagentoCloud\App\GenericException;
12+
use Magento\MagentoCloud\Config\Stage\DeployInterface;
13+
use Magento\MagentoCloud\Filesystem\Flag\Manager as FlagManager;
14+
use Magento\MagentoCloud\Shell\MagentoShell;
15+
use Magento\MagentoCloud\Shell\ShellException;
16+
use Magento\MagentoCloud\Shell\ShellFactory;
17+
use Magento\MagentoCloud\Step\StepException;
18+
use Magento\MagentoCloud\Step\StepInterface;
19+
use Psr\Log\LoggerInterface;
20+
21+
/**
22+
* @inheritdoc
23+
*/
24+
class CacheType implements StepInterface
25+
{
26+
/**
27+
* @var FlagManager
28+
*/
29+
private $flagManager;
30+
31+
/**
32+
* @var DeployInterface
33+
*/
34+
private $stageConfig;
35+
36+
/**
37+
* @var MagentoShell
38+
*/
39+
private $magentoShell;
40+
41+
/**
42+
* @var LoggerInterface
43+
*/
44+
private $logger;
45+
46+
/**
47+
* @param FlagManager $flagManager
48+
* @param DeployInterface $stageConfig
49+
* @param ShellFactory $shellFactory
50+
* @param LoggerInterface $logger
51+
*/
52+
public function __construct(
53+
FlagManager $flagManager,
54+
DeployInterface $stageConfig,
55+
ShellFactory $shellFactory,
56+
LoggerInterface $logger
57+
) {
58+
$this->flagManager = $flagManager;
59+
$this->stageConfig = $stageConfig;
60+
$this->magentoShell = $shellFactory->createMagento();
61+
$this->logger = $logger;
62+
}
63+
64+
/**
65+
* Enable all cache types if env.php was absence
66+
*
67+
* @inheritdoc
68+
*/
69+
public function execute()
70+
{
71+
try {
72+
if ($this->flagManager->exists(FlagManager::FLAG_ENV_FILE_ABSENCE)) {
73+
$this->logger->info('Run cache:enable to restore all cache types');
74+
$this->magentoShell->execute(
75+
'cache:enable',
76+
[$this->stageConfig->get(DeployInterface::VAR_VERBOSE_COMMANDS)]
77+
);
78+
}
79+
} catch (ShellException $exception) {
80+
throw new StepException($exception->getMessage(), Error::DEPLOY_CACHE_ENABLE_FAILED, $exception);
81+
} catch (GenericException $exception) {
82+
throw new StepException($exception->getMessage(), $exception->getCode(), $exception);
83+
}
84+
}
85+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MagentoCloud\Step\Deploy\PreDeploy;
9+
10+
use Magento\MagentoCloud\App\GenericException;
11+
use Magento\MagentoCloud\Config\Magento\Env\ReaderInterface as ConfigReader;
12+
use Magento\MagentoCloud\Filesystem\Flag\Manager as FlagManager;
13+
use Magento\MagentoCloud\Step\StepException;
14+
use Magento\MagentoCloud\Step\StepInterface;
15+
use Psr\Log\LoggerInterface;
16+
17+
/**
18+
* Check state at the beginning of the deployment
19+
*/
20+
class CheckState implements StepInterface
21+
{
22+
/**
23+
* @var LoggerInterface
24+
*/
25+
private $logger;
26+
27+
/**
28+
* @var ConfigReader
29+
*/
30+
private $configReader;
31+
32+
/**
33+
* @var FlagManager
34+
*/
35+
private $flagManager;
36+
37+
/**
38+
* @param ConfigReader $configReader
39+
* @param FlagManager $flagManager
40+
* @param LoggerInterface $logger
41+
*/
42+
public function __construct(
43+
ConfigReader $configReader,
44+
FlagManager $flagManager,
45+
LoggerInterface $logger
46+
) {
47+
$this->configReader = $configReader;
48+
$this->flagManager = $flagManager;
49+
$this->logger = $logger;
50+
}
51+
52+
/**
53+
* Set flag if env.php does not exist
54+
* @inheritdoc
55+
*/
56+
public function execute()
57+
{
58+
try {
59+
$config = $this->configReader->read();
60+
61+
//workaround when Magento creates empty env.php with one cache_type
62+
if (empty($config) || (count($config) == 1 && isset($config['cache_types']))) {
63+
$this->logger->info(sprintf('Set "%s" flag', FlagManager::FLAG_ENV_FILE_ABSENCE));
64+
$this->flagManager->set(FlagManager::FLAG_ENV_FILE_ABSENCE);
65+
}
66+
} catch (GenericException $e) {
67+
throw new StepException($e->getMessage(), $e->getCode(), $e);
68+
}
69+
}
70+
}

src/Step/Deploy/RemoveDeployFailedFlag.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,6 @@ public function execute()
3535
{
3636
$this->manager->delete(Manager::FLAG_DEPLOY_HOOK_IS_FAILED);
3737
$this->manager->delete(Manager::FLAG_IGNORE_SPLIT_DB);
38+
$this->manager->delete(Manager::FLAG_ENV_FILE_ABSENCE);
3839
}
3940
}

0 commit comments

Comments
 (0)