source: LATMOS-Accounts/lib/LATMOS/Accounts.pm @ 400

Last change on this file since 400 was 320, checked in by nanardon, 15 years ago
  • implement connect() and acls in Bases
  • Property svn:keywords set to Id Rev
File size: 4.9 KB
Line 
1package LATMOS::Accounts;
2
3use 5.010000;
4use strict;
5use warnings;
6use base qw(Config::IniFiles);
7use LATMOS::Accounts::Bases;
8use LATMOS::Accounts::Synchro;
9use LATMOS::Accounts::SynchAccess;
10use LATMOS::Accounts::Log;
11use LATMOS::Accounts::Acls;
12
13our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0];
14
15=head1 NAME
16
17LATMOS::Accounts - Core module for LATMOS account management tools
18
19=head1 DESCRITPTION
20
21=cut
22
23=head1 FUNCTION
24
25=head2 new($configfile)
26
27Instanciate a new LATMOS::Accounts object.
28
29$configfile if defined is the Config::IniFiles formatted file to use,
30default to F</etc/latmos-account.ini>.
31
32=cut
33
34sub new {
35    my ($class, $config) = @_;
36
37    $config ||= '/etc/latmos-account.ini';
38
39    my $self = Config::IniFiles->new(
40        -file => $config,
41        '-default' => '_default_',
42    );
43    if ($self->val('_default_', 'acls')) {
44        my $acls = LATMOS::Accounts::Acls->new(
45            $self->val('_default_', 'acls')
46        ) or do {
47            return;
48        };
49        $self->{_acls} = $acls;
50    }
51
52    bless($self, $class)
53}
54
55=head2 list_bases
56
57Return the base list found in config file
58
59=cut
60
61sub list_bases {
62    my ($self) = @_;
63    grep {
64        !m/^_.*_$/ &&
65        !m/^sync:/
66    } $self->Sections
67}
68
69=head2 base($basename)
70
71Return a L<LATMOS::Accounts::Base> object over base named $basename
72defined in the config file.
73
74The base is loaded by this function.
75
76=cut
77
78sub base {
79    my ($self, $section) = @_;
80    # this method perform a cache
81    $self->_load_base($section);
82}
83
84=head2 default_base_name
85
86Return the default base name according config file
87
88=cut
89
90sub default_base_name {
91    my ($self) = @_;
92    $self->val('_default_', 'base', ($self->list_bases)[0]);
93}
94
95=head2 default_base
96
97Return a L<LATMOS::Accounts::Base> object over the default base according
98configuration file.
99
100=cut
101
102sub default_base {
103    my ($self) = @_;
104    my $default = $self->default_base_name or return;
105    $self->base($default);
106}
107
108# do the bad work
109sub _load_base {
110    my ($self, $section) = @_;
111    my $type = $self->val($section, 'type') or return;
112    my %params = (
113        map { $_ => ($self->val($section, $_)) } $self->Parameters($section),
114        defattr => { map { $_ => ($self->val('_defattr_', $_)) } $self->Parameters('_defattr_') },
115    );
116    my $base = LATMOS::Accounts::Bases->new(
117        $type,
118        %params,
119        label => $section,
120        acls => $self->{_acls},
121    ) or do {
122        la_log(LA_WARN, "Cannot instanciate base $section ($type)");
123        return;
124    };
125    $base->load or return;
126    $base;
127}
128
129sub default_synchro_name {
130    my ($self) = @_;
131    $self->val('_default_', 'sync');
132}
133
134sub list_synchro {
135    my ($self) = @_;
136    grep { $_ } map { /^sync:(.*)$/; $1 } $self->Sections
137}
138
139sub default_synchro {
140    my ($self, %options) = @_;
141    my $syncname = $self->default_synchro_name or do {
142        la_log(LA_ERR, 'Cannot find default synchro in config');
143        return;
144    };
145    $self->create_synchro($syncname, %options);
146}
147
148sub sync_from_name {
149    my ($self, $syncname) = @_;
150    return if (!$syncname);
151    $self->val("sync:$syncname", 'from', $self->default_base_name);
152}
153
154sub create_synchro {
155    my ($self, $name, %options) = @_;
156
157    # taking options from config
158    if ($name) {
159        foreach my $param ($self->Parameters("sync:$name")) {
160            if (!defined($options{$param})) {
161                my @args = $self->val("sync:$name", $param);
162                $options{$param} = ($args[1] || $param eq 'to')
163                    ? [ @args ]
164                    : $args[0];
165            }
166        }
167    }
168
169    my $labfrom = $options{from} ? $self->base($options{from}) : $self->default_base;
170
171    my @labto =
172        grep { $_ }
173        map { $self->base($_) }
174        @{ $options{to} || []}
175        or do {
176        la_log(LA_ERR, "No destination base load in this synchro");
177        return;
178    };
179
180    my $sync = LATMOS::Accounts::Synchro->new(
181        $labfrom, [ @labto ],
182        state_file => $self->val('_default_', 'state_file'),
183        %options,
184        name => $name,
185    );
186}
187
188sub sync_access {
189    my ($self, $name, %options) = @_;
190
191    my @obases;
192    if ($name) {
193        @obases =
194        (map { $self->base($_) } ($self->sync_from_name($name), $self->val("sync:$name", 'to')));
195    } elsif(@{ $options{bases} || []}) {
196        @obases = map { $self->base($_) } @{ $options{bases} || []};
197    } elsif (my $sname = $self->default_synchro_name) {
198        @obases = (map { $self->base($_) }
199            ($self->sync_from_name($sname), $self->val("sync:$sname", 'to'))
200        );
201    }
202
203    LATMOS::Accounts::SynchAccess->new([ @obases ]);
204}
205
2061;
207
208__END__
209
210=head1 SEE ALSO
211
212=head1 AUTHOR
213
214Thauvin Olivier, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
215
216=head1 COPYRIGHT AND LICENSE
217
218Copyright (C) 2009 by Thauvin Olivier
219
220This library is free software; you can redistribute it and/or modify
221it under the same terms as Perl itself, either Perl version 5.10.0 or,
222at your option, any later version of Perl 5 you may have available.
223
224=cut
Note: See TracBrowser for help on using the repository browser.