1 | package LATMOS::Accounts::Synchro; |
---|
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::Log; |
---|
9 | use LATMOS::Accounts::Utils qw(exec_command); |
---|
10 | use Fcntl qw(:flock); |
---|
11 | |
---|
12 | =head1 NAME |
---|
13 | |
---|
14 | LATMOS::Accounts::Synchro - Perl extension for blah blah blah |
---|
15 | |
---|
16 | =head1 SYNOPSIS |
---|
17 | |
---|
18 | use LATMOS::Accounts; |
---|
19 | blah blah blah |
---|
20 | |
---|
21 | =head1 DESCRIPTION |
---|
22 | |
---|
23 | Stub documentation for LATMOS::Accounts, created by h2xs. It looks like the |
---|
24 | author of the extension was negligent enough to leave the stub |
---|
25 | unedited. |
---|
26 | |
---|
27 | Blah blah blah. |
---|
28 | |
---|
29 | =head1 FUNCTIONS |
---|
30 | |
---|
31 | =cut |
---|
32 | |
---|
33 | our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0]; |
---|
34 | |
---|
35 | =head2 new($from, $to, %options) |
---|
36 | |
---|
37 | Create a new synchronisation where $from and $to are LATMOS::Accounts::Base |
---|
38 | based objects. $to can be an array ref of objects. |
---|
39 | |
---|
40 | =cut |
---|
41 | |
---|
42 | sub new { |
---|
43 | my ($class, $from, $to, %options) = @_; |
---|
44 | |
---|
45 | my $state_file = $options{state_dir} |
---|
46 | ? $options{state_dir} . '/synchronisation.ini' |
---|
47 | : undef; |
---|
48 | if ($state_file && ! -w $state_file) { |
---|
49 | # don't exists, we have to create it |
---|
50 | open(my $handle, '>', $state_file) or do { |
---|
51 | la_log(LA_ERR, "Cannot open status file %s", $state_file); |
---|
52 | return; |
---|
53 | }; |
---|
54 | print $handle "[_default_]\n"; |
---|
55 | close($handle); |
---|
56 | } |
---|
57 | |
---|
58 | my $self = Config::IniFiles->new( |
---|
59 | $state_file |
---|
60 | ? (-file => $state_file) |
---|
61 | : (), |
---|
62 | ); |
---|
63 | |
---|
64 | if ($state_file && !$self->GetFileName) { |
---|
65 | $self->SetFileName($state_file); |
---|
66 | } |
---|
67 | |
---|
68 | $self->{from} = $from or do { |
---|
69 | la_log(LA_ERR, "No database source"); |
---|
70 | return; |
---|
71 | }; |
---|
72 | $self->{options} = { %options }; |
---|
73 | |
---|
74 | # allow ref and array ref of, eg |
---|
75 | # to = $foo and $to = [ $foo, $bar ] |
---|
76 | foreach (ref($to) eq 'ARRAY' ? @{ $to || []} : ($to)) { |
---|
77 | push(@{$self->{to}}, $_); |
---|
78 | } |
---|
79 | bless($self, $class) |
---|
80 | } |
---|
81 | |
---|
82 | =head2 name |
---|
83 | |
---|
84 | Return the name of this synchronisation |
---|
85 | |
---|
86 | =cut |
---|
87 | |
---|
88 | sub name { |
---|
89 | $_[0]->{options}{name} |
---|
90 | } |
---|
91 | |
---|
92 | =head2 from |
---|
93 | |
---|
94 | Return the base object source for this synchronisation |
---|
95 | |
---|
96 | =cut |
---|
97 | |
---|
98 | sub from { |
---|
99 | my ($self) = @_; |
---|
100 | return $self->{from} |
---|
101 | } |
---|
102 | |
---|
103 | =head2 to |
---|
104 | |
---|
105 | Return the list of base destination for this synchronisation |
---|
106 | |
---|
107 | =cut |
---|
108 | |
---|
109 | sub to { |
---|
110 | my ($self) = @_; |
---|
111 | return @{$self->{to} || []}; |
---|
112 | } |
---|
113 | |
---|
114 | =head2 load_dest |
---|
115 | |
---|
116 | Try to loaded all base, return the count of filtrered base which cannot |
---|
117 | be loaded |
---|
118 | |
---|
119 | =cut |
---|
120 | |
---|
121 | sub load_dest { |
---|
122 | my ($self) = @_; |
---|
123 | my @loaded; |
---|
124 | my $unloaded = 0; |
---|
125 | foreach ($self->to) { |
---|
126 | if($_->load) { |
---|
127 | push(@loaded, $_); |
---|
128 | } else { |
---|
129 | $unloaded++; |
---|
130 | warn "Cannot load $_"; |
---|
131 | } |
---|
132 | } |
---|
133 | $self->{to} = \@loaded; |
---|
134 | return $unloaded; |
---|
135 | } |
---|
136 | |
---|
137 | =head2 enter_synch_mode |
---|
138 | |
---|
139 | Configure base for synchronisation |
---|
140 | |
---|
141 | =cut |
---|
142 | |
---|
143 | sub enter_synch_mode { |
---|
144 | my ($self) = @_; |
---|
145 | $self->from->load or return; |
---|
146 | # if any cannot be loaded, return, |
---|
147 | # TODO we need a way to force if some still can be sync |
---|
148 | $self->load_dest and return; |
---|
149 | my %state = (); |
---|
150 | $state{$self->from->label} = $self->from->wexported( |
---|
151 | $self->{options}{unexported} ? 1 : 0 |
---|
152 | ); |
---|
153 | foreach ($self->to) { |
---|
154 | $state{$_->label} = $_->wexported(1); |
---|
155 | } |
---|
156 | la_log(LA_DEBUG, "Entering synch mode, old state: %s", join(', ', map { |
---|
157 | "$_ => $state{$_}" } sort keys %state)); |
---|
158 | %state |
---|
159 | } |
---|
160 | |
---|
161 | =head2 leave_synch_mode (%state) |
---|
162 | |
---|
163 | Retore base to previous state |
---|
164 | |
---|
165 | =cut |
---|
166 | |
---|
167 | sub leave_synch_mode { |
---|
168 | my ($self, %state) = @_; |
---|
169 | la_log(LA_DEBUG, "Leaving synch mode"); |
---|
170 | $self->from->wexported($state{$self->from->label}); |
---|
171 | foreach my $base (grep { $_ } $self->to) { |
---|
172 | $base->commit; |
---|
173 | $base->wexported($state{$base->label}); |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | =head2 lock |
---|
178 | |
---|
179 | Create a lock to denied another synchronisation to run |
---|
180 | |
---|
181 | =cut |
---|
182 | |
---|
183 | sub lock { |
---|
184 | my ($self) = @_; |
---|
185 | |
---|
186 | $self->{lock}{handle} and return 1; |
---|
187 | la_log(LA_DEBUG, "Trying to lock (pid $$)"); |
---|
188 | if ($self->{options}{state_dir}) { |
---|
189 | my $lockfile = $self->{options}{state_dir} . '/synclock'; |
---|
190 | open(my $handle, '>>', $lockfile) or return; |
---|
191 | flock($handle, LOCK_EX); |
---|
192 | $self->{lock}{handle} = $handle; |
---|
193 | $self->{lock}{filename} = $lockfile; |
---|
194 | la_log(LA_DEBUG, "lock done (pid $$)"); |
---|
195 | return 1; |
---|
196 | } else { return 1 } |
---|
197 | } |
---|
198 | |
---|
199 | =head2 unlock |
---|
200 | |
---|
201 | Remove lock |
---|
202 | |
---|
203 | =cut |
---|
204 | |
---|
205 | sub unlock { |
---|
206 | my ($self) = @_; |
---|
207 | if (my $handle = $self->{lock}{handle}) { |
---|
208 | close($handle); |
---|
209 | delete($self->{lock}{handle}); |
---|
210 | unlink($self->{lock}{filename}); |
---|
211 | delete($self->{lock}{filename}); |
---|
212 | return 1; |
---|
213 | } |
---|
214 | return; |
---|
215 | } |
---|
216 | |
---|
217 | =head2 sync_object ($otype, $uid, %options) |
---|
218 | |
---|
219 | Synchronise object type C<$otype> named C<$uid> |
---|
220 | |
---|
221 | =cut |
---|
222 | |
---|
223 | sub sync_object { |
---|
224 | my ($self, $otype, $uid, %options) = @_; |
---|
225 | |
---|
226 | $self->lock or return; |
---|
227 | |
---|
228 | my %state = $self->enter_synch_mode; |
---|
229 | |
---|
230 | my $res = $self->_sync_object($otype, $uid, %options); |
---|
231 | |
---|
232 | $self->leave_synch_mode(%state); |
---|
233 | |
---|
234 | $self->unlock; |
---|
235 | |
---|
236 | $res; |
---|
237 | } |
---|
238 | |
---|
239 | sub _sync_object { |
---|
240 | my ($self, $otype, $uid, %options) = @_; |
---|
241 | foreach ($self->to) { |
---|
242 | my $res = $_->sync_object_from($self->from, $otype, $uid, %options); |
---|
243 | if (defined $res) { |
---|
244 | la_log(LA_NOTICE, $_->label . " $uid ($otype) $res") if ($res); |
---|
245 | return 1; |
---|
246 | } else { |
---|
247 | la_log(LA_ERR, "error synching $uid ($otype) to " . $_->label); |
---|
248 | return; |
---|
249 | } |
---|
250 | } |
---|
251 | } |
---|
252 | |
---|
253 | =head2 process |
---|
254 | |
---|
255 | Run the syncronisation |
---|
256 | |
---|
257 | =cut |
---|
258 | |
---|
259 | sub process { |
---|
260 | my ($self, %options) = @_; |
---|
261 | |
---|
262 | $self->lock or return; |
---|
263 | |
---|
264 | if (!(my $res = $self->run_pre_synchro({}))) { |
---|
265 | la_log(LA_ERR, "Pre synchro script failed, aborting"); |
---|
266 | $self->unlock; |
---|
267 | return; |
---|
268 | } |
---|
269 | |
---|
270 | my %state = $self->enter_synch_mode; |
---|
271 | |
---|
272 | # tracking current base revision: |
---|
273 | $self->{current_rev} = $self->from->current_rev; |
---|
274 | |
---|
275 | my %desterror; |
---|
276 | my $updated = 0; |
---|
277 | |
---|
278 | # We do base one by one |
---|
279 | |
---|
280 | foreach my $destbase ($self->to) { |
---|
281 | my %objlist; |
---|
282 | foreach my $otype ($self->from->list_supported_objects) { |
---|
283 | $destbase->is_supported_object($otype) or next; |
---|
284 | |
---|
285 | my %existings; |
---|
286 | my %filtering; |
---|
287 | |
---|
288 | $existings{$otype} = { map { $_ => 1 } |
---|
289 | $self->from->list_objects($otype) }; |
---|
290 | |
---|
291 | # Is there a filter to apply: |
---|
292 | my $filtername = 'filter.' . $destbase->label . '.' . $otype; |
---|
293 | if (my $filter = $self->{options}->{$filtername}) { |
---|
294 | la_log(LA_DEBUG, "Found %s param, using it: %s", $filtername, $filter); |
---|
295 | $filtering{$otype} = { map { $_ => 1 } |
---|
296 | $self->from->search_objects($otype, $filter) }; |
---|
297 | } else { |
---|
298 | $filtering{$otype} = $existings{$otype}; |
---|
299 | } |
---|
300 | |
---|
301 | |
---|
302 | # deleting non existing object in dest: |
---|
303 | |
---|
304 | # Sync: noDelete.Base = yes mean no delete |
---|
305 | # noDelete.Base.Otype = yes mean no delete for this object |
---|
306 | if ($self->{options}->{'noDelete'} || |
---|
307 | $self->{options}->{'noDelete.' . $destbase->label} || |
---|
308 | $self->{options}->{'noDelete.' . $destbase->label . '.' . $otype}) { |
---|
309 | la_log(LA_INFO, 'Not deleting object type \'%s` from base \'%s` because %s is set', |
---|
310 | $otype, |
---|
311 | $destbase->label, |
---|
312 | $self->{options}->{'noDelete.' . $destbase->label . '.' . $otype} |
---|
313 | ? 'noDelete.' . $destbase->label . '.' . $otype |
---|
314 | : $self->{options}->{'noDelete.' . $destbase->label} |
---|
315 | ? 'noDelete.' . $destbase->label |
---|
316 | : 'noDelete' |
---|
317 | ); |
---|
318 | } else { |
---|
319 | |
---|
320 | my $deletefiltered = 'deletefiltered.' . $destbase->label . '.' . $otype; |
---|
321 | |
---|
322 | foreach ($destbase->list_objects($otype)) { |
---|
323 | |
---|
324 | if ($filtering{$otype}{$_}) { |
---|
325 | # the object must exists |
---|
326 | next; |
---|
327 | } elsif ($existings{$otype}{$_}) { |
---|
328 | # object exists but is filtered |
---|
329 | if (!$self->{options}->{$deletefiltered}) { |
---|
330 | next; |
---|
331 | } |
---|
332 | } |
---|
333 | |
---|
334 | if (my $res = $destbase->sync_object_from($self->from, |
---|
335 | $otype, $_, %options)) { |
---|
336 | la_log(LA_NOTICE, "%s::%s::%s => %s %s", |
---|
337 | $self->from->label, $otype, $_, $destbase->label, $res, |
---|
338 | ); |
---|
339 | if ($destbase->is_transactionnal) { |
---|
340 | $destbase->commit; |
---|
341 | } |
---|
342 | $updated = 1; |
---|
343 | } else { |
---|
344 | if ($destbase->is_transactionnal) { |
---|
345 | $destbase->rollback; |
---|
346 | } |
---|
347 | } |
---|
348 | } |
---|
349 | } # noDelete.Base |
---|
350 | |
---|
351 | |
---|
352 | # Finding object to synchronize: |
---|
353 | @{$objlist{$otype}} = grep { $filtering{$otype}{$_} } $self->from->list_objects_from_rev( |
---|
354 | $otype, |
---|
355 | $self->val($self->from->label, $destbase->label, 0), |
---|
356 | ); |
---|
357 | } |
---|
358 | |
---|
359 | my %objectok; |
---|
360 | foreach my $pass (1, 0) { |
---|
361 | foreach my $otype ($destbase->ordered_objects) { |
---|
362 | exists($objlist{$otype}) or next; |
---|
363 | foreach (@{$objlist{$otype} || []}) { |
---|
364 | |
---|
365 | # If first synchro is not ok, don't retry to sync the object |
---|
366 | if ((!$pass) && (!$objectok{$otype}{$_})) { |
---|
367 | next; |
---|
368 | } |
---|
369 | |
---|
370 | my $res = $destbase->sync_object_from($self->from, $otype, $_, |
---|
371 | %options, firstpass => $pass); |
---|
372 | if (defined $res) { |
---|
373 | if ($res) { |
---|
374 | la_log(LA_NOTICE, "%s::%s::%s => %s %s", |
---|
375 | $self->from->label, $otype, $_, |
---|
376 | $destbase->label, $res, |
---|
377 | ); |
---|
378 | if ($destbase->is_transactionnal) { |
---|
379 | $destbase->commit; |
---|
380 | } |
---|
381 | $updated = 1; |
---|
382 | $objectok{$otype}{$_} = 1; |
---|
383 | } |
---|
384 | } else { |
---|
385 | la_log(LA_ERR, "Cannot synch %s::%s::%s => %s", |
---|
386 | $self->from->label, $otype, $_, |
---|
387 | $destbase->label, |
---|
388 | ); |
---|
389 | $desterror{$destbase->label} = 1; |
---|
390 | if ($destbase->is_transactionnal) { |
---|
391 | $destbase->rollback; |
---|
392 | } |
---|
393 | } |
---|
394 | } |
---|
395 | } |
---|
396 | } |
---|
397 | } |
---|
398 | |
---|
399 | $self->leave_synch_mode(%state); |
---|
400 | my $res = $self->run_post_synchro( |
---|
401 | { |
---|
402 | UPDATED => $updated || undef, |
---|
403 | } |
---|
404 | ); |
---|
405 | if ($res) { |
---|
406 | foreach my $destbase ($self->to) { |
---|
407 | # don't register savepoint on error |
---|
408 | if ($desterror{$destbase->label}) { next; } |
---|
409 | $self->newval($self->from->label, $destbase->label, $self->{current_rev}); |
---|
410 | } |
---|
411 | |
---|
412 | if(!($self->{options}{nocreate} || |
---|
413 | $self->{options}{test})) { |
---|
414 | $self->write_status; |
---|
415 | } |
---|
416 | } else { |
---|
417 | la_log(LA_ERROR, "Not updating status because post script failed"); |
---|
418 | } |
---|
419 | |
---|
420 | $self->unlock; |
---|
421 | |
---|
422 | if (!$res) { # postscript failure |
---|
423 | return; |
---|
424 | } elsif (grep { $desterror{$_} } keys %desterror) { |
---|
425 | # There were errors :\ |
---|
426 | return; |
---|
427 | } else { |
---|
428 | return 1; |
---|
429 | } |
---|
430 | } |
---|
431 | |
---|
432 | =head2 write_status |
---|
433 | |
---|
434 | Write savepoint file |
---|
435 | |
---|
436 | =cut |
---|
437 | |
---|
438 | sub write_status { |
---|
439 | my ($self) = @_; |
---|
440 | if (my $file = $self->GetFileName) { |
---|
441 | open(my $handle, '>', $file) or do { |
---|
442 | la_log(LA_ERR, "Cannot open status file %s for writing: %s", |
---|
443 | $file, $!); |
---|
444 | return; |
---|
445 | }; |
---|
446 | my $oldfh = select($handle); |
---|
447 | $self->OutputConfig(); |
---|
448 | select($oldfh); |
---|
449 | close($handle); |
---|
450 | return 1; |
---|
451 | } |
---|
452 | |
---|
453 | return 0; |
---|
454 | } |
---|
455 | |
---|
456 | =head2 reset_savepoint |
---|
457 | |
---|
458 | Reset savepoint in status file to force full synchronisation |
---|
459 | |
---|
460 | =cut |
---|
461 | |
---|
462 | sub reset_savepoint { |
---|
463 | my ($self) = @_; |
---|
464 | foreach my $destbase ($self->to) { |
---|
465 | # don't register savepoint on error |
---|
466 | $self->newval($self->from->label, $destbase->label, 0); |
---|
467 | } |
---|
468 | $self->write_status; |
---|
469 | } |
---|
470 | |
---|
471 | =head2 run_pre_synchro |
---|
472 | |
---|
473 | Run task done before synchronisation |
---|
474 | |
---|
475 | =cut |
---|
476 | |
---|
477 | sub run_pre_synchro { |
---|
478 | my ($self, $env) = @_; |
---|
479 | |
---|
480 | $env ||= {}; |
---|
481 | $env->{HOOK_TYPE} = 'PRE'; |
---|
482 | |
---|
483 | foreach my $base ($self->to) { |
---|
484 | if ($base->config('presynchro')) { |
---|
485 | la_log LA_DEBUG, "Executing base pre synchro `%s' for %s", |
---|
486 | $base->config('presynchro'), $base->label; |
---|
487 | exec_command( |
---|
488 | $base->config('presynchro'), |
---|
489 | { |
---|
490 | BASE => $base->label, |
---|
491 | BASETYPE => $base->type, |
---|
492 | %{ $env }, |
---|
493 | } |
---|
494 | ); |
---|
495 | } |
---|
496 | } |
---|
497 | |
---|
498 | $self->{options}{pre} or return 1; |
---|
499 | |
---|
500 | la_log(LA_DEBUG, "Running post synchro `%s'", $self->{options}{pre}); |
---|
501 | |
---|
502 | exec_command($self->{options}{post}, $env); |
---|
503 | } |
---|
504 | |
---|
505 | =head2 run_post_synchro |
---|
506 | |
---|
507 | Run task done after synchronisation |
---|
508 | |
---|
509 | =cut |
---|
510 | |
---|
511 | sub run_post_synchro { |
---|
512 | my ($self, $env) = @_; |
---|
513 | |
---|
514 | $env ||= {}; |
---|
515 | $env->{HOOK_TYPE} = 'PRE'; |
---|
516 | |
---|
517 | foreach my $base ($self->to) { |
---|
518 | if ($base->config('postsynchro')) { |
---|
519 | la_log LA_DEBUG, "Executing base post synchro `%s' for %s", |
---|
520 | $base->config('postsynchro'), $base->label; |
---|
521 | exec_command( |
---|
522 | $base->config('postsynchro'), |
---|
523 | { |
---|
524 | BASE => $base->label, |
---|
525 | BASETYPE => $base->type, |
---|
526 | %{ $env }, |
---|
527 | } |
---|
528 | ); |
---|
529 | } |
---|
530 | } |
---|
531 | |
---|
532 | $self->{options}{post} or return 1; |
---|
533 | |
---|
534 | la_log(LA_DEBUG, "Running post synchro `%s'", $self->{options}{post}); |
---|
535 | |
---|
536 | exec_command($self->{options}{post}, $env); |
---|
537 | } |
---|
538 | |
---|
539 | |
---|
540 | 1; |
---|
541 | |
---|
542 | __END__ |
---|
543 | # Below is stub documentation for your module. You'd better edit it! |
---|
544 | |
---|
545 | =head1 SEE ALSO |
---|
546 | |
---|
547 | Mention other useful documentation such as the documentation of |
---|
548 | related modules or operating system documentation (such as man pages |
---|
549 | in UNIX), or any relevant external documentation such as RFCs or |
---|
550 | standards. |
---|
551 | |
---|
552 | If you have a mailing list set up for your module, mention it here. |
---|
553 | |
---|
554 | If you have a web site set up for your module, mention it here. |
---|
555 | |
---|
556 | =head1 AUTHOR |
---|
557 | |
---|
558 | Thauvin Olivier, E<lt>olivier.thauvin@latmosipsl.frE<gt> |
---|
559 | |
---|
560 | =head1 COPYRIGHT AND LICENSE |
---|
561 | |
---|
562 | Copyright (C) 2009 by Thauvin Olivier |
---|
563 | |
---|
564 | This library is free software; you can redistribute it and/or modify |
---|
565 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
566 | at your option, any later version of Perl 5 you may have available. |
---|
567 | |
---|
568 | |
---|
569 | =cut |
---|