Repair.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 OC\Repair\Events\RepairAdvanceEvent;
  32. use OC\Repair\Events\RepairErrorEvent;
  33. use OC\Repair\Events\RepairFinishEvent;
  34. use OC\Repair\Events\RepairInfoEvent;
  35. use OC\Repair\Events\RepairStartEvent;
  36. use OC\Repair\Events\RepairStepEvent;
  37. use OC\Repair\Events\RepairWarningEvent;
  38. use OCP\App\IAppManager;
  39. use OCP\EventDispatcher\Event;
  40. use OCP\EventDispatcher\IEventDispatcher;
  41. use OCP\IConfig;
  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. private ProgressBar $progress;
  49. private OutputInterface $output;
  50. protected bool $errored = false;
  51. public function __construct(
  52. protected \OC\Repair $repair,
  53. protected IConfig $config,
  54. private IEventDispatcher $dispatcher,
  55. private IAppManager $appManager,
  56. ) {
  57. parent::__construct();
  58. }
  59. protected function configure() {
  60. $this
  61. ->setName('maintenance:repair')
  62. ->setDescription('repair this installation')
  63. ->addOption(
  64. 'include-expensive',
  65. null,
  66. InputOption::VALUE_NONE,
  67. 'Use this option when you want to include resource and load expensive tasks');
  68. }
  69. protected function execute(InputInterface $input, OutputInterface $output): int {
  70. $repairSteps = $this->repair::getRepairSteps();
  71. if ($input->getOption('include-expensive')) {
  72. $repairSteps = array_merge($repairSteps, $this->repair::getExpensiveRepairSteps());
  73. }
  74. foreach ($repairSteps as $step) {
  75. $this->repair->addStep($step);
  76. }
  77. $apps = $this->appManager->getInstalledApps();
  78. foreach ($apps as $app) {
  79. if (!$this->appManager->isEnabledForUser($app)) {
  80. continue;
  81. }
  82. $info = $this->appManager->getAppInfo($app);
  83. if (!is_array($info)) {
  84. continue;
  85. }
  86. \OC_App::loadApp($app);
  87. $steps = $info['repair-steps']['post-migration'];
  88. foreach ($steps as $step) {
  89. try {
  90. $this->repair->addStep($step);
  91. } catch (Exception $ex) {
  92. $output->writeln("<error>Failed to load repair step for $app: {$ex->getMessage()}</error>");
  93. }
  94. }
  95. }
  96. $maintenanceMode = $this->config->getSystemValueBool('maintenance');
  97. $this->config->setSystemValue('maintenance', true);
  98. $this->progress = new ProgressBar($output);
  99. $this->output = $output;
  100. $this->dispatcher->addListener(RepairStartEvent::class, [$this, 'handleRepairFeedBack']);
  101. $this->dispatcher->addListener(RepairAdvanceEvent::class, [$this, 'handleRepairFeedBack']);
  102. $this->dispatcher->addListener(RepairFinishEvent::class, [$this, 'handleRepairFeedBack']);
  103. $this->dispatcher->addListener(RepairStepEvent::class, [$this, 'handleRepairFeedBack']);
  104. $this->dispatcher->addListener(RepairInfoEvent::class, [$this, 'handleRepairFeedBack']);
  105. $this->dispatcher->addListener(RepairWarningEvent::class, [$this, 'handleRepairFeedBack']);
  106. $this->dispatcher->addListener(RepairErrorEvent::class, [$this, 'handleRepairFeedBack']);
  107. $this->repair->run();
  108. $this->config->setSystemValue('maintenance', $maintenanceMode);
  109. return $this->errored ? 1 : 0;
  110. }
  111. public function handleRepairFeedBack(Event $event): void {
  112. if ($event instanceof RepairStartEvent) {
  113. $this->progress->start($event->getMaxStep());
  114. } elseif ($event instanceof RepairAdvanceEvent) {
  115. $this->progress->advance($event->getIncrement());
  116. } elseif ($event instanceof RepairFinishEvent) {
  117. $this->progress->finish();
  118. $this->output->writeln('');
  119. } elseif ($event instanceof RepairStepEvent) {
  120. $this->output->writeln('<info> - ' . $event->getStepName() . '</info>');
  121. } elseif ($event instanceof RepairInfoEvent) {
  122. $this->output->writeln('<info> - ' . $event->getMessage() . '</info>');
  123. } elseif ($event instanceof RepairWarningEvent) {
  124. $this->output->writeln('<comment> - WARNING: ' . $event->getMessage() . '</comment>');
  125. } elseif ($event instanceof RepairErrorEvent) {
  126. $this->output->writeln('<error> - ERROR: ' . $event->getMessage() . '</error>');
  127. $this->errored = true;
  128. }
  129. }
  130. }