Upgrade.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Andreas Fischer <bantu@owncloud.com>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Nils Wittenbrink <nilswittenbrink@web.de>
  11. * @author Owen Winkler <a_github@midnightcircus.com>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Sander Ruitenbeek <sander@grids.be>
  14. * @author Thomas Müller <thomas.mueller@tmit.eu>
  15. * @author Thomas Pulzer <t.pulzer@kniel.de>
  16. * @author Valdnet <47037905+Valdnet@users.noreply.github.com>
  17. * @author Vincent Petry <vincent@nextcloud.com>
  18. *
  19. * @license AGPL-3.0
  20. *
  21. * This code is free software: you can redistribute it and/or modify
  22. * it under the terms of the GNU Affero General Public License, version 3,
  23. * as published by the Free Software Foundation.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU Affero General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU Affero General Public License, version 3,
  31. * along with this program. If not, see <http://www.gnu.org/licenses/>
  32. *
  33. */
  34. namespace OC\Core\Command;
  35. use OC\Console\TimestampFormatter;
  36. use OC\DB\MigratorExecuteSqlEvent;
  37. use OC\Repair\Events\RepairAdvanceEvent;
  38. use OC\Repair\Events\RepairErrorEvent;
  39. use OC\Repair\Events\RepairFinishEvent;
  40. use OC\Repair\Events\RepairInfoEvent;
  41. use OC\Repair\Events\RepairStartEvent;
  42. use OC\Repair\Events\RepairStepEvent;
  43. use OC\Repair\Events\RepairWarningEvent;
  44. use OC\Updater;
  45. use OCP\EventDispatcher\Event;
  46. use OCP\EventDispatcher\IEventDispatcher;
  47. use OCP\IConfig;
  48. use OCP\Util;
  49. use Symfony\Component\Console\Command\Command;
  50. use Symfony\Component\Console\Helper\ProgressBar;
  51. use Symfony\Component\Console\Input\InputInterface;
  52. use Symfony\Component\Console\Output\OutputInterface;
  53. class Upgrade extends Command {
  54. public const ERROR_SUCCESS = 0;
  55. public const ERROR_NOT_INSTALLED = 1;
  56. public const ERROR_MAINTENANCE_MODE = 2;
  57. public const ERROR_UP_TO_DATE = 0;
  58. public const ERROR_INVALID_ARGUMENTS = 4;
  59. public const ERROR_FAILURE = 5;
  60. public function __construct(
  61. private IConfig $config
  62. ) {
  63. parent::__construct();
  64. }
  65. protected function configure() {
  66. $this
  67. ->setName('upgrade')
  68. ->setDescription('run upgrade routines after installation of a new release. The release has to be installed before.');
  69. }
  70. /**
  71. * Execute the upgrade command
  72. *
  73. * @param InputInterface $input input interface
  74. * @param OutputInterface $output output interface
  75. */
  76. protected function execute(InputInterface $input, OutputInterface $output): int {
  77. if (Util::needUpgrade()) {
  78. if (OutputInterface::VERBOSITY_NORMAL < $output->getVerbosity()) {
  79. // Prepend each line with a little timestamp
  80. $timestampFormatter = new TimestampFormatter($this->config, $output->getFormatter());
  81. $output->setFormatter($timestampFormatter);
  82. }
  83. $self = $this;
  84. $updater = \OCP\Server::get(Updater::class);
  85. /** @var IEventDispatcher $dispatcher */
  86. $dispatcher = \OC::$server->get(IEventDispatcher::class);
  87. $progress = new ProgressBar($output);
  88. $progress->setFormat(" %message%\n %current%/%max% [%bar%] %percent:3s%%");
  89. $listener = function (MigratorExecuteSqlEvent $event) use ($progress, $output): void {
  90. $message = $event->getSql();
  91. if (OutputInterface::VERBOSITY_NORMAL < $output->getVerbosity()) {
  92. $output->writeln(' Executing SQL ' . $message);
  93. } else {
  94. if (strlen($message) > 60) {
  95. $message = substr($message, 0, 57) . '...';
  96. }
  97. $progress->setMessage($message);
  98. if ($event->getCurrentStep() === 1) {
  99. $output->writeln('');
  100. $progress->start($event->getMaxStep());
  101. }
  102. $progress->setProgress($event->getCurrentStep());
  103. if ($event->getCurrentStep() === $event->getMaxStep()) {
  104. $progress->setMessage('Done');
  105. $progress->finish();
  106. $output->writeln('');
  107. }
  108. }
  109. };
  110. $repairListener = function (Event $event) use ($progress, $output): void {
  111. if ($event instanceof RepairStartEvent) {
  112. $progress->setMessage('Starting ...');
  113. $output->writeln($event->getCurrentStepName());
  114. $output->writeln('');
  115. $progress->start($event->getMaxStep());
  116. } elseif ($event instanceof RepairAdvanceEvent) {
  117. $desc = $event->getDescription();
  118. if (!empty($desc)) {
  119. $progress->setMessage($desc);
  120. }
  121. $progress->advance($event->getIncrement());
  122. } elseif ($event instanceof RepairFinishEvent) {
  123. $progress->setMessage('Done');
  124. $progress->finish();
  125. $output->writeln('');
  126. } elseif ($event instanceof RepairStepEvent) {
  127. if (OutputInterface::VERBOSITY_NORMAL < $output->getVerbosity()) {
  128. $output->writeln('<info>Repair step: ' . $event->getStepName() . '</info>');
  129. }
  130. } elseif ($event instanceof RepairInfoEvent) {
  131. if (OutputInterface::VERBOSITY_NORMAL < $output->getVerbosity()) {
  132. $output->writeln('<info>Repair info: ' . $event->getMessage() . '</info>');
  133. }
  134. } elseif ($event instanceof RepairWarningEvent) {
  135. $output->writeln('<error>Repair warning: ' . $event->getMessage() . '</error>');
  136. } elseif ($event instanceof RepairErrorEvent) {
  137. $output->writeln('<error>Repair error: ' . $event->getMessage() . '</error>');
  138. }
  139. };
  140. $dispatcher->addListener(MigratorExecuteSqlEvent::class, $listener);
  141. $dispatcher->addListener(RepairStartEvent::class, $repairListener);
  142. $dispatcher->addListener(RepairAdvanceEvent::class, $repairListener);
  143. $dispatcher->addListener(RepairFinishEvent::class, $repairListener);
  144. $dispatcher->addListener(RepairStepEvent::class, $repairListener);
  145. $dispatcher->addListener(RepairInfoEvent::class, $repairListener);
  146. $dispatcher->addListener(RepairWarningEvent::class, $repairListener);
  147. $dispatcher->addListener(RepairErrorEvent::class, $repairListener);
  148. $updater->listen('\OC\Updater', 'maintenanceEnabled', function () use ($output) {
  149. $output->writeln('<info>Turned on maintenance mode</info>');
  150. });
  151. $updater->listen('\OC\Updater', 'maintenanceDisabled', function () use ($output) {
  152. $output->writeln('<info>Turned off maintenance mode</info>');
  153. });
  154. $updater->listen('\OC\Updater', 'maintenanceActive', function () use ($output) {
  155. $output->writeln('<info>Maintenance mode is kept active</info>');
  156. });
  157. $updater->listen('\OC\Updater', 'updateEnd',
  158. function ($success) use ($output, $self) {
  159. if ($success) {
  160. $message = "<info>Update successful</info>";
  161. } else {
  162. $message = "<error>Update failed</error>";
  163. }
  164. $output->writeln($message);
  165. });
  166. $updater->listen('\OC\Updater', 'dbUpgradeBefore', function () use ($output) {
  167. $output->writeln('<info>Updating database schema</info>');
  168. });
  169. $updater->listen('\OC\Updater', 'dbUpgrade', function () use ($output) {
  170. $output->writeln('<info>Updated database</info>');
  171. });
  172. $updater->listen('\OC\Updater', 'incompatibleAppDisabled', function ($app) use ($output) {
  173. $output->writeln('<comment>Disabled incompatible app: ' . $app . '</comment>');
  174. });
  175. $updater->listen('\OC\Updater', 'upgradeAppStoreApp', function ($app) use ($output) {
  176. $output->writeln('<info>Update app ' . $app . ' from App Store</info>');
  177. });
  178. $updater->listen('\OC\Updater', 'appSimulateUpdate', function ($app) use ($output) {
  179. $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>");
  180. });
  181. $updater->listen('\OC\Updater', 'appUpgradeStarted', function ($app, $version) use ($output) {
  182. $output->writeln("<info>Updating <$app> ...</info>");
  183. });
  184. $updater->listen('\OC\Updater', 'appUpgrade', function ($app, $version) use ($output) {
  185. $output->writeln("<info>Updated <$app> to $version</info>");
  186. });
  187. $updater->listen('\OC\Updater', 'failure', function ($message) use ($output, $self) {
  188. $output->writeln("<error>$message</error>");
  189. });
  190. $updater->listen('\OC\Updater', 'setDebugLogLevel', function ($logLevel, $logLevelName) use ($output) {
  191. $output->writeln("<info>Setting log level to debug</info>");
  192. });
  193. $updater->listen('\OC\Updater', 'resetLogLevel', function ($logLevel, $logLevelName) use ($output) {
  194. $output->writeln("<info>Resetting log level</info>");
  195. });
  196. $updater->listen('\OC\Updater', 'startCheckCodeIntegrity', function () use ($output) {
  197. $output->writeln("<info>Starting code integrity check...</info>");
  198. });
  199. $updater->listen('\OC\Updater', 'finishedCheckCodeIntegrity', function () use ($output) {
  200. $output->writeln("<info>Finished code integrity check</info>");
  201. });
  202. $success = $updater->upgrade();
  203. $this->postUpgradeCheck($input, $output);
  204. if (!$success) {
  205. return self::ERROR_FAILURE;
  206. }
  207. return self::ERROR_SUCCESS;
  208. } elseif ($this->config->getSystemValueBool('maintenance')) {
  209. //Possible scenario: Nextcloud core is updated but an app failed
  210. $output->writeln('<comment>Nextcloud is in maintenance mode</comment>');
  211. $output->write('<comment>Maybe an upgrade is already in process. Please check the '
  212. . 'logfile (data/nextcloud.log). If you want to re-run the '
  213. . 'upgrade procedure, remove the "maintenance mode" from '
  214. . 'config.php and call this script again.</comment>', true);
  215. return self::ERROR_MAINTENANCE_MODE;
  216. } else {
  217. $output->writeln('<info>Nextcloud is already latest version</info>');
  218. return self::ERROR_UP_TO_DATE;
  219. }
  220. }
  221. /**
  222. * Perform a post upgrade check (specific to the command line tool)
  223. *
  224. * @param InputInterface $input input interface
  225. * @param OutputInterface $output output interface
  226. */
  227. protected function postUpgradeCheck(InputInterface $input, OutputInterface $output) {
  228. $trustedDomains = $this->config->getSystemValue('trusted_domains', []);
  229. if (empty($trustedDomains)) {
  230. $output->write(
  231. '<warning>The setting "trusted_domains" could not be ' .
  232. 'set automatically by the upgrade script, ' .
  233. 'please set it manually</warning>'
  234. );
  235. }
  236. }
  237. }