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

Last change on this file was 2496, checked in by nanardon, 3 years ago

Add default value for state_dir, factorize it

File size: 5.1 KB
RevLine 
[610]1#!/usr/bin/perl
[578]2
3use strict;
4use warnings;
5use Getopt::Long;
6use Pod::Usage;
[861]7use Config::IniFiles;
[578]8use LATMOS::Accounts;
[861]9use LATMOS::Accounts::SyncManager;
[578]10use LATMOS::Accounts::Log;
[861]11use LATMOS::Accounts::Task;
[578]12
[1023]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
[578]23GetOptions(
24    'nodaemon' => \my $nodaemon,
25    'c|config=s'   => \my $config,
26    'help'         => sub { pod2usage(0) },
[861]27    'wait=i'       => \my $wait,
[578]28) or pod2usage();
29
[1023]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
[2023]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
[1023]120=cut
121
[1740]122my $needsync = 0;
[578]123my $pidfile = undef;
[861]124$wait ||= 5; # default in minutes
[578]125
[861]126my $syncm = LATMOS::Accounts::SyncManager->new($config) or do {
127    la_log LA_ERR, "Cannot instanciate Sync Manager";
128    exit(1);
129};
130
[578]131la_set_log(
132    syslog => [],
133    console => ($nodaemon ? LA_NOTICE : undef),
134);
135
[1141]136# Trap perl message, send it to log
137$SIG{__DIE__} = sub {
[1150]138    la_log LA_ERR, "Die: %s", $_[0] unless($_[0] =~ /^Can't locate/);
[1141]139    die $_[0];
140};
141$SIG{__WARN__} = sub {
[1150]142    la_log LA_WARN, "Warn: %s", $_[0];
[1141]143    warn $_[0];
144};
[861]145
[578]146{
147    my $LA = LATMOS::Accounts->new($config, noacl => 1);
[2496]148    if (my $dir = $LA->state_dir) {
149        $pidfile = $dir . '/sync-manager.pid';
[578]150    }
[861]151    if (my $mail = $LA->val('_network_', 'maillog')) {
152            la_set_log(mail => $mail);
153    }
[578]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
[816]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
[861]181sub process {
[1740]182   
[1744]183    la_log LA_NOTICE, "Start processing modules";
[1740]184    my $pid = fork();
185   
[861]186    if ($pid == 0) {
187        $SIG{INT} = 'DEFAULT';
[1740]188        my $res = 0;
[1824]189        foreach my $module ($syncm->listSortedModules()) {
[1740]190            $res = 1 if (!$syncm->process_module($module));
191        }
[1741]192        exit($res);
[861]193    }
[1740]194
[915]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;
[861]203        }
204    }
[1744]205    la_log LA_NOTICE, "End processing modules";
[915]206    return 1;
[861]207}
208
[578]209while (1) {
[1740]210    $needsync = 0;
211
212    process(); 
213
214    # waiting, to perform next sync
[578]215    if ($needsync) {
[1740]216        la_log(LA_DEBUG, "NeedSync received");
217        next;
[578]218    }
219
[1740]220    la_log(LA_DEBUG, "Sleeping $wait minutes");
221    sleep($wait * 60);
222
[578]223}
224
[1023]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.