-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
allow configuring multiple object store backends #52786
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
385dd36
feat: allow configuring multiple objectstore configurations
icewind1991 2d4bba7
feat: add command to get user objectstore config mappings
icewind1991 b3c53c7
feat: allow object store configuration aliases for easier migrations
icewind1991 4990d75
feat: multi object store rename command
icewind1991 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
| /** | ||
| * SPDX-FileCopyrightText: 2025 Robin Appelman <[email protected]> | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Files\Command\Object\Multi; | ||
|
|
||
| use OC\Core\Command\Base; | ||
| use OC\Files\ObjectStore\PrimaryObjectStoreConfig; | ||
| use OCP\IConfig; | ||
| use OCP\IDBConnection; | ||
| use Symfony\Component\Console\Helper\QuestionHelper; | ||
| use Symfony\Component\Console\Input\InputArgument; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
| use Symfony\Component\Console\Question\ConfirmationQuestion; | ||
|
|
||
| class Rename extends Base { | ||
| public function __construct( | ||
| private readonly IDBConnection $connection, | ||
| private readonly PrimaryObjectStoreConfig $objectStoreConfig, | ||
| private readonly IConfig $config, | ||
| ) { | ||
| parent::__construct(); | ||
| } | ||
|
|
||
| protected function configure(): void { | ||
| parent::configure(); | ||
| $this | ||
| ->setName('files:object:multi:rename-config') | ||
| ->setDescription('Rename an object store configuration and move all users over to the new configuration,') | ||
| ->addArgument('source', InputArgument::REQUIRED, 'Object store configuration to rename') | ||
| ->addArgument('target', InputArgument::REQUIRED, 'New name for the object store configuration'); | ||
| } | ||
|
|
||
| public function execute(InputInterface $input, OutputInterface $output): int { | ||
| $source = $input->getArgument('source'); | ||
| $target = $input->getArgument('target'); | ||
|
|
||
| $configs = $this->objectStoreConfig->getObjectStoreConfigs(); | ||
| if (!isset($configs[$source])) { | ||
| $output->writeln('<error>Unknown object store configuration: ' . $source . '</error>'); | ||
| return 1; | ||
| } | ||
|
|
||
| if ($source === 'root') { | ||
| $output->writeln('<error>Renaming the root configuration is not supported.</error>'); | ||
| return 1; | ||
| } | ||
|
|
||
| if ($source === 'default') { | ||
| $output->writeln('<error>Renaming the default configuration is not supported.</error>'); | ||
| return 1; | ||
| } | ||
|
|
||
| if (!isset($configs[$target])) { | ||
| $output->writeln('<comment>Target object store configuration ' . $target . ' doesn\'t exist yet.</comment>'); | ||
| $output->writeln('The target configuration can be created automatically.'); | ||
| $output->writeln('However, as this depends on modifying the config.php, this only works as long as the instance runs on a single node or all nodes in a clustered setup have a shared config file (such as from a shared network mount).'); | ||
| $output->writeln('If the different nodes have a separate copy of the config.php file, the automatic object store configuration creation will lead to the configuration going out of sync.'); | ||
| $output->writeln('If these requirements are not met, you can manually create the target object store configuration in each node\'s configuration before running the command.'); | ||
| $output->writeln(''); | ||
| $output->writeln('<error>Failure to check these requirements will lead to data loss for users.</error>'); | ||
|
|
||
| /** @var QuestionHelper $helper */ | ||
| $helper = $this->getHelper('question'); | ||
| $question = new ConfirmationQuestion('Automatically create target object store configuration? [y/N] ', false); | ||
| if ($helper->ask($input, $output, $question)) { | ||
| $configs[$target] = $configs[$source]; | ||
|
|
||
| // update all aliases | ||
| foreach ($configs as &$config) { | ||
| if ($config === $source) { | ||
| $config = $target; | ||
| } | ||
| } | ||
| $this->config->setSystemValue('objectstore', $configs); | ||
| } else { | ||
| return 0; | ||
| } | ||
| } elseif (($configs[$source] !== $configs[$target]) || $configs[$source] !== $target) { | ||
| $output->writeln('<error>Source and target configuration differ.</error>'); | ||
| $output->writeln(''); | ||
| $output->writeln('To ensure proper migration of users, the source and target configuration must be the same to ensure that the objects for the moved users exist on the target configuration.'); | ||
| $output->writeln('The usual migration process consists of creating a clone of the old configuration, moving the users from the old configuration to the new one, and then adjust the old configuration that is longer used.'); | ||
| return 1; | ||
| } | ||
|
|
||
| $query = $this->connection->getQueryBuilder(); | ||
| $query->update('preferences') | ||
| ->set('configvalue', $query->createNamedParameter($target)) | ||
| ->where($query->expr()->eq('appid', $query->createNamedParameter('homeobjectstore'))) | ||
| ->andWhere($query->expr()->eq('configkey', $query->createNamedParameter('objectstore'))) | ||
| ->andWhere($query->expr()->eq('configvalue', $query->createNamedParameter($source))); | ||
| $count = $query->executeStatement(); | ||
|
|
||
| if ($count > 0) { | ||
| $output->writeln('Moved <info>' . $count . '</info> users'); | ||
| } else { | ||
| $output->writeln('No users moved'); | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
| /** | ||
| * SPDX-FileCopyrightText: 2025 Robin Appelman <[email protected]> | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Files\Command\Object\Multi; | ||
|
|
||
| use OC\Core\Command\Base; | ||
| use OC\Files\ObjectStore\PrimaryObjectStoreConfig; | ||
| use OCP\IConfig; | ||
| use OCP\IUser; | ||
| use OCP\IUserManager; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Input\InputOption; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
|
|
||
| class Users extends Base { | ||
| public function __construct( | ||
| private readonly IUserManager $userManager, | ||
| private readonly PrimaryObjectStoreConfig $objectStoreConfig, | ||
| private readonly IConfig $config, | ||
| ) { | ||
| parent::__construct(); | ||
| } | ||
|
|
||
| protected function configure(): void { | ||
| parent::configure(); | ||
| $this | ||
| ->setName('files:object:multi:users') | ||
| ->setDescription('Get the mapping between users and object store buckets') | ||
| ->addOption('bucket', 'b', InputOption::VALUE_REQUIRED, 'Only list users using the specified bucket') | ||
| ->addOption('object-store', 'o', InputOption::VALUE_REQUIRED, 'Only list users using the specified object store configuration') | ||
| ->addOption('user', 'u', InputOption::VALUE_REQUIRED, 'Only show the mapping for the specified user, ignores all other options'); | ||
| } | ||
|
|
||
| public function execute(InputInterface $input, OutputInterface $output): int { | ||
| if ($userId = $input->getOption('user')) { | ||
| $user = $this->userManager->get($userId); | ||
| if (!$user) { | ||
| $output->writeln("<error>User $userId not found</error>"); | ||
| return 1; | ||
| } | ||
| $users = new \ArrayIterator([$user]); | ||
| } else { | ||
| $bucket = (string)$input->getOption('bucket'); | ||
| $objectStore = (string)$input->getOption('object-store'); | ||
| if ($bucket !== '' && $objectStore === '') { | ||
| $users = $this->getUsers($this->config->getUsersForUserValue('homeobjectstore', 'bucket', $bucket)); | ||
| } elseif ($bucket === '' && $objectStore !== '') { | ||
| $users = $this->getUsers($this->config->getUsersForUserValue('homeobjectstore', 'objectstore', $objectStore)); | ||
| } elseif ($bucket) { | ||
| $users = $this->getUsers(array_intersect( | ||
| $this->config->getUsersForUserValue('homeobjectstore', 'bucket', $bucket), | ||
| $this->config->getUsersForUserValue('homeobjectstore', 'objectstore', $objectStore) | ||
| )); | ||
| } else { | ||
| $users = $this->userManager->getSeenUsers(); | ||
| } | ||
| } | ||
|
|
||
| $this->writeStreamingTableInOutputFormat($input, $output, $this->infoForUsers($users), 100); | ||
| return 0; | ||
| } | ||
|
|
||
| /** | ||
| * @param string[] $userIds | ||
| * @return \Iterator<IUser> | ||
| */ | ||
| private function getUsers(array $userIds): \Iterator { | ||
| foreach ($userIds as $userId) { | ||
| $user = $this->userManager->get($userId); | ||
| if ($user) { | ||
| yield $user; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param \Iterator<IUser> $users | ||
| * @return \Iterator<array> | ||
| */ | ||
| private function infoForUsers(\Iterator $users): \Iterator { | ||
| foreach ($users as $user) { | ||
| yield $this->infoForUser($user); | ||
| } | ||
| } | ||
|
|
||
| private function infoForUser(IUser $user): array { | ||
| return [ | ||
| 'user' => $user->getUID(), | ||
| 'object-store' => $this->objectStoreConfig->getObjectStoreForUser($user), | ||
| 'bucket' => $this->objectStoreConfig->getSetBucketForUser($user) ?? 'unset', | ||
| ]; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
lib/private/Files/ObjectStore/InvalidObjectStoreConfigurationException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
| /** | ||
| * SPDX-FileCopyrightText: 2025 Robin Appelman <[email protected]> | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OC\Files\ObjectStore; | ||
|
|
||
| class InvalidObjectStoreConfigurationException extends \Exception { | ||
|
|
||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
That’s not an appid⚠️
What about using
core?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.
Having a separate "scope" makes it much easier to work with the values in bulk (see for example the query in the
renamecommand).Fwiw:
homeobjectstoreis already used as an appid for storing the bucket for the user.