Skip to content

Commit

Permalink
Merge pull request PrestaShop#851 from M0rgan01/add-rollback-command
Browse files Browse the repository at this point in the history
Add 'backup:restore' in symfony console
  • Loading branch information
M0rgan01 authored Sep 18, 2024
2 parents 37543d7 + e68f902 commit 9ec8a3b
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 144 deletions.
3 changes: 1 addition & 2 deletions .github/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ runs:
shell: bash
run: |
backupName=$(docker exec prestashop_autoupgrade bash -c "ls -td -- /var/www/html/admin-dev/autoupgrade/backup/*/ | head -n 1 | cut -d'/' -f8 | tr -d '\n'")
docker exec -u www-data prestashop_autoupgrade php modules/autoupgrade/cli-rollback.php --dir="admin-dev" \
--backup=$backupName;
docker exec -u www-data prestashop_autoupgrade php modules/autoupgrade/bin/console backup:restore --backup=$backupName admin-dev
- name: Check endpoints response
shell: bash
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,16 @@ In case you lost the page from your backoffice, note it can be triggered via CLI

### Command line parameters

Rollback can be automated by calling *cli-rollback.php*.
The following parameters are mandatory:

* **--dir**: Tells where the admin directory is.
* **--backup**: Select the backup to restore (this can be found in your folder `<admin>/autoupgrade/backup/`)
For restore your store, you would use:

```
$ php cli-rollback.php --dir=admin-dev --backup=V1.7.5.1_20190502-191341-22e883bd
$ php bin/console backup:restore --backup=[backup-name] <your-admin-dir>
```

You can see all available parameters and options directly from the console by using the `--help` option with any command.

For more information on using commands, please refer to the [PrestaShop developer documentation](https://devdocs.prestashop-project.org/8/basics/keeping-up-to-date/upgrade-module/upgrade-cli/#rollback-cli)

## Documentation

* Documentation is hosted on [the Developer documentation][doc].
Expand Down
2 changes: 2 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/

use PrestaShop\Module\AutoUpgrade\Commands\RestoreCommand;
use PrestaShop\Module\AutoUpgrade\Commands\UpdateCommand;
use Symfony\Component\Console\Application;

Expand All @@ -42,6 +43,7 @@ if (!defined('_PS_ROOT_DIR_')) {
$application = new Application();

$application->add(new UpdateCommand());
$application->add(new RestoreCommand());

try {
$application->run();
Expand Down
81 changes: 81 additions & 0 deletions classes/Commands/AbstractCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/

namespace PrestaShop\Module\AutoUpgrade\Commands;

use Exception;
use PrestaShop\Module\AutoUpgrade\ErrorHandler;
use PrestaShop\Module\AutoUpgrade\Log\CliLogger;
use PrestaShop\Module\AutoUpgrade\Log\Logger;
use PrestaShop\Module\AutoUpgrade\Log\StreamedLogger;
use PrestaShop\Module\AutoUpgrade\UpgradeContainer;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

abstract class AbstractCommand extends Command
{
/**
* @var Logger
*/
protected $logger;
/**
* @var UpgradeContainer
*/
protected $upgradeContainer;

/**
* @throws Exception
*/
protected function setupContainer(InputInterface $input, OutputInterface $output): void
{
$this->logger = $output->isDecorated() ? new CliLogger($output) : new StreamedLogger();
if ($output->isQuiet()) {
$this->logger->setFilter(Logger::ERROR);
} elseif ($output->isVerbose()) {
$this->logger->setFilter(Logger::DEBUG);
} else {
$this->logger->setFilter(Logger::INFO);
}

$prodRootDir = _PS_ROOT_DIR_;
$this->logger->debug('Production root directory: ' . $prodRootDir);

$adminDir = _PS_ROOT_DIR_ . DIRECTORY_SEPARATOR . $input->getArgument('admin-dir');
$this->logger->debug('Admin directory: ' . $adminDir);
define('_PS_ADMIN_DIR_', $adminDir);

$this->upgradeContainer = new UpgradeContainer($prodRootDir, $adminDir);
$this->logger->debug('Upgrade container initialized.');

$this->logger->debug('Logger initialized: ' . get_class($this->logger));

$this->upgradeContainer->setLogger($this->logger);
(new ErrorHandler($this->logger))->enable();
$this->logger->debug('Error handler enabled.');
}
}
88 changes: 88 additions & 0 deletions classes/Commands/RestoreCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/

namespace PrestaShop\Module\AutoUpgrade\Commands;

use Exception;
use PrestaShop\Module\AutoUpgrade\Task\ExitCode;
use PrestaShop\Module\AutoUpgrade\Task\Runner\AllRollbackTasks;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class RestoreCommand extends AbstractCommand
{
/**
* @var string
*/
protected static $defaultName = 'backup:restore';

protected function configure(): void
{
$this
->setDescription('Restore your store.')
->setHelp(
'This command allows you to restore your store from a backup.' .
'See https://devdocs.prestashop-project.org/8/basics/keeping-up-to-date/upgrade-module/upgrade-cli/#rollback-cli for more details'
)
->addArgument('admin-dir', InputArgument::REQUIRED, 'The admin directory name.')
->addOption('backup', null, InputOption::VALUE_REQUIRED, 'Specify the backup name to restore (this can be found in your folder <admin directory>/autoupgrade/backup/)');
}

/**
* @throws Exception
*/
protected function execute(InputInterface $input, OutputInterface $output): ?int
{
try {
$this->setupContainer($input, $output);

$backup = $input->getOption('backup');

if (!$backup) {
$this->logger->error("The '--backup' option is required.");

return ExitCode::FAIL;
}

$controller = new AllRollbackTasks($this->upgradeContainer);
$controller->setOptions([
'backup' => $backup,
]);
$controller->init();
$exitCode = $controller->run();
$this->logger->debug('Process completed with exit code: ' . $exitCode);

return $exitCode;
} catch (Exception $e) {
$this->logger->error('An error occurred during the restoration process: ' . $e->getMessage());

return ExitCode::FAIL;
}
}
}
53 changes: 9 additions & 44 deletions classes/Commands/UpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,17 @@

use Exception;
use PrestaShop\Module\AutoUpgrade\DeveloperDocumentation;
use PrestaShop\Module\AutoUpgrade\ErrorHandler;
use PrestaShop\Module\AutoUpgrade\Log\CliLogger;
use PrestaShop\Module\AutoUpgrade\Log\Logger;
use PrestaShop\Module\AutoUpgrade\Log\StreamedLogger;
use PrestaShop\Module\AutoUpgrade\Task\ExitCode;
use PrestaShop\Module\AutoUpgrade\Task\Miscellaneous\UpdateConfig;
use PrestaShop\Module\AutoUpgrade\Task\Runner\AllUpgradeTasks;
use PrestaShop\Module\AutoUpgrade\UpgradeContainer;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class UpdateCommand extends Command
class UpdateCommand extends AbstractCommand
{
/**
* @var Logger
*/
private $logger;

/**
* @var string
*/
Expand Down Expand Up @@ -77,15 +67,6 @@ protected function configure(): void
*/
protected function execute(InputInterface $input, OutputInterface $output): ?int
{
$this->logger = $output->isDecorated() ? new CliLogger($output) : new StreamedLogger();
if ($output->isQuiet()) {
$this->logger->setFilter(Logger::ERROR);
} elseif ($output->isVerbose()) {
$this->logger->setFilter(Logger::DEBUG);
} else {
$this->logger->setFilter(Logger::INFO);
}

$chainMode = $input->getOption('chain');
$noChainMode = $input->getOption('no-chain');

Expand All @@ -95,28 +76,11 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
return ExitCode::FAIL;
}

$this->logger->debug('Starting the update process.');

try {
$prodRootDir = _PS_ROOT_DIR_;
$this->logger->debug('Production root directory: ' . $prodRootDir);

$adminDir = _PS_ROOT_DIR_ . DIRECTORY_SEPARATOR . $input->getArgument('admin-dir');
$this->logger->debug('Admin directory: ' . $adminDir);
define('_PS_ADMIN_DIR_', $adminDir);

$upgradeContainer = new UpgradeContainer($prodRootDir, $adminDir);
$this->logger->debug('Upgrade container initialized.');

$this->logger->debug('Logger initialized: ' . get_class($this->logger));

$upgradeContainer->setLogger($this->logger);
(new ErrorHandler($this->logger))->enable();
$this->logger->debug('Error handler enabled.');

$this->setupContainer($input, $output);
$configPath = $input->getOption('config-file-path');
if (!empty($configPath)) {
$exitCode = $this->loadConfiguration($configPath, $upgradeContainer);
$exitCode = $this->loadConfiguration($configPath, $this->upgradeContainer);
if ($exitCode !== ExitCode::SUCCESS) {
return $exitCode;
}
Expand All @@ -125,12 +89,13 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
$this->logger->debug('No configuration file defined, use default configuration instead.');
}

$controller = new AllUpgradeTasks($upgradeContainer);
$this->logger->debug('Starting the update process.');
$controller = new AllUpgradeTasks($this->upgradeContainer);
$controller->setOptions([
'data' => $input->getOption('data'),
'action' => $input->getOption('action'),
'channel' => $input->getOption('channel'),
]);
'data' => $input->getOption('data'),
'action' => $input->getOption('action'),
'channel' => $input->getOption('channel'),
]);
$controller->init();
$exitCode = $controller->run();
$this->logger->debug('Process completed with exit code: ' . $exitCode);
Expand Down
44 changes: 0 additions & 44 deletions cli-rollback.php

This file was deleted.

Loading

0 comments on commit 9ec8a3b

Please sign in to comment.