summaryrefslogtreecommitdiffstats
path: root/os-plugins/OpenSLX/OSPlugin/Engine.pm
blob: 5469a98d66d7a89042ad0e1534fdc711cba2a2c4 (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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
# 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 Storable;

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

=head1 NAME

OpenSLX::OSPlugin::Engine - driver class for plugin handling.

=head1 DESCRIPTION

This class works as a driver for the installation/removal of plugins
into/from a vendor.

Additionally, it provides the OS-Plugin support interface.

=head1 PUBLIC METHODS

=over

=item new()

Trivial constructor

=cut

sub new
{
    my $class = shift;

    my $self = {};

    return bless $self, $class;
}

=item initialize($pluginName, $vendorOSName )

Sets up basic data (I<$pluginName> and I<$vendorOSName>) as well as paths and 
loads plugin.

=cut

sub initialize
{
    my $self         = shift;
    my $pluginName   = shift;
    my $vendorOSName = shift;
    my $givenAttrs   = 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'}'");

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

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

        $self->{'chrooted-plugin-repo-path'}
            = "$openslxConfig{'base-path'}/plugin-repo/$self->{'plugin-name'}";
        $self->{'plugin-repo-path'}
            = "$self->{'vendor-os-path'}/$self->{'chrooted-plugin-repo-path'}";
        $self->{'chrooted-plugin-temp-path'}
            = "/tmp/slx-plugin/$self->{'plugin-name'}";
        $self->{'plugin-temp-path'}
            = "$self->{'vendor-os-path'}/$self->{'chrooted-plugin-temp-path'}";
        $self->{'chrooted-openslx-base-path'} = '/mnt/openslx';

        # check and store given attribute set
        my $knownAttrs = $self->{plugin}->getAttrInfo();
        my @unknownAttrs 
            = grep { !exists $knownAttrs->{$_} } keys %$givenAttrs;
        if (@unknownAttrs) {
            die _tr(
                "The plugin '%s' does not support these attributes:\n\t%s",
                $pluginName, join(',', @unknownAttrs)
            );
        }

        # merge attributes that were given on cmdline with the ones that
        # already exist in the DB and finally with the default values
        $self->{'plugin-attrs'} = { %$givenAttrs };
        my $defaultAttrs = $self->{plugin}->getDefaultAttrsForVendorOS(
            $vendorOSName
        );
        my $dbAttrs = $self->_fetchInstalledPluginAttrs($vendorOSName);
        for my $attrName (keys %$defaultAttrs) {
            next if exists $givenAttrs->{$attrName};
            $self->{'plugin-attrs'}->{$attrName}
                = exists $dbAttrs->{$attrName}
                    ? $dbAttrs->{$attrName}
                    : $defaultAttrs->{$attrName}->{default};
        }
    }
    
    return 1;
}

=back

=head2 Driver Interface

The following methods are invoked by the slxos-plugin script in order to
install/remove a plugin into/from a vendor-OS:

=over

=item installPlugin()

Creates an ossetup-engine for the current vendor-OS and asks that to invoke
the plugin's installer method while chrooted into that vendor-OS.

=cut

sub installPlugin
{
    my $self = shift;

    if ($self->{'vendor-os-name'} ne '<<<default>>>') {

        # as the attrs may be changed by the plugin during installation, we
        # have to find a way to pass them back to this process (remember;
        # installation takes place in a forked process in order to do a chroot).
        # We simply serialize the attributes into a temp and deserialize it
        # in the calling process.
        my $serializedAttrsFile 
            = "$self->{'plugin-temp-path'}/serialized-attrs";
        my $chrootedSerializedAttrsFile 
            = "$self->{'chrooted-plugin-temp-path'}/serialized-attrs";
    
        mkpath([ $self->{'plugin-repo-path'}, $self->{'plugin-temp-path'} ]);
    
        # HACK: do a dummy serialization here in order to get Storable 
        # completely loaded (otherwise it will complain in the chroot about 
        # missing modules).
        store $self->{'plugin-attrs'}, $serializedAttrsFile;
    
        $self->_callChrootedFunctionForPlugin(
            sub {
                # invoke plugin and let it install itself into vendor-OS
                $self->{plugin}->installationPhase(
                    $self->{'chrooted-plugin-repo-path'}, 
                    $self->{'chrooted-plugin-temp-path'},
                    $self->{'chrooted-openslx-base-path'},
                    $self->{'plugin-attrs'},
                );

                # serialize possibly changed attributes (executed inside chroot)
                store $self->{'plugin-attrs'}, $chrootedSerializedAttrsFile;
            }
        );

        # now retrieve (deserialize) the current attributes and store them
        $self->{'plugin-attrs'} = retrieve $serializedAttrsFile;
        $self->_addInstalledPluginToDB();
    
        # cleanup temp path
        rmtree([ $self->{'plugin-temp-path'} ]);
    }
    
    return 1;
}

=item removePlugin()

Creates an ossetup-engine for the current vendor-OS and asks that to invoke
the plugin's removal method while chrooted into that vendor-OS.

=cut

sub removePlugin
{
    my $self = shift;

    if ($self->{'vendor-os-name'} ne '<<<default>>>') {

        mkpath([ $self->{'plugin-repo-path'}, $self->{'plugin-temp-path'} ]);

        $self->_callChrootedFunctionForPlugin(
            sub {
                $self->{plugin}->removalPhase(
                    $self->{'chrooted-plugin-repo-path'}, 
                    $self->{'chrooted-plugin-temp-path'},
                    $self->{'chrooted-openslx-base-path'},
                );
            }
        );

        rmtree([ $self->{'plugin-temp-path'} ]);
    }
    
    $self->_removeInstalledPluginFromDB();

    return 1;
}

=item getInstalledPlugins()

Returns the list of names of the plugins that are installed into the current
vendor-OS.

=cut

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;
}

=back

=head2 Support Interface

This is the plugin support interface for OS-plugins, which represents the 
connection between a plugin's implementation and the rest of the OpenSLX system.

Plugin implementations are meant to use this interface in order to find
out details about the current vendor-OS or download files or install packages.

=over

=item vendorOSName()

Returns the name of the current vendor-OS.

=cut

sub vendorOSName
{
    my $self = shift;

    return $self->{'vendor-os-name'};
}

=item distroName()

Returns the name of the distro that the current vendor-OS is based on.

Each distro name always consists of the distro type, a dash and the
distro version, like 'suse-10.2' or 'ubuntu-7.04'.

=cut

sub distroName
{
    my $self = shift;

    return $self->{'ossetup-engine'}->distroName();
}

=item downloadFile($fileURL, $targetPath, $wgetOptions)

Invokes busybox's wget to download a file from the given URL.

=over

=item I<$fileURL>

The URL of the file to download.

=item I<$targetPath> [optional]

The directory where the file should be downloaded into. The default is the
current plugin's temp directory.

=item I<$wgetOptions> [optional]

Any other options you'd like to pass to wget.

=item I<Return Value>

If the downloaded was successful this method returns C<1>, otherwise it dies.

=back

=cut

sub downloadFile
{
    my $self        = shift;
    my $fileURL     = shift || return;
    my $targetPath  = shift || $self->{'chrooted-plugin-temp-path'};
    my $wgetOptions = shift || '';

    my $busybox = $self->{'ossetup-engine'}->busyboxBinary();
    
    if (slxsystem("$busybox wget -P $targetPath $wgetOptions $fileURL")) {
        die _tr('unable to download file "%s"! (%s)', $fileURL, $!);
    }

    return 1;
}

=item getInstalledPackages()

Returns the list of names of the packages (as an array) that are already 
installed in the vendor-OS. 
Useful if a plugin wants to find out whether or not it has to 
install additional packages.

=cut

sub getInstalledPackages
{
    my $self = shift;

    my $metaPackager = $self->{'ossetup-engine'}->metaPackager();
    return if !$metaPackager;

    return $metaPackager->getInstalledPackages();
}

=item getInstallablePackagesForSelection()

Looks at the selection with the given name and returns the list of names of the 
packages (as one string separated by spaces) that need to be installed in order 
to complete the selection.

=cut

sub getInstallablePackagesForSelection
{
    my $self      = shift;
    my $selection = shift;

    return $self->{'ossetup-engine'}->getInstallablePackagesForSelection(
        $selection
    );
}


=item installPackages($packages)

Installs the given packages into the vendor-OS.

N.B: Since this method uses the meta-packager of the vendor-OS, package 
dependencies will be determined and solved automatically.

=over

=item I<$packages>

Contains a list of package names (separated by spaces) that shall be installed.

=item I<Return Value>

If the packages have been installed successfully this method return 1,
otherwise it dies.

=back

=cut

sub installPackages
{
    my $self     = shift;
    my $packages = shift;

    return if !$packages;

    my $metaPackager = $self->{'ossetup-engine'}->metaPackager();
    return if !$metaPackager;

    return $metaPackager->installPackages($packages);
}

=item removePackages($packages)

Removes the given packages from the vendor-OS.

=over

=item I<$packages> [ARRAY-ref]

Contains a list of package names (separated by spaces) that shall be removed.

=item I<Return Value>

If the packages have been removed successfully this method return 1,
otherwise it dies.

=back

=cut

sub removePackages
{
    my $self     = shift;
    my $packages = shift;

    return if !$packages;

    my $metaPackager = $self->{'ossetup-engine'}->metaPackager();
    return if !$metaPackager;

    return $metaPackager->removePackages($packages);
}

=back

=cut

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

    # if there's a distro folder, instantiate the most appropriate distro class
    my $distro;
    if (-d "$self->{'plugin-path'}/OpenSLX/Distro") {
        unshift @INC, $self->{'plugin-path'};
        my $distroName = $self->distroName();
        $distroName =~ tr{.-}{__};
        my @distroModules;
        while($distroName =~ m{^(.+)_[^_]*$}) {
            push @distroModules, $distroName;
            $distroName = $1;
        }
        push @distroModules, $distroName;
        push @distroModules, 'Base';
        for my $distroModule (@distroModules) {
            last if eval {
                $distro = instantiateClass(
                    'OpenSLX::Distro::' . $distroModule, 
                    { pathToClass => $self->{'plugin-path'} }
                );
                1;
            };
        }
        shift @INC;
        if (!$distro) {
            die _tr(
                'unable to load any distro module for vendor-OS %s',
                $self->{'vendor-os-name'}
            );
        }
        $distro->initialize($self);
    }

    $plugin->initialize($self, $distro);

    return $plugin;
}

sub _callChrootedFunctionForPlugin
{
    my $self     = shift;
    my $function = shift;

    # 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 $ok = eval {
        $self->{'ossetup-engine'}->callChrootedFunctionForVendorOS($function);
    };

    if (slxsystem("umount $openslxPathInChroot")) {
        croak(_tr("unable to umount '%s'! (%s)", $openslxPathInChroot, $!));
    }
    
    if (!$ok) {
        die $@;
    }
    
    return;
}

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'}, $self->{'plugin-attrs'}
    );
    $openslxDB->disconnect();

    return 1;
}

sub _fetchInstalledPluginAttrs
{
    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 $installedPlugin = $openslxDB->fetchInstalledPlugins(
        $vendorOS->{id}, $self->{'plugin-name'}
    );
    $openslxDB->disconnect();

    return {} if !$installedPlugin;
    return $installedPlugin->{attrs};
}

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;