Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
enh(update): check platform requirements on updates API call (#11361)
Browse files Browse the repository at this point in the history
Refs: MON-12296
  • Loading branch information
kduret authored Jul 15, 2022
1 parent 35b53b1 commit 6bc3722
Show file tree
Hide file tree
Showing 18 changed files with 751 additions and 4 deletions.
21 changes: 21 additions & 0 deletions config/packages/Centreon.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,23 @@ services:
class: Core\Infrastructure\Platform\Repository\FileReadPlatformRepository
arguments: ['%centreon_etc_path%', '%centreon_install_path%']

Core\Platform\Application\Validator\RequirementValidatorsInterface:
class: Core\Platform\Infrastructure\Validator\RequirementValidators
arguments:
$requirementValidators: !tagged_iterator 'platform.requirement.validators'

Core\Platform\Infrastructure\Validator\RequirementValidators\DatabaseRequirementValidator:
arguments:
$dbRequirementValidators: !tagged_iterator 'platform.requirement.database.validators'

Core\Platform\Infrastructure\Validator\RequirementValidators\PhpRequirementValidator:
arguments:
$requiredPhpVersion: '%required_php_version%'

Core\Platform\Infrastructure\Validator\RequirementValidators\DatabaseRequirementValidators\MariaDbRequirementValidator:
arguments:
$requiredMariaDbMinVersion: '%required_mariadb_min_version%'

Core\Platform\Application\Repository\ReadVersionRepositoryInterface:
class: Core\Platform\Infrastructure\Repository\DbReadVersionRepository
public: true
Expand Down Expand Up @@ -254,6 +271,10 @@ services:
tags: ['authentication.provider.responses']
Core\Security\Infrastructure\Api\FindProviderConfigurations\ProviderPresenter\ProviderPresenterInterface:
tags: ['authentication.provider.presenters']
Core\Platform\Application\Validator\RequirementValidatorInterface:
tags: ['platform.requirement.validators']
Core\Platform\Infrastructure\Validator\RequirementValidators\DatabaseRequirementValidatorInterface:
tags: ['platform.requirement.database.validators']

Centreon\Domain\Monitoring\Interfaces\ResourceRepositoryInterface:
factory: ['@Centreon\Infrastructure\Monitoring\Resource\ResourceRepositoryFactory', 'createResourceRepository']
Expand Down
2 changes: 2 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ parameters:
media_path: "img/media"
redirect_default_page: "/monitoring/resources"
session_expiration_delay: 120
required_php_version: "%env(_CENTREON_PHP_VERSION_)%"
required_mariadb_min_version: "%env(_CENTREON_MARIA_DB_MIN_VERSION_)%"

services:
# Default configuration for services in *this* file
Expand Down
15 changes: 15 additions & 0 deletions lang/fr_FR.UTF-8/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -16943,3 +16943,18 @@ msgstr "Une erreur s'est produite lors de l'application des actions postérieure

msgid "Updates not found"
msgstr "Les mises à jour n'ont pas été trouvées"

msgid "PHP version %s required (%s installed)"
msgstr "La version %s de PHP est requise (%s installée)"

msgid "PHP extension %s not loaded"
msgstr "L'extension %s de PHP n'est pas chargée"

msgid "Error when getting database version"
msgstr "Erreur lors de la récupération de la version de la base de données"

msgid "Cannot retrieve database version information"
msgstr "Les informations de version de la base de données n'ont pas pu être trouvées"

msgid "MariaDB version %s required (%s installed)"
msgstr "La version %s de MariaDB est requise (%s installée)"
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
namespace Core\Platform\Application\UseCase\UpdateVersions;

use Centreon\Domain\Log\LoggerTrait;
use Core\Platform\Application\Validator\RequirementValidatorsInterface;
use Core\Platform\Application\Repository\UpdateLockerRepositoryInterface;
use Core\Platform\Application\Repository\ReadVersionRepositoryInterface;
use Core\Platform\Application\Repository\ReadUpdateRepositoryInterface;
Expand All @@ -37,12 +38,14 @@ class UpdateVersions
use LoggerTrait;

/**
* @param RequirementValidatorsInterface $requirementValidators
* @param UpdateLockerRepositoryInterface $updateLocker
* @param ReadVersionRepositoryInterface $readVersionRepository
* @param ReadUpdateRepositoryInterface $readUpdateRepository
* @param WriteUpdateRepositoryInterface $writeUpdateRepository
*/
public function __construct(
private RequirementValidatorsInterface $requirementValidators,
private UpdateLockerRepositoryInterface $updateLocker,
private ReadVersionRepositoryInterface $readVersionRepository,
private ReadUpdateRepositoryInterface $readUpdateRepository,
Expand All @@ -59,6 +62,8 @@ public function __invoke(
$this->info('Updating versions');

try {
$this->validateRequirementsOrFail();

$this->lockUpdate();

$currentVersion = $this->getCurrentVersionOrFail();
Expand Down Expand Up @@ -93,6 +98,18 @@ public function __invoke(
$presenter->setResponseStatus(new NoContentResponse());
}

/**
* Validate platform requirements or fail
*
* @throws \Exception
*/
private function validateRequirementsOrFail(): void
{
$this->info('Validating platform requirements');

$this->requirementValidators->validateRequirementsOrFail();
}

/**
* Lock update process
*/
Expand Down
27 changes: 27 additions & 0 deletions src/Core/Platform/Application/Validator/RequirementException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* Copyright 2005 - 2022 Centreon (https://www.centreon.com/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information : contact@centreon.com
*
*/
declare(strict_types=1);

namespace Core\Platform\Application\Validator;

class RequirementException extends \Exception
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* Copyright 2005 - 2022 Centreon (https://www.centreon.com/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information : contact@centreon.com
*
*/
declare(strict_types=1);

namespace Core\Platform\Application\Validator;

interface RequirementValidatorInterface
{
/**
* Validate requirement or fail
*
* @throws RequirementException
*/
public function validateRequirementOrFail(): void;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* Copyright 2005 - 2022 Centreon (https://www.centreon.com/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information : contact@centreon.com
*
*/
declare(strict_types=1);

namespace Core\Platform\Application\Validator;

interface RequirementValidatorsInterface
{
/**
* Validate platform requirements or fail
*
* @throws RequirementException
*/
public function validateRequirementsOrFail(): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
use Centreon\Domain\Contact\Contact;
use Core\Platform\Application\UseCase\UpdateVersions\{
UpdateVersions,
UpdateVersionsRequest,
UpdateVersionsPresenterInterface
};
use Core\Application\Common\UseCase\UnauthorizedResponse;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/*
* Copyright 2005 - 2022 Centreon (https://www.centreon.com/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information : contact@centreon.com
*
*/

declare(strict_types=1);

namespace Core\Platform\Infrastructure\Validator;

use Centreon\Domain\Log\LoggerTrait;
use Core\Platform\Application\Validator\RequirementValidatorsInterface;
use Core\Platform\Application\Validator\RequirementValidatorInterface;

class RequirementValidators implements RequirementValidatorsInterface
{
use LoggerTrait;

/**
* @var RequirementValidatorInterface[]
*/
private $requirementValidators;

/**
* @param \Traversable<RequirementValidatorInterface> $requirementValidators
*
* @throws \Exception
*/
public function __construct(
\Traversable $requirementValidators,
) {
if (iterator_count($requirementValidators) === 0) {
throw new \Exception('Requirement validators not found');
}
$this->requirementValidators = iterator_to_array($requirementValidators);
}

/**
* @inheritDoc
*/
public function validateRequirementsOrFail(): void
{
foreach ($this->requirementValidators as $requirementValidator) {
$this->info('Validating platform requirement with ' . $requirementValidator::class);
$requirementValidator->validateRequirementOrFail();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* Copyright 2005 - 2022 Centreon (https://www.centreon.com/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information : contact@centreon.com
*
*/
declare(strict_types=1);

namespace Core\Platform\Infrastructure\Validator\RequirementValidators;

use Core\Platform\Application\Validator\RequirementException;

class DatabaseRequirementException extends RequirementException
{
/**
* @param \Throwable $e
* @return self
*/
public static function errorWhenGettingDatabaseVersion(\Throwable $e): self
{
return new self(
_('Error when getting database version'),
0,
$e,
);
}

/**
* @return self
*/
public static function cannotRetrieveVersionInformation(): self
{
return new self(_('Cannot retrieve database version information'));
}
}
Loading

0 comments on commit 6bc3722

Please sign in to comment.