Skip to content

Commit c8cfd42

Browse files
authored
MCLOUD-7698: Add compatibility with Redis session connection (#43)
1 parent 89cdaee commit c8cfd42

File tree

21 files changed

+556
-322
lines changed

21 files changed

+556
-322
lines changed

.travis.yml

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

src/Config/Validator/Deploy/ServiceVersion.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public function validate(): Validator\ResultInterface
8080
$services = [
8181
ServiceInterface::NAME_RABBITMQ,
8282
ServiceInterface::NAME_REDIS,
83+
ServiceInterface::NAME_REDIS_SESSION,
8384
ServiceInterface::NAME_ELASTICSEARCH,
8485
$this->databaseType->getServiceName()
8586
];

src/Service/EolValidator.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public function validateServiceEol(): array
8787
ServiceInterface::NAME_ELASTICSEARCH,
8888
ServiceInterface::NAME_RABBITMQ,
8989
ServiceInterface::NAME_REDIS,
90+
ServiceInterface::NAME_REDIS_SESSION,
9091
$this->databaseType->getServiceName()
9192
];
9293

@@ -170,14 +171,24 @@ private function getServiceConfigs(string $serviceName) : array
170171

171172
/**
172173
* Perform service name conversions.
173-
* Explicitly resetting 'mysql' to 'mariadb' for MariaDB validation; getting the version from
174-
* relationship returns mysql:<version>.
174+
* Explicitly resetting 'mysql' to 'mariadb' for MariaDB validation
175+
* and 'redis-session' to 'redis' for Redis validation; getting the version
176+
* from relationship returns mysql:<version>.
175177
*
176178
* @param string $serviceName
177179
* @return string
178180
*/
179181
private function getConvertedServiceName(string $serviceName) : string
180182
{
181-
return $serviceName == 'mysql' ? 'mariadb' : $serviceName;
183+
switch ($serviceName) {
184+
case ServiceInterface::NAME_DB_MYSQL:
185+
$serviceName = ServiceInterface::NAME_DB_MARIA;
186+
break;
187+
case ServiceInterface::NAME_REDIS_SESSION:
188+
$serviceName = ServiceInterface::NAME_REDIS;
189+
break;
190+
}
191+
192+
return $serviceName;
182193
}
183194
}

src/Service/Redis.php

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
namespace Magento\MagentoCloud\Service;
99

1010
use Magento\MagentoCloud\Config\Environment;
11-
use Magento\MagentoCloud\Shell\ShellException;
12-
use Magento\MagentoCloud\Shell\ShellInterface;
11+
use Magento\MagentoCloud\Service\Redis\Version;
1312

1413
/**
1514
* Returns Redis service configurations.
@@ -25,9 +24,9 @@ class Redis implements ServiceInterface
2524
private $environment;
2625

2726
/**
28-
* @var ShellInterface
27+
* @var Version
2928
*/
30-
private $shell;
29+
private $versionRetriever;
3130

3231
/**
3332
* @var string
@@ -36,18 +35,18 @@ class Redis implements ServiceInterface
3635

3736
/**
3837
* @param Environment $environment
39-
* @param ShellInterface $shell
38+
* @param Version $versionRetriever
4039
*/
4140
public function __construct(
4241
Environment $environment,
43-
ShellInterface $shell
42+
Version $versionRetriever
4443
) {
4544
$this->environment = $environment;
46-
$this->shell = $shell;
45+
$this->versionRetriever = $versionRetriever;
4746
}
4847

4948
/**
50-
* @inheritdoc
49+
* @inheritDoc
5150
*/
5251
public function getConfiguration(): array
5352
{
@@ -65,36 +64,12 @@ public function getSlaveConfiguration(): array
6564
}
6665

6766
/**
68-
* Retrieves Redis service version whether from relationship configuration
69-
* or using CLI command (for PRO environments)
70-
*
71-
* {@inheritDoc}
67+
* @inheritDoc
7268
*/
7369
public function getVersion(): string
7470
{
7571
if ($this->version === null) {
76-
$this->version = '0';
77-
$redisConfig = $this->getConfiguration();
78-
79-
//on integration environments
80-
if (isset($redisConfig['type']) && strpos($redisConfig['type'], ':') !== false) {
81-
$this->version = explode(':', $redisConfig['type'])[1];
82-
} elseif (isset($redisConfig['host']) && isset($redisConfig['port'])) {
83-
//on dedicated environments
84-
try {
85-
$process = $this->shell->execute(
86-
sprintf(
87-
'redis-cli -p %s -h %s info | grep redis_version',
88-
$redisConfig['port'],
89-
$redisConfig['host']
90-
)
91-
);
92-
preg_match('/^(?:redis_version:)(\d+\.\d+)/', $process->getOutput(), $matches);
93-
$this->version = $matches[1] ?? '0';
94-
} catch (ShellException $exception) {
95-
throw new ServiceException($exception->getMessage());
96-
}
97-
}
72+
$this->version = $this->versionRetriever->getVersion($this->getConfiguration());
9873
}
9974

10075
return $this->version;

src/Service/Redis/Version.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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\Redis;
9+
10+
use Magento\MagentoCloud\Shell\ShellInterface;
11+
use Magento\MagentoCloud\Service\ServiceException;
12+
use Magento\MagentoCloud\Shell\ShellException;
13+
14+
/**
15+
* Returns Redis version
16+
*/
17+
class Version
18+
{
19+
/**
20+
* @var ShellInterface
21+
*/
22+
private $shell;
23+
24+
/**
25+
* @param ShellInterface $shell
26+
*/
27+
public function __construct(ShellInterface $shell)
28+
{
29+
$this->shell = $shell;
30+
}
31+
32+
/**
33+
* Retrieves Redis service version whether from relationship configuration
34+
* or using CLI command (for PRO environments)
35+
*
36+
* @param array $redisConfig
37+
* @return string
38+
* @throws ServiceException
39+
*/
40+
public function getVersion(array $redisConfig): string
41+
{
42+
$version = '0';
43+
44+
//on integration environments
45+
if (isset($redisConfig['type']) && strpos($redisConfig['type'], ':') !== false) {
46+
$version = explode(':', $redisConfig['type'])[1];
47+
} elseif (isset($redisConfig['host']) && isset($redisConfig['port'])) {
48+
//on dedicated environments
49+
try {
50+
$process = $this->shell->execute(
51+
sprintf(
52+
'redis-cli -p %s -h %s info | grep redis_version',
53+
$redisConfig['port'],
54+
$redisConfig['host']
55+
)
56+
);
57+
preg_match('/^(?:redis_version:)(\d+\.\d+)/', $process->getOutput(), $matches);
58+
$version = $matches[1] ?? '0';
59+
} catch (ShellException $exception) {
60+
throw new ServiceException($exception->getMessage());
61+
}
62+
}
63+
64+
return $version;
65+
}
66+
}

src/Service/RedisSession.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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\Config\Environment;
11+
use Magento\MagentoCloud\Service\Redis\Version;
12+
13+
/**
14+
* Returns Redis service configurations for sessions.
15+
*/
16+
class RedisSession implements ServiceInterface
17+
{
18+
const RELATIONSHIP_SESSION_KEY = 'redis-session';
19+
20+
/**
21+
* @var Environment
22+
*/
23+
private $environment;
24+
25+
/**
26+
* @var Version
27+
*/
28+
private $versionRetriever;
29+
30+
/**
31+
* @var string
32+
*/
33+
private $version;
34+
35+
/**
36+
* @param Environment $environment
37+
* @param Version $versionRetriever
38+
*/
39+
public function __construct(
40+
Environment $environment,
41+
Version $versionRetriever
42+
) {
43+
$this->environment = $environment;
44+
$this->versionRetriever = $versionRetriever;
45+
}
46+
47+
/**
48+
* @inheritDoc
49+
*/
50+
public function getConfiguration(): array
51+
{
52+
return $this->environment->getRelationship(self::RELATIONSHIP_SESSION_KEY)[0] ?? [];
53+
}
54+
55+
/**
56+
* @inheritDoc
57+
*/
58+
public function getVersion(): string
59+
{
60+
if ($this->version === null) {
61+
$this->version = $this->versionRetriever->getVersion($this->getConfiguration());
62+
}
63+
64+
return $this->version;
65+
}
66+
}

src/Service/ServiceFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class ServiceFactory
1919
*/
2020
private $serviceMap = [
2121
ServiceInterface::NAME_REDIS => Redis::class,
22+
ServiceInterface::NAME_REDIS_SESSION => RedisSession::class,
2223
ServiceInterface::NAME_ELASTICSEARCH => ElasticSearch::class,
2324
ServiceInterface::NAME_RABBITMQ => RabbitMq::class,
2425
ServiceInterface::NAME_DB_MYSQL => Database::class,

src/Service/ServiceInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ interface ServiceInterface
1818
const NAME_DB_AURORA = 'aurora';
1919
const NAME_NGINX = 'nginx';
2020
const NAME_REDIS = 'redis';
21+
const NAME_REDIS_SESSION = 'redis-session';
2122
const NAME_ELASTICSEARCH = 'elasticsearch';
2223
const NAME_RABBITMQ = 'rabbitmq';
2324
const NAME_NODE = 'node';

src/Service/Validator.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
*/
1717
class Validator
1818
{
19+
/**
20+
* Supported version constraints of Redis services
21+
*/
22+
private const REDIS_SUPPORT_VERSIONS = [
23+
'*' => '~3.2.0 || ~4.0.0 || ~5.0.0 || ~6.0.0',
24+
];
25+
1926
/**
2027
* Supported version constraints of services for every Magento version.
2128
* Magento version constraint is a key in every element of service array
@@ -52,9 +59,8 @@ class Validator
5259
'>=2.2.0 <2.3.3' => '^4.0 || ^5.0',
5360
'>=2.3.3' => '^4.0 || ^5.0 || ^6.2',
5461
],
55-
ServiceInterface::NAME_REDIS => [
56-
'*' => '~3.2.0 || ~4.0.0 || ~5.0.0 || ~6.0.0',
57-
],
62+
ServiceInterface::NAME_REDIS => self::REDIS_SUPPORT_VERSIONS,
63+
ServiceInterface::NAME_REDIS_SESSION => self::REDIS_SUPPORT_VERSIONS,
5864
ServiceInterface::NAME_ELASTICSEARCH => [
5965
'<2.2.0' => '~1.7.0 || ~2.4.0',
6066
'>=2.2.0 <2.2.8 || 2.3.0' => '~1.7.0 || ~2.4.0 || ~5.2.0',

0 commit comments

Comments
 (0)