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

Commit

Permalink
enh(api): remove install directory after update (#11345)
Browse files Browse the repository at this point in the history
Refs: MON-12296
  • Loading branch information
kduret authored Jul 8, 2022
1 parent 2efc0e8 commit 1f22eec
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 54 deletions.
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();

0 comments on commit 1f22eec

Please sign in to comment.