From 57dcbe7eb620ffff1616e8a1f60110d45f1338c0 Mon Sep 17 00:00:00 2001 From: Oliver Tappe Date: Wed, 1 Aug 2007 18:27:48 +0000 Subject: * ConfigDB-objects now reuse active DB-connections in order to reduce the number of times any script asks for the db-password (and to reduce the number of used resources, too). * minor cleanup in slxconfig-demuxer git-svn-id: http://svn.openslx.org/svn/openslx/trunk@1302 95ad53e4-c205-0410-b2fa-d234c58c8868 --- config-db/OpenSLX/ConfigDB.pm | 275 ++++++++++++++++++++++-------------------- config-db/slxconfig-demuxer | 6 +- 2 files changed, 148 insertions(+), 133 deletions(-) (limited to 'config-db') diff --git a/config-db/OpenSLX/ConfigDB.pm b/config-db/OpenSLX/ConfigDB.pm index 061b560f..13aee505 100644 --- a/config-db/OpenSLX/ConfigDB.pm +++ b/config-db/OpenSLX/ConfigDB.pm @@ -42,95 +42,11 @@ my @supportExports = qw( @EXPORT_OK = (@supportExports); %EXPORT_TAGS = ('support' => [@supportExports],); -################################################################################ -### private stuff -################################################################################ use OpenSLX::Basics; use OpenSLX::DBSchema; use OpenSLX::Utils; -sub _checkAndUpgradeDBSchemaIfNecessary -{ - my $metaDB = shift; - - vlog(2, "trying to determine schema version..."); - my $currVersion = $metaDB->schemaFetchDBVersion(); - if (!defined $currVersion) { - # that's bad, someone has messed with our DB, as there is a - # database, but the 'meta'-table is empty. There might still - # be data in the other tables, but we have no way to find out - # which schema version they're in. So it's safer to give up: - croak _tr('Could not determine schema version of database'); - } - - if ($currVersion < $DbSchema->{version}) { - vlog(1, - _tr('Our schema-version is %s, DB is %s, upgrading DB...', - $DbSchema->{version}, $currVersion)); - foreach my $v (sort { $a <=> $b } keys %DbSchemaHistory) { - next if $v <= $currVersion; - my $changeSet = $DbSchemaHistory{$v}; - foreach my $c (0 .. scalar(@$changeSet) - 1) { - my $changeDescr = @{$changeSet}[$c]; - my $cmd = $changeDescr->{cmd}; - if ($cmd eq 'add-table') { - $metaDB->schemaAddTable( - $changeDescr->{'table'}, - $changeDescr->{'cols'}, - $changeDescr->{'vals'} - ); - } elsif ($cmd eq 'drop-table') { - $metaDB->schemaDropTable($changeDescr->{'table'}); - } elsif ($cmd eq 'rename-table') { - $metaDB->schemaRenameTable( - $changeDescr->{'old-table'}, - $changeDescr->{'new-table'}, - $changeDescr->{'cols'} - ); - } elsif ($cmd eq 'add-columns') { - $metaDB->schemaAddColumns( - $changeDescr->{'table'}, - $changeDescr->{'new-cols'}, - $changeDescr->{'new-default-vals'}, - $changeDescr->{'cols'} - ); - } elsif ($cmd eq 'drop-columns') { - $metaDB->schemaDropColumns( - $changeDescr->{'table'}, - $changeDescr->{'drop-cols'}, - $changeDescr->{'cols'} - ); - } elsif ($cmd eq 'rename-columns') { - $metaDB->schemaRenameColumns( - $changeDescr->{'table'}, - $changeDescr->{'col-renames'}, - $changeDescr->{'cols'} - ); - } else { - croak _tr('UnknownDbSchemaCommand', $cmd); - } - } - } - vlog(1, _tr('upgrade done')); - } else { - vlog(1, _tr('DB matches current schema version %s', $currVersion)); - } - return; -} - -sub _aref -{ # transparently converts the given reference to an array-ref - my $ref = shift; - return [] unless defined $ref; - $ref = [$ref] unless ref($ref) eq 'ARRAY'; - return $ref; -} - -sub _unique -{ # return given array filtered to unique elements - my %seenIDs; - return grep { !$seenIDs{$_}++; } @_; -} +our $configDBInstance; ################################################################################ ### data access interface @@ -138,8 +54,22 @@ sub _unique sub new { my $class = shift; - my $self = {}; - return bless $self, $class; + + if ($configDBInstance) { + return $configDBInstance; + } + + $configDBInstance = { + connectedCount => 0, + }; + return bless $configDBInstance, $class; +} + +sub DESTROY +{ + my $self = shift; + + $configDBInstance = undef; } sub connect ## no critic (ProhibitBuiltinHomonyms) @@ -149,53 +79,48 @@ sub connect ## no critic (ProhibitBuiltinHomonyms) # hash-ref with any additional info that might be required by # specific metadb-module (not used yet) - my $dbType = $openslxConfig{'db-type'}; - # name of underlying database module... - - # map db-type to name of module, such that the user doesn't have - # to type the correct case: - my %dbTypeMap = ( - 'mysql' => 'mysql', - 'sqlite' => 'SQLite', - ); - my $lcType = lc($dbType); - if (exists $dbTypeMap{$lcType}) { - $dbType = $dbTypeMap{$lcType}; - } - - my $dbModuleName = "OpenSLX/MetaDB/$dbType.pm"; - my $dbModule = "OpenSLX::MetaDB::$dbType"; - unless (eval { require $dbModuleName } ) { - if ($! == 2) { - die _tr( - "Unable to load DB-module <%s>\nthat database type is not supported (yet?)\n", - $dbModuleName - ); - } else { - die _tr("Unable to load DB-module <%s> (%s)\n", $dbModuleName, $@); + if (!$self->{connectedCount}) { + + my $dbType = $openslxConfig{'db-type'}; + # name of underlying database module... + + my $dbModuleName = "OpenSLX/MetaDB/$dbType.pm"; + my $dbModule = "OpenSLX::MetaDB::$dbType"; + unless (eval { require $dbModuleName } ) { + if ($! == 2) { + die _tr( + "Unable to load DB-module <%s>\nthat database type is not supported (yet?)\n", + $dbModuleName + ); + } else { + die _tr("Unable to load DB-module <%s> (%s)\n", $dbModuleName, $@); + } } - } - my $metaDB = $dbModule->new(); - if (!$metaDB->connect($dbParams)) { - warn _tr("Unable to connect to DB-module <%s>\n%s", $dbModuleName, $@); - warn _tr("These DB-modules seem to work ok:"); - foreach my $dbMod ('mysql', 'SQLite') { - my $fullDbModName = "DBD/$dbMod.pm"; - if (eval { require $fullDbModName }) { - vlog(0, "\t$dbMod\n"); + my $metaDB = $dbModule->new(); + if (!$metaDB->connect($dbParams)) { + warn _tr("Unable to connect to DB-module <%s>\n%s", $dbModuleName, $@); + warn _tr("These DB-modules seem to work ok:"); + foreach my $dbMod ('mysql', 'SQLite') { + my $fullDbModName = "DBD/$dbMod.pm"; + if (eval { require $fullDbModName }) { + vlog(0, "\t$dbMod\n"); + } } + die _tr( + 'Please use slxsettings if you want to switch to another db-type.'); } - die _tr( - 'Please use slxsettings if you want to switch to another db-type.'); - } - - $self->{'db-type'} = $dbType; - $self->{'meta-db'} = $metaDB; - foreach my $tk (keys %{$DbSchema->{tables}}) { - $metaDB->schemaDeclareTable($tk, $DbSchema->{tables}->{$tk}); + + $self->{'db-type'} = $dbType; + $self->{'meta-db'} = $metaDB; + foreach my $tk (keys %{$DbSchema->{tables}}) { + $metaDB->schemaDeclareTable($tk, $DbSchema->{tables}->{$tk}); + } + + _checkAndUpgradeDBSchemaIfNecessary($metaDB); } - _checkAndUpgradeDBSchemaIfNecessary($metaDB); + $self->{connectedCount}++; + return; } @@ -203,7 +128,11 @@ sub disconnect { my $self = shift; - $self->{'meta-db'}->disconnect(); + if ($self->{connectedCount} == 1) { + $self->{'meta-db'}->disconnect(); + } + $self->{connectedCount}--; + return; } @@ -1093,4 +1022,90 @@ sub generatePlaceholderFor return '@@@' . $varName . '@@@'; } +################################################################################ +### private stuff +################################################################################ +sub _checkAndUpgradeDBSchemaIfNecessary +{ + my $metaDB = shift; + + vlog(2, "trying to determine schema version..."); + my $currVersion = $metaDB->schemaFetchDBVersion(); + if (!defined $currVersion) { + # that's bad, someone has messed with our DB, as there is a + # database, but the 'meta'-table is empty. There might still + # be data in the other tables, but we have no way to find out + # which schema version they're in. So it's safer to give up: + croak _tr('Could not determine schema version of database'); + } + + if ($currVersion < $DbSchema->{version}) { + vlog(1, + _tr('Our schema-version is %s, DB is %s, upgrading DB...', + $DbSchema->{version}, $currVersion)); + foreach my $v (sort { $a <=> $b } keys %DbSchemaHistory) { + next if $v <= $currVersion; + my $changeSet = $DbSchemaHistory{$v}; + foreach my $c (0 .. scalar(@$changeSet) - 1) { + my $changeDescr = @{$changeSet}[$c]; + my $cmd = $changeDescr->{cmd}; + if ($cmd eq 'add-table') { + $metaDB->schemaAddTable( + $changeDescr->{'table'}, + $changeDescr->{'cols'}, + $changeDescr->{'vals'} + ); + } elsif ($cmd eq 'drop-table') { + $metaDB->schemaDropTable($changeDescr->{'table'}); + } elsif ($cmd eq 'rename-table') { + $metaDB->schemaRenameTable( + $changeDescr->{'old-table'}, + $changeDescr->{'new-table'}, + $changeDescr->{'cols'} + ); + } elsif ($cmd eq 'add-columns') { + $metaDB->schemaAddColumns( + $changeDescr->{'table'}, + $changeDescr->{'new-cols'}, + $changeDescr->{'new-default-vals'}, + $changeDescr->{'cols'} + ); + } elsif ($cmd eq 'drop-columns') { + $metaDB->schemaDropColumns( + $changeDescr->{'table'}, + $changeDescr->{'drop-cols'}, + $changeDescr->{'cols'} + ); + } elsif ($cmd eq 'rename-columns') { + $metaDB->schemaRenameColumns( + $changeDescr->{'table'}, + $changeDescr->{'col-renames'}, + $changeDescr->{'cols'} + ); + } else { + croak _tr('UnknownDbSchemaCommand', $cmd); + } + } + } + vlog(1, _tr('upgrade done')); + } else { + vlog(1, _tr('DB matches current schema version %s', $currVersion)); + } + return; +} + +sub _aref +{ # transparently converts the given reference to an array-ref + my $ref = shift; + return [] unless defined $ref; + $ref = [$ref] unless ref($ref) eq 'ARRAY'; + return $ref; +} + +sub _unique +{ # return given array filtered to unique elements + my %seenIDs; + return grep { !$seenIDs{$_}++; } @_; +} + 1; diff --git a/config-db/slxconfig-demuxer b/config-db/slxconfig-demuxer index c3220ec8..3720e4e4 100755 --- a/config-db/slxconfig-demuxer +++ b/config-db/slxconfig-demuxer @@ -423,12 +423,12 @@ sub generateInitalRamFS vlog(1, _tr('generating initialramfs %s/initramfs', $pxeVendorOSPath)); my $cmd = "$openslxConfig{'base-path'}/bin/mkdxsinitrd "; - if (length($info->{attr_ramfs_nicmods}) > 0) { + if ($info->{attr_ramfs_nicmods}) { $cmd .= qq[-n "$info->{attr_ramfs_nicmods}" ]; } - my $fsMods = $info->{attr_ramfs_fsmods}; + my $fsMods = $info->{attr_ramfs_fsmods} || ''; $fsMods .= ' ' . $osExportEngine->requiredFSMods(); - if (length($fsMods) > 0) { + if ($fsMods) { $cmd .= qq[-f "$fsMods" ]; } my $rootPath = "$openslxConfig{'private-path'}/stage1/$vendorOS->{name}"; -- cgit v1.2.3-55-g7522