From: rettenbe Date: Mon, 4 Feb 2008 16:27:25 +0000 (+0000) Subject: server events are executed within perl X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=1e03083c09cde004785cfc0819aae47a2cda0d7f;p=gosa.git server events are executed within perl git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@8742 594d385d-05f5-0310-b6e9-bd551577e9d8 --- diff --git a/gosa-si/client/events/installation.pm b/gosa-si/client/events/installation.pm new file mode 100644 index 000000000..a3e62fbf9 --- /dev/null +++ b/gosa-si/client/events/installation.pm @@ -0,0 +1,39 @@ +package installation; +use Exporter; +@ISA = qw(Exporter); +my @events = qw(get_events set_activated_for_installation); +@EXPORT = @events; + +use strict; +use warnings; +use Data::Dumper; +use Fcntl; + +BEGIN {} + +END {} + +### Start ###################################################################### + + +sub get_events { + return \@events; +} + +sub set_activated_for_installation { + my ($msg, $msg_hash) = @_ ; + my $header = @{$msg_hash->{'header'}}[0]; + my $target = @{$msg_hash->{'target'}}[0]; + my $source = @{$msg_hash->{'source'}}[0]; + + my $Datei = "/tmp/set_activated_for_installation"; + open(DATEI, ">$Datei"); + print DATEI "$msg\n"; + close DATEI; + + return; +} + + + +1; diff --git a/gosa-si/modules/GosaPackages.pm b/gosa-si/modules/GosaPackages.pm index b58fe246a..1a219a989 100644 --- a/gosa-si/modules/GosaPackages.pm +++ b/gosa-si/modules/GosaPackages.pm @@ -13,6 +13,9 @@ use Data::Dumper; use GOSA::DBsqlite; use MIME::Base64; +my $event_dir = "/usr/lib/gosa-si/server/events"; +use lib "/usr/lib/gosa-si/server/events"; + BEGIN{} END{} @@ -22,6 +25,7 @@ my ($gosa_activ, $gosa_ip, $gosa_mac_address, $gosa_port, $gosa_passwd, $network my ($job_queue_timeout, $job_queue_file_name); my $gosa_server; +my $event_hash; my %cfg_defaults = ("general" => @@ -33,7 +37,6 @@ my %cfg_defaults = "server_port" => [\$server_port, "20081"], "server_passwd" => [\$server_passwd, ""], "max_clients" => [\$max_clients, 100], - "server_event_dir" => [\$server_event_dir, '/usr/lib/gosa-si/server/events'], }, "bus" => {"bus_activ" => [\$bus_activ, "on"], @@ -67,6 +70,9 @@ my $gosa_address = "$gosa_ip:$gosa_port"; my $xml = new XML::Simple(); +# import events +&import_events(); + ## FUNCTIONS ################################################################# sub get_module_info { @@ -227,6 +233,33 @@ sub get_ip { } +sub import_events { + if (not -e $event_dir) { + daemon_log("ERROR: cannot find directory or directory is not readable: $event_dir", 1); + } + opendir (DIR, $event_dir) or die "ERROR while loading gosa-si-events from directory $event_dir : $!\n"; + + while (defined (my $event = readdir (DIR))) { + if( $event eq "." || $event eq ".." ) { next; } + + eval{ require $event; }; + if( $@ ) { + daemon_log("import of event module '$event' failed", 1); + daemon_log("$@", 8); + next; + } + + $event =~ /(\S*?).pm$/; + my $event_module = $1; + my $events_l = eval( $1."::get_events()") ; + foreach my $event_name (@{$events_l}) { + $event_hash->{$event_name} = $event_module; + } + + } +} + + #=== FUNCTION ================================================================ # NAME: process_incoming_msg # PARAMETERS: crypted_msg - string - incoming crypted message @@ -244,7 +277,7 @@ sub process_incoming_msg { $out_msg = &process_job_msg($msg, $msg_hash); } elsif ($header =~ /^gosa_/) { - $out_msg = &process_gosa_msg($msg, $header); + $out_msg = &process_gosa_msg($msg, $msg_hash); } else { &main::daemon_log("ERROR: $header is not a valid GosaPackage-header, need a 'job_' or a 'gosa_' prefix"); @@ -268,8 +301,10 @@ sub process_incoming_msg { sub process_gosa_msg { - my ($msg, $header) = @_ ; + my ($msg, $msg_hash) = @_ ; my $out_msg; + + my $header = @{$msg_hash->{'header'}}[0]; $header =~ s/gosa_//; # decide wether msg is a core function or a event handler @@ -290,26 +325,33 @@ sub process_gosa_msg { $out_msg = &send_msg("trigger_wake", $server_address, "KNOWN_SERVER", \%data); } else { # msg could not be assigned to core function - # fetch all available eventhandler under $server_event_dir - opendir (DIR, $server_event_dir) or &main::daemon_log("ERROR cannot open $server_event_dir: $!\n", 1) and return; - while (defined (my $file = readdir (DIR))) { - if (not $file eq $header) { - next; - } - # try to deliver incoming msg to eventhandler - my $cmd = File::Spec->join($server_event_dir, $header)." '$msg'"; - &main::daemon_log("GosaPackages: execute event_handler $header", 3); - &main::daemon_log("GosaPackages: cmd: $cmd", 8); - - $out_msg = ""; - open(PIPE, "$cmd 2>&1 |"); - while() { - $out_msg.=$_; - } - close(PIPE); - &main::daemon_log("GosaPackages: answer of cmd: $out_msg", 5); - last; - } + # maybe it is an eventa + if( exists $event_hash->{$header} ) { + # a event exists with the header as name + &main::daemon_log("found event '$header' at event-module '".$event_hash->{$header}."'", 5); + no strict 'refs'; + $out_msg = &{$event_hash->{$header}."::$header"}($msg, $msg_hash); + } + +# opendir (DIR, $server_event_dir) or &main::daemon_log("ERROR cannot open $server_event_dir: $!\n", 1) and return; +# while (defined (my $file = readdir (DIR))) { +# if (not $file eq $header) { +# next; +# } +# # try to deliver incoming msg to eventhandler +# my $cmd = File::Spec->join($server_event_dir, $header)." '$msg'"; +# &main::daemon_log("GosaPackages: execute event_handler $header", 3); +# &main::daemon_log("GosaPackages: cmd: $cmd", 8); +# +# $out_msg = ""; +# open(PIPE, "$cmd 2>&1 |"); +# while() { +# $out_msg.=$_; +# } +# close(PIPE); +# &main::daemon_log("GosaPackages: answer of cmd: $out_msg", 5); +# last; +# } } # if delivery not possible raise error and return diff --git a/gosa-si/server/events/installation.pm b/gosa-si/server/events/installation.pm new file mode 100644 index 000000000..a02c179ff --- /dev/null +++ b/gosa-si/server/events/installation.pm @@ -0,0 +1,35 @@ +package installation; +use Exporter; +@ISA = qw(Exporter); +my @events = qw(get_events set_activated_for_installation ); +@EXPORT = @events; + +use strict; +use warnings; +use GOSA::GosaSupportDaemon; + + +BEGIN {} + +END {} + +### Start ###################################################################### + +#&main::read_configfile($main::cfg_file, %cfg_defaults); + +sub get_events { + return \@events; +} + +sub set_activated_for_installation { + my ($msg, $msg_hash) = @_; + + my $header = @{$msg_hash->{header}}[0]; + my $source = @{$msg_hash->{source}}[0]; + my $target = @{$msg_hash->{target}}[0]; + + my $out_hash = &create_xml_hash("set_activated_for_installation", $source, $target); + my $out_msg = &create_xml_string($out_hash); + + return $out_msg; +} diff --git a/gosa-si/server/events/ping b/gosa-si/server/events/ping deleted file mode 100755 index f8ae99606..000000000 --- a/gosa-si/server/events/ping +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/perl - -use strict; -use warnings; -use GosaSupportDaemon; - - -# transform msg to hash -my $hash = &transform_msg2hash($ARGV[0]); - -# extract from hash all what you need -my $header = @{$hash->{header}}[0]; -my $source = @{$hash->{source}}[0]; -my $target = @{$hash->{target}}[0]; -my $jobdb_id = @{$hash->{jobdb_id}}[0]; - -# and do what ever you want - - -my $out_hash = &create_xml_hash("got_ping", "10.89.1.155:10001", "10.89.1.155:10000"); -if (defined $jobdb_id) { - &add_content2xml_hash($out_hash, 'jobdb_id', $jobdb_id); -} -my $out_xml = &create_xml_string($out_hash); -print $out_xml; - diff --git a/gosa-si/server/events/set_activated_for_installation b/gosa-si/server/events/set_activated_for_installation deleted file mode 100755 index 7ec09dad0..000000000 --- a/gosa-si/server/events/set_activated_for_installation +++ /dev/null @@ -1,37 +0,0 @@ -#!/usr/bin/perl -#=============================================================================== -# -# FILE: set_activated_for_installation.pl -# -# USAGE: ./set_activated_for_installation.pl -# -# DESCRIPTION: -# -# OPTIONS: --- -# REQUIREMENTS: --- -# BUGS: --- -# NOTES: --- -# AUTHOR: (), <> -# COMPANY: -# VERSION: 1.0 -# CREATED: 28.01.2008 17:31:37 CET -# REVISION: --- -#=============================================================================== - -use strict; -use warnings; - -use GOSA::GosaSupportDaemon; - - -# transform msg to hash -my $hash = &transform_msg2hash($ARGV[0]); - -# extract from hash all what you need -my $header = @{$hash->{header}}[0]; -my $source = @{$hash->{source}}[0]; -my $target = @{$hash->{target}}[0]; - -my $out_hash = &create_xml_hash("set_activated_for_installation", $source, $target); -my $out_msg = &create_xml_string($out_hash); -print $out_msg;