Merge pull request #4479 from Icinga/feature/add-preference-migration-command-4471
Add preference migration command
This commit is contained in:
commit
8fc1f8b016
|
@ -46,6 +46,9 @@ v2.6 to v2.8 requires to follow the instructions for v2.7 too.
|
|||
* The configuration backend type `INI` is not configurable anymore. **A database is now mandatory.**
|
||||
* Existing configurations using this configuration backend type will stop working with the
|
||||
release of v2.11.
|
||||
* To migrate your local user preferences to database, enable the `migrate` module and use the command
|
||||
`icingacli migrate preferences`. If you already setup the configuration database, it will work right
|
||||
away. If not, pass it the resource you'd like to use as configuration database with `--resource=`.
|
||||
* Note that this only applies to user preferences. Other configurations are still stored
|
||||
in `.ini` files. (#3770)
|
||||
* The Vagrant file and all its assets will be removed with version 2.11
|
||||
|
|
|
@ -0,0 +1,85 @@
|
|||
<?php
|
||||
/* Icinga Web 2 | (c) 2021 Icinga GmbH | GPLv2+ */
|
||||
|
||||
namespace Icinga\Module\Migrate\Clicommands;
|
||||
|
||||
use Icinga\Application\Config;
|
||||
use Icinga\Application\Logger;
|
||||
use Icinga\Cli\Command;
|
||||
use Icinga\Data\ConfigObject;
|
||||
use Icinga\Data\ResourceFactory;
|
||||
use Icinga\Exception\NotReadableError;
|
||||
use Icinga\Exception\NotWritableError;
|
||||
use Icinga\User;
|
||||
use Icinga\User\Preferences\Store\IniStore;
|
||||
use Icinga\User\Preferences\Store\DbStore;
|
||||
use Icinga\Util\DirectoryIterator;
|
||||
|
||||
class PreferencesCommand extends Command
|
||||
{
|
||||
/**
|
||||
* Migrate local INI user preferences to a database
|
||||
*
|
||||
* USAGE
|
||||
*
|
||||
* icingacli migrate preferences [options]
|
||||
*
|
||||
* OPTIONS:
|
||||
*
|
||||
* --resource=<resource-name> The resource to use. If not given, the current database config backend is used.
|
||||
*/
|
||||
public function indexAction()
|
||||
{
|
||||
$resource = Config::app()->get('global', 'config_resource');
|
||||
if (empty($resource)) {
|
||||
$resource = $this->params->getRequired('resource');
|
||||
}
|
||||
|
||||
$connection = ResourceFactory::create($resource);
|
||||
|
||||
$preferencesPath = Config::resolvePath('preferences');
|
||||
if (! file_exists($preferencesPath)) {
|
||||
Logger::info('There are no local user preferences to migrate');
|
||||
return;
|
||||
}
|
||||
|
||||
$rc = 0;
|
||||
|
||||
$preferenceDirs = new DirectoryIterator($preferencesPath);
|
||||
foreach ($preferenceDirs as $preferenceDir) {
|
||||
if (! is_dir($preferenceDir)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$userName = basename($preferenceDir);
|
||||
|
||||
Logger::info('Migrating INI preferences for user "%s" to database...', $userName);
|
||||
|
||||
$iniStore = new IniStore(new ConfigObject(['location' => $preferencesPath]), new User($userName));
|
||||
$dbStore = new DbStore(new ConfigObject(['connection' => $connection]), new User($userName));
|
||||
|
||||
try {
|
||||
$dbStore->load();
|
||||
$dbStore->save(new User\Preferences($iniStore->load()));
|
||||
} catch (NotReadableError $e) {
|
||||
if ($e->getPrevious() !== null) {
|
||||
Logger::error('%s: %s', $e->getMessage(), $e->getPrevious()->getMessage());
|
||||
} else {
|
||||
Logger::error($e->getMessage());
|
||||
}
|
||||
|
||||
$rc = 128;
|
||||
} catch (NotWritableError $e) {
|
||||
Logger::error('%s: %s', $e->getMessage(), $e->getPrevious()->getMessage());
|
||||
$rc = 256;
|
||||
}
|
||||
}
|
||||
|
||||
if ($rc > 0) {
|
||||
Logger::error('Failed to migrate some user preferences');
|
||||
exit($rc);
|
||||
} else {
|
||||
Logger::info('Successfully migrated all local user preferences to database');
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue