Skip to content

Commit e4dfd23

Browse files
MCLOUD-5706: Add support of new ES option to ECE-Tools (magento#723)
1 parent df74fd0 commit e4dfd23

File tree

19 files changed

+624
-234
lines changed

19 files changed

+624
-234
lines changed

.github/workflows/ci.yaml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ jobs:
88
strategy:
99
matrix:
1010
php-versions: ['7.1', '7.2', '7.3']
11-
env:
12-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
13-
REPO_USERNAME_CE: ${{ secrets.REPO_USERNAME_CE }}
14-
REPO_PASSWORD_CE: ${{ secrets.REPO_PASSWORD_CE }}
1511
steps:
1612
- name: Checkout
1713
uses: actions/checkout@v1
@@ -22,10 +18,12 @@ jobs:
2218
with:
2319
php-version: ${{ matrix.php-versions }}
2420
extensions: sockets
25-
- name: Composer credentials
21+
- name: Remove secure repositories
2622
run: |
27-
composer config http-basic.repo.magento.com ${REPO_USERNAME_CE} ${REPO_PASSWORD_CE}
28-
composer config github-oauth.github.com ${GITHUB_TOKEN}
23+
composer config --unset repositories.repo.magento.com
24+
composer remove --no-update magento/magento-cloud-components
25+
composer remove --no-update magento/magento-cloud-docker
26+
composer remove --no-update magento/magento-cloud-patches
2927
- name: Composer Update
3028
run: composer update
3129
- name: Static and Unit tests

config/services.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<service id="Magento\MagentoCloud\Scenario\Exception\ValidationException" autowire="false" />
3636
<service id="Magento\MagentoCloud\Service\ServiceMismatchException" autowire="false" />
3737
<service id="Magento\MagentoCloud\Config\ValidatorException" autowire="false" />
38+
<service id="Magento\MagentoCloud\Service\ServiceException" autowire="false" />
3839
<service id="Magento\MagentoCloud\Shell\Process" autowire="false" />
3940
<service id="Magento\MagentoCloud\Shell\ProcessException" autowire="false" />
4041
<service id="Magento\MagentoCloud\Step\Build\BackupData" autowire="false" />

scenario/deploy.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<item name="search-configuration" xsi:type="object">Magento\MagentoCloud\Config\Validator\Deploy\SearchConfiguration</item>
2727
<item name="resource-configuration" xsi:type="object">Magento\MagentoCloud\Config\Validator\Deploy\ResourceConfiguration</item>
2828
<item name="session-configuration" xsi:type="object">Magento\MagentoCloud\Config\Validator\Deploy\SessionConfiguration</item>
29+
<item name="elasticsearch-integrity" xsi:type="object">Magento\MagentoCloud\Config\Validator\Deploy\ElasticSearchIntegrity</item>
2930
<item name="elasticsuite-integrity" xsi:type="object">Magento\MagentoCloud\Config\Validator\Deploy\ElasticSuiteIntegrity</item>
3031
</item>
3132
<item name="warning" xsi:type="array">

src/App/GenericException.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
/**
1313
* Base exception for general purposes.
14+
*
15+
* @SuppressWarnings(PHPMD.NumberOfChildren)
1416
*/
1517
class GenericException extends \Exception
1618
{

src/Config/SearchEngine.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77

88
namespace Magento\MagentoCloud\Config;
99

10-
use Composer\Semver\Semver;
1110
use Magento\MagentoCloud\Config\SearchEngine\ElasticSuite;
1211
use Magento\MagentoCloud\Config\Stage\DeployInterface;
1312
use Magento\MagentoCloud\Package\MagentoVersion;
1413
use Magento\MagentoCloud\Package\UndefinedPackageException;
1514
use Magento\MagentoCloud\Service\ElasticSearch;
15+
use Magento\MagentoCloud\Service\ServiceException;
1616

1717
/**
1818
* Returns search configuration.
@@ -138,6 +138,7 @@ public function isESFamily(): bool
138138
* @return array
139139
*
140140
* @throws UndefinedPackageException
141+
* @throws ServiceException
141142
*/
142143
private function getSearchConfig(): array
143144
{
@@ -176,15 +177,12 @@ private function getSolrConfiguration(array $config): array
176177
*
177178
* @param array $config Elasticsearch connection configuration
178179
* @return array
180+
*
181+
* @throws ServiceException
179182
*/
180183
private function getElasticSearchConfiguration(array $config): array
181184
{
182-
$engine = ElasticSearch::ENGINE_NAME;
183-
184-
$esVersion = $this->elasticSearch->getVersion();
185-
if (Semver::satisfies($esVersion, '>= 5')) {
186-
$engine .= (int)$esVersion;
187-
}
185+
$engine = $this->elasticSearch->getFullVersion();
188186

189187
$elasticSearchConfig = [
190188
'engine' => $engine,

src/Config/SearchEngine/ElasticSuite.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ public function get(): array
7373
return $this->configMerger->merge($this->getConfig(), $envConfig);
7474
}
7575

76+
/**
77+
* @return string
78+
*/
79+
public function getServers(): string
80+
{
81+
return $this->get()['es_client']['servers'] ?? '';
82+
}
83+
7684
/**
7785
* Checks if both ElasticSearch and ElasticSuite are installed.
7886
*
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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\Config\Validator\Deploy;
9+
10+
use Magento\MagentoCloud\Config\Validator;
11+
use Magento\MagentoCloud\Config\ValidatorException;
12+
use Magento\MagentoCloud\Config\ValidatorInterface;
13+
use Magento\MagentoCloud\Package\MagentoVersion;
14+
use Magento\MagentoCloud\Package\UndefinedPackageException;
15+
use Magento\MagentoCloud\Service\ElasticSearch;
16+
17+
/**
18+
* Verifies if Elasticsearch service present for Magento 2.4.0 and above
19+
*/
20+
class ElasticSearchIntegrity implements ValidatorInterface
21+
{
22+
/**
23+
* @var MagentoVersion
24+
*/
25+
private $magentoVersion;
26+
27+
/**
28+
* @var Validator\ResultFactory
29+
*/
30+
private $resultFactory;
31+
32+
/**
33+
* @var ElasticSearch
34+
*/
35+
private $elasticsearch;
36+
37+
/**
38+
* @param MagentoVersion $magentoVersion
39+
* @param Validator\ResultFactory $resultFactory
40+
* @param ElasticSearch $elasticSearch
41+
*/
42+
public function __construct(
43+
MagentoVersion $magentoVersion,
44+
Validator\ResultFactory $resultFactory,
45+
ElasticSearch $elasticSearch
46+
) {
47+
$this->magentoVersion = $magentoVersion;
48+
$this->resultFactory = $resultFactory;
49+
$this->elasticsearch = $elasticSearch;
50+
}
51+
52+
/**
53+
* @inheritDoc
54+
*/
55+
public function validate(): Validator\ResultInterface
56+
{
57+
try {
58+
if ($this->magentoVersion->isGreaterOrEqual('2.4.0')
59+
&& !$this->elasticsearch->isInstalled()
60+
) {
61+
return $this->resultFactory->error('Magento 2.4.0 requires Elasticsearch service to be installed.');
62+
}
63+
} catch (UndefinedPackageException $exception) {
64+
throw new ValidatorException($exception->getMessage(), $exception->getCode(), $exception);
65+
}
66+
67+
return $this->resultFactory->success();
68+
}
69+
}

src/Config/Validator/Deploy/ElasticSearchVersion.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,19 @@
1111
use Composer\Semver\Semver;
1212
use Magento\MagentoCloud\Config\SearchEngine;
1313
use Magento\MagentoCloud\Config\Validator;
14+
use Magento\MagentoCloud\Config\ValidatorException;
1415
use Magento\MagentoCloud\Config\ValidatorInterface;
1516
use Magento\MagentoCloud\Package\MagentoVersion;
1617
use Magento\MagentoCloud\Package\Manager;
1718
use Magento\MagentoCloud\Package\UndefinedPackageException;
1819
use Magento\MagentoCloud\Service\ElasticSearch;
20+
use Magento\MagentoCloud\Service\ServiceException;
1921
use Psr\Log\LoggerInterface;
2022

2123
/**
2224
* Validates compatibility of elasticsearch and magento versions.
25+
*
26+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2327
*/
2428
class ElasticSearchVersion implements ValidatorInterface
2529
{
@@ -108,16 +112,20 @@ public function __construct(
108112
* according to version mapping.
109113
* Skips validation if elasticsearch service is not installed or another search engine configured.
110114
*
111-
* @return Validator\ResultInterface
115+
* {@inheritDoc}
112116
*/
113117
public function validate(): Validator\ResultInterface
114118
{
115-
$esServiceVersion = $this->elasticSearch->getVersion();
116-
117-
if ($esServiceVersion === '0') {
119+
if (!$this->elasticSearch->isInstalled()) {
118120
return $this->resultFactory->success();
119121
}
120122

123+
try {
124+
$esServiceVersion = $this->elasticSearch->getVersion();
125+
} catch (ServiceException $exception) {
126+
throw new ValidatorException($exception->getMessage(), $exception->getCode(), $exception);
127+
}
128+
121129
if (!$this->searchEngine->isESFamily()) {
122130
return $this->resultFactory->success();
123131
}

src/Service/ElasticSearch.php

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@
77

88
namespace Magento\MagentoCloud\Service;
99

10+
use Composer\Semver\Semver;
1011
use Magento\MagentoCloud\Config\Environment;
1112
use Magento\MagentoCloud\Http\ClientFactory;
1213
use Psr\Log\LoggerInterface;
14+
use Throwable;
1315

1416
/**
1517
* Returns ElasticSearch service configurations.
1618
*/
1719
class ElasticSearch implements ServiceInterface
1820
{
19-
const RELATIONSHIP_KEY = 'elasticsearch';
20-
const ENGINE_NAME = 'elasticsearch';
21+
private const RELATIONSHIP_KEY = 'elasticsearch';
22+
public const ENGINE_NAME = 'elasticsearch';
2123

2224
/**
2325
* @var ClientFactory
@@ -80,33 +82,79 @@ public function getConfiguration(): array
8082
* elasticsearch doesn't exist in relationships.
8183
*
8284
* @return string
85+
*
86+
* @throws ServiceException
8387
*/
8488
public function getVersion(): string
8589
{
86-
if ($this->version === null) {
87-
$this->version = '0';
88-
89-
$config = $this->getConfiguration();
90-
if (!$config) {
91-
return $this->version;
92-
}
90+
if (!$this->isInstalled()) {
91+
return '0';
92+
}
9393

94+
if ($this->version === null) {
9495
try {
9596
$esConfiguration = $this->call(sprintf(
9697
'%s:%s',
97-
$config['host'],
98-
$config['port']
98+
$this->getHost(),
99+
$this->getPort()
99100
));
100101

101102
$this->version = $esConfiguration['version']['number'];
102-
} catch (\Exception $exception) {
103-
$this->logger->warning('Can\'t get version of elasticsearch: ' . $exception->getMessage());
103+
} catch (Throwable $exception) {
104+
throw new ServiceException('Can\'t get version of elasticsearch: ' . $exception->getMessage());
104105
}
105106
}
106107

107108
return $this->version;
108109
}
109110

111+
/**
112+
* Retrieve host.
113+
*
114+
* @return string
115+
* @throws ServiceException
116+
*/
117+
public function getHost(): string
118+
{
119+
if (!$this->isInstalled()) {
120+
throw new ServiceException('ES service is not installed');
121+
}
122+
123+
return (string)$this->getConfiguration()['host'];
124+
}
125+
126+
/**
127+
* Retrieve port.
128+
*
129+
* @return string
130+
* @throws ServiceException
131+
*/
132+
public function getPort(): string
133+
{
134+
if (!$this->isInstalled()) {
135+
throw new ServiceException('ES service is not installed');
136+
}
137+
138+
return (string)$this->getConfiguration()['port'];
139+
}
140+
141+
/**
142+
* Return full version with engine name.
143+
*
144+
* @return string
145+
* @throws ServiceException
146+
*/
147+
public function getFullVersion(): string
148+
{
149+
$version = $this->getVersion();
150+
151+
if (Semver::satisfies($version, '>= 5')) {
152+
return self::ENGINE_NAME . (int)$version;
153+
}
154+
155+
return self::ENGINE_NAME;
156+
}
157+
110158
/**
111159
* Retrieves default template configuration.
112160
* May contain configuration for replicas and shards.

src/Service/ServiceException.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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\Service;
9+
10+
use Magento\MagentoCloud\App\GenericException;
11+
12+
/**
13+
* Generic service exception.
14+
*/
15+
class ServiceException extends GenericException
16+
{
17+
}

0 commit comments

Comments
 (0)