Upgrade.php 11 KB

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