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

feat(api): endpoint for centreon web upgrade #11307

Merged
merged 28 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions config/packages/Centreon.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,18 @@ services:
class: Core\Infrastructure\Platform\Repository\FileReadPlatformRepository
arguments: ['%centreon_etc_path%', '%centreon_install_path%']

Core\Platform\Application\Repository\ReadVersionRepositoryInterface:
class: Core\Platform\Infrastructure\Repository\DbReadVersionRepository
public: true

Core\Platform\Application\Repository\ReadUpdateRepositoryInterface:
class: Core\Platform\Infrastructure\Repository\FsReadUpdateRepository
public: true

Core\Platform\Application\Repository\WriteUpdateRepositoryInterface:
class: Core\Platform\Infrastructure\Repository\DbWriteUpdateRepository
public: true

# Monitoring resources
_instanceof:
Centreon\Infrastructure\Monitoring\Resource\Provider\ProviderInterface:
Expand Down
6 changes: 6 additions & 0 deletions config/routes/Centreon/platform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ centreon_application_platform_getversion:
controller: 'Centreon\Application\Controller\PlatformController::getVersions'
condition: "request.attributes.get('version') >= 21.10"

centreon_application_platform_updateversions:
methods: PATCH
path: /platform/updates
controller: 'Core\Platform\Infrastructure\Api\UpdateVersions\UpdateVersionsController'
condition: "request.attributes.get('version') >= 22.04"

centreon_application_platformtopology_addplatformtotopology:
methods: POST
path: /platform/topology
Expand Down
3 changes: 3 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ services:
decorates: router
arguments: ['@.inner']

Symfony\Component\Finder\Finder:
shared: false


# Security
Security\Domain\Authentication\Interfaces\AuthenticationRepositoryInterface:
Expand Down
10 changes: 10 additions & 0 deletions src/Centreon/Infrastructure/DatabaseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,14 @@ public function setStorageDbName(string $storageDbName)
{
$this->storageDbName = $storageDbName;
}

/**
* switch connection to another database
*
* @param string $dbName
*/
public function switchToDb(string $dbName): void
{
$this->query('use ' . $dbName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ class AbstractRepositoryDRB
protected function translateDbName(string $request): string
{
return str_replace(
array(':dbstg', ':db'),
array($this->db->getStorageDbName(), $this->db->getCentreonDbName()),
[':dbstg', ':db'],
[$this->db->getStorageDbName(), $this->db->getCentreonDbName()],
$request
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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\Repository;

interface ReadUpdateRepositoryInterface
{
/**
* Get ordered available updates
*
* @param string $currentVersion
* @return string[]
*/
public function getOrderedAvailableUpdates(string $currentVersion): array;
}
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\Repository;

interface ReadVersionRepositoryInterface
{
/**
* Get current version
*
* @return string|null
*/
public function getCurrentVersion(): ?string;
}
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\Repository;

interface WriteUpdateRepositoryInterface
{
/**
* Run update according to given version
*
* @param string $version
*/
public function runUpdate(string $version): void;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?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\UseCase\UpdateVersions;

use Centreon\Domain\Log\LoggerTrait;
use Core\Platform\Application\Repository\ReadVersionRepositoryInterface;
use Core\Platform\Application\Repository\ReadUpdateRepositoryInterface;
use Core\Platform\Application\Repository\WriteUpdateRepositoryInterface;
use Core\Application\Common\UseCase\ErrorResponse;
use Core\Application\Common\UseCase\NoContentResponse;

class UpdateVersions
{
use LoggerTrait;

/**
* @param ReadVersionRepositoryInterface $readVersionRepository
* @param ReadUpdateRepositoryInterface $readUpdateRepository
* @param WriteUpdateRepositoryInterface $writeUpdateRepository
*/
public function __construct(
private ReadVersionRepositoryInterface $readVersionRepository,
private ReadUpdateRepositoryInterface $readUpdateRepository,
private WriteUpdateRepositoryInterface $writeUpdateRepository,
) {
}

/**
* @param UpdateVersionsPresenterInterface $presenter
*/
public function __invoke(
UpdateVersionsPresenterInterface $presenter,
): void {
$this->info('Updating versions');

try {
$currentVersion = $this->getCurrentVersion();

$availableUpdates = $this->getAvailableUpdates($currentVersion);

$this->runUpdates($availableUpdates);
} catch (\Throwable $e) {
$this->error(
$e->getMessage(),
['trace' => $e->getTraceAsString()],
);

$presenter->setResponseStatus(new ErrorResponse($e->getMessage()));
jeremyjaouen marked this conversation as resolved.
Show resolved Hide resolved

kduret marked this conversation as resolved.
Show resolved Hide resolved
return;
}

$presenter->setResponseStatus(new NoContentResponse());
}

/**
* Get current version or fail
*
* @return string
*
* @throws \Exception
*/
private function getCurrentVersion(): string
kduret marked this conversation as resolved.
Show resolved Hide resolved
{
$this->info('Getting current version');

try {
$currentVersion = $this->readVersionRepository->getCurrentVersion();
kduret marked this conversation as resolved.
Show resolved Hide resolved
} catch (\Exception $e) {
throw new \Exception('An error occurred when retrieving current version', 0, $e);
}

if ($currentVersion === null) {
throw new \Exception('Cannot retrieve current version');
}

return $currentVersion;
}

/**
* Get available updates
*
* @param string $currentVersion
* @return string[]
*/
jeremyjaouen marked this conversation as resolved.
Show resolved Hide resolved
private function getAvailableUpdates(string $currentVersion): array
kduret marked this conversation as resolved.
Show resolved Hide resolved
{
try {
$this->info('Getting available updates');

return $this->readUpdateRepository->getOrderedAvailableUpdates($currentVersion);
} catch (\Throwable $e) {
throw new \Exception('An error occurred when getting available updates', 0, $e);
}
}

/**
* Run given updates
*
* @param string[] $updates
*
* @throws \Throwable
*/
private function runUpdates(array $updates): void
{
foreach ($updates as $update) {
try {
$this->info("Running update $update");
$this->writeUpdateRepository->runUpdate($update);
} catch (\Throwable $e) {
throw new \Exception('An error occurred when applying update: ' . $update, 0, $e);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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\UseCase\UpdateVersions;

use Core\Application\Common\UseCase\PresenterInterface;

interface UpdateVersionsPresenterInterface extends PresenterInterface
{
}
Loading