Repair.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Temtaime <temtaime@gmail.com>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. * @author Vincent Petry <vincent@nextcloud.com>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OC\Core\Command\Maintenance;
  30. use Exception;
  31. use OCP\App\IAppManager;
  32. use OCP\EventDispatcher\Event;
  33. use OCP\EventDispatcher\IEventDispatcher;
  34. use OCP\IConfig;
  35. use OC\Repair\Events\RepairAdvanceEvent;
  36. use OC\Repair\Events\RepairErrorEvent;
  37. use OC\Repair\Events\RepairFinishEvent;
  38. use OC\Repair\Events\RepairInfoEvent;
  39. use OC\Repair\Events\RepairStartEvent;
  40. use OC\Repair\Events\RepairStepEvent;
  41. use OC\Repair\Events\RepairWarningEvent;
  42. use Symfony\Component\Console\Command\Command;
  43. use Symfony\Component\Console\Helper\ProgressBar;
  44. use Symfony\Component\Console\Input\InputInterface;
  45. use Symfony\Component\Console\Input\InputOption;
  46. use Symfony\Component\Console\Output\OutputInterface;
  47. class Repair extends Command {
  48. protected \OC\Repair $repair;
  49. protected IConfig $config;
  50. private IEventDispatcher $dispatcher;
  51. private ProgressBar $progress;
  52. private OutputInterface $output;
  53. private IAppManager $appManager;
  54. protected bool $errored = false;
  55. public function __construct(\OC\Repair $repair, IConfig $config, IEventDispatcher $dispatcher, IAppManager $appManager) {
  56. $this->repair = $repair;
  57. $this->config = $config;
  58. $this->dispatcher = $dispatcher;
  59. $this->appManager = $appManager;
  60. parent::__construct();
  61. }
  62. protected function configure() {
  63. $this
  64. ->setName('maintenance:repair')
  65. ->setDescription('repair this installation')
  66. ->addOption(
  67. 'include-expensive',
  68. null,
  69. InputOption::VALUE_NONE,
  70. 'Use this option when you want to include resource and load expensive tasks');
  71. }
  72. protected function execute(InputInterface $input, OutputInterface $output): int {
  73. $repairSteps = $this->repair::getRepairSteps();
  74. if ($input->getOption('include-expensive')) {
  75. $repairSteps = array_merge($repairSteps, $this->repair::getExpensiveRepairSteps());
  76. }
  77. foreach ($repairSteps as $step) {
  78. $this->repair->addStep($step);
  79. }
  80. $apps = $this->appManager->getInstalledApps();
  81. foreach ($apps as $app) {
  82. if (!$this->appManager->isEnabledForUser($app)) {
  83. continue;
  84. }
  85. $info = $this->appManager->getAppInfo($app);
  86. if (!is_array($info)) {
  87. continue;
  88. }
  89. \OC_App::loadApp($app);
  90. $steps = $info['repair-steps']['post-migration'];
  91. foreach ($steps as $step) {
  92. try {
  93. $this->repair->addStep($step);
  94. } catch (Exception $ex) {
  95. $output->writeln("<error>Failed to load repair step for $app: {$ex->getMessage()}</error>");
  96. }
  97. }
  98. }
  99. $maintenanceMode = $this->config->getSystemValueBool('maintenance');
  100. $this->config->setSystemValue('maintenance', true);
  101. $this->progress = new ProgressBar($output);
  102. $this->output = $output;
  103. $this->dispatcher->addListener(RepairStartEvent::class, [$this, 'handleRepairFeedBack']);
  104. $this->dispatcher->addListener(RepairAdvanceEvent::class, [$this, 'handleRepairFeedBack']);
  105. $this->dispatcher->addListener(RepairFinishEvent::class, [$this, 'handleRepairFeedBack']);
  106. $this->dispatcher->addListener(RepairStepEvent::class, [$this, 'handleRepairFeedBack']);
  107. $this->dispatcher->addListener(RepairInfoEvent::class, [$this, 'handleRepairFeedBack']);
  108. $this->dispatcher->addListener(RepairWarningEvent::class, [$this, 'handleRepairFeedBack']);
  109. $this->dispatcher->addListener(RepairErrorEvent::class, [$this, 'handleRepairFeedBack']);
  110. $this->repair->run();
  111. $this->config->setSystemValue('maintenance', $maintenanceMode);
  112. return $this->errored ? 1 : 0;
  113. }
  114. public function handleRepairFeedBack(Event $event): void {
  115. if ($event instanceof RepairStartEvent) {
  116. $this->progress->start($event->getMaxStep());
  117. } elseif ($event instanceof RepairAdvanceEvent) {
  118. $this->progress->advance($event->getIncrement());
  119. } elseif ($event instanceof RepairFinishEvent) {
  120. $this->progress->finish();
  121. $this->output->writeln('');
  122. } elseif ($event instanceof RepairStepEvent) {
  123. $this->output->writeln('<info> - ' . $event->getStepName() . '</info>');
  124. } elseif ($event instanceof RepairInfoEvent) {
  125. $this->output->writeln('<info> - ' . $event->getMessage() . '</info>');
  126. } elseif ($event instanceof RepairWarningEvent) {
  127. $this->output->writeln('<comment> - WARNING: ' . $event->getMessage() . '</comment>');
  128. } elseif ($event instanceof RepairErrorEvent) {
  129. $this->output->writeln('<error> - ERROR: ' . $event->getMessage() . '</error>');
  130. $this->errored = true;
  131. }
  132. }
  133. }