diff options
author | Oliver Tappe | 2008-05-27 01:48:52 +0200 |
---|---|---|
committer | Oliver Tappe | 2008-05-27 01:48:52 +0200 |
commit | a1d960e4e71f8d9b5c525ba5876725b93378bec6 (patch) | |
tree | e883edc04e635f14647a9447664ffcba8800844e /config-db | |
parent | * added support to user provided themes (should live in /etc/opt/openslx/desk... (diff) | |
download | core-a1d960e4e71f8d9b5c525ba5876725b93378bec6.tar.gz core-a1d960e4e71f8d9b5c525ba5876725b93378bec6.tar.xz core-a1d960e4e71f8d9b5c525ba5876725b93378bec6.zip |
* refactored code for synchronization of attributes (moved from DBSchema to ConfigDB)
* When doing synchronization of attributes during a schema update, we now look for
any stale attributes in system_attr, client_attr, group_attr or installed_plugin_attr
and remove all those stale attributes automatically. Additionally, non-existent
plugins that are still installed in a vendorOS are removed from that, too.
* Updated DB-Schema to 0.31 in order to trigger this automatic cleaning process
git-svn-id: http://svn.openslx.org/svn/openslx/openslx/trunk@1805 95ad53e4-c205-0410-b2fa-d234c58c8868
Diffstat (limited to 'config-db')
-rw-r--r-- | config-db/OpenSLX/AttributeRoster.pm | 2 | ||||
-rw-r--r-- | config-db/OpenSLX/ConfigDB.pm | 174 | ||||
-rw-r--r-- | config-db/OpenSLX/DBSchema.pm | 44 | ||||
-rw-r--r-- | config-db/OpenSLX/MetaDB/DBI.pm | 65 |
4 files changed, 225 insertions, 60 deletions
diff --git a/config-db/OpenSLX/AttributeRoster.pm b/config-db/OpenSLX/AttributeRoster.pm index de7b5ced..2f7ca3dd 100644 --- a/config-db/OpenSLX/AttributeRoster.pm +++ b/config-db/OpenSLX/AttributeRoster.pm @@ -392,7 +392,7 @@ An hash-ref with info about all known attributes. sub getAttrInfo { my $class = shift; - my $params = shift; + my $params = shift || {}; $class->_init() if !%AttributeInfo; diff --git a/config-db/OpenSLX/ConfigDB.pm b/config-db/OpenSLX/ConfigDB.pm index c8ba7484..25f2833c 100644 --- a/config-db/OpenSLX/ConfigDB.pm +++ b/config-db/OpenSLX/ConfigDB.pm @@ -91,8 +91,10 @@ my @supportExports = qw( @EXPORT_OK = (@supportExports); %EXPORT_TAGS = ('support' => [@supportExports],); +use OpenSLX::AttributeRoster; use OpenSLX::Basics; use OpenSLX::DBSchema; +use OpenSLX::OSPlugin::Roster; use OpenSLX::Utils; =head1 Methods @@ -253,6 +255,176 @@ sub rollbackTransaction return 1; } +=item C<synchronizeAttributesWithDB()> + +Makes sure that all known attributes are referenced by the default system +(and no unknown ones). + +Additionally, all systems, groups and clients can be checked and get their +stale attributes removed, too. + +=cut + +sub synchronizeAttributesWithDB +{ + my $self = shift; + my $removeStaleRefs = shift; + + my $defaultSystem = $self->fetchSystemByID(0); + return if !$defaultSystem; + + # fetch all known attributes from attribute roster and merge these + # into the existing attributes of the default system + my $attrInfo = OpenSLX::AttributeRoster->getAttrInfo(); + + # add new attributes to default system + my @newAttrs + = grep { !exists $defaultSystem->{attrs}->{$_} } keys %{$attrInfo}; + foreach my $attr (@newAttrs) { + $defaultSystem->{attrs}->{$attr} = $attrInfo->{$attr}->{default}; + } + + # remove unknown attributes from default system + my @unknownAttrs + = grep { !exists $attrInfo->{$_} } keys %{$defaultSystem->{attrs}}; + foreach my $unknownAttr (@unknownAttrs) { + delete $defaultSystem->{attrs}->{$unknownAttr}; + } + + # now write back the updated default system if necessary + if (@newAttrs || @unknownAttrs) { + return if !$self->changeSystem(0, $defaultSystem); + } + + if ($removeStaleRefs) { + return if !$self->removeStaleSystemAttributes(); + return if !$self->removeStaleGroupAttributes(); + return if !$self->removeStaleClientAttributes(); + return if !$self->removeStaleVendorOSAttributes(); + } + + return 1; +} + +=item C<removeStaleSystemAttributes()> + +Removes any stale attributes from every system. + +=cut + +sub removeStaleSystemAttributes +{ + my $self = shift; + + my $attrInfo = OpenSLX::AttributeRoster->getAttrInfo(); + + my @systems = $self->fetchSystemByFilter(); + foreach my $system (@systems) { + my @unknownAttrs + = grep { !exists $attrInfo->{$_} } keys %{$system->{attrs}}; + if (@unknownAttrs) { + foreach my $unknownAttr (@unknownAttrs) { + delete $system->{attrs}->{$unknownAttr}; + } + return if !$self->changeSystem($system->{id}, $system); + } + } + + return 1; +} + +=item C<removeStaleGroupAttributes()> + +Removes any stale attributes from every group. + +=cut + +sub removeStaleGroupAttributes +{ + my $self = shift; + + my $attrInfo = OpenSLX::AttributeRoster->getAttrInfo(); + + my @groups = $self->fetchGroupByFilter(); + foreach my $group (@groups) { + my @unknownAttrs + = grep { !exists $attrInfo->{$_} } keys %{$group->{attrs}}; + if (@unknownAttrs) { + foreach my $unknownAttr (@unknownAttrs) { + delete $group->{attrs}->{$unknownAttr}; + } + return if !$self->changeGroup($group->{id}, $group); + } + } + + return 1; +} + +=item C<removeStaleClientAttributes()> + +Removes any stale attributes from every client. + +=cut + +sub removeStaleClientAttributes +{ + my $self = shift; + + my $attrInfo = OpenSLX::AttributeRoster->getAttrInfo(); + + my @clients = $self->fetchClientByFilter(); + foreach my $client (@clients) { + my @unknownAttrs + = grep { !exists $attrInfo->{$_} } keys %{$client->{attrs}}; + if (@unknownAttrs) { + foreach my $unknownAttr (@unknownAttrs) { + delete $client->{attrs}->{$unknownAttr}; + } + return if !$self->changeClient($client->{id}, $client); + } + } + + return 1; +} + +=item C<removeStaleVendorOSAttributes()> + +Removes any stale attributes from every vendor-OS. + +=cut + +sub removeStaleVendorOSAttributes +{ + my $self = shift; + + my @vendorOSes = $self->fetchVendorOSByFilter(); + foreach my $vendorOS (@vendorOSes) { + my @installedPlugins = $self->fetchInstalledPlugins($vendorOS->{id}); + foreach my $plugin (@installedPlugins) { + my $pluginName = $plugin->{plugin_name}; + my $attrInfo + = OpenSLX::OSPlugin::Roster->getPluginAttrInfo($pluginName); + if ($attrInfo) { + my @unknownAttrs + = grep { !exists $attrInfo->{$_} } keys %{$plugin->{attrs}}; + if (@unknownAttrs) { + foreach my $unknownAttr (@unknownAttrs) { + delete $plugin->{attrs}->{$unknownAttr}; + } + return if !$self->addInstalledPlugin( + $vendorOS->{id}, $pluginName, $plugin->{attrs} + ); + } + } + else { + $self->removeInstalledPlugin($vendorOS->{id}, $pluginName); + } + } + } + + return 1; +} + =back =head2 Data Access Methods @@ -1099,7 +1271,7 @@ sub addInstalledPlugin my $pluginAttrs = shift || {}; # make sure the attributes of this plugin are available via default system - $self->{'db-schema'}->synchronizeAttributesWithDefaultSystem($self); + $self->synchronizeAttributesWithDB(0); return $self->{'meta-db'}->addInstalledPlugin( $vendorOSID, $pluginName, $pluginAttrs diff --git a/config-db/OpenSLX/DBSchema.pm b/config-db/OpenSLX/DBSchema.pm index ee4c9c81..08ef5c60 100644 --- a/config-db/OpenSLX/DBSchema.pm +++ b/config-db/OpenSLX/DBSchema.pm @@ -16,7 +16,6 @@ package OpenSLX::DBSchema; use strict; use warnings; -use OpenSLX::AttributeRoster; use OpenSLX::Basics; ################################################################################ @@ -35,7 +34,7 @@ use OpenSLX::Basics; ### fk => foreign key (integer) ################################################################################ -my $VERSION = 0.30; +my $VERSION = 0.31; my $DbSchema = { 'version' => $VERSION, @@ -280,7 +279,8 @@ sub checkAndUpgradeDBSchemaIfNecessary ); } $metaDB->schemaSetDBVersion($DbSchema->{version}); - $self->synchronizeAttributesWithDefaultSystem($configDB); + $configDB->synchronizeAttributesWithDB(1) + or die _tr('unable to synchronize attributes with DB!'); vlog(1, _tr('DB has been created successfully')); } elsif ($currVersion < $DbSchema->{version}) { vlog( @@ -291,7 +291,8 @@ sub checkAndUpgradeDBSchemaIfNecessary ) ); $self->_schemaUpgradeDBFrom($metaDB, $currVersion); - $self->synchronizeAttributesWithDefaultSystem($configDB); + $configDB->synchronizeAttributesWithDB(1) + or die _tr('unable to synchronize attributes with DB!'); vlog(1, _tr('upgrade done')); } else { vlog(1, _tr('DB matches current schema version (%s)', $currVersion)); @@ -310,33 +311,6 @@ sub getColumnsOfTable @{$DbSchema->{tables}->{$tableName}->{cols}}; } -sub synchronizeAttributesWithDefaultSystem -{ - my $self = shift; - my $configDB = shift; - - my $defaultSystem = $configDB->fetchSystemByID(0); - return if !$defaultSystem; - - # fetch all known attributes from attribute roster and merge these - # into the existing attributes of the default system - my $attrInfo = OpenSLX::AttributeRoster->getAttrInfo(); - foreach my $attr (keys %$attrInfo) { - next if exists $defaultSystem->{attrs}->{$attr}; - $defaultSystem->{attrs}->{$attr} = $attrInfo->{$attr}->{default}; - } - - # remove unknown attributes from default system - my @unknownAttrs - = grep { !exists $attrInfo->{$_} } keys %{$defaultSystem->{attrs}}; - foreach my $unknownAttr (@unknownAttrs) { - delete $defaultSystem->{attrs}->{$unknownAttr}; - } - - # now write back the updated default system - return $configDB->changeSystem(0, $defaultSystem); -} - ################################################################################ ### ### methods for upgrading the DB schema @@ -733,6 +707,14 @@ sub _schemaUpgradeDBFrom return 1; }, + 0.31 => sub { + my $metaDB = shift; + + # dummy schema change, just to trigger the attribute synchronization + # again, as the respective code has been extended + + return 1; + }, ); 1; diff --git a/config-db/OpenSLX/MetaDB/DBI.pm b/config-db/OpenSLX/MetaDB/DBI.pm index 4961cdcc..7acb861f 100644 --- a/config-db/OpenSLX/MetaDB/DBI.pm +++ b/config-db/OpenSLX/MetaDB/DBI.pm @@ -726,7 +726,7 @@ sub addInstalledPlugin my $self = shift; my $vendorOSID = shift; my $pluginName = shift; - my $pluginAttrs = shift; + my $newAttrs = shift; return if !defined $vendorOSID || !$pluginName; @@ -741,30 +741,41 @@ sub addInstalledPlugin = $self->fetchInstalledPlugins($vendorOSID, $pluginName, 1); } return if !$installedPlugin; - for my $pluginAttrName (keys %$pluginAttrs) { - if (exists $installedPlugin->{attrs}->{$pluginAttrName}) { - my $attrInfo = $installedPlugin->{attrs}->{$pluginAttrName}; - my $currVal - = defined $attrInfo->{value} ? $attrInfo->{value} : '-'; - my $givenVal - = defined $pluginAttrs->{$pluginAttrName} - ? $pluginAttrs->{$pluginAttrName} - : '-'; - next if $currVal eq $givenVal; - return if ! $self->_doUpdate( - 'installed_plugin_attr', [ $attrInfo->{id} ], [ { - value => $pluginAttrs->{$pluginAttrName}, - } ] - ); - } - else { - return if ! $self->_doInsert('installed_plugin_attr', [ { - installed_plugin_id => $installedPlugin->{id}, - name => $pluginAttrName, - value => $pluginAttrs->{$pluginAttrName}, - } ] ); - } - } + + # determine the required attribute actions ... + my $oldAttrs = $installedPlugin->{attrs} || {}; + my @attrsToBeInserted + = grep { + exists $newAttrs->{$_} && !exists $oldAttrs->{$_} + } keys %$newAttrs; + my @attrsToBeDeleted = grep { !exists $newAttrs->{$_} } keys %$oldAttrs; + my @attrsToBeUpdated + = grep { + exists $newAttrs->{$_} && exists $oldAttrs->{$_} + && ($oldAttrs->{$_}->{value} || '-') ne ($newAttrs->{$_} || '-') + } keys %$newAttrs; + + # ... insert the new ones ... + my @attrData + = map { + { + installed_plugin_id => $installedPlugin->{id}, + name => $_, + value => $newAttrs->{$_}, + } + } + @attrsToBeInserted; + $self->_doInsert('installed_plugin_attr', \@attrData); + + # ... delete the old ones ... + my @oldIDs = map { $oldAttrs->{$_}->{id} } @attrsToBeDeleted; + $self->_doDelete('installed_plugin_attr', \@oldIDs); + + # ... and update the changed ones ... + my @IDs = map { $oldAttrs->{$_}->{id} } @attrsToBeUpdated; + @attrData = map { { value => $newAttrs->{$_} } } @attrsToBeUpdated; + $self->_doUpdate('installed_plugin_attr', \@IDs, \@attrData); + return 1; } @@ -1149,8 +1160,8 @@ sub setGroupAttrs = map { { group_id => $groupID, - name => $_, - value => $newAttrs->{$_}, + name => $_, + value => $newAttrs->{$_}, } } @attrsToBeInserted; |