Skip to content

Commit 97d4a5f

Browse files
MCLOUD-5706: Add support of new ES option to ECE-Tools
1 parent 10da1fb commit 97d4a5f

File tree

7 files changed

+227
-81
lines changed

7 files changed

+227
-81
lines changed

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" />

src/Config/SearchEngine.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,7 @@ private function getSolrConfiguration(array $config): array
179179
*/
180180
private function getElasticSearchConfiguration(array $config): array
181181
{
182-
$engine = ElasticSearch::ENGINE_NAME;
183-
184-
$esVersion = $this->elasticSearch->getVersion();
185-
if (Semver::satisfies($esVersion, '>= 5')) {
186-
$engine .= (int)$esVersion;
187-
}
182+
$engine = $this->elasticSearch->getFullVersion();
188183

189184
$elasticSearchConfig = [
190185
'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
*

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+
throw new ServiceException('ES service is not installed');
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)