diff --git a/doc/80-Upgrading.md b/doc/80-Upgrading.md index d14ef096d..44f782d61 100644 --- a/doc/80-Upgrading.md +++ b/doc/80-Upgrading.md @@ -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 diff --git a/modules/migrate/application/clicommands/PreferencesCommand.php b/modules/migrate/application/clicommands/PreferencesCommand.php new file mode 100644 index 000000000..65f2b43da --- /dev/null +++ b/modules/migrate/application/clicommands/PreferencesCommand.php @@ -0,0 +1,85 @@ + 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'); + } + } +}