From: rettenbe Date: Tue, 8 Jan 2008 14:46:30 +0000 (+0000) Subject: complete ldap interface for GosaPackages X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=8180fafca372e83a0f3dd21c457086eb65b066ff;p=gosa.git complete ldap interface for GosaPackages git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@8250 594d385d-05f5-0310-b6e9-bd551577e9d8 --- diff --git a/gosa-si/gosa-si-server b/gosa-si/gosa-si-server index 24295fba0..4e4097a89 100755 --- a/gosa-si/gosa-si-server +++ b/gosa-si/gosa-si-server @@ -990,7 +990,7 @@ close( LOCK_FILE ); } # connect to gosa-si job queue -my @job_col_names = ("timestamp", "status", "result", "header", "target", "xmlmessage", "macaddress"); +my @job_col_names = ("timestamp", "status", "result", "headertag", "targettag", "xmlmessage", "macaddress"); $job_db = GOSA::DBsqlite->new($job_queue_file_name); $job_db->create_table('jobs', \@job_col_names); @@ -1142,11 +1142,12 @@ while(1) { print $act_client $in_msg."\n"; #my $act_pipe = $busy_child{$pid}{pipe_rd}; - sleep(10); - close ($act_client); delete $busy_child{$pid}; $free_child{$pid} = $child_hash; + # give the client a chance to read + sleep(2); + close ($act_client); } } } @@ -1164,8 +1165,7 @@ while(1) { my $timestamp = "$year$month$monthday$hours$minutes$seconds"; - #my $res = $job_db->select_dbentry( { table=>$job_queue_table_name, status=>'waiting', timestamp=>'$timestamp'} ); - my $res = $job_db->select_dbentry( { table=>$job_queue_table_name, status=>'waiting', timestamp=>'2007010709014800'} ); + my $res = $job_db->select_dbentry( { table=>$job_queue_table_name, status=>'waiting', timestamp=>$timestamp } ); while( my ($id, $hit) = each %{$res} ) { @@ -1179,6 +1179,13 @@ while(1) { 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); + my $update_hash = { table=>$job_queue_table_name, + update=> [ { status=>['error'], result=>["no host found for mac address"] } ], + where=> [ { ROWID=>[$jobdb_id] } ], + }; + my $res = $job_db->update_dbentry($update_hash); + next; } @@ -1204,12 +1211,20 @@ while(1) { my $error = &send_msg_hash2address($out_msg_hash, "$gosa_ip:$gosa_port", $gosa_passwd); +####################### +# TODO exchange ROWID with jobid, insert column jobid in table jobs befor + if ($error == 0) { - my $sql = "UPDATE '$job_queue_table_name' SET status='processing', target='$target' WHERE ROWID='$jobdb_id'"; + my $sql = "UPDATE '$job_queue_table_name' SET status='processing', targettag='$target' WHERE ROWID='$jobdb_id'"; my $res = $job_db->exec_statement($sql); + } else { + my $update_hash = { table=>$job_queue_table_name, + update=> [ { status=>'error' } ], + where=> [ { ROWID=>$jobdb_id } ], + }; + my $res = $job_db->update_dbentry($update_hash); } - } diff --git a/gosa-si/modules/DBsqlite.pm b/gosa-si/modules/DBsqlite.pm index 76014acde..9f341ec5e 100644 --- a/gosa-si/modules/DBsqlite.pm +++ b/gosa-si/modules/DBsqlite.pm @@ -83,7 +83,8 @@ sub add_dbentry { my $sql_statement = " INSERT INTO $table VALUES ('".join("', '", @add_list)."')"; print " INSERT INTO $table VALUES ('".join("', '", @add_list)."')\n"; - $obj->{dbh}->do($sql_statement); + my $db_res = $obj->{dbh}->do($sql_statement); + print " DB result: $db_res\n"; return 0; } @@ -107,49 +108,41 @@ sub update_dbentry { } else { delete $arg->{table}; } + # extract where parameter from arg hash - my $restric_pram = $arg->{where}; - if (not defined $restric_pram) { - return 2; - } else { - delete $arg->{'where'}; - } - # extrac where value from arg hash - my $restric_val = $arg->{$restric_pram}; - if (not defined $restric_val) { - return 3; - } else { - delete $arg->{$restric_pram}; - } - - # check wether table has all specified columns - my $columns = {}; - my @res = @{$obj->{dbh}->selectall_arrayref("pragma table_info('$table')")}; - foreach my $column (@res) { - $columns->{@$column[1]} = ""; - } - my @pram_list = keys %$arg; - foreach my $pram (@pram_list) { - if (not exists $columns->{$pram}) { - return 4; + my $where_statement = ""; + if( exists $arg->{where} ) { + my $where_hash = @{ $arg->{where} }[0]; + if( 0 < keys %{ $where_hash } ) { + my @where_list; + while( my ($rest_pram, $rest_val) = each %{ $where_hash } ) { + my $statement; + if( $rest_pram eq 'timestamp' ) { + $statement = "$rest_pram<'@{ $rest_val }[0]'"; + } else { + $statement = "$rest_pram='@{ $rest_val }[0]'"; + } + push( @where_list, $statement ); + } + $where_statement .= "WHERE ".join('AND ', @where_list); } } - - - # select all changes - my @change_list; - my $sql_part; - while (my($pram, $val) = each(%{$arg})) { - push(@change_list, "$pram='$val'"); - } - - if (not @change_list) { - return 5; + # extract update parameter from arg hash + my $update_hash = @{ $arg->{update} }[0]; + my $update_statement = ""; + if( 0 < keys %{ $update_hash } ) { + my @update_list; + while( my ($rest_pram, $rest_val) = each %{ $update_hash } ) { + my $statement = "$rest_pram='@{ $rest_val }[0]'"; + push( @update_list, $statement ); + } + $update_statement .= join(', ', @update_list); } - $obj->{dbh}->do("UPDATE $table SET ".join(', ',@change_list)." WHERE $restric_pram='$restric_val'"); - return 0; + my $sql_statement = "UPDATE $table SET $update_statement $where_statement"; + my $db_answer = $obj->{dbh}->do($sql_statement); + return $db_answer; } @@ -165,30 +158,28 @@ sub del_dbentry { } else { delete $arg->{table}; } - # extract where parameter from arg hash - my $restric_pram = $arg->{where}; - if (not defined $restric_pram) { - return 2; - } else { - delete $arg->{'where'}; + + # collect select statements + my @del_list; + while (my ($pram, $val) = each %{$arg}) { + if ( $pram eq 'timestamp' ) { + push(@del_list, "$pram < '$val'"); + } else { + push(@del_list, "$pram = '$val'"); + } } - # extrac where value from arg hash - my $restric_val = $arg->{$restric_pram}; - if (not defined $restric_val) { - return 3; + + my $where_statement; + if( not @del_list ) { + $where_statement = ""; } else { - delete $arg->{$restric_pram}; - } - - # check wether entry exists - my $res = @{$obj->{dbh}->selectall_arrayref( "SELECT * FROM $table WHERE $restric_pram='$restric_val'")}; - if ($res == 0) { - return 4; + $where_statement = "WHERE ".join(' AND ', @del_list); } - $obj->{dbh}->do("DELETE FROM $table WHERE $restric_pram='$restric_val'"); + my $sql_statement = "DELETE FROM $table $where_statement"; + my $db_res = $obj->{dbh}->do($sql_statement); - return 0; + return $db_res; } @@ -227,8 +218,8 @@ sub select_dbentry { } else { push(@select_list, "$pram = '$val'"); } - } + if (@select_list == 0) { $sql_statement = "SELECT ROWID, * FROM '$table'"; } else { diff --git a/gosa-si/modules/GosaPackages.pm b/gosa-si/modules/GosaPackages.pm index a441a2fe1..eea707322 100644 --- a/gosa-si/modules/GosaPackages.pm +++ b/gosa-si/modules/GosaPackages.pm @@ -83,11 +83,8 @@ if ($gosa_activ eq "on") { } # create gosa job queue as a SQLite DB -my @col_names = ("id", "timestamp", "status", "result", "header", - "target", "xml", "mac"); my $table_name = "jobs"; my $sqlite = GOSA::DBsqlite->new($job_queue_file_name); -#$sqlite->create_table($table_name, \@col_names); @@ -262,6 +259,10 @@ sub process_gosa_msg { # decide wether msg is a core function or a event handler if ( $header eq 'query_jobdb') { $out_msg = &query_jobdb } + elsif ($header eq 'delete_jobdb_entry') { $out_msg = &delete_jobdb_entry } + elsif ($header eq 'clear_jobdb') { $out_msg = &clear_jobdb } + elsif ($header eq 'update_status_jobdb_entry' ) { $out_msg = &update_status_jobdb_entry } + elsif ($header eq 'update_timestamp_jobdb_entry' ) { $out_msg = &update_timestamp_jobdb_entry } else { # msg could not be assigned to core function # fetch all available eventhandler under $server_event_dir @@ -312,8 +313,8 @@ sub process_job_msg { timestamp=>@{$msg_hash->{timestamp}}[0], status=>'waiting', result=>'none', - header=>$header, - target=>$target, + headertag=>$header, + targettag=>$target, xmlmessage=>$msg, macaddress=>@{$msg_hash->{mac}}[0], }; @@ -323,7 +324,7 @@ sub process_job_msg { } &main::daemon_log("GosaPackages: $header job successfully added to job queue", 3); - return; + return "<1>$res"; } @@ -376,6 +377,125 @@ sub query_jobdb { return $out_xml; } +sub delete_jobdb_entry { + my ($msg) = @_ ; + my $msg_hash = &transform_msg2hash($msg); + + # prepare query sql statement + my @where = @{$msg_hash->{where}}; + my $where_hash = {table=>$main::job_queue_table_name }; + foreach my $where_pram (@where) { + my $where_val = @{$msg_hash->{$where_pram}}[0]; + if (defined $where_val) { + $where_hash->{$where_pram} = $where_val; + } + } + + # execute db query + my $db_res = $main::job_db->del_dbentry($where_hash); + + my $res; + if( $db_res > 0 ) { + $res = 0 ; + } else { + $res = 1; + } + + # prepare xml answer + my $out_xml = "<1>$res"; + return $out_xml; + +} + +sub clear_jobdb { + my ($msg) = @_ ; + my $msg_hash = &transform_msg2hash($msg); + + my $where_hash = {table=>$main::job_queue_table_name }; + + # execute db query + my $db_res = $main::job_db->del_dbentry($where_hash); + print STDERR "db_res=$db_res\n"; + my $res; + if( $db_res eq '0E0' ) { + $res = 0 ; + } else { + $res = 1; + } + + # prepare xml answer + my $out_xml = "<1>$res"; + return $out_xml; +} + +sub update_status_jobdb_entry { + my ($msg) = @_ ; + my $msg_hash = &transform_msg2hash($msg); + + # prepare query sql statement + my $update_hash = {table=>$main::job_queue_table_name }; + if( exists $msg_hash->{where} ) { + $update_hash->{where} = $msg_hash->{where}; + } else { + $update_hash->{where} = []; + } + + if( not exists $msg_hash->{update}[0]->{status} ) { + return "<1>1"; + } + $update_hash->{update} = [ { status=>$msg_hash->{update}[0]->{status} } ]; + #$update_hash->{update} = $msg_hash->{update}; + + # execute db query + my $db_res = $main::job_db->update_dbentry($update_hash); + + # transform db answer to error returnment + my $res; + if( $db_res > 0 ) { + $res = 0 ; + } else { + $res = 1; + } + + # prepare xml answer + my $out_xml = "<1>$res"; + return $out_xml; +} + +sub update_timestamp_jobdb_entry { + my ($msg) = @_ ; + my $msg_hash = &transform_msg2hash($msg); + + # prepare query sql statement + my $update_hash = {table=>$main::job_queue_table_name }; + if( exists $msg_hash->{where} ) { + $update_hash->{where} = $msg_hash->{where}; + } else { + $update_hash->{where} = []; + } + + if( not exists $msg_hash->{update}[0]->{timestamp} ) { + return "<1>1"; + } + + $update_hash->{update} = [ { timestamp=>$msg_hash->{update}[0]->{timestamp} } ]; + + # execute db query + my $db_res = $main::job_db->update_dbentry($update_hash); + + # transform db answer to error returnment + my $res; + if( $db_res > 0 ) { + $res = 0 ; + } else { + $res = 1; + } + + # prepare xml answer + my $out_xml = "<1>$res"; + return $out_xml; + +} 1; diff --git a/gosa-si/tests/client.php b/gosa-si/tests/client.php index 30c39b9d6..83f4470d6 100755 --- a/gosa-si/tests/client.php +++ b/gosa-si/tests/client.php @@ -10,15 +10,29 @@ $sock->setEncryptionKey("secret-gosa-password"); if($sock->connected()){ /* Prepare a hunge bunch of data to be send */ + +# add #$data = "
gosa_ping
10.89.1.155:2008211:22:33:44:55
"; #$data = "
job_ping
10.89.1.155:2008300:1B:77:04:8A:6C 19700101000000
"; #$data = "
job_sayHello
10.89.1.155:2008300:1B:77:04:8A:6C 20130102133900
"; #$data = "
job_ping
10.89.1.155:2008300:1B:77:04:8A:6C 20130102133900
"; - $data = "
gosa_query_jobdb
statuswaiting
"; - $sock->write($data); +# delete + #$data = "
gosa_delete_jobdb_entry
headertag sayHello
"; + $data = "
gosa_clear_jobdb
"; + +# update + #$data = "
gosa_update_status_jobdb_entry
waiting processing
"; + #$data = "
gosa_update_status_jobdb_entry
waiting
"; + #$data = "
gosa_update_timestamp_jobdb_entry
20130123456789
"; + +# query + #$data = "
gosa_query_jobdb
statuswaiting
"; + +# clear + #$data = "
gosa_clear_jobdb
"; - #$sock->setEncryptionKey("ferdinand_frost"); + $sock->write($data); $answer = $sock->read(); echo "$answer\n";