Skip to content

Commit

Permalink
Merge pull request #83 from jadu/feature/limit-backups
Browse files Browse the repository at this point in the history
MET-89: Add an optional parameter limit-backups on patching stage
  • Loading branch information
dgudgeon authored Jun 16, 2020
2 parents a3a39eb + a791bed commit 1dfa402
Show file tree
Hide file tree
Showing 12 changed files with 305 additions and 48 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* Adds a --limit-backups option at patching stage ([#83](https://github.com/jadu/meteor/pull/83)).
* Adds correction to permission reset command in Troubleshooting documentation ([#94](https://github.com/jadu/meteor/pull/94)).

## v3.1.0
Expand Down
8 changes: 8 additions & 0 deletions docs/_docs/patch.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ If at any time you want to just verify the package the following command can be
php meteor.phar patch:verify
```

## Limiting number of backups

If you want to limit the number of backups that are on an environment, you can pass on an optional `--limit-backups` parameter

```
php meteor.phar patch:apply --limit-backups=3
```

## Configuration

```json
Expand Down
1 change: 0 additions & 1 deletion src/Patch/Backup/BackupFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public function find($installDir, array $config)
$backups = [];
$backupsDir = $installDir . '/backups';
$packageNames = $this->getPackageNames($config);

$backupDirs = [];
foreach (new DirectoryIterator($backupsDir) as $file) {
if (!$file->isDot() && $file->isDir() && preg_match('/^\d{14}$/', $file->getFilename())) {
Expand Down
32 changes: 32 additions & 0 deletions src/Patch/Backup/BackupHandlerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Meteor\Patch\Backup;

trait BackupHandlerTrait
{

public function removeBackups($backups, $count)
{
// Do not remove the latest backups that can be kept
$backups = array_slice($backups, $count);

$this->io->text(sprintf('It is recommended to keep a maximum of %d backups. To free up some disk space the following %d backups should be removed: ', $count, count($backups)));

// Get just the backup directory names
$backupDirs = array_map(function ($backup) {
return $backup->getPath();
}, $backups);

$this->io->listing($backupDirs);

$confirmation = $this->io->askConfirmation('Would you like to remove these backups?', true);
if (!$confirmation) {
return;
}

foreach ($backups as $backup) {
$this->io->text(sprintf('Removing backup <info>%s</info>', $backup->getDate()->format('c')));
$this->filesystem->remove($backup->getPath());
}
}
}
67 changes: 44 additions & 23 deletions src/Patch/ServiceContainer/PatchExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Meteor\Patch\Task\LimitBackupsHandler;
use Meteor\Patch\Task\LimitBackups;

class PatchExtension extends ExtensionBase implements ExtensionInterface, ScriptEventProviderInterface
{
Expand All @@ -41,6 +43,7 @@ class PatchExtension extends ExtensionBase implements ExtensionInterface, Script
const SERVICE_STRATEGY = 'patch.strategy';
const SERVICE_TASK_BUS = 'patch.task_bus';
const SERVICE_TASK_BACKUP_FILES_HANDLER = 'patch.task.backup_files_handler';
const SERVICE_TASK_LIMIT_BACKUPS_HANDLER = 'patch.task.limit_backups_handler';
const SERVICE_TASK_CHECK_DATABASE_CONNECTION_HANDLER = 'patch.task.check_database_connection_handler';
const SERVICE_TASK_CHECK_DISK_SPACE_HANDLER = 'patch.task.check_disk_space_handler';
const SERVICE_TASK_CHECK_MODULE_CMS_DEPENDENCY_HANDLER = 'patch.task.check_module_cms_dependency_handler';
Expand Down Expand Up @@ -113,6 +116,7 @@ public function load(ContainerBuilder $container, array $config)

$this->loadBackupFinder($container);
$this->loadBackupFilesTaskHandler($container);
$this->loadLimitBackupsTaskHandler($container);
$this->loadCheckDatabaseConnectionTaskHandler($container);
$this->loadCheckDiskSpaceHandler($container);
$this->loadCheckModuleCmsDependencyTaskHandler($container);
Expand All @@ -135,7 +139,7 @@ public function load(ContainerBuilder $container, array $config)
}

/**
* @param ContailerBuilder $container
* @param ContainerBuilder $container
*/
private function loadBackupFinder(ContainerBuilder $container)
{
Expand All @@ -146,15 +150,15 @@ private function loadBackupFinder(ContainerBuilder $container)
}

/**
* @param ContailerBuilder $container
* @param ContainerBuilder $container
*/
private function loadTaskBus(ContainerBuilder $container)
{
$container->setDefinition(self::SERVICE_TASK_BUS, new Definition('Meteor\Patch\Task\TaskBus'));
}

/**
* @param ContailerBuilder $container
* @param ContainerBuilder $container
*/
private function loadBackupFilesTaskHandler(ContainerBuilder $container)
{
Expand All @@ -171,7 +175,24 @@ private function loadBackupFilesTaskHandler(ContainerBuilder $container)
}

/**
* @param ContailerBuilder $container
* @param ContainerBuilder $container
*/
private function loadLimitBackupsTaskHandler(ContainerBuilder $container)
{
$definition = new Definition(LimitBackupsHandler::class, [
new Reference(self::SERVICE_BACKUP_FINDER),
new Reference(FilesystemExtension::SERVICE_FILESYSTEM),
new Reference(IOExtension::SERVICE_IO),
]);
$definition->addTag(self::TAG_TASK_HANDLER, [
'task' => LimitBackups::class,
]);

$container->setDefinition(self::SERVICE_TASK_LIMIT_BACKUPS_HANDLER, $definition);
}

/**
* @param ContainerBuilder $container
*/
private function loadCheckDatabaseConnectionTaskHandler(ContainerBuilder $container)
{
Expand All @@ -186,7 +207,7 @@ private function loadCheckDatabaseConnectionTaskHandler(ContainerBuilder $contai
}

/**
* @param ContailerBuilder $container
* @param ContainerBuilder $container
*/
private function loadCheckDiskSpaceHandler(ContainerBuilder $container)
{
Expand All @@ -203,7 +224,7 @@ private function loadCheckDiskSpaceHandler(ContainerBuilder $container)
}

/**
* @param ContailerBuilder $container
* @param ContainerBuilder $container
*/
private function loadCheckModuleCmsDependencyTaskHandler(ContainerBuilder $container)
{
Expand All @@ -218,7 +239,7 @@ private function loadCheckModuleCmsDependencyTaskHandler(ContainerBuilder $conta
}

/**
* @param ContailerBuilder $container
* @param ContainerBuilder $container
*/
private function loadCheckVersionTaskHandler(ContainerBuilder $container)
{
Expand All @@ -234,7 +255,7 @@ private function loadCheckVersionTaskHandler(ContainerBuilder $container)
}

/**
* @param ContailerBuilder $container
* @param ContainerBuilder $container
*/
private function loadCheckWritePermissionTaskHandler(ContainerBuilder $container)
{
Expand All @@ -249,7 +270,7 @@ private function loadCheckWritePermissionTaskHandler(ContainerBuilder $container
}

/**
* @param ContailerBuilder $container
* @param ContainerBuilder $container
*/
private function loadCopyFilesHandler(ContainerBuilder $container)
{
Expand All @@ -266,7 +287,7 @@ private function loadCopyFilesHandler(ContainerBuilder $container)
}

/**
* @param ContailerBuilder $container
* @param ContainerBuilder $container
*/
private function loadDeleteBackupHandler(ContainerBuilder $container)
{
Expand All @@ -282,7 +303,7 @@ private function loadDeleteBackupHandler(ContainerBuilder $container)
}

/**
* @param ContailerBuilder $container
* @param ContainerBuilder $container
*/
private function loadDisplayVersionInfoTaskHandler(ContainerBuilder $container)
{
Expand All @@ -298,7 +319,7 @@ private function loadDisplayVersionInfoTaskHandler(ContainerBuilder $container)
}

/**
* @param ContailerBuilder $container
* @param ContainerBuilder $container
*/
private function loadMigrateDownTaskHandler(ContainerBuilder $container)
{
Expand All @@ -315,7 +336,7 @@ private function loadMigrateDownTaskHandler(ContainerBuilder $container)
}

/**
* @param ContailerBuilder $container
* @param ContainerBuilder $container
*/
private function loadMigrateUpTaskHandler(ContainerBuilder $container)
{
Expand All @@ -331,7 +352,7 @@ private function loadMigrateUpTaskHandler(ContainerBuilder $container)
}

/**
* @param ContailerBuilder $container
* @param ContainerBuilder $container
*/
private function loadSetPermissionsHandler(ContainerBuilder $container)
{
Expand All @@ -347,7 +368,7 @@ private function loadSetPermissionsHandler(ContainerBuilder $container)
}

/**
* @param ContailerBuilder $container
* @param ContainerBuilder $container
*/
private function loadUpdateMigrationVersionFilesTaskHandler(ContainerBuilder $container)
{
Expand All @@ -364,7 +385,7 @@ private function loadUpdateMigrationVersionFilesTaskHandler(ContainerBuilder $co
}

/**
* @param ContailerBuilder $container
* @param ContainerBuilder $container
*/
private function loadApplyCommand(ContainerBuilder $container)
{
Expand All @@ -386,7 +407,7 @@ private function loadApplyCommand(ContainerBuilder $container)
}

/**
* @param ContailerBuilder $container
* @param ContainerBuilder $container
*/
private function loadVerifyCommand(ContainerBuilder $container)
{
Expand All @@ -404,7 +425,7 @@ private function loadVerifyCommand(ContainerBuilder $container)
}

/**
* @param ContailerBuilder $container
* @param ContainerBuilder $container
*/
private function loadManifestChecker(ContainerBuilder $container)
{
Expand All @@ -414,7 +435,7 @@ private function loadManifestChecker(ContainerBuilder $container)
}

/**
* @param ContailerBuilder $container
* @param ContainerBuilder $container
*/
private function loadClearLockCommand(ContainerBuilder $container)
{
Expand All @@ -432,15 +453,15 @@ private function loadClearLockCommand(ContainerBuilder $container)
}

/**
* @param ContailerBuilder $container
* @param ContainerBuilder $container
*/
private function loadLocker(ContainerBuilder $container)
{
$container->setDefinition(self::SERVICE_LOCKER, new Definition('Meteor\Patch\Lock\Locker'));
}

/**
* @param ContailerBuilder $container
* @param ContainerBuilder $container
*/
private function loadRollbackCommand(ContainerBuilder $container)
{
Expand All @@ -463,7 +484,7 @@ private function loadRollbackCommand(ContainerBuilder $container)
}

/**
* @param ContailerBuilder $container
* @param ContainerBuilder $container
*/
private function loadVersionInfoCommand(ContainerBuilder $container)
{
Expand All @@ -479,7 +500,7 @@ private function loadVersionInfoCommand(ContainerBuilder $container)
}

/**
* @param ContailerBuilder $container
* @param ContainerBuilder $container
*/
private function loadVersionComparer(ContainerBuilder $container)
{
Expand Down
6 changes: 6 additions & 0 deletions src/Patch/Strategy/Overwrite/OverwritePatchStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Meteor\Patch\Task\CopyFiles;
use Meteor\Patch\Task\DeleteBackup;
use Meteor\Patch\Task\DisplayVersionInfo;
use Meteor\Patch\Task\LimitBackups;
use Meteor\Patch\Task\MigrateDown;
use Meteor\Patch\Task\MigrateUp;
use Meteor\Patch\Task\SetPermissions;
Expand Down Expand Up @@ -44,6 +45,10 @@ public function apply($patchDir, $installDir, array $options)
$tasks[] = new CheckDatabaseConnection($installDir);
}

if (!empty($options['limit-backups']) && (int) $options['limit-backups'] !== 0) {
$tasks[] = new LimitBackups($installDir, $options['limit-backups']);
}

$tasks[] = new CheckDiskSpace($installDir);

if (!$options['skip-backup']) {
Expand Down Expand Up @@ -72,6 +77,7 @@ public function apply($patchDir, $installDir, array $options)
public function configureApplyCommand(InputDefinition $definition)
{
$definition->addOption(new InputOption('skip-backup', null, InputOption::VALUE_NONE, 'Do not create a backup before applying the patch.'));
$definition->addOption(new InputOption('limit-backups', null, InputOption::VALUE_REQUIRED, 'Limit the number of backups that are stored'));

$this->configureCommand($definition);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Patch/Task/BackupFilesHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct(Filesystem $filesystem, ConfigurationLoader $configu
*/
public function handle(BackupFiles $task, array $config)
{
$this->io->text(sprintf('Creating backup in <info>%s</>', $task->backupDir));
$this->io->text(sprintf('Creating backup in <info>%s</info>', $task->backupDir));

$this->filesystem->ensureDirectoryExists($task->backupDir);

Expand Down
29 changes: 7 additions & 22 deletions src/Patch/Task/CheckDiskSpaceHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
use Meteor\Filesystem\Filesystem;
use Meteor\IO\IOInterface;
use Meteor\Patch\Backup\BackupFinder;
use Meteor\Patch\Backup\BackupHandlerTrait;

class CheckDiskSpaceHandler
{
use BackupHandlerTrait;

/**
* Assuming required space is 300MB for backup and new files. Not checking the real package
* size to avoid performance issues when checking the size of thousands of files.
Expand Down Expand Up @@ -53,7 +56,9 @@ public function __construct(BackupFinder $backupFinder, Filesystem $filesystem,

/**
* @param CheckDiskSpace $task
* @param array $config
* @param array $config
*
* @return bool
*/
public function handle(CheckDiskSpace $task, array $config)
{
Expand Down Expand Up @@ -107,26 +112,6 @@ private function removeOldBackups($installDir, array $config)
return;
}

// Do not remove the latest backups that can be kept
$backups = array_slice($backups, self::MAX_BACKUPS);

$this->io->text(sprintf('It is recommended to keep a maximum of %d backups. To free up some disk space the following %d backups should be removed: ', self::MAX_BACKUPS, count($backups)));

// Get just the backup directory names
$backupDirs = array_map(function ($backup) {
return $backup->getPath();
}, $backups);

$this->io->listing($backupDirs);

$confirmation = $this->io->askConfirmation('Would you like to remove these backups?', true);
if (!$confirmation) {
return;
}

foreach ($backups as $backup) {
$this->io->text(sprintf('Removing backup <info>%s</>', $backup->getDate()->format('c')));
$this->filesystem->remove($backup->getPath());
}
$this->removeBackups($backups, self::MAX_BACKUPS);
}
}
Loading

0 comments on commit 1dfa402

Please sign in to comment.