Upgrade.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 Joas Schilling <coding@schilljs.com>
  8. * @author Jürgen Haas <juergen@paragon-es.de>
  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 Steffen Lindner <mail@steffen-lindner.de>
  15. * @author Thomas Müller <thomas.mueller@tmit.eu>
  16. * @author Thomas Pulzer <t.pulzer@kniel.de>
  17. * @author Vincent Petry <pvince81@owncloud.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\ILogger;
  40. use OCP\Util;
  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. const ERROR_SUCCESS = 0;
  48. const ERROR_NOT_INSTALLED = 1;
  49. const ERROR_MAINTENANCE_MODE = 2;
  50. const ERROR_UP_TO_DATE = 0;
  51. const ERROR_INVALID_ARGUMENTS = 4;
  52. const ERROR_FAILURE = 5;
  53. /** @var IConfig */
  54. private $config;
  55. /** @var ILogger */
  56. private $logger;
  57. /**
  58. * @param IConfig $config
  59. * @param ILogger $logger
  60. * @param Installer $installer
  61. */
  62. public function __construct(IConfig $config, ILogger $logger, Installer $installer) {
  63. parent::__construct();
  64. $this->config = $config;
  65. $this->logger = $logger;
  66. $this->installer = $installer;
  67. }
  68. protected function configure() {
  69. $this
  70. ->setName('upgrade')
  71. ->setDescription('run upgrade routines after installation of a new release. The release has to be installed before.');
  72. }
  73. /**
  74. * Execute the upgrade command
  75. *
  76. * @param InputInterface $input input interface
  77. * @param OutputInterface $output output interface
  78. */
  79. protected function execute(InputInterface $input, OutputInterface $output) {
  80. if(Util::needUpgrade()) {
  81. if (OutputInterface::VERBOSITY_NORMAL < $output->getVerbosity()) {
  82. // Prepend each line with a little timestamp
  83. $timestampFormatter = new TimestampFormatter($this->config, $output->getFormatter());
  84. $output->setFormatter($timestampFormatter);
  85. }
  86. $self = $this;
  87. $updater = new Updater(
  88. $this->config,
  89. \OC::$server->getIntegrityCodeChecker(),
  90. $this->logger,
  91. $this->installer
  92. );
  93. $dispatcher = \OC::$server->getEventDispatcher();
  94. $progress = new ProgressBar($output);
  95. $progress->setFormat(" %message%\n %current%/%max% [%bar%] %percent:3s%%");
  96. $listener = function($event) use ($progress, $output) {
  97. if ($event instanceof GenericEvent) {
  98. $message = $event->getSubject();
  99. if (OutputInterface::VERBOSITY_NORMAL < $output->getVerbosity()) {
  100. $output->writeln(' Checking table ' . $message);
  101. } else {
  102. if (strlen($message) > 60) {
  103. $message = substr($message, 0, 57) . '...';
  104. }
  105. $progress->setMessage($message);
  106. if ($event[0] === 1) {
  107. $output->writeln('');
  108. $progress->start($event[1]);
  109. }
  110. $progress->setProgress($event[0]);
  111. if ($event[0] === $event[1]) {
  112. $progress->setMessage('Done');
  113. $progress->finish();
  114. $output->writeln('');
  115. }
  116. }
  117. }
  118. };
  119. $repairListener = function($event) use ($progress, $output) {
  120. if (!$event instanceof GenericEvent) {
  121. return;
  122. }
  123. switch ($event->getSubject()) {
  124. case '\OC\Repair::startProgress':
  125. $progress->setMessage('Starting ...');
  126. $output->writeln($event->getArgument(1));
  127. $output->writeln('');
  128. $progress->start($event->getArgument(0));
  129. break;
  130. case '\OC\Repair::advance':
  131. $desc = $event->getArgument(1);
  132. if (!empty($desc)) {
  133. $progress->setMessage($desc);
  134. }
  135. $progress->advance($event->getArgument(0));
  136. break;
  137. case '\OC\Repair::finishProgress':
  138. $progress->setMessage('Done');
  139. $progress->finish();
  140. $output->writeln('');
  141. break;
  142. case '\OC\Repair::step':
  143. if(OutputInterface::VERBOSITY_NORMAL < $output->getVerbosity()) {
  144. $output->writeln('<info>Repair step: ' . $event->getArgument(0) . '</info>');
  145. }
  146. break;
  147. case '\OC\Repair::info':
  148. if(OutputInterface::VERBOSITY_NORMAL < $output->getVerbosity()) {
  149. $output->writeln('<info>Repair info: ' . $event->getArgument(0) . '</info>');
  150. }
  151. break;
  152. case '\OC\Repair::warning':
  153. $output->writeln('<error>Repair warning: ' . $event->getArgument(0) . '</error>');
  154. break;
  155. case '\OC\Repair::error':
  156. $output->writeln('<error>Repair error: ' . $event->getArgument(0) . '</error>');
  157. break;
  158. }
  159. };
  160. $dispatcher->addListener('\OC\DB\Migrator::executeSql', $listener);
  161. $dispatcher->addListener('\OC\DB\Migrator::checkTable', $listener);
  162. $dispatcher->addListener('\OC\Repair::startProgress', $repairListener);
  163. $dispatcher->addListener('\OC\Repair::advance', $repairListener);
  164. $dispatcher->addListener('\OC\Repair::finishProgress', $repairListener);
  165. $dispatcher->addListener('\OC\Repair::step', $repairListener);
  166. $dispatcher->addListener('\OC\Repair::info', $repairListener);
  167. $dispatcher->addListener('\OC\Repair::warning', $repairListener);
  168. $dispatcher->addListener('\OC\Repair::error', $repairListener);
  169. $updater->listen('\OC\Updater', 'maintenanceEnabled', function () use($output) {
  170. $output->writeln('<info>Turned on maintenance mode</info>');
  171. });
  172. $updater->listen('\OC\Updater', 'maintenanceDisabled', function () use($output) {
  173. $output->writeln('<info>Turned off maintenance mode</info>');
  174. });
  175. $updater->listen('\OC\Updater', 'maintenanceActive', function () use($output) {
  176. $output->writeln('<info>Maintenance mode is kept active</info>');
  177. });
  178. $updater->listen('\OC\Updater', 'updateEnd',
  179. function ($success) use($output, $self) {
  180. if ($success) {
  181. $message = "<info>Update successful</info>";
  182. } else {
  183. $message = "<error>Update failed</error>";
  184. }
  185. $output->writeln($message);
  186. });
  187. $updater->listen('\OC\Updater', 'dbUpgradeBefore', function () use($output) {
  188. $output->writeln('<info>Updating database schema</info>');
  189. });
  190. $updater->listen('\OC\Updater', 'dbUpgrade', function () use($output) {
  191. $output->writeln('<info>Updated database</info>');
  192. });
  193. $updater->listen('\OC\Updater', 'dbSimulateUpgradeBefore', function () use($output) {
  194. $output->writeln('<info>Checking whether the database schema can be updated (this can take a long time depending on the database size)</info>');
  195. });
  196. $updater->listen('\OC\Updater', 'dbSimulateUpgrade', function () use($output) {
  197. $output->writeln('<info>Checked database schema update</info>');
  198. });
  199. $updater->listen('\OC\Updater', 'incompatibleAppDisabled', function ($app) use($output) {
  200. $output->writeln('<comment>Disabled incompatible app: ' . $app . '</comment>');
  201. });
  202. $updater->listen('\OC\Updater', 'checkAppStoreAppBefore', function ($app) use($output) {
  203. $output->writeln('<info>Checking for update of app ' . $app . ' in appstore</info>');
  204. });
  205. $updater->listen('\OC\Updater', 'upgradeAppStoreApp', function ($app) use($output) {
  206. $output->writeln('<info>Update app ' . $app . ' from appstore</info>');
  207. });
  208. $updater->listen('\OC\Updater', 'checkAppStoreApp', function ($app) use($output) {
  209. $output->writeln('<info>Checked for update of app "' . $app . '" in appstore </info>');
  210. });
  211. $updater->listen('\OC\Updater', 'appUpgradeCheckBefore', function () use ($output) {
  212. $output->writeln('<info>Checking updates of apps</info>');
  213. });
  214. $updater->listen('\OC\Updater', 'appSimulateUpdate', function ($app) use ($output) {
  215. $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>");
  216. });
  217. $updater->listen('\OC\Updater', 'appUpgradeCheck', function () use ($output) {
  218. $output->writeln('<info>Checked database schema update for apps</info>');
  219. });
  220. $updater->listen('\OC\Updater', 'appUpgradeStarted', function ($app, $version) use ($output) {
  221. $output->writeln("<info>Updating <$app> ...</info>");
  222. });
  223. $updater->listen('\OC\Updater', 'appUpgrade', function ($app, $version) use ($output) {
  224. $output->writeln("<info>Updated <$app> to $version</info>");
  225. });
  226. $updater->listen('\OC\Updater', 'failure', function ($message) use($output, $self) {
  227. $output->writeln("<error>$message</error>");
  228. });
  229. $updater->listen('\OC\Updater', 'setDebugLogLevel', function ($logLevel, $logLevelName) use($output) {
  230. $output->writeln("<info>Set log level to debug</info>");
  231. });
  232. $updater->listen('\OC\Updater', 'resetLogLevel', function ($logLevel, $logLevelName) use($output) {
  233. $output->writeln("<info>Reset log level</info>");
  234. });
  235. $updater->listen('\OC\Updater', 'startCheckCodeIntegrity', function () use($output) {
  236. $output->writeln("<info>Starting code integrity check...</info>");
  237. });
  238. $updater->listen('\OC\Updater', 'finishedCheckCodeIntegrity', function () use($output) {
  239. $output->writeln("<info>Finished code integrity check</info>");
  240. });
  241. $success = $updater->upgrade();
  242. $this->postUpgradeCheck($input, $output);
  243. if(!$success) {
  244. return self::ERROR_FAILURE;
  245. }
  246. return self::ERROR_SUCCESS;
  247. } else if($this->config->getSystemValueBool('maintenance')) {
  248. //Possible scenario: Nextcloud core is updated but an app failed
  249. $output->writeln('<warning>Nextcloud is in maintenance mode</warning>');
  250. $output->write('<comment>Maybe an upgrade is already in process. Please check the '
  251. . 'logfile (data/nextcloud.log). If you want to re-run the '
  252. . 'upgrade procedure, remove the "maintenance mode" from '
  253. . 'config.php and call this script again.</comment>'
  254. , true);
  255. return self::ERROR_MAINTENANCE_MODE;
  256. } else {
  257. $output->writeln('<info>Nextcloud is already latest version</info>');
  258. return self::ERROR_UP_TO_DATE;
  259. }
  260. }
  261. /**
  262. * Perform a post upgrade check (specific to the command line tool)
  263. *
  264. * @param InputInterface $input input interface
  265. * @param OutputInterface $output output interface
  266. */
  267. protected function postUpgradeCheck(InputInterface $input, OutputInterface $output) {
  268. $trustedDomains = $this->config->getSystemValue('trusted_domains', array());
  269. if (empty($trustedDomains)) {
  270. $output->write(
  271. '<warning>The setting "trusted_domains" could not be ' .
  272. 'set automatically by the upgrade script, ' .
  273. 'please set it manually</warning>'
  274. );
  275. }
  276. }
  277. }