1 | #!/usr/bin/perl |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use Getopt::Long; |
---|
6 | use Pod::Usage; |
---|
7 | use Config::IniFiles; |
---|
8 | use LATMOS::Accounts; |
---|
9 | use LATMOS::Accounts::SyncManager; |
---|
10 | use LATMOS::Accounts::Log; |
---|
11 | use 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 | |
---|
23 | GetOptions( |
---|
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 | |
---|
36 | Don't go into background |
---|
37 | |
---|
38 | =item -c|--config configdir |
---|
39 | |
---|
40 | Use this configuration instead default one |
---|
41 | |
---|
42 | =item --help |
---|
43 | |
---|
44 | Display this help |
---|
45 | |
---|
46 | =item --wait minutes |
---|
47 | |
---|
48 | Wait this number of minutes before process synchronisation (default is 5 |
---|
49 | minutes) |
---|
50 | |
---|
51 | =back |
---|
52 | |
---|
53 | =head1 CONFIUGRATION |
---|
54 | |
---|
55 | =head2 CONFIGURATION FILE |
---|
56 | |
---|
57 | See L<la-sync-manager.ini> |
---|
58 | |
---|
59 | =head2 AVAILABLE TASK MODULE |
---|
60 | |
---|
61 | =head3 L<LATMOS::Accounts::Task::Basessynchro> |
---|
62 | |
---|
63 | This module is always laod and run. It synchronize base given in |
---|
64 | L<latmos-accounts.ini> |
---|
65 | |
---|
66 | =head3 L<LATMOS::Accounts::Task::Dummy> |
---|
67 | |
---|
68 | A fake module for testing |
---|
69 | |
---|
70 | =head3 L<LATMOS::Accounts::Task::Buildlistes> |
---|
71 | |
---|
72 | This use L<la-sync-list.ini> config file to build mailing list file |
---|
73 | typically usuable by C<sync_members> mailman program. |
---|
74 | |
---|
75 | =head3 L<LATMOS::Accounts::Task::Buildnet> |
---|
76 | |
---|
77 | Generate DNS and DHCP files from C<netzone> objects |
---|
78 | |
---|
79 | =head3 L<LATMOS::Accounts::Task::Employment> |
---|
80 | |
---|
81 | This module update user's information according the status matching current |
---|
82 | date. |
---|
83 | |
---|
84 | =head3 L<LATMOS::Accounts::Task::Refreshexpired> |
---|
85 | |
---|
86 | Increasing revision number for user to force update in othere base not supporting |
---|
87 | properly expiration date. |
---|
88 | |
---|
89 | =head3 L<LATMOS::Accounts::Task::Updatedyndata> |
---|
90 | |
---|
91 | Force automatic object (such as group with filter) to be rebuilt in case it depend |
---|
92 | on external base event like date. |
---|
93 | |
---|
94 | =head3 L<LATMOS::Accounts::Task::Delexpiredusers> |
---|
95 | |
---|
96 | Make user's account unexported after expiration delay. |
---|
97 | |
---|
98 | =head3 L<LATMOS::Accounts::Task::Expiredaliases> |
---|
99 | |
---|
100 | Allow to automatically add an alias to user when account get expired. |
---|
101 | The basis idea is to redirect mail to an automatic responder claiming account is |
---|
102 | expired. |
---|
103 | |
---|
104 | =head3 L<LATMOS::Accounts::Task::Stats> |
---|
105 | |
---|
106 | Collect current data according C<Stat> objects definition. |
---|
107 | |
---|
108 | =head3 L<LATMOS::Accounts::Task::Unexportexpired> |
---|
109 | |
---|
110 | For C<Aliases> and <Nethost>, unexport object when expiration date is reached. |
---|
111 | |
---|
112 | =head3 L<LATMOS::Accounts::Task::Unusedhosts> |
---|
113 | |
---|
114 | Unexport C<Nethost> when owner/user is expired or unexported |
---|
115 | |
---|
116 | =head3 L<LATMOS::Accounts::Task::Iprecover> |
---|
117 | |
---|
118 | Delete IP set on nethost unexported and not midified for the specified delay. |
---|
119 | |
---|
120 | =cut |
---|
121 | |
---|
122 | my $needsync = 0; |
---|
123 | my $pidfile = undef; |
---|
124 | $wait ||= 5; # default in minutes |
---|
125 | |
---|
126 | my $syncm = LATMOS::Accounts::SyncManager->new($config) or do { |
---|
127 | la_log LA_ERR, "Cannot instanciate Sync Manager"; |
---|
128 | exit(1); |
---|
129 | }; |
---|
130 | |
---|
131 | la_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 |
---|
157 | if (!$nodaemon) { |
---|
158 | my $pid = fork; |
---|
159 | if ($pid) { |
---|
160 | exit 0; |
---|
161 | } |
---|
162 | } |
---|
163 | |
---|
164 | if ($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 | |
---|
181 | sub 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 | |
---|
209 | while (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 | |
---|
229 | L<la-sync-manager.ini>, L<latmos-accounts.ini> |
---|
230 | |
---|
231 | =head1 AUTHOR |
---|
232 | |
---|
233 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
234 | |
---|
235 | =head1 COPYRIGHT AND LICENSE |
---|
236 | |
---|
237 | Copyright (C) 2008, 2009, 2010, 2011, 2012 CNRS SA/CETP/LATMOS |
---|
238 | |
---|
239 | This library is free software; you can redistribute it and/or modify |
---|
240 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
241 | at your option, any later version of Perl 5 you may have available. |
---|
242 | |
---|
243 | =cut |
---|