source: trunk/LATMOS-Accounts/bin/la-sync-manager @ 2379

Last change on this file since 2379 was 2023, checked in by nanardon, 7 years ago

Doc completion

File size: 5.1 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use Getopt::Long;
6use Pod::Usage;
7use Config::IniFiles;
8use LATMOS::Accounts;
9use LATMOS::Accounts::SyncManager;
10use LATMOS::Accounts::Log;
11use LATMOS::Accounts::Task;
12
13=head1 NAME
14
15    la-sync-manager - Daemon performing synchronisation and various tasks
16
17=head1 SYNOPSIS
18
19    la-sync-manager [options...]
20
21=cut
22
23GetOptions(
24    'nodaemon' => \my $nodaemon,
25    'c|config=s'   => \my $config,
26    'help'         => sub { pod2usage(0) },
27    'wait=i'       => \my $wait,
28) or pod2usage();
29
30=head1 OPTIONS
31
32=over 4
33
34=item --nodaemon
35
36Don't go into background
37
38=item -c|--config configdir
39
40Use this configuration instead default one
41
42=item --help
43
44Display this help
45
46=item --wait minutes
47
48Wait this number of minutes before process synchronisation (default is 5
49minutes)
50
51=back
52
53=head1 CONFIUGRATION
54
55=head2 CONFIGURATION FILE
56
57See L<la-sync-manager.ini>
58
59=head2 AVAILABLE TASK MODULE
60
61=head3 L<LATMOS::Accounts::Task::Basessynchro>
62
63This module is always laod and run. It synchronize base given in
64L<latmos-accounts.ini>
65
66=head3 L<LATMOS::Accounts::Task::Dummy>
67
68A fake module for testing
69
70=head3 L<LATMOS::Accounts::Task::Buildlistes>
71
72This use L<la-sync-list.ini> config file to build mailing list file
73typically usuable by C<sync_members> mailman program.
74
75=head3 L<LATMOS::Accounts::Task::Buildnet>
76
77Generate DNS and DHCP files from C<netzone> objects
78
79=head3 L<LATMOS::Accounts::Task::Employment>
80
81This module update user's information according the status matching current
82date.
83
84=head3 L<LATMOS::Accounts::Task::Refreshexpired>
85
86Increasing revision number for user to force update in othere base not supporting
87properly expiration date.
88
89=head3 L<LATMOS::Accounts::Task::Updatedyndata>
90
91Force automatic object (such as group with filter) to be rebuilt in case it depend
92on external base event like date.
93
94=head3 L<LATMOS::Accounts::Task::Delexpiredusers>
95
96Make user's account unexported after expiration delay.
97
98=head3 L<LATMOS::Accounts::Task::Expiredaliases>
99
100Allow to automatically add an alias to user when account get expired.
101The basis idea is to redirect mail to an automatic responder claiming account is
102expired.
103
104=head3 L<LATMOS::Accounts::Task::Stats>
105
106Collect current data according C<Stat> objects definition.
107
108=head3 L<LATMOS::Accounts::Task::Unexportexpired>
109
110For C<Aliases> and <Nethost>, unexport object when expiration date is reached.
111
112=head3 L<LATMOS::Accounts::Task::Unusedhosts>
113
114Unexport C<Nethost> when owner/user is expired or unexported
115
116=head3 L<LATMOS::Accounts::Task::Iprecover>
117
118Delete IP set on nethost unexported and not midified for the specified delay.
119
120=cut
121
122my $needsync = 0;
123my $pidfile = undef;
124$wait ||= 5; # default in minutes
125
126my $syncm = LATMOS::Accounts::SyncManager->new($config) or do {
127    la_log LA_ERR, "Cannot instanciate Sync Manager";
128    exit(1);
129};
130
131la_set_log(
132    syslog => [],
133    console => ($nodaemon ? LA_NOTICE : undef),
134);
135
136# Trap perl message, send it to log
137$SIG{__DIE__} = sub {
138    la_log LA_ERR, "Die: %s", $_[0] unless($_[0] =~ /^Can't locate/);
139    die $_[0];
140};
141$SIG{__WARN__} = sub {
142    la_log LA_WARN, "Warn: %s", $_[0];
143    warn $_[0];
144};
145
146{
147    my $LA = LATMOS::Accounts->new($config, noacl => 1);
148    if ($LA->val('_default_', 'state_dir')) {
149        $pidfile = $LA->val('_default_', 'state_dir') . '/sync-manager.pid';
150    }
151    if (my $mail = $LA->val('_network_', 'maillog')) {
152            la_set_log(mail => $mail);
153    }
154}
155
156# Daemonize
157if (!$nodaemon) {
158    my $pid = fork;
159    if ($pid) {
160        exit 0;
161    }
162}
163
164if ($pidfile) {
165    # maybe we want to lock here !!
166    open(my $fh, '>', $pidfile) or die "Cannot open pidfile $pidfile\n";
167    print $fh "$$\n";
168    close($fh);
169}
170
171$SIG{INT} = sub {
172    unlink($pidfile) if ($pidfile);
173    exit 0;
174};
175
176$SIG{'HUP'} = sub {
177    $needsync = 1;
178    la_log LA_NOTICE, "SigHup received, synchronise now";
179};
180
181sub process {
182   
183    la_log LA_NOTICE, "Start processing modules";
184    my $pid = fork();
185   
186    if ($pid == 0) {
187        $SIG{INT} = 'DEFAULT';
188        my $res = 0;
189        foreach my $module ($syncm->listSortedModules()) {
190            $res = 1 if (!$syncm->process_module($module));
191        }
192        exit($res);
193    }
194
195    my $retpid;
196    while(($retpid = waitpid(-1, 0)) <= 0) {}
197    local $SIG{HUP} = 'IGNORE';
198    if ($retpid) {
199        my $res = $? >> 8;
200        if ($res) {
201            la_log LA_ERR, "Sync process exit with $res";
202            return;
203        }
204    }
205    la_log LA_NOTICE, "End processing modules";
206    return 1;
207}
208
209while (1) {
210    $needsync = 0;
211
212    process(); 
213
214    # waiting, to perform next sync
215    if ($needsync) {
216        la_log(LA_DEBUG, "NeedSync received");
217        next;
218    }
219
220    la_log(LA_DEBUG, "Sleeping $wait minutes");
221    sleep($wait * 60);
222
223}
224
225__END__
226
227=head1 SEE ALSO
228
229L<la-sync-manager.ini>, L<latmos-accounts.ini>
230
231=head1 AUTHOR
232
233Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
234
235=head1 COPYRIGHT AND LICENSE
236
237Copyright (C) 2008, 2009, 2010, 2011, 2012 CNRS SA/CETP/LATMOS
238
239This library is free software; you can redistribute it and/or modify
240it under the same terms as Perl itself, either Perl version 5.10.0 or,
241at your option, any later version of Perl 5 you may have available.
242
243=cut
Note: See TracBrowser for help on using the repository browser.