source: trunk/LATMOS-Accounts/lib/LATMOS/Accounts.pm @ 1022

Last change on this file since 1022 was 1022, checked in by nanardon, 12 years ago
  • bump version according Changes
  • Property svn:keywords set to Id Rev
File size: 7.7 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 = '3.0.0';
14
15=head1 NAME
16
17LATMOS::Accounts - Core module for LATMOS account management tools
18
19=head1 DESCRITPTION
20
21=cut
22
23sub _configdir {
24   my ($self) = @_;
25   ($self || {})->{_configdir} || '/etc/latmos-accounts'
26}
27
28=head1 FUNCTION
29
30=head2 new($configdir)
31
32Instanciate a new LATMOS::Accounts object.
33
34$configdir if defined is the directory containing files to use,
35default to F</etc/latmos-accounts/>.
36
37=cut
38
39sub new {
40    my ($class, $config, %options) = @_;
41
42    $config ||= _configdir();
43    my $oldconfig ||= '/etc/latmos-account.ini';
44
45    # If config file not found, fallback to old one
46    my $configfile = -f join('/', $config, 'latmos-accounts.ini')
47        ? join('/', $config, 'latmos-accounts.ini')
48        : '/etc/latmos-account.ini';
49
50    my $self = Config::IniFiles->new(
51        -file => $configfile,
52        '-default' => '_default_',
53    ) or do {
54        la_log(LA_ERR, 'Can\'t open main config file %s', $configfile);
55        return;
56    };
57
58    $self->{_configdir} = $config;
59    bless($self, $class);
60
61    if (!$options{noacl}) {
62        if ($self->val('_default_', 'acls')) {
63            $self->{_acls} = LATMOS::Accounts::Acls->new(
64                $self->val('_default_', 'acls')
65            ) or do {
66                la_log(LA_ERR,
67                    'Cannot load ACL file %s', $self->val('_default_', 'acls')
68                );
69                return;
70            };
71        } elsif (-f (my $aclf = join('/', $self->_configdir, 'la-acls.ini'))) {
72            $self->{_acls} = LATMOS::Accounts::Acls->new($aclf) or do {
73                la_log(LA_ERR, 'Cannot load ACL file %s', $aclf);
74                return;
75            };
76        }
77    }
78
79    if ($self->val('_default_', 'allowed_values')) {
80        $self->{_allowed_values} = Config::IniFiles->new(
81            -file => $self->val('_default_', 'allowed_values'),
82        ) or do {
83            la_log(LA_ERR, 'Cannot load ALLOWED VALUES %s',
84                $self->val('_default_', 'allowed_values'));
85            return;
86        };
87    } elsif (-f (my $allowf = join('/', $self->_configdir,
88                'la-allowed-values.ini'))) {
89        $self->{_allowed_values} = Config::IniFiles->new(
90            -file => $allowf,
91        ) or do {
92            la_log(LA_ERR, 'Cannot load ALLOWED VALUES %s', $allowf);
93            return;
94        };
95    }
96
97    $self
98}
99
100=head2 call_batch_sync
101
102Send signal to L<la-sync-manager> daemon to synchronize bases.
103
104=cut
105
106sub call_batch_sync {
107    my ($self) = @_;
108    if (my $sd = $self->val('_default_', 'state_dir')) {
109        if (open(my $fh, '<', $sd . '/sync-manager.pid')) {
110            my $pid = <$fh> || '';
111            chomp($pid);
112            close($fh);
113            if ($pid && kill 1, $pid) {
114                return 1; # \o/ we succeed
115            } else {
116                la_log(LA_ERR, "Can send signal -1 to la-sync-manager (pid: %s, %s)",
117                    $pid || 'none', $!);
118                return;
119            }
120        } else {
121            la_log(LA_ERR, 'Cannot open la-sync-manager pid file');
122            return;
123        }
124    } else {
125        la_log(LA_WARN, "No statedir setup, cannot find la-sync-manager pid file");
126        return;
127    }
128}
129
130=head2 list_bases
131
132Return the base list found in config file
133
134=cut
135
136sub list_bases {
137    my ($self) = @_;
138    grep {
139        !m/^_.*_$/ &&
140        !m/^sync:/
141    } $self->Sections
142}
143
144=head2 base($basename)
145
146Return a L<LATMOS::Accounts::Base> object over base named $basename
147defined in the config file.
148
149The base is loaded by this function.
150
151=cut
152
153sub base {
154    my ($self, $section) = @_;
155    # this method perform a cache
156    $self->_load_base($section || $self->default_base_name);
157}
158
159=head2 default_base_name
160
161Return the default base name according config file
162
163=cut
164
165sub default_base_name {
166    my ($self) = @_;
167    $self->val('_default_', 'base', ($self->list_bases)[0]);
168}
169
170=head2 default_base
171
172Return a L<LATMOS::Accounts::Base> object over the default base according
173configuration file.
174
175=cut
176
177sub default_base {
178    my ($self) = @_;
179    $self->base();
180}
181
182# do the bad work
183sub _load_base {
184    my ($self, $section) = @_;
185    my $type = $self->val($section, 'type') or return;
186    my %params = (
187        map { $_ => ($self->val($section, $_)) } $self->Parameters($section),
188        defattr => { map { $_ => ($self->val('_defattr_', $_)) } $self->Parameters('_defattr_') },
189    );
190    my $base = LATMOS::Accounts::Bases->new(
191        $type,
192        %params,
193        label => $section,
194        acls => $self->{_acls},
195        allowed_values => $self->{_allowed_values},
196        configdir => $self->_configdir,
197        _la => $self,
198    ) or do {
199        la_log(LA_WARN, "Cannot instanciate base $section ($type)");
200        return;
201    };
202    $base->load or return;
203    $base;
204}
205
206=head2 default_synchro_name
207
208Return de default synchronisation name
209
210=cut
211
212sub default_synchro_name {
213    my ($self) = @_;
214    $self->val('_default_', 'sync');
215}
216
217=head2 list_synchro
218
219List synchronisation setup in L<latmos-accounts.ini>
220
221=cut
222
223sub list_synchro {
224    my ($self) = @_;
225    grep { $_ } map { /^sync:(.*)$/; $1 } $self->Sections
226}
227
228=head2 default_synchro
229
230Return a reference to default synchronisation object
231
232=cut
233
234sub default_synchro {
235    my ($self, %options) = @_;
236    my $syncname = $self->default_synchro_name or do {
237        la_log(LA_ERR, 'Cannot find default synchro in config');
238        return;
239    };
240    $self->create_synchro($syncname, %options);
241}
242
243=head2 create_synchro($name, %options)
244
245Return a reference to synchronisation object for C<$name> synchronisation.
246
247=cut
248
249sub create_synchro {
250    my ($self, $name, %options) = @_;
251
252    # taking options from config
253    if ($name) {
254        foreach my $param ($self->Parameters("sync:$name")) {
255            if (!defined($options{$param})) {
256                my @args = $self->val("sync:$name", $param);
257                $options{$param} = ($args[1] || $param eq 'to')
258                    ? [ @args ]
259                    : $args[0];
260            }
261        }
262    }
263
264    my $labfrom = $options{from} ? $self->base($options{from}) : $self->default_base;
265
266    my @labto =
267        grep { $_ }
268        map { $self->base($_) }
269        @{ $options{to} || []}
270        or do {
271        la_log(LA_ERR, "No destination base load in this synchro");
272        return;
273    };
274
275    my $sync = LATMOS::Accounts::Synchro->new(
276        $labfrom, [ @labto ],
277        state_dir => ($self->val('_default_', 'state_dir') || undef),
278        %options,
279        name => $name,
280    );
281}
282
283=head2 sync_access($name, %options)
284
285Return a L<LATMOS::Accounts::SynchAccess> object over C<$name> synchronisation.
286
287=cut
288
289sub sync_access {
290    my ($self, $name, %options) = @_;
291
292    my @obases;
293    if ($name) {
294        @obases =
295        (map { $self->base($_) } ($self->sync_from_name($name), $self->val("sync:$name", 'to')));
296    } elsif(@{ $options{bases} || []}) {
297        @obases = map { $self->base($_) } @{ $options{bases} || []};
298    } elsif (my $sname = $self->default_synchro_name) {
299        @obases = (map { $self->base($_) }
300            ($self->sync_from_name($sname), $self->val("sync:$sname", 'to'))
301        );
302    }
303
304    LATMOS::Accounts::SynchAccess->new([ @obases ]);
305}
306
3071;
308
309__END__
310
311=head1 SEE ALSO
312
313=head1 AUTHOR
314
315Thauvin Olivier, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
316
317=head1 COPYRIGHT AND LICENSE
318
319Copyright (C) 2009, 2010, 2011, 2012 by Thauvin Olivier
320
321This library is free software; you can redistribute it and/or modify
322it under the same terms as Perl itself, either Perl version 5.10.0 or,
323at your option, any later version of Perl 5 you may have available.
324
325=cut
Note: See TracBrowser for help on using the repository browser.