Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CLI] Improve state management #1031

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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 classes/Commands/CreateBackupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
try {
$this->setupEnvironment($input, $output);

$this->upgradeContainer->getFileConfigurationStorage()->cleanAllBackupFiles();
$controller = new AllBackupTasks($this->upgradeContainer);
$controller->init();
$exitCode = $controller->run();
Expand Down
2 changes: 1 addition & 1 deletion classes/Commands/RestoreCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
return ExitCode::SUCCESS;
}
}

$this->upgradeContainer->getFileConfigurationStorage()->cleanAllRestoreFiles();
$controller = new AllRestoreTasks($this->upgradeContainer);
$controller->setOptions([
'backup' => $backup,
Expand Down
31 changes: 16 additions & 15 deletions classes/Commands/UpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeConfiguration;
use PrestaShop\Module\AutoUpgrade\Task\ExitCode;
use PrestaShop\Module\AutoUpgrade\Task\Runner\AllUpdateTasks;
use PrestaShop\Module\AutoUpgrade\Task\TaskName;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -56,10 +57,9 @@ protected function configure(): void
->addArgument('admin-dir', InputArgument::REQUIRED, 'The admin directory name.')
->addOption('chain', null, InputOption::VALUE_NONE, 'True by default. Allows you to chain update commands automatically. The command will continue executing subsequent tasks without requiring manual intervention to restart the process.')
->addOption('no-chain', null, InputOption::VALUE_NONE, 'Prevents chaining of update commands. The command will execute a task and then stop, logging the next command that needs to be run. You will need to manually restart the process to continue with the next step.')
->addOption('channel', null, InputOption::VALUE_REQUIRED, "Selects what update to run ('local' / 'online')")
->addOption('channel', null, InputOption::VALUE_REQUIRED, "Selects what update to run ('" . UpgradeConfiguration::CHANNEL_LOCAL . "' / '" . UpgradeConfiguration::CHANNEL_ONLINE . "')")
->addOption('config-file-path', null, InputOption::VALUE_REQUIRED, 'Configuration file location for update.')
->addOption('action', null, InputOption::VALUE_REQUIRED, 'Advanced users only. Sets the step you want to start from (Default: UpgradeNow, see ' . DeveloperDocumentation::DEV_DOC_UPGRADE_CLI_URL . ' for other values available)')
->addOption('data', null, InputOption::VALUE_REQUIRED, 'Advanced users only. Contains the state of the update process encoded in base64');
->addOption('action', null, InputOption::VALUE_REQUIRED, 'Advanced users only. Sets the step you want to start from. Only the "' . TaskName::TASK_UPDATE_INITIALIZATION . '" task updates the configuration. (Default: ' . TaskName::TASK_UPDATE_INITIALIZATION . ', see ' . DeveloperDocumentation::DEV_DOC_UPGRADE_CLI_URL . ' for other values available)');
}

/**
Expand All @@ -77,9 +77,16 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
try {
$this->setupEnvironment($input, $output);

// in the case of commands containing the update status, it is not necessary to update the configuration
// also we do not want to repeat the update of the config in the recursive commands
if ($input->getOption('data') === null) {
$action = $input->getOption('action');

$isFirstUpdateProcess = $action === null || $action === TaskName::TASK_UPDATE_INITIALIZATION;
if ($isFirstUpdateProcess) {
$this->logger->debug('Cleaning previous state files.');
$this->upgradeContainer->getFileConfigurationStorage()->cleanAllUpdateFiles();
}

// if we are in the 1st step of the update, we update the configuration
if ($isFirstUpdateProcess) {
$configPath = $input->getOption('config-file-path');
$exitCode = $this->loadConfiguration($configPath, $this->upgradeContainer);
if ($exitCode !== ExitCode::SUCCESS) {
Expand All @@ -91,8 +98,7 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
$this->logger->debug('Starting the update process.');
$controller = new AllUpdateTasks($this->upgradeContainer);
$controller->setOptions([
'data' => $input->getOption('data'),
'action' => $input->getOption('action'),
'action' => $action,
UpgradeConfiguration::CHANNEL => $input->getOption('channel'),
]);
$controller->init();
Expand Down Expand Up @@ -125,15 +131,10 @@ private function chainCommand(OutputInterface $output): int
if (preg_match('/--action=(\S+)/', $lastInfo, $actionMatches)) {
$action = $actionMatches[1];
$this->logger->debug('Action parameter found: ' . $action);
}

if (preg_match('/--data=(\S+)/', $lastInfo, $dataMatches)) {
$data = $dataMatches[1];
$this->logger->debug('Data parameter found: ' . $data);
}
if (empty($action) || empty($data)) {
} else {
throw new InvalidArgumentException('The command does not contain the necessary information to continue the update process.');
}

$new_string = str_replace('INFO - $ ', '', $lastInfo);
$decorationParam = $output->isDecorated() ? ' --ansi' : '';
system('php ' . $new_string . $decorationParam, $exitCode);
Expand Down
8 changes: 0 additions & 8 deletions classes/Task/AbstractTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,6 @@ public function __construct(UpgradeContainer $container)
}
}

/**
* @return string base64 encoded data from AjaxResponse
*/
public function getEncodedResponse(): string
{
return base64_encode($this->getJsonResponse());
}

/**
* @return string Json encoded data from AjaxResponse
*/
Expand Down
12 changes: 2 additions & 10 deletions classes/Task/Runner/AllUpdateTasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@ public function setOptions(array $options): void

$this->container->getUpgradeConfiguration()->merge($config);
}

if (!empty($options['data'])) {
$this->container->getState()->importFromEncodedData($options['data']);
}
}

/**
Expand All @@ -101,15 +97,11 @@ protected function checkIfRestartRequested(AjaxResponse $response): bool
$this->logger->info('Restart requested. Please run the following command to continue your upgrade:');
$args = $_SERVER['argv'];
foreach ($args as $key => $arg) {
if (
strpos($arg, '--data') === 0
|| strpos($arg, '--action') === 0
|| strpos($arg, '--config-file-path') === 0
) {
if (strpos($arg, '--action') === 0 || strpos($arg, '--config-file-path') === 0) {
unset($args[$key]);
}
}
$this->logger->info('$ ' . implode(' ', $args) . ' --action=' . $response->getNext() . ' --data=' . $this->getEncodedResponse());
$this->logger->info('$ ' . implode(' ', $args) . ' --action=' . $response->getNext());

return true;
}
Expand Down
4 changes: 0 additions & 4 deletions classes/Task/Runner/SingleTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ public function setOptions(array $options): void
if (!empty($options['action'])) {
$this->step = $options['action'];
}

if (!empty($options['data'])) {
$this->container->getState()->importFromEncodedData($options['data']);
}
}

protected function canContinue(): bool
Expand Down
2 changes: 2 additions & 0 deletions controllers/admin/AdminSelfUpgradeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ public function init()
if (!$this->ajax) {
// removing temporary files before init state to make sure state is already available
$this->upgradeContainer->getFileConfigurationStorage()->cleanAllUpdateFiles();
$this->upgradeContainer->getFileConfigurationStorage()->cleanAllBackupFiles();
$this->upgradeContainer->getFileConfigurationStorage()->cleanAllRestoreFiles();
}

if (!$this->upgradeContainer->getState()->isInitialized()) {
Expand Down