Code

* fixes save_log probelm: install logs are now commited to si-server again
[gosa.git] / gosa-si / client / events / gosaTriggered.pm
2 =head1 NAME
4 gosaTriggered.pm
6 =head1 SYNOPSIS
8 use GOSA::GosaSupportDaemon;
10 use Data::Dumper;
12 use MIME::Base64
14 =head1 DESCRIPTION
16 This module contains all GOsa-SI-client processing instructions concerning actions controllable from GOsa.
18 =head1 VERSION
20 Version 1.0
22 =head1 AUTHOR
24 Andreas Rettenberger <rettenberger at gonicus dot de>
26 =head1 FUNCTIONS
28 =cut
31 package gosaTriggered;
32 use Exporter;
33 @ISA = qw(Exporter);
34 my @events = (
35     "get_events",
36     "usr_msg",
37     "trigger_action_localboot",
38     "trigger_action_halt",
39     "trigger_action_faireboot",
40     "trigger_action_reboot",
41     "trigger_action_reinstall",
42     "trigger_action_update",
43     "trigger_action_instant_update",
44     "trigger_goto_settings_reload",
45     );
46 @EXPORT = @events;
48 use strict;
49 use warnings;
50 use GOSA::GosaSupportDaemon;
51 use Data::Dumper;
52 use MIME::Base64;
53 use File::Temp qw/ tempfile/;
55 BEGIN {}
57 END {}
59 ###############################################################################
60 =over 
62 =item B<get_events ()>
64 =over
66 =item description 
68     Reports all provided functions.
70 =item parameter
72     None.
74 =item return 
76     \@events - ARRAYREF - array containing all functions 
78 =back
80 =back
82 =cut
83 ###############################################################################
84 sub get_events { return \@events; }
87 ###############################################################################
88 =over 
90 =item B<usr_msg ($$)>
92 =over
94 =item description 
96     Executes '/usr/bin/goto-notify' wich displays the message, subject und receiver at screen
98 =item parameter
100     $msg - STRING - complete GOsa-si message
101     $msg_hash - HASHREF - content of GOsa-si message in a hash
103 =item GOsa-si message xml content
104     
105     <to> - STRING - username message should be deliverd to
106     <subject> - STRING - subject of the message, base64 encoded
107     <message> - STRING - message itself, base64 encoded
109 =item return 
111     $out_msg - STRING - GOsa-si valid xml message, feedback that message was deliverd
113 =back
115 =back
117 =cut
118 ###############################################################################
119 sub usr_msg {
120     my ($msg, $msg_hash) = @_;
121     my $header = @{$msg_hash->{'header'}}[0];
122     my $source = @{$msg_hash->{'source'}}[0];
123     my $target = @{$msg_hash->{'target'}}[0];
125     my $to = @{$msg_hash->{'usr'}}[0];
126     my $subject = &decode_base64(@{$msg_hash->{'subject'}}[0]);
127     my $message = &decode_base64(@{$msg_hash->{'message'}}[0]);
129     my ($rand_fh, $rand_file) = tempfile( SUFFIX => '.goto_notify');
130     print $rand_fh "source:$source\ntarget:$target\nusr:$to\nsubject:".@{$msg_hash->{'subject'}}[0]."\nmessage:".@{$msg_hash->{'message'}}[0]."\n";
131     close $rand_fh;
133     my $feedback = system("/usr/bin/goto-notify user-message '$to' '$subject' '$message' '$rand_file' &" );
135     return
139 ###############################################################################
140 =over 
142 =item B<trigger_action_localboot ($$)>
144 =over
146 =item description 
148     Executes '/sbin/shutdown -r' if  no user is logged in otherwise write 
149     'trigger_action_localboot' to '/etc/gosa-si/event'
151 =item parameter
153     $msg - STRING - complete GOsa-si message
154     $msg_hash - HASHREF - content of GOsa-si message in a hash
156 =item GOsa-si message xml content
158     <timeout> - INTEGER - timeout to wait befor restart, default 0
160 =item return 
161     
162     Nothing.
164 =back
166 =back
168 =cut
169 ###############################################################################
170 sub trigger_action_localboot {
171     my ($msg, $msg_hash) = @_;
172     my $timeout;
174     if((not exists $msg_hash->{timeout} ) || (1 != @{$msg_hash->{timeout}} ) ) {
175         $timeout = -1;
176     } 
177     else {
178         $timeout = @{$msg_hash->{timeout}}[0];
179     }
181     # check logged in user
182     my $logged_in_user = 1;
183     if( $logged_in_user ) {
184         # TODO do something
185     }
186     else {
187         $timeout = 0;
188     }
189         
190     # execute function
191     if( $timeout == 0 ) {
192         print STDERR ("shutdown -r +$timeout\n");
193     }
194     elsif( $timeout > 0 ) {
195         print STDERR ("shutdown -r +$timeout\n");
196     }
197     elsif( $timeout < 0 ) {
198         print STDERR "The administrator has sent a signal to reboot this workstation. It will reboot after you've logged out.\n";
199         open(FILE, "> /etc/gosa-si/event");
200         print FILE "trigger_action_localboot\n";
201         close(FILE);
202     }
203     else {
204         # TODO do something, error handling, logging
205     }
207     return;
211 ###############################################################################
212 =over 
214 =item B<trigger_action_faireboot ($$)>
216 =over
218 =item description 
220     Executes '/usr/sbin/faireboot'.
222 =item parameter
224     $msg - STRING - complete GOsa-si message
225     $msg_hash - HASHREF - content of GOsa-si message in a hash
227 =item GOsa-si message xml content
229     None.
231 =item return 
232     
233     Nothing.
235 =back
237 =back
239 =cut
240 ###############################################################################
241 sub trigger_action_faireboot {
242     my ($msg, $msg_hash) = @_;
243         &main::daemon_log("DEBUG: run /usr/sbin/faireboot\n", 7); 
244     system("/usr/sbin/faireboot");
245     return;
249 ###############################################################################
250 =over 
252 =item B<trigger_action_reboot ($$)>
254 =over
256 =item description 
258     Executes '/usr/bin/goto-notify reboot' and '/sbin/shutdown -r'  if  no 
259     user is logged in otherwise write 'reboot' to '/etc/gosa-si/event'
261 =item parameter
263     $msg - STRING - complete GOsa-si message
264     $msg_hash - HASHREF - content of GOsa-si message in a hash
266 =item GOsa-si message xml content
268     <timeout> - INTEGER - timeout to wait befor reboot, default 0
270 =item return 
271     
272     Nothing.
274 =back
276 =back
278 =cut
279 ###############################################################################
280 sub trigger_action_reboot {
281     my ($msg, $msg_hash) = @_;
282     my $timeout;
284     if((not exists $msg_hash->{timeout} ) || (1 != @{$msg_hash->{timeout}} ) ) {
285         $timeout = 0;
286     } 
287     else {
288         $timeout = @{$msg_hash->{timeout}}[0];
289     }
291     # check logged in user
292     my @user_list = &get_logged_in_users;
293     if( @user_list >= 1 ) {
294         system( "/usr/bin/goto-notify reboot" );
295         open(FILE, "> /etc/gosa-si/event");
296         print FILE "reboot\n";
297         close(FILE);
298     }
299     else {
300         system( "/sbin/shutdown -r +$timeout &" );
301     }
303     return;
307 ###############################################################################
308 =over 
310 =item B<trigger_action_halt ($$)>
312 =over
314 =item description 
316     Executes '/usr/bin/goto-notify halt' and '/sbin/shutdown -h' if  no 
317     user is logged in otherwise write 'halt' to '/etc/gosa-si/event'
319 =item parameter
321     $msg - STRING - complete GOsa-si message
322     $msg_hash - HASHREF - content of GOsa-si message in a hash
324 =item GOsa-si message xml content
326     <timeout> - INTEGER - timeout to wait befor halt, default 0
328 =item return 
329     
330     Nothing.    
332 =back
334 =back
336 =cut
337 ###############################################################################
338 sub trigger_action_halt {
339     my ($msg, $msg_hash) = @_;
340     my $timeout;
342     if((not exists $msg_hash->{timeout} ) || (1 != @{$msg_hash->{timeout}} ) ) {
343         $timeout = 0;
344     } 
345     else {
346         $timeout = @{$msg_hash->{timeout}}[0];
347     }
349     # check logged in user
350     my @user_list = &get_logged_in_users;
351     if( @user_list >= 1 ) {
352         system( "/usr/bin/goto-notify halt" );
353         open(FILE, "> /etc/gosa-si/event");
354         print FILE "halt\n";
355         close(FILE);
356     }
357     else {
358         system( "/sbin/shutdown -h +$timeout &" );
359     }
361     return;
365 ###############################################################################
366 =over 
368 =item B<trigger_action_reinstall>
370 =over
372 =item description 
374     Executes '/usr/bin/goto-notify install' and '/sbin/shutdown -r now' if no 
375     user is logged in otherwise write 'install' to '/etc/gosa-si/event'
377 =item parameter
379     $msg - STRING - complete GOsa-si message
380     $msg_hash - HASHREF - content of GOsa-si message in a hash
382 =item GOsa-si message xml content
384     None.
386 =item return 
387     
388     Nothing.
390 =back
392 =back
394 =cut
395 ###############################################################################
396 sub trigger_action_reinstall {
397     my ($msg, $msg_hash) = @_;
399     # check logged in user
400     my @user_list = &get_logged_in_users;
401     if( @user_list >= 1 ) {
402         system( "/usr/bin/goto-notify install" );
403         open(FILE, "> /etc/gosa-si/event");
404         print FILE "install\n";
405         close(FILE);
406     }
407     else {
408         system( "/sbin/shutdown -r now &" );
409     }
411     return;
415 ###############################################################################
416 =over 
418 =item B<trigger_action_updae>
420 =over
422 =item description 
424     Executes 'DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &'
426 =item parameter
428     $msg - STRING - complete GOsa-si message
429     $msg_hash - HASHREF - content of GOsa-si message in a hash
431 =item GOsa-si message xml content
433     None.
435 =item return 
436     
437     Nothing
439 =back
441 =back
443 =cut
444 ###############################################################################
445 # Backward compatibility
446 sub trigger_action_update {
447     my ($msg, $msg_hash) = @_;
449     # Execute update
450     system( "DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &" );
452     return;
456 ###############################################################################
457 =over 
459 =item B<trigger_action_instant_update ($$)>
461 =over
463 =item description 
465     Executes 'DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &'
467 =item parameter
469     $msg - STRING - complete GOsa-si message
470     $msg_hash - HASHREF - content of GOsa-si message in a hash
472 =item GOsa-si message xml content
474     None.
476 =item return 
477     
478     Nothing.
480 =back
482 =back
484 =cut
485 ###############################################################################
486 # Backward compatibility
487 sub trigger_action_instant_update {
488     my ($msg, $msg_hash) = @_;
490     # Execute update
491     system( "DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &" );
493     return;
496 sub trigger_goto_settings_reload {
497     my ($msg, $msg_hash) = @_;
499     # Execute goto settings reload
500     my $cmd = "/etc/init.d/goto-agents";
501     my $pram = "start";
502     if (-f $cmd){
503         my $feedback = system("$cmd $pram") or &main::daemon_log("ERROR: $@");
504     } else {
505         &main::daemon_log("ERROR: cannot exec $cmd, file not found!");
506     }
508     return;
512 1;