From 4f6b63abf59c2478968506e0e779d0992323534f Mon Sep 17 00:00:00 2001 From: Oliver Tappe Date: Mon, 24 Mar 2008 10:41:16 +0000 Subject: * Switched from explicit cleanup functions to "resource acquisition by definition", implemented by the new ScopedResource class. This change improves robustness with respect to signals and unexpected errors and makes the code cleaner. git-svn-id: http://svn.openslx.org/svn/openslx/openslx/trunk@1678 95ad53e4-c205-0410-b2fa-d234c58c8868 --- installer/OpenSLX/OSSetup/Engine.pm | 75 ++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 31 deletions(-) (limited to 'installer/OpenSLX/OSSetup/Engine.pm') diff --git a/installer/OpenSLX/OSSetup/Engine.pm b/installer/OpenSLX/OSSetup/Engine.pm index 9518c74a..7fce8a59 100644 --- a/installer/OpenSLX/OSSetup/Engine.pm +++ b/installer/OpenSLX/OSSetup/Engine.pm @@ -31,6 +31,7 @@ use File::Basename; use URI; use OpenSLX::Basics; +use OpenSLX::ScopedResource; use OpenSLX::Syscall; use OpenSLX::Utils; @@ -117,8 +118,7 @@ use vars qw(%supportedDistros); }, ); -our $localHttpServerMasterPID; -our %localHttpServers; +my %localHttpServers; ################################################################################ ### interface methods @@ -132,22 +132,6 @@ sub new return bless $self, $class; } -sub DESTROY -{ - my $self = shift; - - my $httpServerPID = $localHttpServerMasterPID || '0'; - if ($httpServerPID == $$) { - # we are the master process, so we clean up all the servers that we - # have started: - while (my ($localURL, $serverInfo) = each %localHttpServers) { - vlog(1, _tr("stopping local HTTP-server for URL '%s'.", $localURL)); - kill TERM => $serverInfo->{pid}; - } - } - return; -} - sub initialize { my $self = shift; @@ -291,7 +275,11 @@ sub installVendorOS } $self->_createVendorOSPath(); - $self->_startLocalURLServersAsNeeded(); + my $httpServers = OpenSLX::ScopedResource->new({ + name => 'local-http-servers', + acquire => sub { $self->_startLocalURLServersAsNeeded(); 1 }, + release => sub { $self->_stopLocalURLServers(); 1 }, + }); my $baseSystemFile = "$self->{'vendor-os-path'}/.openslx-base-system"; if (-e $baseSystemFile) { @@ -436,7 +424,11 @@ sub updateVendorOS $self->{'vendor-os-path'}); } - $self->_startLocalURLServersAsNeeded(); + my $httpServers = OpenSLX::ScopedResource->new({ + name => 'local-http-servers', + acquire => sub { $self->_startLocalURLServersAsNeeded(); 1 }, + release => sub { $self->_stopLocalURLServers(); 1 }, + }); callInSubprocess( sub { @@ -467,7 +459,11 @@ sub startChrootedShellForVendorOS ); } - $self->_startLocalURLServersAsNeeded(); + my $httpServers = OpenSLX::ScopedResource->new({ + name => 'local-http-servers', + acquire => sub { $self->_startLocalURLServersAsNeeded(); 1 }, + release => sub { $self->_stopLocalURLServers(); 1 }, + }); callInSubprocess( sub { @@ -500,7 +496,11 @@ sub callChrootedFunctionForVendorOS ); } - $self->_startLocalURLServersAsNeeded(); + my $httpServers = OpenSLX::ScopedResource->new({ + name => 'local-http-servers', + acquire => sub { $self->_startLocalURLServersAsNeeded(); 1 }, + release => sub { $self->_stopLocalURLServers(); 1 }, + }); callInSubprocess( sub { @@ -1189,8 +1189,6 @@ sub _startLocalURLServersAsNeeded { my $self = shift; - $localHttpServerMasterPID ||= $$; - my $port = 5080; my %portForURL; foreach my $repoInfo (values %{$self->{'distro-info'}->{repository}}) { @@ -1220,6 +1218,16 @@ sub _startLocalURLServersAsNeeded return; } +sub _stopLocalURLServers +{ + my $self = shift; + + while (my ($localURL, $serverInfo) = each %localHttpServers) { + vlog(1, _tr("stopping local HTTP-server for URL '%s'.", $localURL)); + kill TERM => $serverInfo->{pid}; + } +} + sub _setupStage1A { my $self = shift; @@ -1582,15 +1590,20 @@ sub _callChrootedFunction 'updateConfig' => '?', }); - $self->{distro}->startSession($params->{chrootDir}); + my $distro = $self->{distro}; + my $distroSession = OpenSLX::ScopedResource->new({ + name => 'ossetup::distro::session', + acquire => sub { $distro->startSession($params->{chrootDir}); 1 }, + release => sub { $distro->finishSession(); 1 }, + }); - # invoke given function: - $params->{function}->(); + die $@ if ! eval { + # invoke given function: + $params->{function}->(); + $distro->updateDistroConfig() if $params->{updateConfig}; + 1; + }; - if ($params->{updateConfig}) { - $self->{'distro'}->updateDistroConfig(); - } - $self->{'distro'}->finishSession(); return; } -- cgit v1.2.3-55-g7522