summaryrefslogtreecommitdiffstats
path: root/os-plugins/OpenSLX/OSPlugin/Engine.pm
blob: 5db6f9b8a05e2b6d87def8254a00cabccccd7da3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# 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;