1 | package LATMOS::Accounts::SyncManager; |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use LATMOS::Accounts::Log; |
---|
6 | use LATMOS::Accounts; |
---|
7 | |
---|
8 | sub new { |
---|
9 | my ($class, $config) = @_; |
---|
10 | |
---|
11 | my $ini = Config::IniFiles->new( |
---|
12 | -file => join('/', ($config || '/etc/latmos-accounts'), |
---|
13 | 'la-sync-manager.ini'), |
---|
14 | ) or do { |
---|
15 | la_log LA_ERR, "Cannot load config"; |
---|
16 | return; |
---|
17 | }; |
---|
18 | |
---|
19 | bless { |
---|
20 | ini => $ini, |
---|
21 | config => $config, |
---|
22 | lastrev => 0, |
---|
23 | }, $class; |
---|
24 | } |
---|
25 | |
---|
26 | sub ini { $_[0]->{ini} } |
---|
27 | |
---|
28 | sub dbrev { $_[0]->{lastrev} } |
---|
29 | |
---|
30 | sub updrev { |
---|
31 | my ($self) = @_; |
---|
32 | my $LA = LATMOS::Accounts->new($self->{config}, noacl => 1); |
---|
33 | my $base = $LA->default_base; |
---|
34 | my $newrev = $base->current_rev; |
---|
35 | $self->{lastrev} = $newrev; |
---|
36 | return $newrev; |
---|
37 | } |
---|
38 | |
---|
39 | sub list_modules { |
---|
40 | my ($self) = @_; |
---|
41 | $self->ini->Sections; |
---|
42 | } |
---|
43 | |
---|
44 | sub process_module { |
---|
45 | my ($self, $module, $dbrev) = @_; |
---|
46 | |
---|
47 | if (!$self->ini->SectionExists($module)) { |
---|
48 | la_log LA_ERR, "Cannot run inexistant module %s", $module; |
---|
49 | return; |
---|
50 | } |
---|
51 | |
---|
52 | my $modtype = $self->ini->val($module, 'type', $module); |
---|
53 | my $task = LATMOS::Accounts::Task->new( |
---|
54 | $modtype, |
---|
55 | config => $self->{config}, |
---|
56 | ) |
---|
57 | or do { |
---|
58 | la_log(LA_ERR, 'Cannot load module %s', $modtype); |
---|
59 | return; |
---|
60 | }; |
---|
61 | la_log LA_NOTICE, "Processing sync module %s (%s)", $module, $modtype; |
---|
62 | if (!$task->init) { |
---|
63 | la_log(LA_ERR, 'init() failed for module %s', $module); |
---|
64 | return; |
---|
65 | } |
---|
66 | |
---|
67 | if (!$task->needupd($dbrev, $self)) { |
---|
68 | la_log LA_DEBUG, "No change on main base, aborting"; |
---|
69 | return 1; |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | my $res = $task->run; |
---|
74 | if (!$res) { |
---|
75 | la_log LA_ERR, "Task %s did not end successfully", $module; |
---|
76 | } |
---|
77 | $task->post if ($res); |
---|
78 | la_log LA_DEBUG, "end process $module"; |
---|
79 | return $res; |
---|
80 | } |
---|
81 | |
---|
82 | 1; |
---|