upgrade.php 9.4 KB

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