[861] | 1 | package LATMOS::Accounts::SyncManager; |
---|
| 2 | |
---|
| 3 | use strict; |
---|
| 4 | use warnings; |
---|
| 5 | use LATMOS::Accounts::Log; |
---|
| 6 | use LATMOS::Accounts; |
---|
[1200] | 7 | use Config::IniFiles; |
---|
[861] | 8 | |
---|
[1023] | 9 | =head1 NAME |
---|
| 10 | |
---|
| 11 | LATMOS::Accounts::SyncManager - Routine task manager |
---|
| 12 | |
---|
| 13 | =head1 DESCRIPTION |
---|
| 14 | |
---|
| 15 | Handle Task process run by L<la-sync-manager> |
---|
| 16 | |
---|
| 17 | =head1 FUNCTIONS |
---|
| 18 | |
---|
| 19 | =cut |
---|
| 20 | |
---|
| 21 | =head2 new($config) |
---|
| 22 | |
---|
| 23 | Instaciate object. |
---|
| 24 | |
---|
| 25 | C<$config> is a path to an alternative configuration file to default one. |
---|
| 26 | |
---|
| 27 | =cut |
---|
| 28 | |
---|
[861] | 29 | sub new { |
---|
| 30 | my ($class, $config) = @_; |
---|
| 31 | |
---|
| 32 | my $ini = Config::IniFiles->new( |
---|
| 33 | -file => join('/', ($config || '/etc/latmos-accounts'), |
---|
| 34 | 'la-sync-manager.ini'), |
---|
| 35 | ) or do { |
---|
| 36 | la_log LA_ERR, "Cannot load config"; |
---|
| 37 | return; |
---|
| 38 | }; |
---|
| 39 | |
---|
[1198] | 40 | foreach my $section (qw(basessynchro refreshexpired automatedrequest)) { |
---|
| 41 | if ( !grep { |
---|
| 42 | lc($ini->val($_, 'type', '')) eq $section |
---|
| 43 | } $ini->Sections ) { |
---|
| 44 | $ini->newval("_$section", 'type', $section); |
---|
| 45 | } |
---|
[1135] | 46 | } |
---|
| 47 | |
---|
[861] | 48 | bless { |
---|
| 49 | ini => $ini, |
---|
| 50 | config => $config, |
---|
| 51 | lastrev => 0, |
---|
| 52 | }, $class; |
---|
| 53 | } |
---|
| 54 | |
---|
[1023] | 55 | =head2 ini |
---|
| 56 | |
---|
| 57 | Return a reference to the L<Ini::Files> object handling configuration. |
---|
| 58 | |
---|
| 59 | =cut |
---|
| 60 | |
---|
[861] | 61 | sub ini { $_[0]->{ini} } |
---|
| 62 | |
---|
[1023] | 63 | =head2 dbrev |
---|
| 64 | |
---|
| 65 | Return the current base revision |
---|
| 66 | |
---|
| 67 | =cut |
---|
| 68 | |
---|
[1200] | 69 | sub dbrev { |
---|
| 70 | my ($self) = @_; |
---|
| 71 | my $LA = LATMOS::Accounts->new($self->{config}, noacl => 1); |
---|
| 72 | my $base = $LA->base; |
---|
| 73 | return $base->current_rev; |
---|
| 74 | } |
---|
[861] | 75 | |
---|
[1200] | 76 | sub _load_state_ini { |
---|
| 77 | my ($self) = @_; |
---|
| 78 | my $la = LATMOS::Accounts->new($self->{config}, noacl => 1); |
---|
[1023] | 79 | |
---|
[1200] | 80 | my $state_file = $la->val('_default_', 'state_dir', '/'); |
---|
| 81 | $state_file .= '/la-sync-manager.ini'; |
---|
| 82 | la_log(LA_DEBUG, "Status file is %s", $state_file); |
---|
| 83 | if ($state_file && ! -w $state_file) { |
---|
| 84 | # don't exists, we have to create it |
---|
| 85 | open(my $handle, '>', $state_file) or do { |
---|
| 86 | la_log(LA_ERR, "Cannot open build net status file %s", |
---|
| 87 | $state_file); |
---|
| 88 | return; |
---|
| 89 | }; |
---|
| 90 | print $handle "[_default_]\n"; |
---|
| 91 | close($handle); |
---|
| 92 | } |
---|
| 93 | return Config::IniFiles->new( |
---|
| 94 | -file => $state_file |
---|
| 95 | ); |
---|
| 96 | } |
---|
[1023] | 97 | |
---|
[1200] | 98 | =head2 get_last_rev($module) |
---|
| 99 | |
---|
| 100 | Return the last revision the module succeed |
---|
| 101 | |
---|
[1023] | 102 | =cut |
---|
| 103 | |
---|
[1200] | 104 | sub get_last_rev { |
---|
| 105 | my ($self, $module) = @_; |
---|
| 106 | |
---|
| 107 | my $ini = $self->_load_state_ini or return; |
---|
| 108 | |
---|
| 109 | return ($ini->val($module, 'rev', 0), $ini->val($module, 'time', 0)); |
---|
[861] | 110 | } |
---|
| 111 | |
---|
[1023] | 112 | =head2 list_modules |
---|
| 113 | |
---|
| 114 | List configured task module |
---|
| 115 | |
---|
| 116 | =cut |
---|
| 117 | |
---|
[861] | 118 | sub list_modules { |
---|
| 119 | my ($self) = @_; |
---|
| 120 | $self->ini->Sections; |
---|
| 121 | } |
---|
| 122 | |
---|
[1200] | 123 | =head2 process_module($module) |
---|
[1023] | 124 | |
---|
| 125 | Process C<$module>. |
---|
| 126 | |
---|
| 127 | =cut |
---|
| 128 | |
---|
| 129 | # TODO what is $dbrev, why is it need here ?? |
---|
| 130 | |
---|
[861] | 131 | sub process_module { |
---|
[1200] | 132 | my ($self, $module) = @_; |
---|
[861] | 133 | |
---|
[1016] | 134 | eval { |
---|
| 135 | if (!$self->ini->SectionExists($module)) { |
---|
| 136 | la_log LA_ERR, "Cannot run inexistant module %s", $module; |
---|
| 137 | return; |
---|
| 138 | } |
---|
[861] | 139 | |
---|
[1016] | 140 | my $modtype = $self->ini->val($module, 'type', $module); |
---|
| 141 | my $task = LATMOS::Accounts::Task->new( |
---|
| 142 | $modtype, |
---|
| 143 | config => $self->{config}, |
---|
[1200] | 144 | name => $module, |
---|
| 145 | syncm => $self, |
---|
[1016] | 146 | ) |
---|
| 147 | or do { |
---|
| 148 | la_log(LA_ERR, 'Cannot load module %s', $modtype); |
---|
| 149 | return; |
---|
| 150 | }; |
---|
| 151 | la_log LA_NOTICE, "Processing sync module %s (%s)", $module, $modtype; |
---|
| 152 | if (!$task->init) { |
---|
| 153 | la_log(LA_ERR, 'init() failed for module %s', $module); |
---|
| 154 | return; |
---|
| 155 | } |
---|
[861] | 156 | |
---|
[1200] | 157 | my ($lastrev) = $self->get_last_rev($module); |
---|
| 158 | my $currentrev = $self->dbrev; |
---|
| 159 | if (!$task->needupd($lastrev, $currentrev, $self)) { |
---|
[1016] | 160 | la_log LA_DEBUG, "No change on main base, aborting"; |
---|
| 161 | return 1; |
---|
| 162 | } |
---|
[861] | 163 | |
---|
[1016] | 164 | my $res = $task->run; |
---|
| 165 | if (!$res) { |
---|
[1203] | 166 | my $ini = $self->_load_state_ini or return; |
---|
| 167 | $ini->newval($module, 'error', LATMOS::Accounts::Log::lastmessage()); |
---|
[1016] | 168 | la_log LA_ERR, "Task %s did not end successfully", $module; |
---|
[1203] | 169 | $ini->RewriteConfig; |
---|
| 170 | } else { |
---|
| 171 | $task->post if ($res); |
---|
| 172 | my $ini = $self->_load_state_ini or return; |
---|
[1200] | 173 | |
---|
[1203] | 174 | $ini->delval($module, 'error'); |
---|
| 175 | $ini->newval($module, 'rev', $currentrev); |
---|
| 176 | my $time = time; |
---|
| 177 | $ini->newval($module, 'time', $time); |
---|
| 178 | $ini->SetParameterComment( |
---|
| 179 | $module, 'time', |
---|
| 180 | scalar(localtime(time)), |
---|
| 181 | ); |
---|
| 182 | $ini->RewriteConfig; |
---|
[1200] | 183 | |
---|
[1203] | 184 | la_log LA_DEBUG, "end process $module"; |
---|
| 185 | } |
---|
[1016] | 186 | return $res; |
---|
| 187 | }; |
---|
| 188 | |
---|
| 189 | if ($@) { |
---|
| 190 | la_log(LA_CRIT, 'Fatal Perl Error: %s', $@); |
---|
| 191 | return; |
---|
[1017] | 192 | } else { |
---|
| 193 | return 1; |
---|
[861] | 194 | } |
---|
| 195 | } |
---|
| 196 | |
---|
[1206] | 197 | =head2 reset_module_savepoint($module) |
---|
[1201] | 198 | |
---|
[1206] | 199 | Reset the savepoint for module C<$module> to force it to run at next |
---|
[1201] | 200 | synchronisation. |
---|
| 201 | |
---|
| 202 | =cut |
---|
| 203 | |
---|
[1206] | 204 | sub reset_module_savepoint { |
---|
[1201] | 205 | my ($self, $module) = @_; |
---|
| 206 | if (!$self->ini->SectionExists($module)) { |
---|
| 207 | la_log LA_ERR, "Cannot run inexistant module %s", $module; |
---|
| 208 | return; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | my $modtype = $self->ini->val($module, 'type', $module); |
---|
| 212 | my $task = LATMOS::Accounts::Task->new( |
---|
| 213 | $modtype, |
---|
| 214 | config => $self->{config}, |
---|
| 215 | name => $module, |
---|
| 216 | syncm => $self, |
---|
| 217 | ) |
---|
| 218 | or do { |
---|
| 219 | la_log(LA_ERR, 'Cannot load module %s', $modtype); |
---|
| 220 | return; |
---|
| 221 | }; |
---|
| 222 | $task->init or do { |
---|
| 223 | la_log LA_ERR, "Cannot init module %s", $_; |
---|
| 224 | return; |
---|
| 225 | }; |
---|
| 226 | |
---|
| 227 | my $ini = $self->_load_state_ini or return; |
---|
| 228 | |
---|
| 229 | $ini->delval($module, 'rev'); |
---|
| 230 | $ini->newval($module, 'time', 0); |
---|
| 231 | $ini->SetParameterComment( |
---|
| 232 | $module, 'time', |
---|
| 233 | 'Reset to 0 the ' . scalar(localtime(time)), |
---|
| 234 | ); |
---|
| 235 | |
---|
[1206] | 236 | $task->reset_savepoint; # Specific reset |
---|
[1201] | 237 | $ini->RewriteConfig; |
---|
| 238 | } |
---|
| 239 | |
---|
[861] | 240 | 1; |
---|
[1023] | 241 | |
---|
| 242 | __END__ |
---|
| 243 | |
---|
| 244 | =head1 SEE ALSO |
---|
| 245 | |
---|
| 246 | L<LATMOS::Accountsi::Task> |
---|
| 247 | |
---|
| 248 | =head1 AUTHOR |
---|
| 249 | |
---|
| 250 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
| 251 | |
---|
| 252 | =head1 COPYRIGHT AND LICENSE |
---|
| 253 | |
---|
| 254 | Copyright (C) 2012 CNRS SA/CETP/LATMOS |
---|
| 255 | |
---|
| 256 | This library is free software; you can redistribute it and/or modify |
---|
| 257 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
| 258 | at your option, any later version of Perl 5 you may have available. |
---|
| 259 | |
---|
| 260 | =cut |
---|