From 0e2de0eb04845891d90f6bf127224ac5110dcf1c Mon Sep 17 00:00:00 2001 From: rettenbe Date: Wed, 20 Feb 2008 18:49:27 +0000 Subject: [PATCH] bugfix: job queue can handle gosa triggered events git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@8997 594d385d-05f5-0310-b6e9-bd551577e9d8 --- gosa-si/gosa-si-server | 97 ++++++++++++++------------------- gosa-si/modules/GosaPackages.pm | 73 +++++++++++++++++-------- gosa-si/tests/client.php | 6 +- 3 files changed, 93 insertions(+), 83 deletions(-) diff --git a/gosa-si/gosa-si-server b/gosa-si/gosa-si-server index 5e61e9a55..10a6d1185 100755 --- a/gosa-si/gosa-si-server +++ b/gosa-si/gosa-si-server @@ -362,9 +362,9 @@ $SIG{INT} = \&sig_int_handler; sub check_key_and_xml_validity { my ($crypted_msg, $module_key) = @_; - my $msg; my $msg_hash; + my $error_string; eval{ $msg = &decrypt_msg($crypted_msg, $module_key); @@ -372,40 +372,36 @@ sub check_key_and_xml_validity { &main::daemon_log("decrypted_msg: \n$msg", 8); $msg_hash = $xml->XMLin($msg, ForceArray=>1); + ############## # check header + if( not exists $msg_hash->{'header'} ) { die "no header specified"; } my $header_l = $msg_hash->{'header'}; - if( 1 != @{$header_l} ) { - die 'header error'; - } + if( 1 > @{$header_l} ) { die 'empty header tag'; } + if( 1 < @{$header_l} ) { die 'more than one header specified'; } my $header = @{$header_l}[0]; - if( 0 == length $header) { - die 'header error'; - } + if( 0 == length $header) { die 'empty string in header tag'; } + ############## # check source + if( not exists $msg_hash->{'source'} ) { die "no source specified"; } my $source_l = $msg_hash->{'source'}; - if( not defined @{$source_l} or 1 != @{$source_l} ) { - die 'source error'; - } + if( 1 > @{$source_l} ) { die 'empty source tag'; } + if( 1 < @{$source_l} ) { die 'more than one source specified'; } my $source = @{$source_l}[0]; - if( 0 == length $source) { - die 'source error'; - } + if( 0 == length $source) { die 'source error'; } + ############## # check target + if( not exists $msg_hash->{'target'} ) { die "no target specified"; } my $target_l = $msg_hash->{'target'}; - if( 1 != @{$target_l} ) { - die'target error'; - } - my $target = @{$target_l}[0]; - if( 0 == length $target) { - die 'target error'; - } + if( 1 > @{$target_l} ) { die 'empty target tag'; } } }; if($@) { &main::daemon_log("WARNING: do not understand the message", 5); &main::daemon_log("$@", 8); + $msg = undef; + $msg_hash = undef; } return ($msg, $msg_hash); @@ -555,6 +551,7 @@ sub input_from_unknown_host { no strict "refs"; my ($input) = @_ ; my ($msg, $msg_hash, $module); + my $error_string; my %act_modules = %$known_modules; @@ -562,7 +559,7 @@ sub input_from_unknown_host { # check a key exists for this module my $module_key = ${$mod."_key"}; - if( ! $module_key ) { + if( not defined $module_key ) { if( $mod eq 'ArpHandler' ) { next; } @@ -573,8 +570,7 @@ sub input_from_unknown_host { # check if module can open msg envelope with module key ($msg, $msg_hash) = &check_key_and_xml_validity($input, $module_key); - if( (!$msg) || (!$msg_hash) ) { - #daemon_log("$mod: deciphering failed", 5); + if( (not defined $msg) || (not defined $msg_hash) ) { next; } else { @@ -1063,26 +1059,23 @@ sub watch_for_new_jobs { # check gosa job queue for jobs with executable timestamp my $timestamp = &get_time(); - my $sql_statement = "SELECT * FROM ".$job_queue_table_name. " WHERE status='waiting' AND timestamp<'$timestamp'"; - my $res = $job_db->select_dbentry( $sql_statement ); while( my ($id, $hit) = each %{$res} ) { - my $jobdb_id = $hit->{id}; - my $macaddress = $hit->{macaddress}; - my $job_msg_hash = &transform_msg2hash($hit->{xmlmessage}); - my $out_msg_hash = $job_msg_hash; + my $macaddress = $hit->{'macaddress'}; + my $job_msg = $hit->{'xmlmessage'}; + my $header = $hit->{'headertag'}; my $sql_statement = "SELECT * FROM known_clients WHERE macaddress='$macaddress'"; my $res_hash = $known_clients_db->select_dbentry( $sql_statement ); # expect macaddress is unique!!!!!! my $target = $res_hash->{1}->{hostname}; if (not defined $target) { - &daemon_log("ERROR: no host found for mac address: $job_msg_hash->{mac}[0]", 1); - &daemon_log("xml message: $hit->{xmlmessage}", 5); + &daemon_log("ERROR: no host found for mac address: $macaddress", 1); + &daemon_log("$hit->{xmlmessage}", 8); my $sql_statement = "UPDATE $job_queue_table_name ". "SET status='error', result='no host found for mac address' ". "WHERE id='$jobdb_id'"; @@ -1090,37 +1083,29 @@ sub watch_for_new_jobs { next; } - # add target - &add_content2xml_hash($out_msg_hash, "target", $target); - - # add new header - my $out_header = $job_msg_hash->{header}[0]; - $out_header =~ s/job_/gosa_/; - delete $out_msg_hash->{header}; - &add_content2xml_hash($out_msg_hash, "header", $out_header); + # change header + $job_msg =~ s/
job_/
gosa_/; # add sqlite_id - &add_content2xml_hash($out_msg_hash, "jobdb_id", $jobdb_id); + $job_msg =~ s/<\/xml>$/$jobdb_id<\/jobdb_id><\/xml>/; - my $out_msg = &create_xml_string($out_msg_hash); - # encrypt msg as a GosaPackage module - my $cipher = &create_ciphering($GosaPackages_key); - my $crypted_out_msg = &encrypt_msg($out_msg, $cipher); +print STDERR "=======================================================\n$job_msg\n"; - my $error = &send_msg_hash2address($out_msg_hash, "$gosa_ip:$gosa_port", $GosaPackages_key); + &send_msg_to_target($job_msg, $server_address, $GosaPackages_key, $header); - if ($error == 0) { - my $sql_statement = "UPDATE $job_queue_table_name ". - "SET status='processing', targettag='$target' ". - "WHERE id='$jobdb_id'"; - my $res = $job_db->update_dbentry($sql_statement); - } else { - my $sql_statement = "UPDATE $job_queue_table_name ". - "SET status='error' ". - "WHERE id='$jobdb_id'"; - my $res = $job_db->update_dbentry($sql_statement); - } +# my $error = 1; +# if ($error == 0) { +# my $sql_statement = "UPDATE $job_queue_table_name ". +# "SET status='done', targettag='$target' ". +# "WHERE id='$jobdb_id'"; +# my $res = $job_db->update_dbentry($sql_statement); +# } else { +# my $sql_statement = "UPDATE $job_queue_table_name ". +# "SET status='error' ". +# "WHERE id='$jobdb_id'"; +# my $res = $job_db->update_dbentry($sql_statement); +# } } $kernel->delay_set('watch_for_new_jobs',3); diff --git a/gosa-si/modules/GosaPackages.pm b/gosa-si/modules/GosaPackages.pm index e2e7b3fc3..8314850c7 100644 --- a/gosa-si/modules/GosaPackages.pm +++ b/gosa-si/modules/GosaPackages.pm @@ -273,12 +273,12 @@ sub process_incoming_msg { foreach my $out_msg ( @msg_l ) { - # keep job queue uptodate and save result and status - if (defined ($out_msg) && $out_msg =~ /(\d*?)<\/jobdb_id>/) { + # keep job queue up-to-date and save result and status + if (defined ($out_msg) && $out_msg =~ /(\d+)<\/jobdb_id>/) { my $job_id = $1; - my $sql = "UPDATE '".$main::job_queue_table_name. - "' SET status='done', result='".$out_msg. - "' WHERE id='$job_id'"; + my $sql = "UPDATE '".$main::job_queue_table_name."'". + " SET status='done'". + " WHERE id='$job_id'"; my $res = $main::job_db->exec_statement($sql); } @@ -337,34 +337,59 @@ sub process_gosa_msg { sub process_job_msg { - my ($msg, $msg_hash)= @_ ; + my ($msg, $msg_hash, $session_id)= @_ ; my $out_msg; + my $error = 0; - my $header = @{$msg_hash->{header}}[0]; + my $header = @{$msg_hash->{'header'}}[0]; $header =~ s/job_//; + # if no timestamp is specified, use 19700101000000 + my $timestamp = "19700101000000"; + if( exists $msg_hash->{'timestamp'} ) { + $timestamp = @{$msg_hash->{'timestamp'}}[0]; + } + + #if no macaddress is specified, raise error + my $macaddress; + if( exists $msg_hash->{'macaddress'} ) { + $macaddress = @{$msg_hash->{'macaddress'}}[0]; + } else { + $error ++; + $out_msg = "". + "
answer
". + "$server_address". + "GOSA". + "1". + "no mac address specified". + "
"; + } + # check wether mac address is already known in known_daemons or known_clients my $target = 'none'; - # add job to job queue - my $func_dic = {table=>$main::job_queue_table_name, - primkey=>'id', - timestamp=>@{$msg_hash->{timestamp}}[0], - status=>'waiting', - result=>'none', - headertag=>$header, - targettag=>$target, - xmlmessage=>$msg, - macaddress=>@{$msg_hash->{mac}}[0], - }; - my $res = $main::job_db->add_dbentry($func_dic); - if (not $res == 0) { - &main::daemon_log("ERROR: GosaPackages: process_job_msg: $res", 1); - } else { - &main::daemon_log("INFO: GosaPackages: $header job successfully added to job queue", 5); + if( $error == 0 ) { + # add job to job queue + my $func_dic = {table=>$main::job_queue_table_name, + primkey=>'id', + timestamp=>$timestamp, + status=>'waiting', + result=>'none', + headertag=>$header, + targettag=>$target, + xmlmessage=>$msg, + macaddress=>$macaddress, + }; + + my $res = $main::job_db->add_dbentry($func_dic); + if (not $res == 0) { + &main::daemon_log("ERROR: GosaPackages: process_job_msg: $res", 1); + } else { + &main::daemon_log("INFO: GosaPackages: $header job successfully added to job queue", 5); + } + $out_msg = "
answer
$server_addressGOSA$res
"; } - $out_msg = "
answer
$server_addressGOSA$res
"; my @out_msg_l = ( $out_msg ); return @out_msg_l; } diff --git a/gosa-si/tests/client.php b/gosa-si/tests/client.php index dbaeb8f3a..0ff74479c 100755 --- a/gosa-si/tests/client.php +++ b/gosa-si/tests/client.php @@ -24,7 +24,7 @@ if($sock->connected()){ #$data = "
gosa_delete_jobdb_entry
3
"; # update -$data = "
gosa_update_status_jobdb_entry
waiting processing update
"; +#$data = "
gosa_update_status_jobdb_entry
waiting processing update
"; # query #$data = "
gosa_query_jobdb
andgt0le5
"; @@ -53,8 +53,8 @@ $data = "
gosa_update_status_jobdb_entry

write($data); -- 2.30.2