1 | package LATMOS::Accounts; |
---|
2 | |
---|
3 | use 5.010000; |
---|
4 | use strict; |
---|
5 | use warnings; |
---|
6 | use base qw(Config::IniFiles); |
---|
7 | use LATMOS::Accounts::Bases; |
---|
8 | use LATMOS::Accounts::Synchro; |
---|
9 | use LATMOS::Accounts::SynchAccess; |
---|
10 | use LATMOS::Accounts::Log; |
---|
11 | use LATMOS::Accounts::Acls; |
---|
12 | |
---|
13 | our $VERSION = '3.0.0'; |
---|
14 | |
---|
15 | =head1 NAME |
---|
16 | |
---|
17 | LATMOS::Accounts - Core module for LATMOS account management tools |
---|
18 | |
---|
19 | =head1 DESCRITPTION |
---|
20 | |
---|
21 | =cut |
---|
22 | |
---|
23 | sub _configdir { |
---|
24 | my ($self) = @_; |
---|
25 | ($self || {})->{_configdir} || '/etc/latmos-accounts' |
---|
26 | } |
---|
27 | |
---|
28 | =head1 FUNCTION |
---|
29 | |
---|
30 | =head2 new($configdir) |
---|
31 | |
---|
32 | Instanciate a new LATMOS::Accounts object. |
---|
33 | |
---|
34 | $configdir if defined is the directory containing files to use, |
---|
35 | default to F</etc/latmos-accounts/>. |
---|
36 | |
---|
37 | =cut |
---|
38 | |
---|
39 | sub 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 | |
---|
102 | Send signal to L<la-sync-manager> daemon to synchronize bases. |
---|
103 | |
---|
104 | =cut |
---|
105 | |
---|
106 | sub 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 | |
---|
132 | Return the base list found in config file |
---|
133 | |
---|
134 | =cut |
---|
135 | |
---|
136 | sub list_bases { |
---|
137 | my ($self) = @_; |
---|
138 | grep { |
---|
139 | !m/^_.*_$/ && |
---|
140 | !m/^sync:/ |
---|
141 | } $self->Sections |
---|
142 | } |
---|
143 | |
---|
144 | =head2 base($basename) |
---|
145 | |
---|
146 | Return a L<LATMOS::Accounts::Base> object over base named $basename |
---|
147 | defined in the config file. |
---|
148 | |
---|
149 | The base is loaded by this function. |
---|
150 | |
---|
151 | =cut |
---|
152 | |
---|
153 | sub 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 | |
---|
161 | Return the default base name according config file |
---|
162 | |
---|
163 | =cut |
---|
164 | |
---|
165 | sub default_base_name { |
---|
166 | my ($self) = @_; |
---|
167 | $self->val('_default_', 'base', ($self->list_bases)[0]); |
---|
168 | } |
---|
169 | |
---|
170 | =head2 default_base |
---|
171 | |
---|
172 | Return a L<LATMOS::Accounts::Base> object over the default base according |
---|
173 | configuration file. |
---|
174 | |
---|
175 | =cut |
---|
176 | |
---|
177 | sub default_base { |
---|
178 | my ($self) = @_; |
---|
179 | $self->base(); |
---|
180 | } |
---|
181 | |
---|
182 | # do the bad work |
---|
183 | sub _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 | |
---|
208 | Return de default synchronisation name |
---|
209 | |
---|
210 | =cut |
---|
211 | |
---|
212 | sub default_synchro_name { |
---|
213 | my ($self) = @_; |
---|
214 | $self->val('_default_', 'sync'); |
---|
215 | } |
---|
216 | |
---|
217 | =head2 list_synchro |
---|
218 | |
---|
219 | List synchronisation setup in L<latmos-accounts.ini> |
---|
220 | |
---|
221 | =cut |
---|
222 | |
---|
223 | sub list_synchro { |
---|
224 | my ($self) = @_; |
---|
225 | grep { $_ } map { /^sync:(.*)$/; $1 } $self->Sections |
---|
226 | } |
---|
227 | |
---|
228 | =head2 default_synchro |
---|
229 | |
---|
230 | Return a reference to default synchronisation object |
---|
231 | |
---|
232 | =cut |
---|
233 | |
---|
234 | sub 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 | |
---|
245 | Return a reference to synchronisation object for C<$name> synchronisation. |
---|
246 | |
---|
247 | =cut |
---|
248 | |
---|
249 | sub 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 | |
---|
285 | Return a L<LATMOS::Accounts::SynchAccess> object over C<$name> synchronisation. |
---|
286 | |
---|
287 | =cut |
---|
288 | |
---|
289 | sub 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 | |
---|
307 | 1; |
---|
308 | |
---|
309 | __END__ |
---|
310 | |
---|
311 | =head1 SEE ALSO |
---|
312 | |
---|
313 | =head1 AUTHOR |
---|
314 | |
---|
315 | Thauvin Olivier, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
316 | |
---|
317 | =head1 COPYRIGHT AND LICENSE |
---|
318 | |
---|
319 | Copyright (C) 2009, 2010, 2011, 2012 by Thauvin Olivier |
---|
320 | |
---|
321 | This library is free software; you can redistribute it and/or modify |
---|
322 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
323 | at your option, any later version of Perl 5 you may have available. |
---|
324 | |
---|
325 | =cut |
---|