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

Last change on this file since 989 was 989, checked in by nanardon, 12 years ago
  • LATMOS::Accounts version set into pm file instead Makefile.PL
  • Property svn:keywords set to Id Rev
File size: 7.2 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 = '2.0.6';
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
100sub call_batch_sync {
101    my ($self) = @_;
102    if (my $sd = $self->val('_default_', 'state_dir')) {
103        if (open(my $fh, '<', $sd . '/sync-manager.pid')) {
104            my $pid = <$fh> || '';
105            chomp($pid);
106            close($fh);
107            if ($pid && kill 1, $pid) {
108                return 1; # \o/ we succeed
109            } else {
110                la_log(LA_ERR, "Can send signal -1 to la-sync-manager (pid: %s, %s)",
111                    $pid || 'none', $!);
112                return;
113            }
114        } else {
115            la_log(LA_ERR, 'Cannot open la-sync-manager pid file');
116            return;
117        }
118    } else {
119        la_log(LA_WARN, "No statedir setup, cannot find la-sync-manager pid file");
120        return;
121    }
122}
123
124=head2 list_bases
125
126Return the base list found in config file
127
128=cut
129
130sub list_bases {
131    my ($self) = @_;
132    grep {
133        !m/^_.*_$/ &&
134        !m/^sync:/
135    } $self->Sections
136}
137
138=head2 base($basename)
139
140Return a L<LATMOS::Accounts::Base> object over base named $basename
141defined in the config file.
142
143The base is loaded by this function.
144
145=cut
146
147sub base {
148    my ($self, $section) = @_;
149    # this method perform a cache
150    $self->_load_base($section || $self->default_base_name);
151}
152
153=head2 default_base_name
154
155Return the default base name according config file
156
157=cut
158
159sub default_base_name {
160    my ($self) = @_;
161    $self->val('_default_', 'base', ($self->list_bases)[0]);
162}
163
164=head2 default_base
165
166Return a L<LATMOS::Accounts::Base> object over the default base according
167configuration file.
168
169=cut
170
171sub default_base {
172    my ($self) = @_;
173    $self->base();
174}
175
176# do the bad work
177sub _load_base {
178    my ($self, $section) = @_;
179    my $type = $self->val($section, 'type') or return;
180    my %params = (
181        map { $_ => ($self->val($section, $_)) } $self->Parameters($section),
182        defattr => { map { $_ => ($self->val('_defattr_', $_)) } $self->Parameters('_defattr_') },
183    );
184    my $base = LATMOS::Accounts::Bases->new(
185        $type,
186        %params,
187        label => $section,
188        acls => $self->{_acls},
189        allowed_values => $self->{_allowed_values},
190        configdir => $self->_configdir,
191        _la => $self,
192    ) or do {
193        la_log(LA_WARN, "Cannot instanciate base $section ($type)");
194        return;
195    };
196    $base->load or return;
197    $base;
198}
199
200sub default_synchro_name {
201    my ($self) = @_;
202    $self->val('_default_', 'sync');
203}
204
205sub list_synchro {
206    my ($self) = @_;
207    grep { $_ } map { /^sync:(.*)$/; $1 } $self->Sections
208}
209
210sub default_synchro {
211    my ($self, %options) = @_;
212    my $syncname = $self->default_synchro_name or do {
213        la_log(LA_ERR, 'Cannot find default synchro in config');
214        return;
215    };
216    $self->create_synchro($syncname, %options);
217}
218
219sub sync_from_name {
220    my ($self, $syncname) = @_;
221    return if (!$syncname);
222    $self->val("sync:$syncname", 'from', $self->default_base_name);
223}
224
225sub create_synchro {
226    my ($self, $name, %options) = @_;
227
228    # taking options from config
229    if ($name) {
230        foreach my $param ($self->Parameters("sync:$name")) {
231            if (!defined($options{$param})) {
232                my @args = $self->val("sync:$name", $param);
233                $options{$param} = ($args[1] || $param eq 'to')
234                    ? [ @args ]
235                    : $args[0];
236            }
237        }
238    }
239
240    my $labfrom = $options{from} ? $self->base($options{from}) : $self->default_base;
241
242    my @labto =
243        grep { $_ }
244        map { $self->base($_) }
245        @{ $options{to} || []}
246        or do {
247        la_log(LA_ERR, "No destination base load in this synchro");
248        return;
249    };
250
251    my $sync = LATMOS::Accounts::Synchro->new(
252        $labfrom, [ @labto ],
253        state_dir => ($self->val('_default_', 'state_dir') || undef),
254        %options,
255        name => $name,
256    );
257}
258
259sub sync_access {
260    my ($self, $name, %options) = @_;
261
262    my @obases;
263    if ($name) {
264        @obases =
265        (map { $self->base($_) } ($self->sync_from_name($name), $self->val("sync:$name", 'to')));
266    } elsif(@{ $options{bases} || []}) {
267        @obases = map { $self->base($_) } @{ $options{bases} || []};
268    } elsif (my $sname = $self->default_synchro_name) {
269        @obases = (map { $self->base($_) }
270            ($self->sync_from_name($sname), $self->val("sync:$sname", 'to'))
271        );
272    }
273
274    LATMOS::Accounts::SynchAccess->new([ @obases ]);
275}
276
2771;
278
279__END__
280
281=head1 SEE ALSO
282
283=head1 AUTHOR
284
285Thauvin Olivier, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
286
287=head1 COPYRIGHT AND LICENSE
288
289Copyright (C) 2009 by Thauvin Olivier
290
291This library is free software; you can redistribute it and/or modify
292it under the same terms as Perl itself, either Perl version 5.10.0 or,
293at your option, any later version of Perl 5 you may have available.
294
295=cut
Note: See TracBrowser for help on using the repository browser.