Upgrade.php 12 KB

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