Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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