Skip to content

Commit

Permalink
MCLOUD-7098: Validation which compare service versions and magento ve…
Browse files Browse the repository at this point in the history
…rsion doesn't work on PRO (#6)
  • Loading branch information
NadiyaS authored Oct 29, 2020
1 parent eaea526 commit 3806fe0
Show file tree
Hide file tree
Showing 14 changed files with 698 additions and 268 deletions.
8 changes: 3 additions & 5 deletions src/Config/Validator/Deploy/ServiceEol.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
namespace Magento\MagentoCloud\Config\Validator\Deploy;

use Magento\MagentoCloud\App\Error;
use Magento\MagentoCloud\App\GenericException;
use Magento\MagentoCloud\Config\Validator;
use Magento\MagentoCloud\Filesystem\FileSystemException;
use Magento\MagentoCloud\Service\EolValidator as EOLValidator;
use Magento\MagentoCloud\Config\ValidatorInterface;
use Magento\MagentoCloud\Service\ServiceMismatchException;

/**
* Class to check if services approaching their EOLs.
Expand Down Expand Up @@ -52,8 +51,7 @@ public function __construct(
/**
* Get the defined services and versions and check for their EOLs by error level.
*
* @return Validator\ResultInterface
* @throws FileSystemException
* {@inheritDoc}
*/
public function validate(): Validator\ResultInterface
{
Expand All @@ -70,7 +68,7 @@ public function validate(): Validator\ResultInterface
$this->errorLevel == ValidatorInterface::LEVEL_WARNING ? Error::WARN_SERVICE_PASSED_EOL : null
);
}
} catch (ServiceMismatchException $e) {
} catch (GenericException $e) {
return $this->resultFactory->error('Can\'t validate version of some services: ' . $e->getMessage());
}

Expand Down
15 changes: 14 additions & 1 deletion src/Config/Validator/Deploy/ServiceVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Magento\MagentoCloud\Service\Validator as ServiceVersionValidator;
use Magento\MagentoCloud\Config\Validator;
use Magento\MagentoCloud\Config\ValidatorInterface;
use Psr\Log\LoggerInterface;

/**
* Validates installed service versions according to version mapping.
Expand All @@ -36,19 +37,27 @@ class ServiceVersion implements ValidatorInterface
*/
private $serviceFactory;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @param Validator\ResultFactory $resultFactory
* @param ServiceVersionValidator $serviceVersionValidator
* @param ServiceFactory $serviceFactory
* @param LoggerInterface $logger
*/
public function __construct(
Validator\ResultFactory $resultFactory,
ServiceVersionValidator $serviceVersionValidator,
ServiceFactory $serviceFactory
ServiceFactory $serviceFactory,
LoggerInterface $logger
) {
$this->resultFactory = $resultFactory;
$this->serviceVersionValidator = $serviceVersionValidator;
$this->serviceFactory = $serviceFactory;
$this->logger = $logger;
}

/**
Expand All @@ -69,6 +78,10 @@ public function validate(): Validator\ResultInterface
foreach ($services as $serviceName) {
$service = $this->serviceFactory->create($serviceName);
$serviceVersion = $service->getVersion();

$logMsq = $serviceVersion ? 'is ' . $serviceVersion : 'is not detected';
$this->logger->info(sprintf('Version of service \'%s\' %s', $serviceName, $logMsq));

if ($serviceVersion !== '0' &&
$error = $this->serviceVersionValidator->validateService($serviceName, $serviceVersion)
) {
Expand Down
93 changes: 93 additions & 0 deletions src/DB/Data/ConnectionTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MagentoCloud\DB\Data;

use Magento\MagentoCloud\Config\Environment;

/**
* Returns database service configurations.
*/
class ConnectionTypes
{
const RELATIONSHIP_KEY = 'database';
const RELATIONSHIP_SLAVE_KEY = 'database-slave';

const RELATIONSHIP_QUOTE_KEY = 'database-quote';
const RELATIONSHIP_QUOTE_SLAVE_KEY = 'database-quote-slave';

const RELATIONSHIP_SALES_KEY = 'database-sales';
const RELATIONSHIP_SALES_SLAVE_KEY = 'database-sales-slave';

/**
* @var Environment
*/
private $environment;

/**
* @param Environment $environment
*/
public function __construct(
Environment $environment
) {
$this->environment = $environment;
}

/**
* @inheritdoc
*/
public function getConfiguration(): array
{
return $this->environment->getRelationship(self::RELATIONSHIP_KEY)[0] ?? [];
}

/**
* Returns service configuration for slave.
*
* @return array
*/
public function getSlaveConfiguration(): array
{
return $this->environment->getRelationship(self::RELATIONSHIP_SLAVE_KEY)[0] ?? [];
}

/**
* Returns configuration for quote service.
*/
public function getQuoteConfiguration(): array
{
return $this->environment->getRelationship(self::RELATIONSHIP_QUOTE_KEY)[0] ?? [];
}

/**
* Returns configuration for quote slave service.
*
* @return array
*/
public function getQuoteSlaveConfiguration(): array
{
return $this->environment->getRelationship(self::RELATIONSHIP_QUOTE_SLAVE_KEY)[0] ?? [];
}

/**
* Returns configuration for sales service.
*/
public function getSalesConfiguration(): array
{
return $this->environment->getRelationship(self::RELATIONSHIP_SALES_KEY)[0] ?? [];
}

/**
* Returns configuration for slave sales service.
*
* @return array
*/
public function getSalesSlaveConfiguration(): array
{
return $this->environment->getRelationship(self::RELATIONSHIP_SALES_SLAVE_KEY)[0] ?? [];
}
}
24 changes: 11 additions & 13 deletions src/DB/Data/RelationshipConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

namespace Magento\MagentoCloud\DB\Data;

use Magento\MagentoCloud\Service\Database;

/**
* Responsible for creating and configuring Magento\MagentoCloud\DB\Data\ConnectionInterface instances.
*/
Expand All @@ -24,16 +22,16 @@ class RelationshipConnectionFactory
const CONNECTION_SALES_SLAVE = 'sales-slave';

/**
* @var Database
* @var ConnectionTypes
*/
private $database;
private $connectionType;

/**
* @param Database $database
* @param ConnectionTypes $connectionType
*/
public function __construct(Database $database)
public function __construct(ConnectionTypes $connectionType)
{
$this->database = $database;
$this->connectionType = $connectionType;
}

/**
Expand All @@ -47,22 +45,22 @@ public function create(string $connectionType): ConnectionInterface
{
switch ($connectionType) {
case self::CONNECTION_MAIN:
$configuration = $this->database->getConfiguration();
$configuration = $this->connectionType->getConfiguration();
break;
case self::CONNECTION_SLAVE:
$configuration = $this->database->getSlaveConfiguration();
$configuration = $this->connectionType->getSlaveConfiguration();
break;
case self::CONNECTION_QUOTE_MAIN:
$configuration = $this->database->getQuoteConfiguration();
$configuration = $this->connectionType->getQuoteConfiguration();
break;
case self::CONNECTION_QUOTE_SLAVE:
$configuration = $this->database->getQuoteSlaveConfiguration();
$configuration = $this->connectionType->getQuoteSlaveConfiguration();
break;
case self::CONNECTION_SALES_MAIN:
$configuration = $this->database->getSalesConfiguration();
$configuration = $this->connectionType->getSalesConfiguration();
break;
case self::CONNECTION_SALES_SLAVE:
$configuration = $this->database->getSalesSlaveConfiguration();
$configuration = $this->connectionType->getSalesSlaveConfiguration();
break;
default:
throw new \RuntimeException(
Expand Down
101 changes: 33 additions & 68 deletions src/Service/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,108 +7,73 @@

namespace Magento\MagentoCloud\Service;

use Magento\MagentoCloud\Config\Environment;
use Magento\MagentoCloud\DB\ConnectionInterface;
use Magento\MagentoCloud\DB\Data\ConnectionTypes;

/**
* Returns database service configurations.
* Returns main database service configurations.
*/
class Database implements ServiceInterface
{
const RELATIONSHIP_KEY = 'database';
const RELATIONSHIP_SLAVE_KEY = 'database-slave';

const RELATIONSHIP_QUOTE_KEY = 'database-quote';
const RELATIONSHIP_QUOTE_SLAVE_KEY = 'database-quote-slave';

const RELATIONSHIP_SALES_KEY = 'database-sales';
const RELATIONSHIP_SALES_SLAVE_KEY = 'database-sales-slave';
/**
* @var ConnectionTypes
*/
private $connectionType;

/**
* @var Environment
* @var ConnectionInterface
*/
private $environment;
private $connection;

/**
* @var string
*/
private $version;

/**
* @param Environment $environment
* @param ConnectionTypes $connectionType
* @param ConnectionInterface $connection
*/
public function __construct(Environment $environment)
{
$this->environment = $environment;
public function __construct(
ConnectionTypes $connectionType,
ConnectionInterface $connection
) {
$this->connectionType = $connectionType;
$this->connection = $connection;
}

/**
* @inheritdoc
*/
public function getConfiguration(): array
{
return $this->environment->getRelationship(self::RELATIONSHIP_KEY)[0] ?? [];
}

/**
* Returns service configuration for slave.
*
* @return array
*/
public function getSlaveConfiguration(): array
{
return $this->environment->getRelationship(self::RELATIONSHIP_SLAVE_KEY)[0] ?? [];
}

/**
* Returns configuration for quote service.
*/
public function getQuoteConfiguration(): array
{
return $this->environment->getRelationship(self::RELATIONSHIP_QUOTE_KEY)[0] ?? [];
}

/**
* Returns configuration for quote slave service.
*
* @return array
*/
public function getQuoteSlaveConfiguration(): array
{
return $this->environment->getRelationship(self::RELATIONSHIP_QUOTE_SLAVE_KEY)[0] ?? [];
return $this->connectionType->getConfiguration();
}

/**
* Returns configuration for sales service.
*/
public function getSalesConfiguration(): array
{
return $this->environment->getRelationship(self::RELATIONSHIP_SALES_KEY)[0] ?? [];
}

/**
* Returns configuration for slave sales service.
*
* @return array
*/
public function getSalesSlaveConfiguration(): array
{
return $this->environment->getRelationship(self::RELATIONSHIP_SALES_SLAVE_KEY)[0] ?? [];
}

/**
* Returns version of the service.
* Retrieves MySQL service version whether from relationship configuration
* or using SQL query (for PRO environments)
*
* @return string
* {@inheritDoc}
*/
public function getVersion(): string
{
if ($this->version === null) {
$this->version = '0';

$databaseConfig = $this->getConfiguration();
try {
$databaseConfig = $this->getConfiguration();

if (isset($databaseConfig['type']) && strpos($databaseConfig['type'], ':') !== false) {
$this->version = explode(':', $databaseConfig['type'])[1];
} elseif (!empty($databaseConfig['host'])) {
$rawVersion = $this->connection->selectOne('SELECT VERSION() as version');
preg_match('/^\d+\.\d+/', $rawVersion['version'] ?? '', $matches);

if (isset($databaseConfig['type']) && strpos($databaseConfig['type'], ':') !== false) {
$this->version = explode(':', $databaseConfig['type'])[1];
$this->version = $matches[0] ?? '0';
}
} catch (\Exception $e) {
throw new ServiceException($e->getMessage());
}
}

Expand Down
Loading

0 comments on commit 3806fe0

Please sign in to comment.