Skip to content

Commit 48ed5d4

Browse files
MC-37699: Add support to read-only pub/static directory (magento#781)
1 parent c143cfe commit 48ed5d4

File tree

9 files changed

+213
-40
lines changed

9 files changed

+213
-40
lines changed

src/Config/Environment.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class Environment
2424
public const VAL_DISABLED = 'disabled';
2525
public const VARIABLE_CRYPT_KEY = 'CRYPT_KEY';
2626

27+
public const MOUNT_PUB_STATIC = 'pub/static';
28+
2729
/**
2830
* The environment variable for controlling the directory nesting level for error reporting
2931
*/
@@ -151,4 +153,23 @@ public function isMasterBranch(): bool
151153
return !empty($branchName)
152154
&& preg_match(self::GIT_MASTER_BRANCH_RE, $branchName);
153155
}
156+
157+
/**
158+
* Checks whether application has specific mount.
159+
*
160+
* The name of the mount may have slash in the beginning (env variable)
161+
* or does not have it. Method checks both cases.
162+
*
163+
* @param string $name
164+
* @return bool
165+
*/
166+
public function hasMount(string $name): bool
167+
{
168+
$application = $this->getApplication();
169+
170+
$name = ltrim($name, '/');
171+
$slashName = '/' . $name;
172+
173+
return isset($application['mounts'][$name]) || isset($application['mounts'][$slashName]);
174+
}
154175
}

src/Config/EnvironmentData.php

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,31 +136,56 @@ public function getApplication(): array
136136
return $this->data['application'];
137137
}
138138

139-
$application = $this->getEnvVar(SystemConfigInterface::VAR_ENV_APPLICATION, []);
139+
$applicationEnvConfig = $this->getEnvVar(SystemConfigInterface::VAR_ENV_APPLICATION, []);
140+
$applicationFileConfig = $this->readApplicationConfig();
140141

141-
if (!$application) {
142+
if (!$applicationEnvConfig) {
143+
return $this->data['application'] = $applicationFileConfig;
144+
}
145+
146+
/**
147+
* Temporary fix for the case when environment data does not accurately represent file configuration.
148+
*
149+
* @url https://github.com/magento/magento-cloud-docker/issues/292
150+
*/
151+
if (!isset($applicationEnvConfig['mounts']) && isset($applicationFileConfig['mounts'])) {
152+
$applicationEnvConfig['mounts'] = $applicationFileConfig['mounts'];
153+
}
154+
155+
return $this->data['application'] = $applicationEnvConfig;
156+
}
157+
158+
/**
159+
* Read file config file if exists.
160+
*
161+
* @return array
162+
*/
163+
private function readApplicationConfig(): array
164+
{
165+
$configFile = $this->fileList->getAppConfig();
166+
167+
if ($this->file->isExists($configFile)) {
142168
try {
143-
$application = Yaml::parse(
144-
$this->file->fileGetContents($this->fileList->getAppConfig())
145-
);
146-
} catch (FileSystemException $e) {
169+
return Yaml::parse($this->file->fileGetContents($configFile));
170+
} catch (FileSystemException $exception) {
147171
// Do nothing as $application needs to be empty
148172
}
149173
}
150174

151-
return $this->data['application'] = $application;
175+
return [];
152176
}
153177

154178
/**
155179
* Returns name of environment branch
180+
*
156181
* @return string
157182
*
158183
*/
159184
public function getBranchName(): string
160185
{
161186
$envVarName = $this->systemConfig->get(SystemConfigInterface::VAR_ENV_ENVIRONMENT);
162187

163-
return $this->getEnv($envVarName) ? (string) $this->getEnv($envVarName) : '';
188+
return $this->getEnv($envVarName) ? (string)$this->getEnv($envVarName) : '';
164189
}
165190

166191
/**

src/Step/Build/BackupData/StaticContent.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99

1010
use Magento\MagentoCloud\App\Error;
1111
use Magento\MagentoCloud\App\GenericException;
12+
use Magento\MagentoCloud\Config\Environment;
1213
use Magento\MagentoCloud\Filesystem\DirectoryList;
1314
use Magento\MagentoCloud\Filesystem\Driver\File;
1415
use Magento\MagentoCloud\Filesystem\FileSystemException;
1516
use Magento\MagentoCloud\Filesystem\Flag\Manager as FlagManager;
17+
use Magento\MagentoCloud\Package\UndefinedPackageException;
1618
use Magento\MagentoCloud\Step\StepException;
1719
use Magento\MagentoCloud\Step\StepInterface;
1820
use Psr\Log\LoggerInterface;
@@ -42,28 +44,36 @@ class StaticContent implements StepInterface
4244
*/
4345
private $flagManager;
4446

47+
/**
48+
* @var Environment
49+
*/
50+
private $environment;
51+
4552
/**
4653
* @param File $file
4754
* @param LoggerInterface $logger
4855
* @param DirectoryList $directoryList
4956
* @param FlagManager $flagManager
57+
* @param Environment $environmentData
5058
*/
5159
public function __construct(
5260
File $file,
5361
LoggerInterface $logger,
5462
DirectoryList $directoryList,
55-
FlagManager $flagManager
63+
FlagManager $flagManager,
64+
Environment $environmentData
5665
) {
5766
$this->file = $file;
5867
$this->logger = $logger;
5968
$this->directoryList = $directoryList;
6069
$this->flagManager = $flagManager;
70+
$this->environment = $environmentData;
6171
}
6272

6373
/**
6474
* @inheritdoc
6575
*/
66-
public function execute()
76+
public function execute(): void
6777
{
6878
try {
6979
$this->flagManager->delete(FlagManager::FLAG_REGENERATE);
@@ -73,17 +83,25 @@ public function execute()
7383

7484
return;
7585
}
86+
} catch (GenericException $e) {
87+
throw new StepException($e->getMessage(), $e->getCode(), $e);
88+
}
89+
90+
if (!$this->environment->hasMount(Environment::MOUNT_PUB_STATIC)) {
91+
$this->logger->info('Static content was not moved to ./init directory');
7692

93+
return;
94+
}
95+
96+
try {
7797
$initPubStatic = $this->directoryList->getPath(DirectoryList::DIR_INIT) . '/pub/static';
7898
$originalPubStatic = $this->directoryList->getPath(DirectoryList::DIR_STATIC);
79-
80-
$this->cleanInitPubStatic($initPubStatic);
81-
$this->moveStaticContent($originalPubStatic, $initPubStatic);
82-
} catch (StepException $e) {
83-
throw $e;
84-
} catch (GenericException $e) {
85-
throw new StepException($e->getMessage(), $e->getCode(), $e);
99+
} catch (UndefinedPackageException $exception) {
100+
throw new StepException($exception->getMessage(), $exception->getCode(), $exception);
86101
}
102+
103+
$this->cleanInitPubStatic($initPubStatic);
104+
$this->moveStaticContent($originalPubStatic, $initPubStatic);
87105
}
88106

89107
/**

src/Step/Deploy/PreDeploy/CleanStaticContent.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class CleanStaticContent implements StepInterface
2727
/**
2828
* @var Environment
2929
*/
30-
private $env;
30+
private $environment;
3131

3232
/**
3333
* @var LoggerInterface
@@ -56,22 +56,22 @@ class CleanStaticContent implements StepInterface
5656

5757
/**
5858
* @param LoggerInterface $logger
59-
* @param Environment $env
59+
* @param Environment $environment
6060
* @param File $file
6161
* @param DirectoryList $directoryList
6262
* @param FlagManager $flagManager
6363
* @param DeployInterface $stageConfig
6464
*/
6565
public function __construct(
6666
LoggerInterface $logger,
67-
Environment $env,
67+
Environment $environment,
6868
File $file,
6969
DirectoryList $directoryList,
7070
FlagManager $flagManager,
7171
DeployInterface $stageConfig
7272
) {
7373
$this->logger = $logger;
74-
$this->env = $env;
74+
$this->environment = $environment;
7575
$this->file = $file;
7676
$this->directoryList = $directoryList;
7777
$this->flagManager = $flagManager;
@@ -83,11 +83,13 @@ public function __construct(
8383
*
8484
* {@inheritdoc}
8585
*/
86-
public function execute()
86+
public function execute(): void
8787
{
8888
try {
8989
if (!$this->flagManager->exists(FlagManager::FLAG_STATIC_CONTENT_DEPLOY_IN_BUILD)
90-
|| !$this->stageConfig->get(DeployInterface::VAR_CLEAN_STATIC_FILES)) {
90+
|| !$this->stageConfig->get(DeployInterface::VAR_CLEAN_STATIC_FILES)
91+
|| !$this->environment->hasMount(Environment::MOUNT_PUB_STATIC)
92+
) {
9193
return;
9294
}
9395

src/Test/Unit/Config/EnvironmentDataTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ public function testGetApplicationWithNoSystemVariablesFileExists(): void
153153
{
154154
$_ENV = null;
155155

156+
$this->fileMock->expects($this->once())
157+
->method('isExists')
158+
->willReturn(true);
156159
$this->fileMock->expects($this->once())
157160
->method('fileGetContents')
158161
->willReturn('[]');
@@ -165,6 +168,9 @@ public function testGetApplicationWithNoSystemVariablesFileNotExists(): void
165168
$_ENV = null;
166169
$exception = new FilesystemException('.magento.app.yaml not exist');
167170

171+
$this->fileMock->expects($this->once())
172+
->method('isExists')
173+
->willReturn(true);
168174
$this->fileMock->expects($this->once())
169175
->method('fileGetContents')
170176
->willThrowException($exception);

src/Test/Unit/Config/EnvironmentTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,22 @@ public function testGetEnvVarMageErrorReportDirNestingLevel(): void
148148

149149
$this->assertSame(1, $this->environment->getEnvVarMageErrorReportDirNestingLevel());
150150
}
151+
152+
public function testHasMount(): void
153+
{
154+
$this->environmentDataMock->method('getApplication')
155+
->willReturn([
156+
'mounts' => [
157+
'test' => [],
158+
'/test_with_slash' => []
159+
]
160+
]);
161+
162+
self::assertTrue($this->environment->hasMount('test'));
163+
self::assertTrue($this->environment->hasMount('/test'));
164+
self::assertTrue($this->environment->hasMount('test_with_slash'));
165+
self::assertTrue($this->environment->hasMount('/test_with_slash'));
166+
self::assertFalse($this->environment->hasMount('/unknown'));
167+
self::assertFalse($this->environment->hasMount('unknown'));
168+
}
151169
}

0 commit comments

Comments
 (0)