You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Upgrade.php 10KB

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