Upgrade.php 10 KB

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