Upgrade.php 10 KB

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