Upgrade.php 9.1 KB

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