diff options
author | Vincent Petry <pvince81@owncloud.com> | 2014-06-10 11:47:27 +0200 |
---|---|---|
committer | Vincent Petry <pvince81@owncloud.com> | 2014-06-12 17:38:26 +0200 |
commit | ca690c4d02d6c59e3fb9edb0f75ae640d744b612 (patch) | |
tree | 110d83d8c96d0b781155b42a6d9eead576fab6e7 /lib/private | |
parent | 6fcd1af4db2d1bf8d61fa0c627c308e7257294b9 (diff) | |
download | nextcloud-server-ca690c4d02d6c59e3fb9edb0f75ae640d744b612.tar.gz nextcloud-server-ca690c4d02d6c59e3fb9edb0f75ae640d744b612.zip |
Added RepairStep interface and default repair step lists
The updater is using "before update" repair steps and "regular" repair
steps.
The "regular" repair steps are also used by the CLI tool.
Currently no steps exist but can be added later in the static methods in
the \OC\Repair class.
Added unit test to test messaging, error and exception cases.
Diffstat (limited to 'lib/private')
-rw-r--r-- | lib/private/repair.php | 67 | ||||
-rw-r--r-- | lib/private/repairstep.php | 44 | ||||
-rw-r--r-- | lib/private/updater.php | 83 |
3 files changed, 131 insertions, 63 deletions
diff --git a/lib/private/repair.php b/lib/private/repair.php index 4a155c403a6..8979d04808b 100644 --- a/lib/private/repair.php +++ b/lib/private/repair.php @@ -11,51 +11,72 @@ namespace OC; use OC\Hooks\BasicEmitter; class Repair extends BasicEmitter { - private $stepClasses; + /** + * @var array + **/ + private $repairSteps; /** * Creates a new repair step runner * - * @param array $stepClasses optional list of step classes + * @param array $repairSteps array of RepairStep instances */ - public function __construct($stepClasses = array()) { - $this->stepClasses = $stepClasses; + public function __construct($repairSteps = array()) { + $this->repairSteps = $repairSteps; } /** * Run a series of repair steps for common problems */ public function run() { - $steps = array(); - - // instantiate all classes, just to make - // sure they all exist before starting - foreach ($this->stepClasses as $className) { - $steps[] = new $className(); - } - $self = $this; + if (count($this->repairSteps) === 0) { + $this->emit('\OC\Repair', 'info', array('No repair steps available')); + return; + } // run each repair step - foreach ($steps as $step) { + foreach ($this->repairSteps as $step) { $this->emit('\OC\Repair', 'step', array($step->getName())); - $step->listen('\OC\Repair', 'error', function ($description) use ($self) { - $self->emit('\OC\Repair', 'error', array($description)); - }); - $step->listen('\OC\Repair', 'info', function ($description) use ($self) { - $self->emit('\OC\Repair', 'info', array($description)); - }); + if ($step instanceof BasicEmitter) { + $step->listen('\OC\Repair', 'warning', function ($description) use ($self) { + $self->emit('\OC\Repair', 'warning', array($description)); + }); + $step->listen('\OC\Repair', 'info', function ($description) use ($self) { + $self->emit('\OC\Repair', 'info', array($description)); + }); + } + $step->run(); } } /** - * Add repair step class + * Add repair step * - * @param string $className name of a repair step class + * @param RepairStep $repairStep repair step */ - public function addStep($className) { - $this->stepClasses[] = $className; + public function addStep($repairStep) { + $this->repairSteps[] = $repairStep; } + /** + * Returns the default repair steps to be run on the + * command line or after an upgrade. + * + * @return array of RepairStep instances + */ + public static function getRepairSteps() { + return array(); + } + + /** + * Returns the repair steps to be run before an + * upgrade. + * + * @return array of RepairStep instances + */ + public static function getBeforeUpgradeRepairSteps() { + return array(); + } } diff --git a/lib/private/repairstep.php b/lib/private/repairstep.php new file mode 100644 index 00000000000..3c00cdb44a7 --- /dev/null +++ b/lib/private/repairstep.php @@ -0,0 +1,44 @@ +<?php +/** + * ownCloud + * + * @author Vincent Petry + * @copyright 2014 Vincent Petry pvince81@owncloud.com + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE + * License as published by the Free Software Foundation; either + * version 3 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU Affero General Public + * License along with this library. If not, see <http://www.gnu.org/licenses/>. + * + */ +namespace OC; + +/** + * Repair step + */ +interface RepairStep { + + /** + * Returns the step's name + * + * @return string + */ + public function getName(); + + /** + * Run repair step. + * Must throw exception on error. + * + * @throws \Exception in case of failure + */ + public function run(); + +} diff --git a/lib/private/updater.php b/lib/private/updater.php index 9cc1b3322eb..d50c2554c75 100644 --- a/lib/private/updater.php +++ b/lib/private/updater.php @@ -125,6 +125,7 @@ class Updater extends BasicEmitter { public function upgrade() { \OC_DB::enableCaching(false); \OC_Config::setValue('maintenance', true); + $installedVersion = \OC_Config::getValue('version', '0.0.0'); $currentVersion = implode('.', \OC_Util::getVersion()); if ($this->log) { @@ -132,6 +133,26 @@ class Updater extends BasicEmitter { } $this->emit('\OC\Updater', 'maintenanceStart'); + try { + $this->doUpgrade($currentVersion, $installedVersion); + } catch (\Exception $exception) { + $this->emit('\OC\Updater', 'failure', array($exception->getMessage())); + } + + \OC_Config::setValue('maintenance', false); + $this->emit('\OC\Updater', 'maintenanceEnd'); + } + + /** + * runs the update actions in maintenance mode, does not upgrade the source files + * except the main .htaccess file + * + * @param string $currentVersion current version to upgrade to + * @param string $installedVersion previous version from which to upgrade from + * + * @return bool true if the operation succeeded, false otherwise + */ + private function doUpgrade($currentVersion, $installedVersion) { // Update htaccess files for apache hosts if (isset($_SERVER['SERVER_SOFTWARE']) && strstr($_SERVER['SERVER_SOFTWARE'], 'Apache')) { \OC_Setup::updateHtaccess(); @@ -155,35 +176,28 @@ class Updater extends BasicEmitter { * STOP CONFIG CHANGES FOR OLDER VERSIONS */ - $canUpgrade = false; + // pre-upgrade repairs + $repair = new \OC\Repair(\OC\Repair::getBeforeUpgradeRepairSteps()); + $repair->run(); // simulate DB upgrade if ($this->simulateStepEnabled) { - try { - // simulate core DB upgrade - \OC_DB::simulateUpdateDbFromStructure(\OC::$SERVERROOT . '/db_structure.xml'); - - // simulate apps DB upgrade - $version = \OC_Util::getVersion(); - $apps = \OC_App::getEnabledApps(); - foreach ($apps as $appId) { - $info = \OC_App::getAppInfo($appId); - if (\OC_App::isAppCompatible($version, $info) && \OC_App::shouldUpgrade($appId)) { - if (file_exists(\OC_App::getAppPath($appId) . '/appinfo/database.xml')) { - \OC_DB::simulateUpdateDbFromStructure(\OC_App::getAppPath($appId) . '/appinfo/database.xml'); - } + // simulate core DB upgrade + \OC_DB::simulateUpdateDbFromStructure(\OC::$SERVERROOT . '/db_structure.xml'); + + // simulate apps DB upgrade + $version = \OC_Util::getVersion(); + $apps = \OC_App::getEnabledApps(); + foreach ($apps as $appId) { + $info = \OC_App::getAppInfo($appId); + if (\OC_App::isAppCompatible($version, $info) && \OC_App::shouldUpgrade($appId)) { + if (file_exists(\OC_App::getAppPath($appId) . '/appinfo/database.xml')) { + \OC_DB::simulateUpdateDbFromStructure(\OC_App::getAppPath($appId) . '/appinfo/database.xml'); } } - - $this->emit('\OC\Updater', 'dbSimulateUpgrade'); - - $canUpgrade = true; - } catch (\Exception $exception) { - $this->emit('\OC\Updater', 'failure', array($exception->getMessage())); } - } - else { - $canUpgrade = true; + + $this->emit('\OC\Updater', 'dbSimulateUpgrade'); } // upgrade from OC6 to OC7 @@ -193,17 +207,11 @@ class Updater extends BasicEmitter { \OC_Appconfig::setValue('core', 'shareapi_only_share_with_group_members', 'yes'); } - if ($this->updateStepEnabled && $canUpgrade) { - // proceed with real upgrade - try { - // do the real upgrade - \OC_DB::updateDbFromStructure(\OC::$SERVERROOT . '/db_structure.xml'); - $this->emit('\OC\Updater', 'dbUpgrade'); + if ($this->updateStepEnabled) { + // do the real upgrade + \OC_DB::updateDbFromStructure(\OC::$SERVERROOT . '/db_structure.xml'); + $this->emit('\OC\Updater', 'dbUpgrade'); - } catch (\Exception $exception) { - $this->emit('\OC\Updater', 'failure', array($exception->getMessage())); - return false; - } // TODO: why not do this at the end ? \OC_Config::setValue('version', implode('.', \OC_Util::getVersion())); $disabledApps = \OC_App::checkAppsRequirements(); @@ -213,18 +221,13 @@ class Updater extends BasicEmitter { // load all apps to also upgrade enabled apps \OC_App::loadApps(); - $repair = new Repair(); + // post-upgrade repairs + $repair = new \OC\Repair(\OC\Repair::getRepairSteps()); $repair->run(); //Invalidate update feed \OC_Appconfig::setValue('core', 'lastupdatedat', 0); } - - \OC_Config::setValue('maintenance', false); - $this->emit('\OC\Updater', 'maintenanceEnd'); - - return $canUpgrade; } - } |