You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 lines
9.6 KiB

  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Core\Command;
  8. use OC\Console\TimestampFormatter;
  9. use OC\DB\MigratorExecuteSqlEvent;
  10. use OC\Repair\Events\RepairAdvanceEvent;
  11. use OC\Repair\Events\RepairErrorEvent;
  12. use OC\Repair\Events\RepairFinishEvent;
  13. use OC\Repair\Events\RepairInfoEvent;
  14. use OC\Repair\Events\RepairStartEvent;
  15. use OC\Repair\Events\RepairStepEvent;
  16. use OC\Repair\Events\RepairWarningEvent;
  17. use OC\Updater;
  18. use OCP\EventDispatcher\Event;
  19. use OCP\EventDispatcher\IEventDispatcher;
  20. use OCP\IConfig;
  21. use OCP\IURLGenerator;
  22. use OCP\Server;
  23. use OCP\Util;
  24. use Symfony\Component\Console\Command\Command;
  25. use Symfony\Component\Console\Helper\ProgressBar;
  26. use Symfony\Component\Console\Input\InputInterface;
  27. use Symfony\Component\Console\Output\OutputInterface;
  28. class Upgrade extends Command {
  29. public const ERROR_SUCCESS = 0;
  30. public const ERROR_NOT_INSTALLED = 1;
  31. public const ERROR_MAINTENANCE_MODE = 2;
  32. public const ERROR_UP_TO_DATE = 0;
  33. public const ERROR_INVALID_ARGUMENTS = 4;
  34. public const ERROR_FAILURE = 5;
  35. public function __construct(
  36. private IConfig $config,
  37. private IURLGenerator $urlGenerator,
  38. ) {
  39. parent::__construct();
  40. }
  41. protected function configure() {
  42. $this
  43. ->setName('upgrade')
  44. ->setDescription('run upgrade routines after installation of a new release. The release has to be installed before.');
  45. }
  46. /**
  47. * Execute the upgrade command
  48. *
  49. * @param InputInterface $input input interface
  50. * @param OutputInterface $output output interface
  51. */
  52. protected function execute(InputInterface $input, OutputInterface $output): int {
  53. if (Util::needUpgrade()) {
  54. if ($output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
  55. // Prepend each line with a little timestamp
  56. $timestampFormatter = new TimestampFormatter($this->config, $output->getFormatter());
  57. $output->setFormatter($timestampFormatter);
  58. }
  59. $self = $this;
  60. $updater = Server::get(Updater::class);
  61. $incompatibleOverwrites = $this->config->getSystemValue('app_install_overwrite', []);
  62. /** @var IEventDispatcher $dispatcher */
  63. $dispatcher = Server::get(IEventDispatcher::class);
  64. $progress = new ProgressBar($output);
  65. $progress->setFormat(" %message%\n %current%/%max% [%bar%] %percent:3s%%");
  66. $listener = function (MigratorExecuteSqlEvent $event) use ($progress, $output): void {
  67. $message = $event->getSql();
  68. if ($output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
  69. $output->writeln(' Executing SQL ' . $message);
  70. } else {
  71. if (strlen($message) > 60) {
  72. $message = substr($message, 0, 57) . '...';
  73. }
  74. $progress->setMessage($message);
  75. if ($event->getCurrentStep() === 1) {
  76. $output->writeln('');
  77. $progress->start($event->getMaxStep());
  78. }
  79. $progress->setProgress($event->getCurrentStep());
  80. if ($event->getCurrentStep() === $event->getMaxStep()) {
  81. $progress->setMessage('Done');
  82. $progress->finish();
  83. $output->writeln('');
  84. }
  85. }
  86. };
  87. $repairListener = function (Event $event) use ($progress, $output): void {
  88. if ($event instanceof RepairStartEvent) {
  89. $progress->setMessage('Starting ...');
  90. $output->writeln($event->getCurrentStepName());
  91. $output->writeln('');
  92. $progress->start($event->getMaxStep());
  93. } elseif ($event instanceof RepairAdvanceEvent) {
  94. $desc = $event->getDescription();
  95. if (!empty($desc)) {
  96. $progress->setMessage($desc);
  97. }
  98. $progress->advance($event->getIncrement());
  99. } elseif ($event instanceof RepairFinishEvent) {
  100. $progress->setMessage('Done');
  101. $progress->finish();
  102. $output->writeln('');
  103. } elseif ($event instanceof RepairStepEvent) {
  104. if ($output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
  105. $output->writeln('<info>Repair step: ' . $event->getStepName() . '</info>');
  106. }
  107. } elseif ($event instanceof RepairInfoEvent) {
  108. if ($output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
  109. $output->writeln('<info>Repair info: ' . $event->getMessage() . '</info>');
  110. }
  111. } elseif ($event instanceof RepairWarningEvent) {
  112. $output->writeln('<error>Repair warning: ' . $event->getMessage() . '</error>');
  113. } elseif ($event instanceof RepairErrorEvent) {
  114. $output->writeln('<error>Repair error: ' . $event->getMessage() . '</error>');
  115. }
  116. };
  117. $dispatcher->addListener(MigratorExecuteSqlEvent::class, $listener);
  118. $dispatcher->addListener(RepairStartEvent::class, $repairListener);
  119. $dispatcher->addListener(RepairAdvanceEvent::class, $repairListener);
  120. $dispatcher->addListener(RepairFinishEvent::class, $repairListener);
  121. $dispatcher->addListener(RepairStepEvent::class, $repairListener);
  122. $dispatcher->addListener(RepairInfoEvent::class, $repairListener);
  123. $dispatcher->addListener(RepairWarningEvent::class, $repairListener);
  124. $dispatcher->addListener(RepairErrorEvent::class, $repairListener);
  125. $updater->listen('\OC\Updater', 'maintenanceEnabled', function () use ($output): void {
  126. $output->writeln('<info>Turned on maintenance mode</info>');
  127. });
  128. $updater->listen('\OC\Updater', 'maintenanceDisabled', function () use ($output): void {
  129. $output->writeln('<info>Turned off maintenance mode</info>');
  130. });
  131. $updater->listen('\OC\Updater', 'maintenanceActive', function () use ($output): void {
  132. $output->writeln('<info>Maintenance mode is kept active</info>');
  133. });
  134. $updater->listen('\OC\Updater', 'updateEnd',
  135. function ($success) use ($output, $self): void {
  136. if ($success) {
  137. $message = '<info>Update successful</info>';
  138. } else {
  139. $message = '<error>Update failed</error>';
  140. }
  141. $output->writeln($message);
  142. });
  143. $updater->listen('\OC\Updater', 'dbUpgradeBefore', function () use ($output): void {
  144. $output->writeln('<info>Updating database schema</info>');
  145. });
  146. $updater->listen('\OC\Updater', 'dbUpgrade', function () use ($output): void {
  147. $output->writeln('<info>Updated database</info>');
  148. });
  149. $updater->listen('\OC\Updater', 'incompatibleAppDisabled', function ($app) use ($output, &$incompatibleOverwrites): void {
  150. if (!in_array($app, $incompatibleOverwrites)) {
  151. $output->writeln('<comment>Disabled incompatible app: ' . $app . '</comment>');
  152. }
  153. });
  154. $updater->listen('\OC\Updater', 'upgradeAppStoreApp', function ($app) use ($output): void {
  155. $output->writeln('<info>Update app ' . $app . ' from App Store</info>');
  156. });
  157. $updater->listen('\OC\Updater', 'appSimulateUpdate', function ($app) use ($output): void {
  158. $output->writeln("<info>Checking whether the database schema for <$app> can be updated (this can take a long time depending on the database size)</info>");
  159. });
  160. $updater->listen('\OC\Updater', 'appUpgradeStarted', function ($app, $version) use ($output): void {
  161. $output->writeln("<info>Updating <$app> ...</info>");
  162. });
  163. $updater->listen('\OC\Updater', 'appUpgrade', function ($app, $version) use ($output): void {
  164. $output->writeln("<info>Updated <$app> to $version</info>");
  165. });
  166. $updater->listen('\OC\Updater', 'failure', function ($message) use ($output, $self): void {
  167. $output->writeln("<error>$message</error>");
  168. });
  169. $updater->listen('\OC\Updater', 'setDebugLogLevel', function ($logLevel, $logLevelName) use ($output): void {
  170. $output->writeln('<info>Setting log level to debug</info>');
  171. });
  172. $updater->listen('\OC\Updater', 'resetLogLevel', function ($logLevel, $logLevelName) use ($output): void {
  173. $output->writeln('<info>Resetting log level</info>');
  174. });
  175. $updater->listen('\OC\Updater', 'startCheckCodeIntegrity', function () use ($output): void {
  176. $output->writeln('<info>Starting code integrity check...</info>');
  177. });
  178. $updater->listen('\OC\Updater', 'finishedCheckCodeIntegrity', function () use ($output): void {
  179. $output->writeln('<info>Finished code integrity check</info>');
  180. });
  181. $success = $updater->upgrade();
  182. $this->postUpgradeCheck($input, $output);
  183. if (!$success) {
  184. return self::ERROR_FAILURE;
  185. }
  186. return self::ERROR_SUCCESS;
  187. } elseif ($this->config->getSystemValueBool('maintenance')) {
  188. //Possible scenario: Nextcloud core is updated but an app failed
  189. $output->writeln('<comment>Nextcloud is in maintenance mode</comment>');
  190. $output->write('<comment>Maybe an upgrade is already in process. Please check the '
  191. . 'logfile (data/nextcloud.log). If you want to re-run the '
  192. . 'upgrade procedure, remove the "maintenance mode" from '
  193. . 'config.php and call this script again.</comment>', true);
  194. return self::ERROR_MAINTENANCE_MODE;
  195. } else {
  196. $output->writeln('<info>No upgrade required.</info>');
  197. $output->writeln('');
  198. $output->writeln('Note: This command triggers the upgrade actions associated with a new version. The new version\'s updated source files must be deployed in advance.');
  199. $doc = $this->urlGenerator->linkToDocs('admin-update');
  200. $output->writeln('See the upgrade documentation: ' . $doc . ' for more information.');
  201. return self::ERROR_UP_TO_DATE;
  202. }
  203. }
  204. /**
  205. * Perform a post upgrade check (specific to the command line tool)
  206. *
  207. * @param InputInterface $input input interface
  208. * @param OutputInterface $output output interface
  209. */
  210. protected function postUpgradeCheck(InputInterface $input, OutputInterface $output) {
  211. $trustedDomains = $this->config->getSystemValue('trusted_domains', []);
  212. if (empty($trustedDomains)) {
  213. $output->write(
  214. '<warning>The setting "trusted_domains" could not be '
  215. . 'set automatically by the upgrade script, '
  216. . 'please set it manually</warning>'
  217. );
  218. }
  219. }
  220. }