diff --git a/.github/action.yml b/.github/action.yml index bc53b90f4..8482a28ad 100644 --- a/.github/action.yml +++ b/.github/action.yml @@ -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 diff --git a/README.md b/README.md index b4be0f440..2d39ecc8c 100644 --- a/README.md +++ b/README.md @@ -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 `/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] ``` +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]. diff --git a/bin/console b/bin/console index 71fc17d18..17e67d459 100755 --- a/bin/console +++ b/bin/console @@ -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; @@ -42,6 +43,7 @@ if (!defined('_PS_ROOT_DIR_')) { $application = new Application(); $application->add(new UpdateCommand()); +$application->add(new RestoreCommand()); try { $application->run(); diff --git a/classes/Commands/AbstractCommand.php b/classes/Commands/AbstractCommand.php new file mode 100644 index 000000000..c38317806 --- /dev/null +++ b/classes/Commands/AbstractCommand.php @@ -0,0 +1,81 @@ + + * @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.'); + } +} diff --git a/classes/Commands/RestoreCommand.php b/classes/Commands/RestoreCommand.php new file mode 100644 index 000000000..d5586b080 --- /dev/null +++ b/classes/Commands/RestoreCommand.php @@ -0,0 +1,88 @@ + + * @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 /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; + } + } +} diff --git a/classes/Commands/UpdateCommand.php b/classes/Commands/UpdateCommand.php index adf8b5e1c..16f6f3685 100644 --- a/classes/Commands/UpdateCommand.php +++ b/classes/Commands/UpdateCommand.php @@ -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 */ @@ -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'); @@ -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; } @@ -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); diff --git a/cli-rollback.php b/cli-rollback.php deleted file mode 100644 index 647795759..000000000 --- a/cli-rollback.php +++ /dev/null @@ -1,44 +0,0 @@ - - * @copyright Since 2007 PrestaShop SA and Contributors - * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) - */ - -use PrestaShop\Module\AutoUpgrade\ErrorHandler; - -if (PHP_SAPI !== 'cli') { - echo 'This script must be called from CLI'; - exit(1); -} - -require_once realpath(dirname(__FILE__) . '/../../modules/autoupgrade') . '/ajax-upgradetabconfig.php'; -$container = autoupgrade_init_container(dirname(__FILE__)); - -$logger = new PrestaShop\Module\AutoUpgrade\Log\StreamedLogger(); -$container->setLogger($logger); -(new ErrorHandler($logger))->enable(); -$controller = new \PrestaShop\Module\AutoUpgrade\Task\Runner\AllRollbackTasks($container); -$controller->setOptions(getopt('', ['backup::'])); -$controller->init(); -exit($controller->run()); diff --git a/cli-updateconfig.php b/cli-updateconfig.php deleted file mode 100644 index 460934d10..000000000 --- a/cli-updateconfig.php +++ /dev/null @@ -1,46 +0,0 @@ - - * @copyright Since 2007 PrestaShop SA and Contributors - * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) - */ -if (PHP_SAPI !== 'cli') { - echo 'This script must be called from CLI'; - exit(1); -} - -$inputConfigurationFile = getopt('', ['from::'])['from']; -if (!file_exists($inputConfigurationFile)) { - echo sprintf('Invalid input configuation file %s', $inputConfigurationFile) . PHP_EOL; - exit(1); -} - -$inputData = json_decode(file_get_contents($inputConfigurationFile), true); - -require_once realpath(dirname(__FILE__) . '/../../modules/autoupgrade') . '/ajax-upgradetabconfig.php'; -$container = autoupgrade_init_container(dirname(__FILE__)); - -$container->setLogger(new \PrestaShop\Module\AutoUpgrade\Log\StreamedLogger()); -$controller = new \PrestaShop\Module\AutoUpgrade\Task\Miscellaneous\UpdateConfig($container); -$controller->inputCliParameters($inputData); -$controller->init(); -exit($controller->run()); diff --git a/tests/phpstan/phpstan.neon b/tests/phpstan/phpstan.neon index 896e20cd8..45699863c 100644 --- a/tests/phpstan/phpstan.neon +++ b/tests/phpstan/phpstan.neon @@ -10,8 +10,6 @@ parameters: - ./../../ajax-upgradetab.php - ./../../ajax-upgradetabconfig.php - ./../../autoupgrade.php - - ./../../cli-rollback.php - - ./../../cli-updateconfig.php excludePaths: - ./../../classes/Tools14.php - ./../../classes/pclzip.lib.php