Skip to content

Commit c29c546

Browse files
oshmyheliukshiftedreality
authored andcommitted
MAGECLOUD-3369: [Cloud Docker] Redis is Required for Deploy Phase even if it is Absent in Services (#443)
1 parent 9f2c887 commit c29c546

File tree

15 files changed

+565
-63
lines changed

15 files changed

+565
-63
lines changed

composer.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@
8383
"dist/docker/mysql",
8484
"docker/mysql"
8585
],
86-
[
87-
"dist/docker/config.php",
88-
"docker/config.php.dist"
89-
],
9086
[
9187
"dist/docker/global.php",
9288
"docker/global.php.dist"

dist/docker/config.php

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/App/Container.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ function () {
146146
\Magento\MagentoCloud\View\RendererInterface::class,
147147
\Magento\MagentoCloud\View\TwigRenderer::class
148148
);
149+
$this->container->singleton(
150+
\Magento\MagentoCloud\PlatformVariable\DecoderInterface::class,
151+
\Magento\MagentoCloud\PlatformVariable\Decoder::class
152+
);
149153
/**
150154
* Contextual binding.
151155
*/

src/Command/Docker/Build.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\MagentoCloud\Config\RepositoryFactory;
1212
use Magento\MagentoCloud\Docker\BuilderFactory;
1313
use Magento\MagentoCloud\Docker\BuilderInterface;
14+
use Magento\MagentoCloud\Docker\Config\DistGenerator;
1415
use Magento\MagentoCloud\Docker\ConfigurationMismatchException;
1516
use Magento\MagentoCloud\Filesystem\Driver\File;
1617
use Magento\MagentoCloud\Filesystem\FileSystemException;
@@ -22,6 +23,8 @@
2223

2324
/**
2425
* Builds Docker configuration for Magento project.
26+
*
27+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2528
*/
2629
class Build extends Command
2730
{
@@ -53,22 +56,30 @@ class Build extends Command
5356
*/
5457
private $configFactory;
5558

59+
/**
60+
* @var DistGenerator
61+
*/
62+
private $distGenerator;
63+
5664
/**
5765
* @param BuilderFactory $builderFactory
5866
* @param File $file
5967
* @param Environment $environment
6068
* @param RepositoryFactory $configFactory
69+
* @param DistGenerator $distGenerator
6170
*/
6271
public function __construct(
6372
BuilderFactory $builderFactory,
6473
File $file,
6574
Environment $environment,
66-
RepositoryFactory $configFactory
75+
RepositoryFactory $configFactory,
76+
DistGenerator $distGenerator
6777
) {
6878
$this->builderFactory = $builderFactory;
6979
$this->file = $file;
7080
$this->environment = $environment;
7181
$this->configFactory = $configFactory;
82+
$this->distGenerator = $distGenerator;
7283

7384
parent::__construct();
7485
}
@@ -146,6 +157,8 @@ public function execute(InputInterface $input, OutputInterface $output)
146157
Yaml::dump($builder->build($config), 4, 2, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK)
147158
);
148159

160+
$this->distGenerator->generate();
161+
149162
$this->getApplication()
150163
->find(ConfigConvert::NAME)
151164
->run($input, $output);

src/Config/Environment.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\MagentoCloud\Config;
77

8+
use Magento\MagentoCloud\PlatformVariable\DecoderInterface;
89
use Magento\MagentoCloud\Config\System\Variables;
910

1011
/**
@@ -38,14 +39,21 @@ class Environment
3839
const DEFAULT_ADMIN_FIRSTNAME = 'Admin';
3940
const DEFAULT_ADMIN_LASTNAME = 'Username';
4041

42+
/**
43+
* @var DecoderInterface
44+
*/
45+
private $decoder;
46+
4147
/**
4248
* Environment constructor.
4349
*
4450
* @param Variables $systemConfig
51+
* @param DecoderInterface $decoder
4552
*/
46-
public function __construct(Variables $systemConfig)
53+
public function __construct(Variables $systemConfig, DecoderInterface $decoder)
4754
{
4855
$this->systemConfig = $systemConfig;
56+
$this->decoder = $decoder;
4957
}
5058

5159
/**
@@ -82,7 +90,7 @@ public function get(string $key, $default = null)
8290
return $default;
8391
}
8492

85-
return json_decode(base64_decode($value), true);
93+
return $this->decoder->decode($value);
8694
}
8795

8896
/**

src/Docker/Config/DistGenerator.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\MagentoCloud\Docker\Config;
7+
8+
use Magento\MagentoCloud\Docker\ConfigurationMismatchException;
9+
use Magento\MagentoCloud\Filesystem\DirectoryList;
10+
use Magento\MagentoCloud\Filesystem\Driver\File;
11+
use Magento\MagentoCloud\Filesystem\FileSystemException;
12+
use Magento\MagentoCloud\Util\PhpFormatter;
13+
14+
/**
15+
* Creates docker/config.php.dist file
16+
*/
17+
class DistGenerator
18+
{
19+
/**
20+
* @var DirectoryList
21+
*/
22+
private $directoryList;
23+
24+
/**
25+
* @var File
26+
*/
27+
private $file;
28+
29+
/**
30+
* @var PhpFormatter
31+
*/
32+
private $phpFormatter;
33+
34+
/**
35+
* @var Relationship
36+
*/
37+
private $relationship;
38+
39+
/**
40+
* @var array
41+
*/
42+
private $baseConfig = [
43+
'MAGENTO_CLOUD_ROUTES' => [
44+
'http://magento2.docker/' => [
45+
'type' => 'upstream',
46+
'original_url' => 'http://{default}'
47+
],
48+
'https://magento2.docker/' => [
49+
'type' => 'upstream',
50+
'original_url' => 'https://{default}'
51+
],
52+
],
53+
'MAGENTO_CLOUD_VARIABLES' => [
54+
'ADMIN_EMAIL' => 'admin@example.com',
55+
'ADMIN_PASSWORD' => '123123q',
56+
'ADMIN_URL' => 'admin'
57+
],
58+
];
59+
60+
/**
61+
* @param DirectoryList $directoryList
62+
* @param File $file
63+
* @param Relationship $relationship
64+
* @param PhpFormatter $phpFormatter
65+
*/
66+
public function __construct(
67+
DirectoryList $directoryList,
68+
File $file,
69+
Relationship $relationship,
70+
PhpFormatter $phpFormatter
71+
) {
72+
$this->directoryList = $directoryList;
73+
$this->file = $file;
74+
$this->phpFormatter = $phpFormatter;
75+
$this->relationship = $relationship;
76+
}
77+
78+
/**
79+
* Create docker/config.php.dist file
80+
* generate MAGENTO_CLOUD_RELATIONSHIPS according to services enablements.
81+
*
82+
* @throws FileSystemException if file can't be saved
83+
* @throws ConfigurationMismatchException if can't obtain relationships
84+
*/
85+
public function generate()
86+
{
87+
$configPath = $this->directoryList->getMagentoRoot() . '/docker/config.php.dist';
88+
89+
$config = array_merge(
90+
['MAGENTO_CLOUD_RELATIONSHIPS' => $this->relationship->get()],
91+
$this->baseConfig
92+
);
93+
94+
$this->saveConfig($configPath, $config);
95+
}
96+
97+
/**
98+
* Formats and save configuration to file.
99+
*
100+
* @param string $filePath
101+
* @param array $config
102+
* @throws FileSystemException
103+
*/
104+
private function saveConfig(string $filePath, array $config)
105+
{
106+
$result = "<?php\n\nreturn [";
107+
foreach ($config as $key => $value) {
108+
$result .= "\n '{$key}' => ";
109+
$result .= 'base64_encode(json_encode(' . $this->phpFormatter->varExportShort($value, 2) .')),';
110+
}
111+
$result .= "\n];\n";
112+
113+
$this->file->filePutContents($filePath, $result);
114+
}
115+
}

src/Docker/Config/Relationship.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\MagentoCloud\Docker\Config;
7+
8+
use Magento\MagentoCloud\Docker\Config;
9+
use Magento\MagentoCloud\Docker\ConfigurationMismatchException;
10+
11+
/**
12+
* Generates relationship data for current configuration
13+
* based on services in .magento/service.yaml and relationships in .magento.app.yaml
14+
*/
15+
class Relationship
16+
{
17+
/**
18+
* @var Config
19+
*/
20+
private $config;
21+
22+
/**
23+
* Default relationships configuration
24+
*
25+
* @var array
26+
*/
27+
private $defaultConfiguration = [
28+
'database' => [
29+
[
30+
'host' => 'db',
31+
'path' => 'magento2',
32+
'password' => 'magento2',
33+
'username' => 'magento2',
34+
'port' => '3306'
35+
],
36+
],
37+
'redis' => [
38+
[
39+
'host' => 'redis',
40+
'port' => '6379'
41+
]
42+
],
43+
'elasticsearch' => [
44+
[
45+
'host' => 'elasticsearch',
46+
'port' => '9200',
47+
],
48+
],
49+
'rabbitmq' => [
50+
[
51+
'host' => 'rabbitmq',
52+
'port' => '5672',
53+
'username' => 'guest',
54+
'password' => 'guest',
55+
]
56+
],
57+
];
58+
59+
/**
60+
* @param Config $config
61+
*/
62+
public function __construct(
63+
Config $config
64+
) {
65+
$this->config = $config;
66+
}
67+
68+
/**
69+
* Generates relationship data for current configuration
70+
*
71+
* @throws ConfigurationMismatchException
72+
*/
73+
public function get(): array
74+
{
75+
$relationships = [];
76+
foreach ($this->defaultConfiguration as $serviceName => $serviceConfig) {
77+
if ($this->config->getServiceVersion($this->convertServiceName($serviceName))) {
78+
$relationships[$serviceName] = $serviceConfig;
79+
}
80+
}
81+
82+
return $relationships;
83+
}
84+
85+
/**
86+
* Convert services names for compatibility with `getServiceVersion` method.
87+
*
88+
* @param string $serviceName
89+
* @return string
90+
*/
91+
private function convertServiceName(string $serviceName): string
92+
{
93+
$map = [
94+
'database' => Config::KEY_DB
95+
];
96+
97+
return $map[$serviceName] ?? $serviceName;
98+
}
99+
}

src/PlatformVariable/Decoder.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\MagentoCloud\PlatformVariable;
7+
8+
/**
9+
* @inheritdoc
10+
*/
11+
class Decoder implements DecoderInterface
12+
{
13+
/**
14+
* @inheritdoc
15+
*/
16+
public function decode(string $encodedString)
17+
{
18+
return json_decode(base64_decode($encodedString), true);
19+
}
20+
}

0 commit comments

Comments
 (0)