-
-
Couldn't load subscription status.
- Fork 1.5k
Add a command to update all local plugins #21521
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
Open
cedric-anne
wants to merge
4
commits into
glpi-project:11.0/bugfixes
Choose a base branch
from
cedric-anne:11.0/marketplace-update-all
base: 11.0/bugfixes
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+308
−81
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9cf6f4b
Add a command to update all local plugins
cedric-anne daa8dfd
Use database data to list plugins
cedric-anne ee96160
add the username option, and make it optional
cedric-anne 965111a
Update src/Glpi/Console/Marketplace/UpdateLocalPluginsCommand.php
cedric-anne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
216 changes: 216 additions & 0 deletions
216
src/Glpi/Console/Marketplace/UpdateLocalPluginsCommand.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,216 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * --------------------------------------------------------------------- | ||
| * | ||
| * GLPI - Gestionnaire Libre de Parc Informatique | ||
| * | ||
| * http://glpi-project.org | ||
| * | ||
| * @copyright 2015-2025 Teclib' and contributors. | ||
| * @licence https://www.gnu.org/licenses/gpl-3.0.html | ||
| * | ||
| * --------------------------------------------------------------------- | ||
| * | ||
| * LICENSE | ||
| * | ||
| * This file is part of GLPI. | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| * | ||
| * --------------------------------------------------------------------- | ||
| */ | ||
|
|
||
| namespace Glpi\Console\Marketplace; | ||
|
|
||
| use Glpi\Console\AbstractCommand; | ||
| use Glpi\Marketplace\Api\Plugins; | ||
| use Glpi\Marketplace\Controller; | ||
| use GLPINetwork; | ||
| use Plugin; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Input\InputOption; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
| use Throwable; | ||
|
|
||
| final class UpdateLocalPluginsCommand extends AbstractCommand | ||
| { | ||
| protected function configure() | ||
| { | ||
| parent::configure(); | ||
|
|
||
| $this->setName('marketplace:update_local_plugins'); | ||
| $this->setDescription(__('Download up-to-date sources for all local plugins and process updates of active plugins')); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Download and update all plugins to their latest compatible versions, then reactivate active ones." |
||
|
|
||
| $this->addOption( | ||
| 'username', | ||
| 'u', | ||
| InputOption::VALUE_REQUIRED, | ||
| __('Name of user used during installation script (among other things to set plugin admin rights)') | ||
| ); | ||
| } | ||
|
|
||
| protected function execute(InputInterface $input, OutputInterface $output) | ||
| { | ||
| if (!Controller::isCLIAllowed()) { | ||
| $output->writeln("<error>" . __('Access to the marketplace CLI commands is disallowed by the GLPI configuration') . "</error>"); | ||
| return self::FAILURE; | ||
| } | ||
|
|
||
| if (!GLPINetwork::isRegistered()) { | ||
| $output->writeln("<error>" . __("The GLPI Network registration key is missing or invalid") . "</error>"); | ||
| return self::FAILURE; | ||
| } | ||
|
|
||
| $username = $input->getOption('username'); | ||
| if ($username !== null) { | ||
| $this->loadUserSession($username); | ||
| } | ||
|
|
||
| $has_errors = false; | ||
|
|
||
| $plugins_manager = new Plugin(); | ||
| $plugins_api = new Plugins(); | ||
|
|
||
| $local_plugins_data = $plugins_manager->find(); | ||
| $local_versions = \array_column($local_plugins_data, 'version', 'directory'); | ||
| $active_plugins = \array_column( | ||
| \array_filter($local_plugins_data, fn($plugin_data) => $plugin_data['state'] === Plugin::ACTIVATED), | ||
| 'id', | ||
| 'directory' | ||
| ); | ||
|
|
||
| // Update all plugins sources, to be sure that all plugins have the latest version. | ||
| $updated_plugins = []; | ||
| foreach ($local_versions as $plugin_key => $local_version) { | ||
| $exists_on_filesystem = $plugins_manager->isLoadable($plugin_key); | ||
|
|
||
| $latest_version = $plugins_api->getPlugin($plugin_key)['version'] ?? null; | ||
|
|
||
| if ($latest_version === null) { | ||
| $msg = '<comment>' | ||
| . sprintf(__('Plugin "%s" is not available for your GLPI version.'), $plugin_key) | ||
| . '</comment>' | ||
| ; | ||
| $output->writeln($msg); | ||
| continue; | ||
| } | ||
|
|
||
| if ($exists_on_filesystem && \version_compare($local_version, $latest_version, '<') === false) { | ||
| $msg = '<comment>' | ||
| . sprintf(__('Plugin "%s" is already up-to-date.'), $plugin_key) | ||
| . '</comment>' | ||
| ; | ||
| $output->writeln($msg); | ||
| continue; | ||
| } | ||
|
|
||
| $controller = new Controller($plugin_key); | ||
| if (!$controller->canBeOverwritten()) { | ||
| if ($controller::hasVcsDirectory($plugin_key)) { | ||
| $msg = '<comment>' | ||
| . sprintf(__('Plugin "%s" has a local source versioning directory.'), $plugin_key) | ||
| . ' ' | ||
| . __('To avoid overwriting a potential branch under development, downloading is disabled.') | ||
| . '</comment>' | ||
| ; | ||
| } else { | ||
| $msg = '<comment>' | ||
| . sprintf(__('Plugin "%s" has an available update but its directory is not writable.'), $plugin_key) | ||
| . '</comment>' | ||
| ; | ||
| } | ||
|
|
||
| $output->writeln($msg); | ||
| continue; | ||
| } | ||
|
|
||
| $result = $controller->downloadPlugin(false, $latest_version); | ||
| if ($result) { | ||
| $updated_plugins[] = $plugin_key; | ||
| $output->writeln('<info>' . sprintf(__('Plugin "%s" downloaded successfully'), $plugin_key) . '</info>'); | ||
| } else { | ||
| $has_errors = true; | ||
| $output->writeln( | ||
| '<error>' . sprintf(__('Plugin "%s" could not be downloaded'), $plugin_key) . '</error>', | ||
| OutputInterface::VERBOSITY_QUIET | ||
| ); | ||
| $this->outputSessionBufferedMessages([WARNING, ERROR]); | ||
| } | ||
| } | ||
|
|
||
| // Automatically process plugin update and reactivation of active plugins. | ||
| if (count($active_plugins) > 0) { | ||
| \asort($active_plugins); | ||
|
|
||
| Plugin::forcePluginsExecution(true); // Temporarly force the plugins execution | ||
| foreach ($active_plugins as $plugin_key => $plugin_id) { | ||
| if (!\in_array($plugin_key, $updated_plugins, true)) { | ||
| continue; | ||
| } | ||
|
|
||
| $plugin = new Plugin(); | ||
|
|
||
| try { | ||
| $plugin->install($plugin_id); | ||
| $installed = \in_array($plugin->fields['state'], [Plugin::NOTACTIVATED, Plugin::TOBECONFIGURED]); | ||
| } catch (Throwable $e) { | ||
| global $PHPLOGGER; | ||
| $PHPLOGGER->error( | ||
| sprintf('Error while installing plugin `%s`, error was: `%s`.', $plugin_key, $e->getMessage()), | ||
| ['exception' => $e] | ||
| ); | ||
|
|
||
| $installed = false; | ||
| } | ||
| if (!$installed) { | ||
| $has_errors = true; | ||
| $output->writeln( | ||
| '<error>' . sprintf(__('Plugin "%s" installation failed.'), $plugin_key) . '</error>', | ||
| OutputInterface::VERBOSITY_QUIET | ||
| ); | ||
| $this->outputSessionBufferedMessages([WARNING, ERROR]); | ||
| continue; | ||
| } | ||
|
|
||
| try { | ||
| $activated = $plugin->activate($plugin_id); | ||
| } catch (Throwable $e) { | ||
| global $PHPLOGGER; | ||
| $PHPLOGGER->error( | ||
| sprintf('Error while activating plugin `%s`, error was: `%s`.', $plugin_key, $e->getMessage()), | ||
| ['exception' => $e] | ||
| ); | ||
|
|
||
| $activated = false; | ||
| } | ||
|
|
||
| if (!$activated) { | ||
| $has_errors = true; | ||
| $output->writeln( | ||
| '<error>' . sprintf(__('Plugin "%s" activation failed.'), $plugin_key) . '</error>', | ||
| OutputInterface::VERBOSITY_QUIET | ||
| ); | ||
| $this->outputSessionBufferedMessages([WARNING, ERROR]); | ||
| continue; | ||
| } | ||
|
|
||
| $output->writeln('<info>' . sprintf(__('Plugin "%1$s" has been updated and reactivated.'), $plugin_key) . '</info>', ); | ||
| } | ||
| Plugin::forcePluginsExecution(false); | ||
| } | ||
|
|
||
| return $has_errors ? self::FAILURE : self::SUCCESS; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"marketplace:update"