summaryrefslogtreecommitdiffstats
path: root/os-plugins/OpenSLX/OSPlugin/Roster.pm
blob: 7bfed044aa09a1a3ae8dedd37bf349f055177eb9 (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
# 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/
# -----------------------------------------------------------------------------
# OSPlugin::Roster.pm
#    - provides information about all available plugins
# -----------------------------------------------------------------------------
package OpenSLX::OSPlugin::Roster;

use strict;
use warnings;

use OpenSLX::Basics;
use Clone qw(clone);

my %plugins;

=item C<getAvailablePlugins()>

Returns a hash that keys the names of available plugins to their info hash.

=cut

sub getAvailablePlugins
{
    my $class = shift;

    $class->_init() if !%plugins;

    my %pluginInfo;
    foreach my $pluginName (keys %plugins) {
        $pluginInfo{$pluginName} = $plugins{$pluginName}->getInfo();
    }
    return \%pluginInfo;
}

=item C<getPlugin()>

Returns an instance of the plugin with the given name

=cut

sub getPlugin
{
    my $class      = shift;
    my $pluginName = shift;

    $class->_init() if !%plugins;

    my $plugin = $plugins{$pluginName};
    return if !$plugin;

    return clone($plugin);
}

=item C<getPluginAttrInfo()>

Returns a hash that contains info about the attributes support by the 
given plugin

=cut

sub getPluginAttrInfo
{
    my $class      = shift;
    my $pluginName = shift;

    $class->_init() if !%plugins;

    return if !$plugins{$pluginName};

    return $plugins{$pluginName}->getAttrInfo();
}

=item C<addAllAttributesToHash()>

Fetches attribute info from all available plugins and adds it to the given 
hash-ref.

If a plugin name has been given, only the attributes of that plugin will be
added.

=over

=item Return Value

1

=back

=cut

sub addAllAttributesToHash
{
    my $class      = shift;
    my $attrInfo   = shift;
    my $pluginName = shift;

    return $class->_addAttributesToHash($attrInfo, $pluginName, sub { 1 } );
}

=item C<addAllStage1AttributesToHash()>

Fetches attribute info relevant for stage1 (i.e. vendor-OS-attributes) 
from all available plugins and adds it to the given hash-ref.

If a plugin name has been given, only the attributes of that plugin will be
added.

=over

=item Return Value

1

=back

=cut

sub addAllStage1AttributesToHash
{
    my $class      = shift;
    my $attrInfo   = shift;
    my $pluginName = shift;

    return $class->_addAttributesToHash($attrInfo, $pluginName, sub {
        my $attr = shift;
        return $attr->{applies_to_vendor_os};
    } );
}

=item C<addAllStage3AttributesToHash()>

Fetches attribute info relevant for stage3 (i.e. system- or client-attributes) 
from all available plugins and adds it to the given hash-ref.

If a plugin name has been given, only the attributes of that plugin will be
added.

=over

=item Return Value

1

=back

=cut

sub addAllStage3AttributesToHash
{
    my $class      = shift;
    my $attrInfo   = shift;
    my $pluginName = shift;

    return $class->_addAttributesToHash($attrInfo, $pluginName, sub {
        my $attr = shift;
        return $attr->{applies_to_systems} || $attr->{applies_to_clients};
    } );
}

sub _addAttributesToHash
{
    my $class      = shift;
    my $attrInfo   = shift;
    my $pluginName = shift;
    my $testFunc   = shift;

    $class->_init() if !%plugins;

    foreach my $plugin (values %plugins) {
        next if $pluginName && $plugin->{name} ne $pluginName;
        my $pluginAttrInfo = $plugin->getAttrInfo();
        foreach my $attr (keys %$pluginAttrInfo) {
            next if !$testFunc->($pluginAttrInfo->{$attr});
            $attrInfo->{$attr} = clone($pluginAttrInfo->{$attr});
        }
    }
    return 1;
}

sub _init
{
    my $class = shift;

    %plugins = ();
    my $pluginPath = "$openslxConfig{'base-path'}/lib/plugins";
    foreach my $modulePath (glob("$pluginPath/*")) {
        next if $modulePath !~ m{/([^/]+)$};
        my $pluginName = $1;
        if (!-e "$modulePath/OpenSLX/OSPlugin/$pluginName.pm") {
            vlog(
                1, 
                "skipped plugin-folder $modulePath as no corresponding perl "
                    . "module could be found."
            );
            next;
        }
        my $class = "OpenSLX::OSPlugin::$pluginName";
        vlog(2, "loading plugin $class from path '$modulePath'");
        my $plugin = instantiateClass($class, { pathToClass => $modulePath });
        $plugins{$pluginName} = $plugin;
    }
    return;
}

1;