From 086ccfc2d34e8401486b97eb05ace116a14fe353 Mon Sep 17 00:00:00 2001 From: Oliver Tappe Date: Mon, 2 Apr 2007 13:36:40 +0000 Subject: * added support for removing an export (from disk and config-DB) git-svn-id: http://svn.openslx.org/svn/openslx/trunk@855 95ad53e4-c205-0410-b2fa-d234c58c8868 --- installer/OpenSLX/OSExport/Engine.pm | 177 ++++++++++++++------- installer/OpenSLX/OSExport/ExportType/Base.pm | 26 +-- .../OpenSLX/OSExport/ExportType/NBD_Squash.pm | 44 ++--- installer/OpenSLX/OSExport/ExportType/NFS.pm | 34 ++++ installer/slxos-export | 52 +++++- 5 files changed, 221 insertions(+), 112 deletions(-) diff --git a/installer/OpenSLX/OSExport/Engine.pm b/installer/OpenSLX/OSExport/Engine.pm index a6ab6640..03c4c224 100644 --- a/installer/OpenSLX/OSExport/Engine.pm +++ b/installer/OpenSLX/OSExport/Engine.pm @@ -97,6 +97,55 @@ sub initialize } } } + my $distro = accessDistro($distroName); + $distro->initialize($self); + $self->{distro} = $distro; + + # load module for the requested export type: + my $exporter = accessExporter($exportType); + $exporter->initialize($self); + $self->{'exporter'} = $exporter; + + # setup source and target paths: + $self->{'vendor-os-path'} + = "$openslxConfig{'stage1-path'}/$vendorOSName"; + $self->{'export-path'} + = "$openslxConfig{'export-path'}/$exportType/$vendorOSName"; + vlog 1, _tr("vendor-OS from '%s' will be exported to '%s'", + $self->{'vendor-os-path'}, $self->{'export-path'}); +} + +sub exportVendorOS +{ + my $self = shift; + + $self->{'exporter'}->exportVendorOS( + $self->{'vendor-os-path'}, + $self->{'export-path'} + ); + vlog 0, _tr("vendor-OS '%s' successfully exported to '%s'!", + $self->{'vendor-os-path'}, $self->{'export-path'}); + $self->addExportToConfigDB(); +} + +sub purgeExport +{ + my $self = shift; + + if ($self->{'exporter'}->purgeExport($self->{'export-path'})) { + vlog 0, _tr("export '%s' successfully removed!", + $self->{'export-path'}); + } + $self->removeExportFromConfigDB(); +} + +################################################################################ +### implementation methods +################################################################################ +sub accessDistro +{ + my $distroName = shift; + my $distroModule = "OpenSLX::OSExport::Distro::" .$supportedDistros{lc($distroName)}->{module}; @@ -112,11 +161,13 @@ sub initialize die _tr('Could not load module <%s> (Version <%s> required, but <%s> found)', $distroModule, 1.01, $modVersion); } - my $distro = $distroModule->new; - $distro->initialize($self); - $self->{distro} = $distro; + return $distroModule->new; +} + +sub accessExporter +{ + my $exportType = shift; - # load module for the requested export type: my $exportTypeModule = "OpenSLX::OSExport::ExportType::" .$supportedExportTypes{lc($exportType)}->{module}; @@ -132,37 +183,11 @@ sub initialize die _tr('Could not load module <%s> (Version <%s> required, but <%s> found)', $exportTypeModule, 1.01, $modVersion); } - my $exporter = $exportTypeModule->new; - $exporter->initialize($self); - $self->{'exporter'} = $exporter; - - # setup source and target paths: - $self->{'vendor-os-path'} - = "$openslxConfig{'stage1-path'}/$vendorOSName"; - $self->{'export-path'} - = "$openslxConfig{'export-path'}/$exportType/$vendorOSName"; - vlog 1, _tr("vendor-OS from '%s' will be exported to '%s'", - $self->{'vendor-os-path'}, $self->{'export-path'}); + return $exportTypeModule->new; } -sub exportVendorOS -{ - my $self = shift; - - $self->{'exporter'}->exportVendorOS( - $self->{'vendor-os-path'}, - $self->{'export-path'} - ); - $self->addExportToConfigDB(); -} - -################################################################################ -### implementation methods -################################################################################ -sub addExportToConfigDB +sub accessConfigDB { - my $self = shift; - my $configDBModule = "OpenSLX::ConfigDB"; unless (eval "require $configDBModule") { if ($! == 2) { @@ -176,40 +201,72 @@ sub addExportToConfigDB die _tr('Could not load module <%s> (Version <%s> required, but <%s> found)', $configDBModule, 1.01, $modVersion); } - my $openslxDB = $configDBModule->new(); - $openslxDB->connect(); + } + return $configDBModule->new(); +} + +sub addExportToConfigDB +{ + my $self = shift; - # insert new export if it doesn't already exist in DB: - my $exportName = $self->{'vendor-os-name'}; - my $export - = $openslxDB->fetchExportByFilter({ + my $openslxDB = accessConfigDB(); + $openslxDB->connect(); + + # insert new export if it doesn't already exist in DB: + my $exportName = $self->{'vendor-os-name'}; + my $export + = $openslxDB->fetchExportByFilter({ + 'name' => $exportName, + 'type' => $self->{'export-type'}, + }); + if (defined $export) { + vlog 0, _tr("No need to change export '%s' in OpenSLX-database.\n", + $exportName); + } else { + my $vendorOSName = $self->{'vendor-os-name'}; + my $vendorOS + = $openslxDB->fetchVendorOSByFilter({'name' => $vendorOSName}); + if (!defined $vendorOS) { + die _tr("vendor-OS '%s' could not be found in OpenSLX-database, giving up!\n", + $vendorOSName); + } + my $id = $openslxDB->addExport( + { + 'vendor_os_id' => $vendorOS->{id}, 'name' => $exportName, 'type' => $self->{'export-type'}, - }); - if (defined $export) { - vlog 0, _tr("No need to change export '%s' in OpenSLX-database.\n", - $exportName); - } else { - my $vendorOSName = $self->{'vendor-os-name'}; - my $vendorOS - = $openslxDB->fetchVendorOSByFilter({'name' => $vendorOSName}); - if (!defined $vendorOS) { - die _tr("vendor-OS '%s' could not be found in OpenSLX-database, giving up!\n", - $vendorOSName); } - my $id = $openslxDB->addExport( - { - 'vendor_os_id' => $vendorOS->{id}, - 'name' => $exportName, - 'type' => $self->{'export-type'}, - } - ); - vlog 0, _tr("Export <%s> has been added to DB (ID=%s).\n", - $exportName, $id); - } + ); + vlog 0, _tr("Export '%s' has been added to DB (ID=%s).\n", + $exportName, $id); + } - $openslxDB->disconnect(); + $openslxDB->disconnect(); +} + +sub removeExportFromConfigDB +{ + my $self = shift; + + my $openslxDB = accessConfigDB(); + $openslxDB->connect(); + + # remove export from DB: + my $exportName = $self->{'vendor-os-name'}; + my $export + = $openslxDB->fetchExportByFilter({ + 'name' => $exportName, + 'type' => $self->{'export-type'}, + }); + if (!defined $export) { + vlog 0, _tr("Export '%s' didn't exist in OpenSLX-database.\n", + $exportName); + } else { + $openslxDB->removeExport($export->{id}); + vlog 0, _tr("Export '%s' has been removed from DB.\n", $exportName); } + + $openslxDB->disconnect(); } 1; diff --git a/installer/OpenSLX/OSExport/ExportType/Base.pm b/installer/OpenSLX/OSExport/ExportType/Base.pm index 864a6ffd..54d1f7e4 100644 --- a/installer/OpenSLX/OSExport/ExportType/Base.pm +++ b/installer/OpenSLX/OSExport/ExportType/Base.pm @@ -42,31 +42,13 @@ sub exportVendorOS { } -################################################################################ -### implementation methods -################################################################################ -sub copyViaRsync +sub purgeExport { - my $self = shift; - my $source = shift; - my $target = shift; - - if (system("mkdir -p $target")) { - die _tr("unable to create directory '%s', giving up! (%s)\n", - $target, $!); - } - my $includeExcludeList = $self->determineIncludeExcludeList(); - vlog 1, _tr("using include-exclude-filter:\n%s\n", $includeExcludeList); - open(RSYNC, "| rsync -av --delete --exclude-from=- $source/ $target") - or die _tr("unable to start rsync for source '%s', giving up! (%s)", - $source, $!); - print RSYNC $includeExcludeList; - if (!close(RSYNC)) { - die _tr("unable to export to target '%s', giving up! (%s)", - $target, $!); - } } +################################################################################ +### implementation methods +################################################################################ sub determineIncludeExcludeList { my $self = shift; diff --git a/installer/OpenSLX/OSExport/ExportType/NBD_Squash.pm b/installer/OpenSLX/OSExport/ExportType/NBD_Squash.pm index dc6bc2ed..4b1e078c 100644 --- a/installer/OpenSLX/OSExport/ExportType/NBD_Squash.pm +++ b/installer/OpenSLX/OSExport/ExportType/NBD_Squash.pm @@ -41,28 +41,28 @@ sub exportVendorOS my $source = shift; my $target = shift; - # TODO: once the include/exclude-patch by Vito has been applied - # to mksquashfs, the extra route via rsync isn't necessary anymore: - my $mksquashfsCanFilter = 1; - if ($mksquashfsCanFilter) { - my $includeExcludeList = $self->determineIncludeExcludeList(); - # in order to do the filtering as part of mksquashfs, we need to map - # our internal (rsync-)filter format to regexes: - $includeExcludeList - = mapRsyncFilter2Regex($source, $includeExcludeList); - vlog 1, _tr("using include-exclude-filter:\n%s\n", $includeExcludeList); - $self->createSquashFS($source, $target, $includeExcludeList); - } else { - # do filtering via an rsync copy: - vlog 0, _tr("taking detour via rsync..."); - my $tmpTarget = "${target}_###RSYNC_TMP###"; - $self->copyViaRsync($source, $tmpTarget); - $self->createSquashFS($tmpTarget, $target); -# system("rm -r $tmpTarget"); - } + my $includeExcludeList = $self->determineIncludeExcludeList(); + # in order to do the filtering as part of mksquashfs, we need to map + # our internal (rsync-)filter format to regexes: + $includeExcludeList + = mapRsyncFilter2Regex($source, $includeExcludeList); + vlog 1, _tr("using include-exclude-filter:\n%s\n", $includeExcludeList); + $self->createSquashFS($source, $target, $includeExcludeList); $self->showNbdParams($target); } +sub purgeExport +{ + my $self = shift; + my $target = shift; + + if (system("rm $target")) { + vlog 0, _tr("unable to remove export '%s'!", $target); + return 0; + } + 1; +} + ################################################################################ ### implementation methods ################################################################################ @@ -74,7 +74,7 @@ sub createSquashFS my $target = shift; my $includeExcludeList = shift; - system("rm -rf $target"); + system("rm -f $target"); # mksquasfs isn't significantly faster if fs already exists, but it # causes the filesystem to grow somewhat, so we remove it in order to # get the smallest FS-file possible. @@ -94,8 +94,8 @@ sub createSquashFS unlink($filterFile); # ... remove filter file if done if ($res) { - die _tr("unable to create squashfs for source '%s into target '%s', giving up! ($!)", - $source, $target); + die _tr("unable to create squashfs for source '%s' as target '%s', giving up! (%s)", + $source, $target, $!); } } diff --git a/installer/OpenSLX/OSExport/ExportType/NFS.pm b/installer/OpenSLX/OSExport/ExportType/NFS.pm index 8bb8ae48..b25736b8 100644 --- a/installer/OpenSLX/OSExport/ExportType/NFS.pm +++ b/installer/OpenSLX/OSExport/ExportType/NFS.pm @@ -46,9 +46,43 @@ sub exportVendorOS $self->addTargetToNfsExports($target); } +sub purgeExport +{ + my $self = shift; + my $target = shift; + + if (system("rm -r $target")) { + vlog 0, _tr("unable to remove export '%s'!", $target); + return 0; + } + 1; +} + ################################################################################ ### implementation methods ################################################################################ +sub copyViaRsync +{ + my $self = shift; + my $source = shift; + my $target = shift; + + if (system("mkdir -p $target")) { + die _tr("unable to create directory '%s', giving up! (%s)\n", + $target, $!); + } + my $includeExcludeList = $self->determineIncludeExcludeList(); + vlog 1, _tr("using include-exclude-filter:\n%s\n", $includeExcludeList); + open(RSYNC, "| rsync -av --delete --exclude-from=- $source/ $target") + or die _tr("unable to start rsync for source '%s', giving up! (%s)", + $source, $!); + print RSYNC $includeExcludeList; + if (!close(RSYNC)) { + die _tr("unable to export to target '%s', giving up! (%s)", + $target, $!); + } +} + sub addTargetToNfsExports { my $self = shift; diff --git a/installer/slxos-export b/installer/slxos-export index 00a15c29..3b3da55e 100755 --- a/installer/slxos-export +++ b/installer/slxos-export @@ -86,7 +86,7 @@ if ($action =~ m[^list-ex]i) { } elsif ($action =~ m[^list-ty]i) { print _tr("List of supported export types:\n\t"); print join("\n\t", sort keys %supportedExportTypes)."\n"; -} elsif ($action =~ m[^export]i) { +} elsif ($action =~ m[^(add|export)]i) { if (scalar(@ARGV) != 2) { print STDERR _tr("You need to specify exactly one vendor-os-name and one export-type!\n"); pod2usage(2); @@ -107,12 +107,30 @@ if ($action =~ m[^list-ex]i) { $engine->{'vendor-os-path'}); } $engine->exportVendorOS(); +} elsif ($action =~ m[^remove]i) { + if (scalar(@ARGV) != 2) { + print STDERR _tr("You need to specify exactly one export-name and one export-type!\n"); + pod2usage(2); + } + my $exportName = shift @ARGV; + my $exportType = shift @ARGV; + + # we chdir into the script's folder such that all relative paths have + # a known starting point: + chdir($FindBin::RealBin) + or die _tr("can't chdir to script-path <%> (%s)", $FindBin::RealBin, $!); + + # create OSExport-engine for given export type and start it: + my $engine = OpenSLX::OSExport::Engine->new; + $engine->initialize($exportName, $exportType); + $engine->purgeExport(); } else { print STDERR _tr("You need to specify exactly one action: - export + add list-exported list-installed list-types + remove Try '%s --help' for more info.\n", $0); } @@ -140,9 +158,10 @@ slxos-export [options] =over 8 -=item B<< export >> +=item B<< add >> -exports the vendor-OS with the given name via the given export type +exports the vendor-OS with the given name using the given export type and +adds it to the config-DB, too =item B<< list-exported >> @@ -156,6 +175,11 @@ list all installed vendor-OSes list all supported export types +=item B<< remove >> + +removes the export with the given name and export type from disk and +config-DB + =back =head1 OPTIONS @@ -186,9 +210,9 @@ Prints the version and exits. =head3 Exporting a Vendor-OS via NFS -=item B<< slxos-export export suse-10.2 nfs >> +=item B<< slxos-export add suse-10.2 nfs >> -exports the installed vendor-OS suse-10.2 via nfs, the +Exports the installed vendor-OS suse-10.2 via nfs, the resulting NFS-export will live in C. =back @@ -197,11 +221,23 @@ resulting NFS-export will live in C. =over 8 -=item B<< slxos-export export ubuntu-6.10 nbd-squash >> +=item B<< slxos-export add ubuntu-6.10 nbd-squash >> -exports the installed vendor-OS ubuntu-6.10 via nbd-squash, the resulting +Exports the installed vendor-OS ubuntu-6.10 via nbd-squash, the resulting Squash-FS will live in C. =back +=head3 Removing an Export + +=over 8 + +=item B<< slxos-export remove ubuntu-6.10 nbd-squash >> + +Wipes the squash-FS of the export named 'ubuntu-6.10' from disk (i.e. the +file C will be deleted) and +removes that export from the config-DB, too. + +=back + =cut \ No newline at end of file -- cgit v1.2.3-55-g7522