Upgrade.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Andreas Fischer <bantu@owncloud.com>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Morris Jobke <hey@morrisjobke.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 Vincent Petry <pvince81@owncloud.com>
  17. *
  18. * @license AGPL-3.0
  19. *
  20. * This code is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU Affero General Public License, version 3,
  22. * as published by the Free Software Foundation.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU Affero General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Affero General Public License, version 3,
  30. * along with this program. If not, see <http://www.gnu.org/licenses/>
  31. *
  32. */
  33. namespace OC\Core\Command;
  34. use OC\Console\TimestampFormatter;
  35. use OC\Installer;
  36. use OC\Updater;
  37. use OCP\IConfig;
  38. use OCP\ILogger;
  39. use OCP\Util;
  40. use Symfony\Component\Console\Command\Command;
  41. use Symfony\Component\Console\Helper\ProgressBar;
  42. use Symfony\Component\Console\Input\InputInterface;
  43. use Symfony\Component\Console\Output\OutputInterface;
  44. use Symfony\Component\EventDispatcher\GenericEvent;
  45. class Upgrade extends Command {
  46. public const ERROR_SUCCESS = 0;
  47. public const ERROR_NOT_INSTALLED = 1;
  48. public const ERROR_MAINTENANCE_MODE = 2;
  49. public const ERROR_UP_TO_DATE = 0;
  50. public const ERROR_INVALID_ARGUMENTS = 4;
  51. public const ERROR_FAILURE = 5;
  52. /** @var IConfig */
  53. private $config;
  54. /** @var ILogger */
  55. private $logger;
  56. /**
  57. * @param IConfig $config
  58. * @param ILogger $logger
  59. * @param Installer $installer
  60. */
  61. public function __construct(IConfig $config, ILogger $logger, Installer $installer) {
  62. parent::__construct();
  63. $this->config = $config;
  64. $this->logger = $logger;
  65. $this->installer = $installer;
  66. }
  67. protected function configure() {
  68. $this
  69. ->setName('upgrade')
  70. ->setDescription('run upgrade routines after installation of a new release. The release has to be installed before.');
  71. }
  72. /**
  73. * Execute the upgrade command
  74. *
  75. * @param InputInterface $input input interface
  76. * @param OutputInterface $output output interface
  77. */
  78. protected function execute(InputInterface $input, OutputInterface $output) {
  79. if (Util::needUpgrade()) {
  80. if (OutputInterface::VERBOSITY_NORMAL < $output->getVerbosity()) {
  81. // Prepend each line with a little timestamp
  82. $timestampFormatter = new TimestampFormatter($this->config, $output->getFormatter());
  83. $output->setFormatter($timestampFormatter);
  84. }
  85. $self = $this;
  86. $updater = new Updater(
  87. $this->config,
  88. \OC::$server->getIntegrityCodeChecker(),
  89. $this->logger,
  90. $this->installer
  91. );
  92. $dispatcher = \OC::$server->getEventDispatcher();
  93. $progress = new ProgressBar($output);
  94. $progress->setFormat(" %message%\n %current%/%max% [%bar%] %percent:3s%%");
  95. $listener = function ($event) use ($progress, $output) {
  96. if ($event instanceof GenericEvent) {
  97. $message = $event->getSubject();
  98. if (OutputInterface::VERBOSITY_NORMAL < $output->getVerbosity()) {
  99. $output->writeln(' Checking table ' . $message);
  100. } else {
  101. if (strlen($message) > 60) {
  102. $message = substr($message, 0, 57) . '...';
  103. }
  104. $progress->setMessage($message);
  105. if ($event[0] === 1) {
  106. $output->writeln('');
  107. $progress->start($event[1]);
  108. }
  109. $progress->setProgress($event[0]);
  110. if ($event[0] === $event[1]) {
  111. $progress->setMessage('Done');
  112. $progress->finish();
  113. $output->writeln('');
  114. }
  115. }
  116. }
  117. };
  118. $repairListener = function ($event) use ($progress, $output) {
  119. if (!$event instanceof GenericEvent) {
  120. return;
  121. }
  122. switch ($event->getSubject()) {
  123. case '\OC\Repair::startProgress':
  124. $progress->setMessage('Starting ...');
  125. $output->writeln($event->getArgument(1));
  126. $output->writeln('');
  127. $progress->start($event->getArgument(0));
  128. break;
  129. case '\OC\Repair::advance':
  130. $desc = $event->getArgument(1);
  131. if (!empty($desc)) {
  132. $progress->setMessage($desc);
  133. }
  134. $progress->advance($event->getArgument(0));
  135. break;
  136. case '\OC\Repair::finishProgress':
  137. $progress->setMessage('Done');
  138. $progress->finish();
  139. $output->writeln('');
  140. break;
  141. case '\OC\Repair::step':
  142. if (OutputInterface::VERBOSITY_NORMAL < $output->getVerbosity()) {
  143. $output->writeln('<info>Repair step: ' . $event->getArgument(0) . '</info>');
  144. }
  145. break;
  146. case '\OC\Repair::info':
  147. if (OutputInterface::VERBOSITY_NORMAL < $output->getVerbosity()) {
  148. $output->writeln('<info>Repair info: ' . $event->getArgument(0) . '</info>');
  149. }
  150. break;
  151. case '\OC\Repair::warning':
  152. $output->writeln('<error>Repair warning: ' . $event->getArgument(0) . '</error>');
  153. break;
  154. case '\OC\Repair::error':
  155. $output->writeln('<error>Repair error: ' . $event->getArgument(0) . '</error>');
  156. break;
  157. }
  158. };
  159. $dispatcher->addListener('\OC\DB\Migrator::executeSql', $listener);
  160. $dispatcher->addListener('\OC\DB\Migrator::checkTable', $listener);
  161. $dispatcher->addListener('\OC\Repair::startProgress', $repairListener);
  162. $dispatcher->addListener('\OC\Repair::advance', $repairListener);
  163. $dispatcher->addListener('\OC\Repair::finishProgress', $repairListener);
  164. $dispatcher->addListener('\OC\Repair::step', $repairListener);
  165. $dispatcher->addListener('\OC\Repair::info', $repairListener);
  166. $dispatcher->addListener('\OC\Repair::warning', $repairListener);
  167. $dispatcher->addListener('\OC\Repair::error', $repairListener);
  168. $updater->listen('\OC\Updater', 'maintenanceEnabled', function () use ($output) {
  169. $output->writeln('<info>Turned on maintenance mode</info>');
  170. });
  171. $updater->listen('\OC\Updater', 'maintenanceDisabled', function () use ($output) {
  172. $output->writeln('<info>Turned off maintenance mode</info>');
  173. });
  174. $updater->listen('\OC\Updater', 'maintenanceActive', function () use ($output) {
  175. $output->writeln('<info>Maintenance mode is kept active</info>');
  176. });
  177. $updater->listen('\OC\Updater', 'updateEnd',
  178. function ($success) use ($output, $self) {
  179. if ($success) {
  180. $message = "<info>Update successful</info>";
  181. } else {
  182. $message = "<error>Update failed</error>";
  183. }
  184. $output->writeln($message);
  185. });
  186. $updater->listen('\OC\Updater', 'dbUpgradeBefore', function () use ($output) {
  187. $output->writeln('<info>Updating database schema</info>');
  188. });
  189. $updater->listen('\OC\Updater', 'dbUpgrade', function () use ($output) {
  190. $output->writeln('<info>Updated database</info>');
  191. });
  192. $updater->listen('\OC\Updater', 'dbSimulateUpgradeBefore', function () use ($output) {
  193. $output->writeln('<info>Checking whether the database schema can be updated (this can take a long time depending on the database size)</info>');
  194. });
  195. $updater->listen('\OC\Updater', 'dbSimulateUpgrade', function () use ($output) {
  196. $output->writeln('<info>Checked database schema update</info>');
  197. });
  198. $updater->listen('\OC\Updater', 'incompatibleAppDisabled', function ($app) use ($output) {
  199. $output->writeln('<comment>Disabled incompatible app: ' . $app . '</comment>');
  200. });
  201. $updater->listen('\OC\Updater', 'checkAppStoreAppBefore', function ($app) use ($output) {
  202. $output->writeln('<info>Checking for update of app ' . $app . ' in appstore</info>');
  203. });
  204. $updater->listen('\OC\Updater', 'upgradeAppStoreApp', function ($app) use ($output) {
  205. $output->writeln('<info>Update app ' . $app . ' from appstore</info>');
  206. });
  207. $updater->listen('\OC\Updater', 'checkAppStoreApp', function ($app) use ($output) {
  208. $output->writeln('<info>Checked for update of app "' . $app . '" in appstore </info>');
  209. });
  210. $updater->listen('\OC\Updater', 'appUpgradeCheckBefore', function () use ($output) {
  211. $output->writeln('<info>Checking updates of apps</info>');
  212. });
  213. $updater->listen('\OC\Updater', 'appSimulateUpdate', function ($app) use ($output) {
  214. $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>");
  215. });
  216. $updater->listen('\OC\Updater', 'appUpgradeCheck', function () use ($output) {
  217. $output->writeln('<info>Checked database schema update for apps</info>');
  218. });
  219. $updater->listen('\OC\Updater', 'appUpgradeStarted', function ($app, $version) use ($output) {
  220. $output->writeln("<info>Updating <$app> ...</info>");
  221. });
  222. $updater->listen('\OC\Updater', 'appUpgrade', function ($app, $version) use ($output) {
  223. $output->writeln("<info>Updated <$app> to $version</info>");
  224. });
  225. $updater->listen('\OC\Updater', 'failure', function ($message) use ($output, $self) {
  226. $output->writeln("<error>$message</error>");
  227. });
  228. $updater->listen('\OC\Updater', 'setDebugLogLevel', function ($logLevel, $logLevelName) use ($output) {
  229. $output->writeln("<info>Set log level to debug</info>");
  230. });
  231. $updater->listen('\OC\Updater', 'resetLogLevel', function ($logLevel, $logLevelName) use ($output) {
  232. $output->writeln("<info>Reset log level</info>");
  233. });
  234. $updater->listen('\OC\Updater', 'startCheckCodeIntegrity', function () use ($output) {
  235. $output->writeln("<info>Starting code integrity check...</info>");
  236. });
  237. $updater->listen('\OC\Updater', 'finishedCheckCodeIntegrity', function () use ($output) {
  238. $output->writeln("<info>Finished code integrity check</info>");
  239. });
  240. $success = $updater->upgrade();
  241. $this->postUpgradeCheck($input, $output);
  242. if (!$success) {
  243. return self::ERROR_FAILURE;
  244. }
  245. return self::ERROR_SUCCESS;
  246. } elseif ($this->config->getSystemValueBool('maintenance')) {
  247. //Possible scenario: Nextcloud core is updated but an app failed
  248. $output->writeln('<warning>Nextcloud is in maintenance mode</warning>');
  249. $output->write('<comment>Maybe an upgrade is already in process. Please check the '
  250. . 'logfile (data/nextcloud.log). If you want to re-run the '
  251. . 'upgrade procedure, remove the "maintenance mode" from '
  252. . 'config.php and call this script again.</comment>'
  253. , true);
  254. return self::ERROR_MAINTENANCE_MODE;
  255. } else {
  256. $output->writeln('<info>Nextcloud is already latest version</info>');
  257. return self::ERROR_UP_TO_DATE;
  258. }
  259. }
  260. /**
  261. * Perform a post upgrade check (specific to the command line tool)
  262. *
  263. * @param InputInterface $input input interface
  264. * @param OutputInterface $output output interface
  265. */
  266. protected function postUpgradeCheck(InputInterface $input, OutputInterface $output) {
  267. $trustedDomains = $this->config->getSystemValue('trusted_domains', []);
  268. if (empty($trustedDomains)) {
  269. $output->write(
  270. '<warning>The setting "trusted_domains" could not be ' .
  271. 'set automatically by the upgrade script, ' .
  272. 'please set it manually</warning>'
  273. );
  274. }
  275. }
  276. }