summaryrefslogtreecommitdiffstats
path: root/src/os-plugins/OpenSLX/OSPlugin/Base.pm
blob: 2af0f04c0345ccf96b37d12800f2c7676c1a5c36 (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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
# 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/
# -----------------------------------------------------------------------------
# Base.pm
#    - provides empty base of the OpenSLX OSPlugin API.
# -----------------------------------------------------------------------------
package OpenSLX::OSPlugin::Base;

use strict;
use warnings;

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

=head1 NAME

OpenSLX::OSPlugin::Base - the base class for all OpenSLX OS-plugins.

=head1 DESCRIPTION

This class defines the OpenSLX API for OS-plugins.

The general idea behind OS-plugins is to extend any installed vendor-OS with
a specific features. Each feature is implemented as a separate, small software 
component in order to make them easy to understand and maintain. 

Since all of these software components are plugged into the OpenSLX system by 
means of a common API, we call them B<OS-plugin>s.

This API can be separated into different parts:

=over

=item - L</Declarative Interface> (provide info about a plugin)

=item - L</Vendor-OS Interface> (installing or removing a plugin into/from a 
vendor-OS)

=item - L</Initramfs Interface> (integrating a plugin into an initramfs)

=back

=head1 MORE INFO

Please read the user-level introduction on plugins in the OpenSLX-wiki:
L<http://openslx.org/trac/de/openslx/wiki/PluginKonzept> (in German).

If you'd like to know how a plugin is implemented, please have a look at the
'example' plugin, which contains some explainations and useful hints.

If you have any questions regarding the concept of OS-plugins and their
implementation, please drop a mail to: ot@openslx.com, or join the IRC-channel
'#openslx' (on freenode).

=cut

use Scalar::Util qw( weaken );

use OpenSLX::Basics;
use OpenSLX::OSPlugin::Roster;

=head1 PLUGIN API

=head2 Declarative Interface

=over

=item new()

Every plugin should provide a new-method and provide it's own name in the
'name' entry of $self. 

Please note that by convention, plugin names are all lowercase!

=cut

sub new
{
    confess "Creating OpenSLX::OSPlugin::Base-objects directly makes no sense!";
}

=item initialize()

Initializes basic context for this plugin (esp. a reference to the OSPlugin
engine that drives this plugin.

=cut

sub initialize
{
    my $self = shift;

    $self->{'os-plugin-engine'} = shift;
    $self->{'distro'}           = shift;

    weaken($self->{'os-plugin-engine'});
        # avoid circular reference between plugin and its engine
    
    return;
}

=item getInfo()

Returns a hash-ref with administrative information about this plugin (what does 
it do and how does it relate to other plugins). Every plugin needs to provide
this method and return the information about itself.

The returned hash-ref must include at least the following entries:

=over

=item B<description>

Explains the purpose of this plugins.

=item B<precedence>

Specifies the execution precedence of this plugin with respect to all other
plugins (plugins with lower precedences will be started before the ones with
a higher precedence).

Valid values range from 0-99. If your plugin does not have any requirements
in this context, just specify the default value '50'.

=item B<required>

Specifies the list of plugins that are required by this plugin.

Before any plugin can be installed, all other plugins that are required by it
must have been installed.

=back
    
=cut

sub getInfo
{
    my $self = shift;

    return {
        # a short (one-liner) description of this plugin
        description => '',
    };
}

=item getAttrInfo()

Returns a hash-ref with information about all attributes supported by this 
specific plugin. 

This default configuration will be added as attributes to the default system, 
such that it can be overruled for any specific system by means of B<slxconfig>.

The returned hash-ref must include at least the following entries:

=over

=item B<I<plugin-name>::active>

Indicates whether or not this plugin is active (1 for active, 0 for inactive).

=back

=cut

sub getAttrInfo
{
    my $self = shift;

    # This default configuration will be added as attributes to the default
    # system, such that it can be overruled for any specific system by means 
    # of slxconfig.
    return {
        # attribute 'active' is mandatory for all plugins
    };
}

=item getDefaultAttrsForVendorOS()

Returns a hash-ref with the default attribute values for the given vendor-OS.

=cut

sub getDefaultAttrsForVendorOS
{
    my $self = shift;

    # the default implementation does not change the default values at all:
    return $self->getAttrInfo();
}

=item checkStage3AttrValues()

Checks if the stage3 values given in B<$stage3Attrs> are allowed and make sense.

This method returns an array-ref of problems found. If there were no problems, 
this methods returns undef.

Plugins may override this implementation to do checks that for instance look
at the stage1 vendor-OS-attributes given in B<$vendorOSAttrs>.

N.B.: this method is called while being chrooted into the vendor-OS, so it
      may invoke all distro methods that expect to be run in this environment,
      too

=cut

sub checkStage3AttrValues
{
    my $self          = shift;
    my $stage3Attrs   = shift;
    my $vendorOSAttrs = shift;

    # this default implementation does no further checks (thus relying on the
    # attributte regex check that is done in the AttributeRoster)
    return;
}

=item dependsOnPlugin()

=cut

sub dependsOnPlugin
{
    my $self      = shift;
    my $otherName = shift;
    
    if (!defined $self->{dependsOn}) {
        my @dependsOn = $self->_determineAllPluginsWeDependOn();
        $self->{dependsOn} = \@dependsOn;
    }
    
    return grep { $_ eq $otherName } @{$self->{dependsOn}};
}

=back

=head2 Vendor-OS Interface

=over

=item installationPhase()

In this method, the plugin should install itself into the given vendor-OS.

What "installation" means is up to the plugin. Some plugins may just copy
a file from the OpenSLX host installation into the vendor-OS, while others may 
need to download files from the internet and/or install packages through the
vendor-OS' meta packager.

N.B.: This method is invoked while chrooted into the vendor-OS root.

The hash-ref given in B<$info> contains vital information for the installation
process:

=over

=item C<plugin-repo-path>

The folder where the stage1-plugin should store all files required by the 
corresponding stage3 runlevel script.

=item C<plugin-temp-path>

A temporary playground that will be cleaned up automatically.

=item C<openslx-base-path>

In order to make the OpenSLX files from the host available, the OpenSLX base 
folder (normally /opt/openslx) will be mounted into the chroot. 
So if you have to copy any files from the host, fetch them from this path.

=item C<openslx-config-path>

In order to make the OpenSLX config files from the host available, the OpenSLX 
config folder (normally /etc/opt/openslx) will be mounted into the chroot. 
So if you have to copy any config files from the host, fetch them from this 
path.

=item C<plugin-attrs>

Contains the attributes in effect for the installation of this plugin.

=back

=cut

sub installationPhase
{
    my $self = shift;
    my $info = shift;
    
    return;
}

=item removalPhase()

In this method, the plugin should remove itself from the given vendor-OS.

What "removal" means is up to the plugin. Some plugins may just delete
a file from the vendor-OS, while others may need to uninstall packages through 
the vendor-OS' meta packager.

N.B.: This method is invoked while chrooted into the vendor-OS root.

The hash-ref given in B<$info> contains vital information for the installation
process:

=over

=item C<plugin-repo-path>

The folder where the stage1-plugin should store all files required by the 
corresponding stage3 runlevel script.

=item C<plugin-temp-path>

A temporary playground that will be cleaned up automatically.

=item C<openslx-base-path>

In order to make the OpenSLX files from the host available, the OpenSLX base 
folder (normally /opt/openslx) will be mounted into the chroot. 
So if you have to copy any files from the host, fetch them from this path.

=item C<openslx-config-path>

In order to make the OpenSLX config files from the host available, the OpenSLX 
config folder (normally /etc/opt/openslx) will be mounted into the chroot. 
So if you have to copy any config files from the host, fetch them from this 
path.

=item C<plugin-attrs>

Contains the attributes in effect for the installation of this plugin.

=back

=cut

sub removalPhase
{
    my $self = shift;
    my $info = shift;
    
    return;
}

=item preInstallationPhase()

In this method, any preparations for installation of the plugin into a vendor-OS
should be executed. As this method is being called immediately before the chroot
is entered, this is the last/only chance to copy any files into the chroot that
are required from within (in installationPhase()).

The given parameters are similar to the ones for installationPhase(), except 
that all paths are now relative to the root-fs instead of being relative to the 
chroot (i.e. the paths are ready to be used from outside the chroot):

A "exit 1;" will result in a not installed plugin.

=over

=item C<plugin-repo-path>

The folder where the stage1-plugin should store all files required by the 
corresponding stage3 runlevel script.

=item C<plugin-temp-path>

A temporary playground that will be cleaned up automatically. 

If a plugin needs to unpack any archives, these archives should be copied to
this folder (as it will be cleaned automatically).

=item C<openslx-base-path>

In order to make the OpenSLX files from the host available, the OpenSLX base 
folder (normally /opt/openslx) will be mounted into the chroot. 
So if you have to copy any files from the host, fetch them from this path.

=item C<openslx-config-path>

In order to make the OpenSLX config files from the host available, the OpenSLX 
config folder (normally /etc/opt/openslx) will be mounted into the chroot. 
So if you have to copy any config files from the host, fetch them from this 
path.

=item C<plugin-attrs>

Contains the attributes in effect for the installation of this plugin.

=item C<vendor-os-path>

Contains the path to the vendor-OS into which the plugin will be installed.

=back

=cut

sub preInstallationPhase
{
    my $self = shift;
    my $info = shift;
    
    return;
}

=item postRemovalPhase()

In this method, any plugin has the chance to do any necessary cleanup that
must be executed outside of the chroot.

This method is invoked immediately after leaving the chroot into the vendor-OS 
root, but before the plugin-temp-path has been cleaned up. So if required, any
files could be copied out of the temp-path somewhere into the root-fs.

The given parameters are similar to the ones for removalPhase(), except that all
paths are now relative to the root-fs instead of being relative to the chroot
(i.e. the paths are ready to be used from outside the chroot):

=over

=item C<plugin-repo-path>

The folder where the stage1-plugin should store all files required by the 
corresponding stage3 runlevel script.

=item C<plugin-temp-path>

A temporary playground that will be cleaned up automatically.

=item C<openslx-base-path>

In order to make the OpenSLX files from the host available, the OpenSLX base 
folder (normally /opt/openslx) will be mounted into the chroot. 
So if you have to copy any files from the host, fetch them from this path.

=item C<openslx-config-path>

In order to make the OpenSLX config files from the host available, the OpenSLX 
config folder (normally /etc/opt/openslx) will be mounted into the chroot. 
So if you have to copy any config files from the host, fetch them from this 
path.

=item C<plugin-attrs>

Contains the attributes in effect for the installation of this plugin.

=item C<vendor-os-path>

Contains the path to the vendor-OS from which the plugin has been removed.

=back

=cut

sub postRemovalPhase
{
    my $self = shift;
    my $info = shift;
    
    return;
}

=back

=head2 Initramfs Interface

All of the following methods are invoked by the config demuxer when it makes an 
initramfs for a system that has this plugin activated. Through these methods,
each plugin can integrate itself into that initramfs.

=over

=item suggestAdditionalKernelParams()

Called in order to give the plugin a chance to add any kernel params it 
requires.

In order to do so, the plugin should return a list of additional kernel params 
that it would like to see added.

=cut

sub suggestAdditionalKernelParams
{
    my $self                = shift;
    my $makeInitRamFSEngine = shift;
    
    return;
}

=item suggestAdditionalKernelModules()

Called in order to give the plugin a chance to add any kernel modules it 
requires.

In order to do so, the plugin should return the names of additional kernel
modules that it would like to see added.
    
=cut

sub suggestAdditionalKernelModules
{
    my $self                = shift;
    my $makeInitRamFSEngine = shift;
    my $attrs               = shift;
    
    return;
}

=item copyRequiredFilesIntoInitramfs()

Called in order to give the plugin a chance to copy all required files from the 
vendor-OS into the initramfs.

N.B.: Only files that are indeed required by the initramfs should be copied 
here, i.e. files that are needed *before* the root-fs has been mounted. 
All other files should be taken from the root-fs instead!

=cut

sub copyRequiredFilesIntoInitramfs
{
    my $self                = shift;
    my $targetPath          = shift;
    my $attrs               = shift;
    my $makeInitRamFSEngine = shift;
    
    return;
}

=item setupPluginInInitramfs()

Called in order to let the plugin setup all the files it requires in the 
initramfs.

Normally, you don't need to override this method in your own plugin,
as it is usually enough to override suggestAdditionalKernelParams(),
suggestAdditionalKernelModules() and maybe copyRequiredFilesIntoInitramfs().

=cut

sub setupPluginInInitramfs
{
    my $self                = shift;
    my $attrs               = shift;
    my $makeInitRamFSEngine = shift;

    my $pluginName      = $self->{name};
    my $pluginSrcPath   = "$openslxConfig{'base-path'}/lib/plugins";
    my $buildPath       = $makeInitRamFSEngine->{'build-path'};
    my $pluginInitdPath = "$buildPath/etc/plugin-init.d";
    my $initHooksPath   = "$buildPath/etc/init-hooks";

    # copy runlevel script
    my $precedence = sprintf('%02d', $self->getInfo()->{precedence});
    my $scriptName = "$pluginSrcPath/$pluginName/XX_${pluginName}.sh";
    my $targetName = "$pluginInitdPath/${precedence}_${pluginName}.sh";
    if (-e $scriptName) {
        $makeInitRamFSEngine->addCMD("cp $scriptName $targetName");
        $makeInitRamFSEngine->addCMD("chmod a+x $targetName");
    }

    # copy init hook scripts, if any
    if (-d "$pluginSrcPath/$pluginName/init-hooks") {
        my $hookSrcPath = "$pluginSrcPath/$pluginName/init-hooks";
        $makeInitRamFSEngine->addCMD(
            "cp -r $hookSrcPath/* $buildPath/etc/init-hooks/"
        );
    }

    # invoke hook methods to suggest additional kernel params ...
    my @suggestedParams 
        = $self->suggestAdditionalKernelParams($makeInitRamFSEngine);
    if (@suggestedParams) {
        my $params = join ' ', @suggestedParams;
        vlog(1, "plugin $pluginName suggests these kernel params: $params");
        $makeInitRamFSEngine->addKernelParams(@suggestedParams);
    }

    # ... and kernel modules
    my @suggestedModules 
        = $self->suggestAdditionalKernelModules($makeInitRamFSEngine, $attrs);
    if (@suggestedModules) {
        my $modules = join(',', @suggestedModules);
        vlog(1, "plugin $pluginName suggests these kernel modules: $modules");
        $makeInitRamFSEngine->addKernelModules(@suggestedModules);
    }

    # invoke hook method to copy any further files that are required in stage3
    # before the root-fs has been mounted
    $self->copyRequiredFilesIntoInitramfs(
        $buildPath, $attrs, $makeInitRamFSEngine
    );

    return 1;
}

sub _determineAllPluginsWeDependOn
{
    my $self = shift;
    my $seen = shift || {};

    return if $seen->{$self->{name}};
    $seen->{$self->{name}} = 1;

    my %dependsOn;
    if ($self->getInfo()->{required}) {
        @dependsOn{@{$self->getInfo()->{required}}} = ();
    }

    foreach my $depName (keys %dependsOn) {
        my $depPlugin = OpenSLX::OSPlugin::Roster->getPlugin($depName);
        my @subDeps = $depPlugin->_determineAllPluginsWeDependOn($seen);
        @dependsOn{@subDeps} = ();
    }

    return keys %dependsOn;
}

=back

1;