summaryrefslogblamecommitdiffstats
path: root/os-plugins/OpenSLX/OSPlugin/Engine.pm
blob: 5db6f9b8a05e2b6d87def8254a00cabccccd7da3 (plain) (tree)




















                                                                               
               
















                                                                                





                                 
                                                  




                                                                        




                                                                                
 



                                                         
                 





                         





                                                                                













                                                                                   

                                                                                           





                                                                              



                                                                                          
                 


                                                                            

                                                                                         


                                  



                                                                                            
         



                                         










                               

                         




                                                                                
        

                                                                                           





                                                                              



                                                                                          
                 
        






                                                                                        
         



                                              

 



















                                                                                         







                                                                        

                           
                                   
 

                       









































                                                                                         
# Copyright (c) 2007 - OpenSLX GmbH
#
# This program is free software distributed under the GPL version 2.
# See http://openslx.org/COPYING
#
# If you have any feedback please consult http://openslx.org/feedback and
# send your suggestions, praise, or complaints to feedback@openslx.org
#
# General information about OpenSLX can be found at http://openslx.org/
# -----------------------------------------------------------------------------
# Engine.pm
#	- provides driver engine for the OSPlugin API.
# -----------------------------------------------------------------------------
package OpenSLX::OSPlugin::Engine;

use strict;
use warnings;

our $VERSION = 1.01;    # API-version . implementation-version

use File::Basename;
use File::Path;

use OpenSLX::Basics;
use OpenSLX::OSSetup::Engine;
use OpenSLX::Utils;

################################################################################
### interface methods
################################################################################
sub new
{
	my $class = shift;

	my $self = {};

	return bless $self, $class;
}

sub initialize
{
	my $self         = shift;
	my $pluginName   = shift;
	my $vendorOSName = shift;

	$self->{'vendor-os-name'} = $vendorOSName;

	$self->{'vendor-os-path'} 
		= "$openslxConfig{'private-path'}/stage1/$vendorOSName";
	vlog(1, "vendor-OS path is '$self->{'vendor-os-path'}'");

	if ($pluginName) {
		$self->{'plugin-name'} = $pluginName;
		$self->{'plugin-path'} 
			= "$openslxConfig{'base-path'}/lib/plugins/$pluginName";
		vlog(1, "plugin path is '$self->{'plugin-path'}'");

		$self->{'plugin'} = $self->_loadPlugin();
		return if !$self->{'plugin'};
	}
	
	return 1;
}

sub installPlugin
{
	my $self = shift;

	if ($self->{'vendor-os-name'} ne '<<<default>>>') {
		# create ossetup-engine for given vendor-OS:
		my $osSetupEngine = OpenSLX::OSSetup::Engine->new;
		$osSetupEngine->initialize($self->{'vendor-os-name'}, 'plugin');
		$self->{'os-setup-engine'} = $osSetupEngine;

		# bind mount openslx basepath to /mnt/openslx of vendor-OS:
		my $basePath 			= $openslxConfig{'base-path'};
		my $openslxPathInChroot = "$self->{'vendor-os-path'}/mnt/openslx";
		mkpath( [ $openslxPathInChroot ] );
		if (slxsystem("mount -o bind $basePath $openslxPathInChroot")) {
			croak(
				_tr(
					"unable to bind mount '%s' to '%s'! (%s)", 
					$basePath, $openslxPathInChroot, $!
				)
			);
		}

		# now let plugin install itself into vendor-OS
		my $chrootedPluginRepoPath 
			= "$openslxConfig{'base-path'}/plugin-repo/$self->{'plugin-name'}";
		my $pluginRepoPath 
			= "$self->{'vendor-os-path'}/$chrootedPluginRepoPath";
		my $chrootedPluginTempPath 
			= "/tmp/slx-plugin/$self->{'plugin-name'}";
		my $pluginTempPath 
			= "$self->{'vendor-os-path'}/$chrootedPluginTempPath";
		foreach my $path ($pluginRepoPath, $pluginTempPath) {
			if (slxsystem("mkdir -p $path")) {
				croak(_tr("unable to create path '%s'! (%s)", $path, $!));
			}
		}
		$self->{'os-setup-engine'}->callChrootedFunctionForVendorOS(
			sub {
				$self->{plugin}->installationPhase(
					$chrootedPluginRepoPath, $chrootedPluginTempPath,
					'/mnt/openslx',
				);
			}
		);

		if (slxsystem("umount $openslxPathInChroot")) {
			croak(_tr("unable to umount '%s'! (%s)", $openslxPathInChroot, $!));
		}
	}
	
	$self->_addInstalledPluginToDB();

	return 1;
}

sub getPlugin
{
	my $self = shift;

	return $self->{plugin};
}
	
sub removePlugin
{
	my $self = shift;

	if ($self->{'vendor-os-name'} ne '<<<default>>>') {
		# create ossetup-engine for given vendor-OS:
		my $osSetupEngine = OpenSLX::OSSetup::Engine->new;
		$osSetupEngine->initialize($self->{'vendor-os-name'}, 'plugin');
		$self->{'os-setup-engine'} = $osSetupEngine;
	
		my $chrootedPluginRepoPath 
			= "$openslxConfig{'base-path'}/plugin-repo/$self->{'plugin-name'}";
		my $pluginRepoPath 
			= "$self->{'vendor-os-path'}/$chrootedPluginRepoPath";
		my $chrootedPluginTempPath 
			= "/tmp/slx-plugin/$self->{'plugin-name'}";
		my $pluginTempPath 
			= "$self->{'vendor-os-path'}/$chrootedPluginTempPath";
		foreach my $path ($pluginRepoPath, $pluginTempPath) {
			if (slxsystem("mkdir -p $path")) {
				croak(_tr("unable to create path '%s'! (%s)", $path, $!));
			}
		}
	
		$self->{'os-setup-engine'}->callChrootedFunctionForVendorOS(
			sub {
				$self->{plugin}->removalPhase(
					$chrootedPluginRepoPath, $chrootedPluginTempPath
				);
			}
		);
	}
	
	$self->_removeInstalledPluginFromDB();

	return 1;
}

sub getInstalledPlugins
{
	my $self = shift;
	
	my $openslxDB = instantiateClass("OpenSLX::ConfigDB");
	$openslxDB->connect();
	my $vendorOS = $openslxDB->fetchVendorOSByFilter( { 
		name => $self->{'vendor-os-name'},
	} );
	if (!$vendorOS) {
		die _tr(
			'unable to find vendor-OS "%s" in DB!', $self->{'vendor-os-name'}
		);
	}
	my @installedPlugins = $openslxDB->fetchInstalledPlugins($vendorOS->{id});
	$openslxDB->disconnect();

	return @installedPlugins;
}

sub _loadPlugin
{
	my $self = shift;
	
	my $pluginModule = "OpenSLX::OSPlugin::$self->{'plugin-name'}";
	my $plugin = instantiateClass(
		$pluginModule, { pathToClass => $self->{'plugin-path'} }
	);
	return if !$plugin;

	$plugin->initialize($self);

	return $plugin;
}

sub _addInstalledPluginToDB
{
	my $self = shift;
	
	my $openslxDB = instantiateClass("OpenSLX::ConfigDB");
	$openslxDB->connect();
	my $vendorOS = $openslxDB->fetchVendorOSByFilter( { 
		name => $self->{'vendor-os-name'},
	} );
	if (!$vendorOS) {
		die _tr(
			'unable to find vendor-OS "%s" in DB!', $self->{'vendor-os-name'}
		);
	}
	$openslxDB->addInstalledPlugin($vendorOS->{id}, $self->{'plugin-name'});
	$openslxDB->disconnect();

	return 1;
}

sub _removeInstalledPluginFromDB
{
	my $self = shift;
	
	my $openslxDB = instantiateClass("OpenSLX::ConfigDB");
	$openslxDB->connect();
	my $vendorOS = $openslxDB->fetchVendorOSByFilter( { 
		name => $self->{'vendor-os-name'},
	} );
	if (!$vendorOS) {
		die _tr(
			'unable to find vendor-OS "%s" in DB!', $self->{'vendor-os-name'}
		);
	}
	$openslxDB->removeInstalledPlugin($vendorOS->{id}, $self->{'plugin-name'});
	$openslxDB->disconnect();

	return 1;
}

1;