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

enh(api): remove install directory after update #11345

Merged
merged 52 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
e87de28
add skeleton
kduret Jun 28, 2022
ef13a22
split interfaces
kduret Jun 28, 2022
ce0ea79
implement use case
kduret Jun 29, 2022
164618d
move into repository
kduret Jun 29, 2022
2fe87c6
read updates
kduret Jun 29, 2022
38c013a
apply php upgrades
kduret Jun 29, 2022
9750123
manage sql upgrades
kduret Jun 29, 2022
a21a543
check user is admin
kduret Jun 30, 2022
c63153d
phpdoc
kduret Jun 30, 2022
bfd16a7
refacto
kduret Jul 1, 2022
7255ca9
use symfony finder
kduret Jul 1, 2022
12bf4be
legacy use new repository
kduret Jul 1, 2022
66f63a0
continue
kduret Jul 1, 2022
31a7a22
remove request
kduret Jul 1, 2022
13d168f
add unit tests
kduret Jul 1, 2022
7bdd56a
Merge branch 'develop' into MON-12296-updates
kduret Jul 1, 2022
09ea038
locking update
kduret Jul 4, 2022
a725e8c
add phpdoc
kduret Jul 4, 2022
728bc8c
improve error message
kduret Jul 4, 2022
e70406a
take into accounts tamaz feedbacks
kduret Jul 4, 2022
15077f4
begin refacto
kduret Jul 4, 2022
b2e7783
refacto sql upgrade method
kduret Jul 4, 2022
4dc1790
Merge branch 'MON-12296-updates' into MON-12296-lock
kduret Jul 4, 2022
1f2ec51
postinstall
kduret Jul 4, 2022
e4289b6
improve repository
kduret Jul 5, 2022
7338990
rename repository and handle feedbacks
kduret Jul 5, 2022
84b03fc
fix tests
kduret Jul 5, 2022
ed2f423
fix
kduret Jul 5, 2022
9bb95e4
handle feedbacks
kduret Jul 5, 2022
412c0fc
Merge branch 'MON-12296-updates' into MON-12296-lock
kduret Jul 5, 2022
1aff442
remove useless import
kduret Jul 5, 2022
535fca5
remove locker
kduret Jul 5, 2022
5416212
handle properly exceptions
kduret Jul 5, 2022
bf93575
Merge branch 'MON-12296-updates' into MON-12296-lock
kduret Jul 5, 2022
7983e48
Merge branch 'MON-12296-updates' into MON-12296-post
kduret Jul 5, 2022
74daaf3
fix install dir deletion
kduret Jul 5, 2022
1efa18c
Merge branch 'MON-12296' into MON-12296-lock
kduret Jul 6, 2022
9d85a88
handle feedbacks
kduret Jul 7, 2022
8110dbc
Update lang/fr_FR.UTF-8/LC_MESSAGES/messages.po
kduret Jul 7, 2022
cec7b4f
Update lang/fr_FR.UTF-8/LC_MESSAGES/messages.po
kduret Jul 7, 2022
7ad1714
lock update with wizard
kduret Jul 7, 2022
e120dd2
Merge branch 'MON-12296-lock' of https://github.com/centreon/centreon…
kduret Jul 7, 2022
01838eb
Merge branch 'MON-12296-lock' into MON-12296-post
kduret Jul 7, 2022
d39fba1
move post update actions
kduret Jul 7, 2022
49aa4fb
Merge branch 'MON-12296' into MON-12296-post
kduret Jul 7, 2022
58dccf4
fix tests
kduret Jul 7, 2022
0996694
fix empty update
kduret Jul 7, 2022
e8e1cc9
linting
kduret Jul 7, 2022
2e5b20e
Apply suggestions from code review
kduret Jul 8, 2022
08f5745
Apply suggestions from code review
kduret Jul 8, 2022
6b80c7d
handle feedbacks
kduret Jul 8, 2022
6212eb1
Merge branch 'MON-12296-post' of https://github.com/centreon/centreon…
kduret Jul 8, 2022
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
1 change: 1 addition & 0 deletions config/packages/Centreon.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ services:

Core\Platform\Application\Repository\WriteUpdateRepositoryInterface:
class: Core\Platform\Infrastructure\Repository\DbWriteUpdateRepository
arguments: ['%centreon_var_lib%', '%centreon_install_path%']
public: true

# Monitoring resources
Expand Down
5 changes: 4 additions & 1 deletion lang/fr_FR.UTF-8/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -16935,5 +16935,8 @@ msgstr "Une erreur s'est produite lors de l'application de la mise à jour %s (%
msgid "Error while locking update process"
msgstr "Erreur lors du verrouillage du processus de mise à jour"

msgid "'Error while unlocking update process"
msgid "Error while unlocking update process"
msgstr "Erreur lors du déverrouillage du processus de mise à jour"

msgid "An error occurred when applying post update actions"
msgstr "Une erreur s'est produite lors de l'application des actions postérieures à la mise à jour"
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,11 @@ interface WriteUpdateRepositoryInterface
* @param string $version
*/
public function runUpdate(string $version): void;

/**
* Run post update actions
*
* @param string $currentVersion
*/
public function runPostUpdate(string $currentVersion): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public function __invoke(
$this->runUpdates($availableUpdates);

$this->unlockUpdate();

$this->runPostUpdate($this->getCurrentVersionOrFail());
} catch (\Throwable $e) {
$this->error(
$e->getMessage(),
Expand Down Expand Up @@ -166,4 +168,22 @@ private function runUpdates(array $versions): void
}
}
}

/**
* Run post update actions
*
* @param string $currentVersion
*
* @throws UpdateVersionsException
*/
private function runPostUpdate(string $currentVersion): void
{
$this->info("Running post update actions");

try {
$this->writeUpdateRepository->runPostUpdate($currentVersion);
} catch (\Throwable $e) {
throw UpdateVersionsException::errorWhenApplyingPostUpdate($e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,13 @@ public static function errorWhenApplyingUpdate(
$e
);
}

/**
* @param \Throwable $e
* @return self
*/
public static function errorWhenApplyingPostUpdate(\Throwable $e): self
{
return new self(_('An error occurred when applying post update actions'), 0, $e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,26 @@
use Centreon\Infrastructure\DatabaseConnection;
use Centreon\Infrastructure\Repository\AbstractRepositoryDRB;
use Core\Platform\Application\Repository\WriteUpdateRepositoryInterface;
use Symfony\Component\Filesystem\Filesystem;
use Centreon\Domain\Repository\RepositoryException;

class DbWriteUpdateRepository extends AbstractRepositoryDRB implements WriteUpdateRepositoryInterface
{
use LoggerTrait;

private const INSTALL_DIR = __DIR__ . '/../../../../../www/install';

/**
* @param string $libDir
* @param string $installDir
* @param Container $dependencyInjector
* @param DatabaseConnection $db
* @param Filesystem $filesystem
*/
public function __construct(
private string $libDir,
private string $installDir,
private Container $dependencyInjector,
DatabaseConnection $db,
private Filesystem $filesystem,
) {
$this->db = $db;
}
Expand All @@ -59,14 +64,65 @@ public function runUpdate(string $version): void
$this->updateVersionInformation($version);
}

/**
* @inheritDoc
*/
public function runPostUpdate(string $currentVersion): void
{
if (! $this->filesystem->exists($this->installDir)) {
return;
}

$this->backupInstallDirectory($currentVersion);
$this->removeInstallDirectory();
}

/**
* Backup installation directory
*
* @param string $currentVersion
*/
private function backupInstallDirectory(string $currentVersion): void
{
$backupDirectory = $this->libDir . '/installs/install-' . $currentVersion . '-' . date('Ymd_His');

$this->info(
"Backing up installation directory",
[
'source' => $this->installDir,
'destination' => $backupDirectory,
],
);

$this->filesystem->mirror(
$this->installDir,
$backupDirectory,
);
}

/**
* Remove installation directory
*/
private function removeInstallDirectory(): void
{
$this->info(
"Removing installation directory",
[
'installation_directory' => $this->installDir,
],
);

$this->filesystem->remove($this->installDir);
}

/**
* Run sql queries on monitoring database
*
* @param string $version
*/
private function runMonitoringSql(string $version): void
{
$upgradeFilePath = self::INSTALL_DIR . '/sql/centstorage/Update-CSTG-' . $version . '.sql';
$upgradeFilePath = $this->installDir . '/sql/centstorage/Update-CSTG-' . $version . '.sql';
if (is_readable($upgradeFilePath)) {
$this->db->switchToDb($this->db->getStorageDbName());
$this->runSqlFile($upgradeFilePath);
Expand All @@ -83,7 +139,7 @@ private function runScript(string $version): void
$pearDB = $this->dependencyInjector['configuration_db'];
$pearDBO = $this->dependencyInjector['realtime_db'];

$upgradeFilePath = self::INSTALL_DIR . '/php/Update-' . $version . '.php';
$upgradeFilePath = $this->installDir . '/php/Update-' . $version . '.php';
if (is_readable($upgradeFilePath)) {
include_once $upgradeFilePath;
}
Expand All @@ -96,7 +152,7 @@ private function runScript(string $version): void
*/
private function runConfigurationSql(string $version): void
{
$upgradeFilePath = self::INSTALL_DIR . '/sql/centreon/Update-DB-' . $version . '.sql';
$upgradeFilePath = $this->installDir . '/sql/centreon/Update-DB-' . $version . '.sql';
if (is_readable($upgradeFilePath)) {
$this->db->switchToDb($this->db->getCentreonDbName());
$this->runSqlFile($upgradeFilePath);
Expand All @@ -113,7 +169,7 @@ private function runPostScript(string $version): void
$pearDB = $this->dependencyInjector['configuration_db'];
$pearDBO = $this->dependencyInjector['realtime_db'];

$upgradeFilePath = self::INSTALL_DIR . '/php/Update-' . $version . '.post.php';
$upgradeFilePath = $this->installDir . '/php/Update-' . $version . '.post.php';
if (is_readable($upgradeFilePath)) {
include_once $upgradeFilePath;
}
Expand Down Expand Up @@ -146,7 +202,7 @@ private function runSqlFile(string $filePath): void
set_time_limit(0);

$fileName = basename($filePath);
$tmpFile = self::INSTALL_DIR . '/tmp/' . $fileName;
$tmpFile = $this->installDir . '/tmp/' . $fileName;

$alreadyExecutedQueriesCount = $this->getAlreadyExecutedQueriesCount($tmpFile);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,21 @@

use Centreon\Domain\Log\LoggerTrait;
use Core\Platform\Application\Repository\ReadUpdateRepositoryInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;

class FsReadUpdateRepository implements ReadUpdateRepositoryInterface
{
use LoggerTrait;

private const INSTALL_DIR = __DIR__ . '/../../../../../www/install';

/**
* @param Filesystem $filesystem
* @param Finder $finder
*/
public function __construct(
private Filesystem $filesystem,
private Finder $finder,
) {
}
Expand All @@ -57,10 +62,15 @@ public function findOrderedAvailableUpdates(string $currentVersion): array
*/
private function findAvailableUpdates(string $currentVersion): array
{
if (! $this->filesystem->exists(self::INSTALL_DIR)) {
return [];
}

$fileNameVersionRegex = '/Update-(?<version>[a-zA-Z0-9\-\.]+)\.php/';
$availableUpdates = [];

$updateFiles = $this->finder->files()
->in(__DIR__ . '/../../../../../www/install/php')
->in(self::INSTALL_DIR)
->name($fileNameVersionRegex);

foreach ($updateFiles as $updateFile) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@
->willReturn(true);

$this->readVersionRepository
->expects($this->once())
->expects($this->exactly(2))
->method('findCurrentVersion')
->willReturn('22.04.0');
->will($this->onConsecutiveCalls('22.04.0', '22.10.1'));

$this->readUpdateRepository
->expects($this->once())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,33 @@
namespace Tests\Core\Platform\Infrastructure\Repository;

use Core\Platform\Infrastructure\Repository\FsReadUpdateRepository;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;

beforeEach(function () {
$this->filesystem = $this->createMock(Filesystem::class);
$this->finder = $this->createMock(Finder::class);
});

it('should not find updates if install directory does not exist', function () {
$repository = new FsReadUpdateRepository($this->filesystem, $this->finder);

$this->filesystem
->expects($this->once())
->method('exists')
->willReturn(false);

$availableUpdates = $repository->findOrderedAvailableUpdates('22.04.0');
expect($availableUpdates)->toEqual([]);
});

it('should order found updates', function () {
$repository = new FsReadUpdateRepository($this->finder);
$repository = new FsReadUpdateRepository($this->filesystem, $this->finder);

$this->filesystem
->expects($this->once())
->method('exists')
->willReturn(true);

$this->finder
->expects($this->once())
Expand Down
58 changes: 16 additions & 42 deletions www/install/step_upgrade/process/process_step5.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,44 +37,15 @@
require_once __DIR__ . '/../../../../bootstrap.php';
require_once '../../steps/functions.php';

function recurseRmdir($dir)
{
$files = array_diff(scandir($dir), array('.', '..'));
foreach ($files as $file) {
(is_dir("$dir/$file")) ? recurseRmdir("$dir/$file") : unlink("$dir/$file");
}
return rmdir($dir);
}

function recurseCopy($source, $dest)
{
if (is_link($source)) {
return symlink(readlink($source), $dest);
}
use Core\Platform\Application\Repository\WriteUpdateRepositoryInterface;
use Core\Platform\Application\UseCase\UpdateVersions\UpdateVersionsException;

if (is_file($source)) {
return copy($source, $dest);
}

if (!is_dir($dest)) {
mkdir($dest);
}
$kernel = \App\Kernel::createForWeb();

$dir = dir($source);
while (false !== $entry = $dir->read()) {
if ($entry == '.' || $entry == '..') {
continue;
}

recurseCopy("$source/$entry", "$dest/$entry");
}

$dir->close();
return true;
}
$updateWriteRepository = $kernel->getContainer()->get(WriteUpdateRepositoryInterface::class);

$parameters = filter_input_array(INPUT_POST);
$current = filter_var($_POST['current'] ?? "step 5", FILTER_SANITIZE_STRING);
$current = filter_var($_POST['current'] ?? "step 5", FILTER_SANITIZE_FULL_SPECIAL_CHARS);

if ($parameters) {
if ((int)$parameters["send_statistics"] === 1) {
Expand All @@ -88,16 +59,19 @@ function recurseCopy($source, $dest)
$db->query($query);
}

$name = 'install-' . $_SESSION['CURRENT_VERSION'] . '-' . date('Ymd_His');
$completeName = _CENTREON_VARLIB_ . '/installs/' . $name;
$sourceInstallDir = str_replace('step_upgrade', '', realpath(dirname(__FILE__) . '/../'));

try {
if (recurseCopy($sourceInstallDir, $completeName)) {
recurseRmdir($sourceInstallDir);
if (!isset($_SESSION['CURRENT_VERSION']) || ! preg_match('/^\d+\.\d+\.\d+/', $_SESSION['CURRENT_VERSION'])) {
throw new \Exception('Cannot get current version');
}
} catch (Exception $e) {
exitUpgradeProcess(1, $current, '', $e->getMessage());

$updateWriteRepository->runPostUpdate($_SESSION['CURRENT_VERSION']);
} catch (\Throwable $e) {
exitUpgradeProcess(
1,
$current,
'',
UpdateVersionsException::errorWhenApplyingPostUpdate($e)->getMessage()
);
}

session_destroy();