summaryrefslogtreecommitdiffstats
path: root/installer/OpenSLX/OSExport/Engine.pm
blob: 03c4c22467e14f7758cd7c398f3b01e3f0ee28dd (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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# Copyright (c) 2006, 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 OSExport API.
# -----------------------------------------------------------------------------
package OpenSLX::OSExport::Engine;

use vars qw(@ISA @EXPORT $VERSION);
$VERSION = 1.01;		# API-version . implementation-version

use Exporter;
@ISA = qw(Exporter);

@EXPORT = qw(
	%supportedExportTypes %supportedDistros
);

use strict;
use Carp;
use File::Basename;
use OpenSLX::Basics;

use vars qw(%supportedExportTypes %supportedDistros);

%supportedExportTypes = (
	'nfs'
		=> { module => 'NFS' },
	'nbd-squash'
		=> { module => 'NBD_Squash' },
);

%supportedDistros = (
	'<any>'
		=> { module => 'Any' },
	'debian'
		=> { module => 'Debian' },
	'fedora'
		=> { module => 'Fedora' },
	'gentoo'
		=> { module => 'Gentoo' },
	'suse'
		=> { module => 'SUSE' },
	'ubuntu'
		=> { module => 'Ubuntu' },
);

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

	my $self = {
	};

	return bless $self, $class;
}

sub initialize
{
	my $self = shift;
	my $vendorOSName = shift;
	my $exportType = lc(shift);

	if (!exists $supportedExportTypes{lc($exportType)}) {
		print _tr("Sorry, export type '%s' is unsupported.\n", $exportType);
		print _tr("List of supported export types:\n\t");
		print join("\n\t", sort keys %supportedExportTypes)."\n";
		exit 1;
	}

	$self->{'vendor-os-name'} = $vendorOSName;
	$self->{'export-type'} = $exportType;
	$vendorOSName =~ m[^(.+?\-[^-]+)];
	my $distroName = $1;
	$self->{'distro-name'} = $distroName;

	# load module for the requested distro:
	if (!exists $supportedDistros{lc($distroName)}) {
		# try without _x86_64:
		$distroName =~ s[_x86_64$][];
		if (!exists $supportedDistros{lc($distroName)}) {
			# try basic distro-type (e.g. debian or suse):
			$distroName =~ s[-.+$][];
			if (!exists $supportedDistros{lc($distroName)}) {
				# fallback to generic implementation:
				$distroName = '<any>';
			}
		}
	}
	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};
	unless (eval "require $distroModule") {
		if ($! == 2) {
			die _tr("Distro-module <%s> not found!\n", $distroModule);
		} else {
			die _tr("Unable to load distro-module <%s> (%s)\n", $distroModule, $@);
		}
	}
	my $modVersion = $distroModule->VERSION;
	if ($modVersion < 1.01) {
		die _tr('Could not load module <%s> (Version <%s> required, but <%s> found)',
				$distroModule, 1.01, $modVersion);
	}
	return $distroModule->new;
}

sub accessExporter
{
	my $exportType = shift;

	my $exportTypeModule
		= "OpenSLX::OSExport::ExportType::"
			.$supportedExportTypes{lc($exportType)}->{module};
	unless (eval "require $exportTypeModule") {
		if ($! == 2) {
			die _tr("Export-type-module <%s> not found!\n", $exportTypeModule);
		} else {
			die _tr("Unable to load export-type-module <%s> (%s)\n", $exportTypeModule, $@);
		}
	}
	my $modVersion = $exportTypeModule->VERSION;
	if ($modVersion < 1.01) {
		die _tr('Could not load module <%s> (Version <%s> required, but <%s> found)',
				$exportTypeModule, 1.01, $modVersion);
	}
	return $exportTypeModule->new;
}

sub accessConfigDB
{
	my $configDBModule = "OpenSLX::ConfigDB";
	unless (eval "require $configDBModule") {
		if ($! == 2) {
			vlog 1, _tr("ConfigDB-module not found, unable to access OpenSLX-database.\n");
		} else {
			die _tr("Unable to load ConfigDB-module <%s> (%s)\n", $configDBModule, $@);
		}
	} else {
		my $modVersion = $configDBModule->VERSION;
		if ($modVersion < 1.01) {
			die _tr('Could not load module <%s> (Version <%s> required, but <%s> found)',
					$configDBModule, 1.01, $modVersion);
		}
	}
	return $configDBModule->new();
}

sub addExportToConfigDB
{
	my $self = shift;

	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'},
			}
		);
		vlog 0, _tr("Export '%s' has been added to DB (ID=%s).\n",
					$exportName, $id);
	}

	$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;
################################################################################

=pod

=head1 NAME

OpenSLX::OSExport::Engine -

=head1 SYNOPSIS

=head1 DESCRIPTION

...

=cut