-
Notifications
You must be signed in to change notification settings - Fork 2.1k
migrate oc8 mount.json external storages to the database #28662
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b2ea702
dc4f22d
e1a1487
ed1a7e4
69d3cf1
613e0e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| <?php | ||
| /** | ||
| * @author Robin Appelman <[email protected]> | ||
| * @author Philipp Schaffrath <[email protected]> | ||
| * | ||
| * @copyright Copyright (c) 2017, ownCloud GmbH | ||
| * @license AGPL-3.0 | ||
| * | ||
| * This code is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License, version 3, | ||
| * as published by the Free Software Foundation. | ||
| * | ||
| * This program 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, version 3, | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/> | ||
| * | ||
| */ | ||
|
|
||
| namespace OCA\Files_External\Migrations; | ||
|
|
||
| use OC\Files\External\Service\GlobalStoragesService; | ||
| use OC\Files\External\Service\LegacyStoragesService; | ||
| use OCP\Migration\ISimpleMigration; | ||
| use OCP\Migration\IOutput; | ||
|
|
||
| /** | ||
| * migrate mount.json mounts into the database | ||
| */ | ||
| class Version20170814051424 implements ISimpleMigration { | ||
|
|
||
| /** | ||
| * @param IOutput $out | ||
| */ | ||
| public function run(IOutput $out) { | ||
|
|
||
| /** @var GlobalStoragesService $globalStoragesService */ | ||
| $globalStoragesService = \OC::$server->query('GlobalStoragesService'); | ||
| $legacyStoragesService = new LegacyStoragesService(\OC::$server->getStoragesBackendService()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. above you query by string name and here by direct method, consistency would be nice (injection even better)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not want to add the LegacyStorageService to the DI container, just so others don't get the idea to use it 😛 |
||
|
|
||
| $legacyStorages = $legacyStoragesService->getAllStorages(); | ||
|
|
||
| foreach ($legacyStorages as $legacyStorage) { | ||
| try { | ||
| $mountOptions = $legacyStorage->getMountOptions(); | ||
| if (!empty($mountOptions) && !isset($mountOptions['enable_sharing'])) { | ||
| // existing mounts must have sharing enabled by default to avoid surprises | ||
| $mountOptions['enable_sharing'] = true; | ||
| $legacyStorage->setMountOptions($mountOptions); | ||
| } | ||
| $globalStoragesService->addStorage($legacyStorage); | ||
| } catch (\Exception $exception) { | ||
| $out->warning( | ||
| 'There has been an error migrating an external storage from mount.json to the database. The affected mount point is "' . | ||
| $legacyStorage->getMountPoint() . '" and the type is "' . $legacyStorage->getBackend()->getIdentifier() . '"' | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,12 +13,12 @@ | |
| <admin>admin-external-storage</admin> | ||
| </documentation> | ||
| <rememberlogin>false</rememberlogin> | ||
| <version>0.7.0</version> | ||
| <version>0.7.1</version> | ||
| <types> | ||
| <filesystem/> | ||
| </types> | ||
| <ocsid>166048</ocsid> | ||
|
|
||
| <use-migrations>true</use-migrations> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you likely also need to increase the app version to trigger the migrations?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, forgot about that.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But for anyone who actually needs this migration, the version would already bump. (oc8 update). |
||
| <namespace>Files_External</namespace> | ||
|
|
||
| <dependencies> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,228 @@ | ||
| <?php | ||
| /** | ||
| * @author Robin Appelman <[email protected]> | ||
| * @author Philipp Schaffrath <[email protected]> | ||
| * | ||
| * @copyright Copyright (c) 2017, ownCloud GmbH | ||
| * @license AGPL-3.0 | ||
| * | ||
| * This code is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License, version 3, | ||
| * as published by the Free Software Foundation. | ||
| * | ||
| * This program 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, version 3, | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/> | ||
| */ | ||
|
|
||
| namespace OC\Files\External\Service; | ||
| use OC\Files\External\LegacyUtil; | ||
| use OC\Files\External\StorageConfig; | ||
| use OCP\Files\External\IStoragesBackendService; | ||
| use OCP\Util; | ||
|
|
||
| /** | ||
| * Read mount config from legacy mount.json | ||
| */ | ||
| class LegacyStoragesService { | ||
| /** | ||
| * @var IStoragesBackendService | ||
| */ | ||
| protected $backendService; | ||
|
|
||
| /** | ||
| * @var LegacyUtil | ||
| */ | ||
| private $legacyUtil; | ||
|
|
||
| /** | ||
| * @param IStoragesBackendService $backendService | ||
| */ | ||
| public function __construct(IStoragesBackendService $backendService) { | ||
| $this->backendService = $backendService; | ||
| $this->legacyUtil = new LegacyUtil(); | ||
| } | ||
|
|
||
| /** | ||
| * @return array list of mount configs | ||
| */ | ||
| protected function readLegacyConfig() { | ||
| return $this->legacyUtil->readData(); | ||
| } | ||
|
|
||
| /** | ||
| * Copy legacy storage options into the given storage config object. | ||
| * | ||
| * @param StorageConfig $storageConfig storage config to populate | ||
| * @param string $mountType mount type | ||
| * @param string $applicable applicable user or group | ||
| * @param array $storageOptions legacy storage options | ||
| * | ||
| * @return StorageConfig populated storage config | ||
| */ | ||
| protected function populateStorageConfigWithLegacyOptions( | ||
| &$storageConfig, | ||
| $mountType, | ||
| $applicable, | ||
| $storageOptions | ||
| ) { | ||
| $backend = $this->backendService->getBackend($storageOptions['backend']); | ||
| if (!$backend) { | ||
| throw new \UnexpectedValueException('Invalid backend ' . $storageOptions['backend']); | ||
| } | ||
| $storageConfig->setBackend($backend); | ||
| if (isset($storageOptions['authMechanism']) && $storageOptions['authMechanism'] !== 'builtin::builtin') { | ||
| $authMechanism = $this->backendService->getAuthMechanism($storageOptions['authMechanism']); | ||
| } else { | ||
| $authMechanisms = $this->backendService->getAuthMechanisms(); | ||
| $authMechanism = $authMechanisms[0]; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using the first available AuthMechanism should be fine. |
||
| $storageOptions['authMechanism'] = 'null'; // to make error handling easier | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wouldn't this make it use the "null" authentication mechanism ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is about what the old code was like, just that it gets another
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if it works fine then ok |
||
| } | ||
| if (!$authMechanism) { | ||
| throw new \UnexpectedValueException('Invalid authentication mechanism ' . $storageOptions['authMechanism']); | ||
| } | ||
| $storageConfig->setAuthMechanism($authMechanism); | ||
| $storageConfig->setBackendOptions($storageOptions['options']); | ||
| if (isset($storageOptions['mountOptions'])) { | ||
| $storageConfig->setMountOptions($storageOptions['mountOptions']); | ||
| } | ||
| if (!isset($storageOptions['priority'])) { | ||
| $storageOptions['priority'] = $backend->getPriority(); | ||
| } | ||
| $storageConfig->setPriority($storageOptions['priority']); | ||
|
|
||
| if ($mountType === LegacyUtil::MOUNT_TYPE_USER) { | ||
| $applicableUsers = $storageConfig->getApplicableUsers(); | ||
| if ($applicable !== 'all') { | ||
| $applicableUsers[] = $applicable; | ||
| $storageConfig->setApplicableUsers($applicableUsers); | ||
| } | ||
| } else if ($mountType === LegacyUtil::MOUNT_TYPE_GROUP) { | ||
| $applicableGroups = $storageConfig->getApplicableGroups(); | ||
| $applicableGroups[] = $applicable; | ||
| $storageConfig->setApplicableGroups($applicableGroups); | ||
| } | ||
| return $storageConfig; | ||
| } | ||
|
|
||
| /** | ||
| * Read the external storages config | ||
| * | ||
| * @return StorageConfig[] map of storage id to storage config | ||
| */ | ||
| public function getAllStorages() { | ||
| $mountPoints = $this->readLegacyConfig(); | ||
| /** | ||
| * Here is the how the horribly messy mount point array looks like | ||
| * from the mount.json file: | ||
| * | ||
| * $storageOptions = $mountPoints[$mountType][$applicable][$mountPath] | ||
| * | ||
| * - $mountType is either "user" or "group" | ||
| * - $applicable is the name of a user or group (or the current user for personal mounts) | ||
| * - $mountPath is the mount point path (where the storage must be mounted) | ||
| * - $storageOptions is a map of storage options: | ||
| * - "priority": storage priority | ||
| * - "backend": backend identifier | ||
| * - "class": LEGACY backend class name | ||
| * - "options": backend-specific options | ||
| * - "authMechanism": authentication mechanism identifier | ||
| * - "mountOptions": mount-specific options (ex: disable previews, scanner, etc) | ||
| */ | ||
| // group by storage id | ||
| /** @var StorageConfig[] $storages */ | ||
| $storages = []; | ||
| // for storages without id (legacy), group by config hash for | ||
| // later processing | ||
| $storagesWithConfigHash = []; | ||
| foreach ($mountPoints as $mountType => $applicables) { | ||
| foreach ($applicables as $applicable => $mountPaths) { | ||
| foreach ($mountPaths as $rootMountPath => $storageOptions) { | ||
| $currentStorage = null; | ||
| /** | ||
| * Flag whether the config that was read already has an id. | ||
| * If not, it will use a config hash instead and generate | ||
| * a proper id later | ||
| * | ||
| * @var boolean | ||
| */ | ||
| $hasId = false; | ||
| // the root mount point is in the format "/$user/files/the/mount/point" | ||
| // we remove the "/$user/files" prefix | ||
| $parts = explode('/', ltrim($rootMountPath, '/'), 3); | ||
| if (count($parts) < 3) { | ||
| // something went wrong, skip | ||
| Util::writeLog( | ||
| 'files_external', | ||
| 'Could not parse mount point "' . $rootMountPath . '"', | ||
| Util::ERROR | ||
| ); | ||
| continue; | ||
| } | ||
| $relativeMountPath = rtrim($parts[2], '/'); | ||
| // note: we cannot do this after the loop because the decrypted config | ||
| // options might be needed for the config hash | ||
| $storageOptions['options'] = \OC_Mount_Config::decryptPasswords($storageOptions['options']); | ||
| if (!isset($storageOptions['backend'])) { | ||
| $storageOptions['backend'] = $storageOptions['class']; // legacy compat | ||
| } | ||
| if (!isset($storageOptions['authMechanism'])) { | ||
| $storageOptions['authMechanism'] = null; // ensure config hash works | ||
| } | ||
| if (isset($storageOptions['id'])) { | ||
| $configId = (int)$storageOptions['id']; | ||
| if (isset($storages[$configId])) { | ||
| $currentStorage = $storages[$configId]; | ||
| } | ||
| $hasId = true; | ||
| } else { | ||
| // missing id in legacy config, need to generate | ||
| // but at this point we don't know the max-id, so use | ||
| // first group it by config hash | ||
| $storageOptions['mountpoint'] = $rootMountPath; | ||
| $configId = LegacyUtil::makeConfigHash($storageOptions); | ||
| if (isset($storagesWithConfigHash[$configId])) { | ||
| $currentStorage = $storagesWithConfigHash[$configId]; | ||
| } | ||
| } | ||
| if (is_null($currentStorage)) { | ||
| // create new | ||
| $currentStorage = new StorageConfig($configId); | ||
| $currentStorage->setMountPoint($relativeMountPath); | ||
| } | ||
| try { | ||
| $this->populateStorageConfigWithLegacyOptions( | ||
| $currentStorage, | ||
| $mountType, | ||
| $applicable, | ||
| $storageOptions | ||
| ); | ||
| if ($hasId) { | ||
| $storages[$configId] = $currentStorage; | ||
| } else { | ||
| $storagesWithConfigHash[$configId] = $currentStorage; | ||
| } | ||
| } catch (\UnexpectedValueException $e) { | ||
| // don't die if a storage backend doesn't exist | ||
| Util::writeLog( | ||
| 'files_external', | ||
| 'Could not load storage: "' . $e->getMessage() . '"', | ||
| Util::ERROR | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // convert parameter values | ||
| foreach ($storages as $storage) { | ||
| $storage->getBackend()->validateStorageDefinition($storage); | ||
| $storage->getAuthMechanism()->validateStorageDefinition($storage); | ||
| } | ||
| return $storages; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess injection in constructor doesn't work in migrations ?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Migration files are
requiredby theMigrationService, so we can't use the DI for them. Making them "container aware" like that should be fine. (even if container aware stinks)