Upgrade.php 10 KB

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