From 3c32028f3562253ed1bc2566fa73d67849e415cd Mon Sep 17 00:00:00 2001 From: rettenbe Date: Tue, 5 Aug 2008 09:59:42 +0000 Subject: [PATCH] update: opsi server integration git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@12148 594d385d-05f5-0310-b6e9-bd551577e9d8 --- gosa-si/gosa-si-server | 15 +- gosa-si/modules/GosaPackages.pm | 56 ++++--- gosa-si/server/events/opsi_com.pm | 234 +++++++++++++++--------------- gosa-si/tests/client.php | 20 +-- 4 files changed, 166 insertions(+), 159 deletions(-) diff --git a/gosa-si/gosa-si-server b/gosa-si/gosa-si-server index ba12f2b97..9f97a8a79 100755 --- a/gosa-si/gosa-si-server +++ b/gosa-si/gosa-si-server @@ -1508,9 +1508,13 @@ sub watch_for_opsi_jobs { while (my ($id, $hit) = each %$res ) { - # Ask OPSI for an update of the running jobs + # Determine current parameters of the job my $hostId = $hit->{'plainname'}; -print STDERR "\n\n$hostId\n"; + my $macaddress = $hit->{'macaddress'}; + my $progress = $hit->{'progress'}; + + # Ask OPSI for an update of the running jobs +#print STDERR "\n\n$hostId\n"; my $result= {}; # For hosts, only return the products that are or get installed @@ -1555,10 +1559,13 @@ print STDERR "\n\n$hostId\n"; $result->{'progress'}= int($installed * 100 / $products); } - #return $result; -print STDERR Dumper($result); +#print STDERR Dumper($result); # Set updates to job queue + if ($progress ne $result->{'progress'}) { + my $update_statement = "UPDATE $job_queue_tn SET progress='".$result->{'progress'}."', modified='1' WHERE macaddress='$macaddress'"; + my $update_res = $job_db->update_dbentry($update_statement); + } } diff --git a/gosa-si/modules/GosaPackages.pm b/gosa-si/modules/GosaPackages.pm index fb3a7048f..cd0716f2b 100644 --- a/gosa-si/modules/GosaPackages.pm +++ b/gosa-si/modules/GosaPackages.pm @@ -222,13 +222,13 @@ sub process_job_msg { $header =~ s/job_//; my $target = @{$msg_hash->{'target'}}[0]; - # if no timestamp is specified, use 19700101000000 + # 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 + # If no macaddress is specified, raise error my $macaddress; if( exists $msg_hash->{'macaddress'} ) { $macaddress = @{$msg_hash->{'macaddress'}}[0]; @@ -245,31 +245,39 @@ sub process_job_msg { ""; } - # if mac address is already known in ldap, set plain_name to 'cn' + # Determine plain_name for host my $plain_name; - my $ldap_handle = &main::get_ldap_handle($session_id); - if( not defined $ldap_handle ) { - &main::daemon_log("$session_id ERROR: cannot connect to ldap", 1); - $plain_name = "none"; - - # try to fetch a 'real name' - } else { - my $mesg = $ldap_handle->search( - base => $main::ldap_base, - scope => 'sub', - attrs => ['cn'], - filter => "(macAddress=$macaddress)"); - if($mesg->code) { - &main::daemon_log($mesg->error, 1); - $plain_name = "none"; - } else { - my $entry= $mesg->entry(0); - $plain_name = $entry->get_value("cn"); - } - } + if ($header eq "opsi_install_client") { # Opsi installing clients use hostId as plain_name + if (not exists $msg_hash->{'hostId'}) { + $error++; + &daemon_log("$session_id ERROR: opsi_install_client-message has no xml-tag 'hostID', job was not created: $msg", 1); + } else { + $plain_name = $msg_hash->{'hostId'}[0]; + } + } else { # Try to determine plain_name via ladp search + my $ldap_handle = &main::get_ldap_handle($session_id); + if( not defined $ldap_handle ) { + &main::daemon_log("$session_id ERROR: cannot connect to ldap", 1); + $plain_name = "none"; + } else { + my $mesg = $ldap_handle->search( + base => $main::ldap_base, + scope => 'sub', + attrs => ['cn'], + filter => "(macAddress=$macaddress)"); + if($mesg->code) { + &main::daemon_log($mesg->error, 1); + $plain_name = "none"; + } else { + my $entry= $mesg->entry(0); + $plain_name = $entry->get_value("cn"); + } + } + } + + # Add job to job queue if( $error == 0 ) { - # add job to job queue my $func_dic = {table=>$main::job_queue_tn, primkey=>['macaddress', 'headertag'], timestamp=>$timestamp, diff --git a/gosa-si/server/events/opsi_com.pm b/gosa-si/server/events/opsi_com.pm index 5b934ccfc..a7076b137 100644 --- a/gosa-si/server/events/opsi_com.pm +++ b/gosa-si/server/events/opsi_com.pm @@ -9,15 +9,15 @@ use Exporter; my @events = ( "get_events", "opsi_install_client", - #"opsi_get_netboot_products", - #"opsi_get_local_products", - #"opsi_get_client_hardware", - #"opsi_get_client_software", - #"opsi_get_product_properties", - #"opsi_set_product_properties", - #"opsi_list_clients", - #"opsi_del_client", - #"opsi_install_client", + "opsi_get_netboot_products", + "opsi_get_local_products", + "opsi_get_client_hardware", + "opsi_get_client_software", + "opsi_get_product_properties", + "opsi_set_product_properties", + "opsi_list_clients", + "opsi_del_client", + "opsi_install_client", ); @EXPORT = @events; @@ -26,6 +26,7 @@ use strict; use warnings; use GOSA::GosaSupportDaemon; use Data::Dumper; +use XML::Quote qw(:all); BEGIN {} @@ -45,92 +46,92 @@ sub get_events { # @param msg - STRING - xml message with tags macaddress and hostId # @param msg_hash - HASHREF - message information parsed into a hash # @param session_id - INTEGER - POE session id of the processing of this message -sub opsi_install_client { - my ($msg, $msg_hash, $session_id) = @_ ; - my $error = 0; - my $out_msg; - my $out_hash; - - # Prepare incoming message - $msg =~ s/
gosa_/
/; - $msg_hash->{'header'}[0] =~ s/gosa_//; - - - # Assign variables - my $header = @{$msg_hash->{'header'}}[0]; - my $source = @{$msg_hash->{'source'}}[0]; - my $target = @{$msg_hash->{'target'}}[0]; - - - # If no timestamp is specified in incoming message, 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'}) && - ($msg_hash->{'macaddress'}[0] =~ /^([0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2})$/i)) { - $macaddress = $1; - } else { - $error ++; - $out_msg = "". - "
answer
". - "$main::server_address". - "GOSA". - "1". - "no mac address specified in macaddres-tag". - "
"; - } - - - # Set hostID to plain_name - my $plain_name; - if (not $error) { - if (exists $msg_hash->{'hostId'}) { - $plain_name = $msg_hash->{'hostId'}[0]; - } else { - $error++; - $out_msg = "". - "
answer
". - "$main::server_address". - "GOSA". - "1". - "no hostId specified in hostId-tag". - "
"; - } - } - - - # Add installation job to job queue - if (not $error) { - my $insert_dic = {table=>$main::job_queue_tn, - primkey=>['macaddress', 'headertag'], - timestamp=>&get_time(), - status=>'processing', - result=>'none', - progress=>'none', - headertag=>$header, - targettag=>$target, - xmlmessage=>$msg, - macaddress=>$macaddress, - plainname=>$plain_name, - siserver=>"localhost", - modified=>"1", - }; - my $res = $main::job_db->add_dbentry($insert_dic); - if (not $res == 0) { - &main::daemon_log("$session_id ERROR: Cannot add opsi-job to job_queue: $msg", 1); - } else { - &main::daemon_log("$session_id INFO: '$header'-job successfully added to job queue", 5); - } - $out_msg = $msg; # forward GOsa message to client - } - - return ($out_msg); -} +#sub opsi_install_client { +# my ($msg, $msg_hash, $session_id) = @_ ; +# my $error = 0; +# my $out_msg; +# my $out_hash; +# +# # Prepare incoming message +# $msg =~ s/
gosa_/
/; +# $msg_hash->{'header'}[0] =~ s/gosa_//; +# +# +# # Assign variables +# my $header = @{$msg_hash->{'header'}}[0]; +# my $source = @{$msg_hash->{'source'}}[0]; +# my $target = @{$msg_hash->{'target'}}[0]; +# +# +# # If no timestamp is specified in incoming message, 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'}) && +# ($msg_hash->{'macaddress'}[0] =~ /^([0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2})$/i)) { +# $macaddress = $1; +# } else { +# $error ++; +# $out_msg = "". +# "
answer
". +# "$main::server_address". +# "GOSA". +# "1". +# "no mac address specified in macaddres-tag". +# "
"; +# } +# +# +# # Set hostID to plain_name +# my $plain_name; +# if (not $error) { +# if (exists $msg_hash->{'hostId'}) { +# $plain_name = $msg_hash->{'hostId'}[0]; +# } else { +# $error++; +# $out_msg = "". +# "
answer
". +# "$main::server_address". +# "GOSA". +# "1". +# "no hostId specified in hostId-tag". +# "
"; +# } +# } +# +# +# # Add installation job to job queue +# if (not $error) { +# my $insert_dic = {table=>$main::job_queue_tn, +# primkey=>['macaddress', 'headertag'], +# timestamp=>&get_time(), +# status=>'processing', +# result=>'none', +# progress=>'none', +# headertag=>$header, +# targettag=>$target, +# xmlmessage=>$msg, +# macaddress=>$macaddress, +# plainname=>$plain_name, +# siserver=>"localhost", +# modified=>"1", +# }; +# my $res = $main::job_db->add_dbentry($insert_dic); +# if (not $res == 0) { +# &main::daemon_log("$session_id ERROR: Cannot add opsi-job to job_queue: $msg", 1); +# } else { +# &main::daemon_log("$session_id INFO: '$header'-job successfully added to job queue", 5); +# } +# $out_msg = $msg; # forward GOsa message to client +# } +# +# return ($out_msg); +#} ## @method opsi_get_netboot_products @@ -148,7 +149,6 @@ sub opsi_get_netboot_products { # build return message with twisted target and source my $out_hash = &main::create_xml_hash("answer_$header", $target, $source); - #&add_content2xml_hash($out_hash, "session_id", $session_id); ### possibly not needed, to be checked if (defined $forward_to_gosa) { &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa); } @@ -174,7 +174,7 @@ sub opsi_get_netboot_products { my %r = (); for (@{$res->result}) { $r{$_} = 1 } - if (check_res($res)){ + if (&main::check_opsi_res($res)){ if (defined $hostId){ $callobj = { @@ -184,7 +184,7 @@ sub opsi_get_netboot_products { }; my $hres = $main::opsi_client->call($main::opsi_url, $callobj); - if (check_res($hres)){ + if (&main::check_opsi_res($hres)){ my $htmp= $hres->result->{$hostId}; # check state != not_installed or action == setup -> load and add @@ -206,7 +206,7 @@ sub opsi_get_netboot_products { }; my $sres = $main::opsi_client->call($main::opsi_url, $callobj); - if (check_res($sres)){ + if (&main::check_opsi_res($sres)){ my $tres= $sres->result; my $name= xml_quote($tres->{'name'}); @@ -231,7 +231,7 @@ sub opsi_get_netboot_products { }; my $sres = $main::opsi_client->call($main::opsi_url, $callobj); - if (check_res($sres)){ + if (&main::check_opsi_res($sres)){ my $tres= $sres->result; my $name= xml_quote($tres->{'name'}); @@ -268,7 +268,6 @@ sub opsi_get_product_properties { # build return message with twisted target and source my $out_hash = &main::create_xml_hash("answer_$header", $target, $source); - &add_content2xml_hash($out_hash, "session_id", $session_id); # Get hostID if defined if (defined @{$msg_hash->{'hostId'}}[0]){ @@ -288,7 +287,7 @@ sub opsi_get_product_properties { id => 1, }; my $res = $main::opsi_client->call($main::opsi_url, $callobj); - if (check_res($res)){ + if (&main::check_opsi_res($res)){ foreach my $action (@{$res->result}){ &add_content2xml_hash($out_hash, "action", $action); } @@ -309,7 +308,7 @@ sub opsi_get_product_properties { $res = $main::opsi_client->call($main::opsi_url, $callobj); - if (check_res($res)){ + if (&main::check_opsi_res($res)){ my $r= $res->result; foreach my $key (keys %{$r}) { my $item= ""; @@ -349,7 +348,6 @@ sub opsi_set_product_properties { # build return message with twisted target and source my $out_hash = &main::create_xml_hash("answer_$header", $target, $source); - &add_content2xml_hash($out_hash, "session_id", $session_id); &add_content2xml_hash($out_hash, "ProductId", $productId); # Get hostID if defined @@ -391,7 +389,7 @@ sub opsi_set_product_properties { my $res = $main::opsi_client->call($main::opsi_url, $callobj); - if (!check_res($res)){ + if (!&main::check_opsi_res($res)){ &main::daemon_log("ERROR: no communication failed while setting '".$item->{'name'}[0]."': ".$res->error_message, 1); &add_content2xml_hash($out_hash, "error", $res->error_message); } @@ -418,7 +416,6 @@ sub opsi_get_client_hardware { # build return message with twisted target and source my $out_hash = &main::create_xml_hash("answer_$header", $target, $source); - &add_content2xml_hash($out_hash, "session_id", $session_id); if (defined $forward_to_gosa) { &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa); @@ -435,7 +432,7 @@ sub opsi_get_client_hardware { }; my $res = $main::opsi_client->call($main::opsi_url, $callobj); - if (check_res($res)){ + if (&main::check_opsi_res($res)){ my $result= $res->result; foreach my $r (keys %{$result}){ my $item= "".xml_quote($r).""; @@ -475,7 +472,6 @@ sub opsi_list_clients { # build return message with twisted target and source my $out_hash = &main::create_xml_hash("answer_$header", $target, $source); - &add_content2xml_hash($out_hash, "session_id", $session_id); if (defined $forward_to_gosa) { &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa); @@ -492,7 +488,7 @@ sub opsi_list_clients { }; my $res = $main::opsi_client->call($main::opsi_url, $callobj); - if (check_res($res)){ + if (&main::check_opsi_res($res)){ foreach my $host (@{$res->result}){ my $item= "".$host->{'hostId'}.""; @@ -525,7 +521,6 @@ sub opsi_get_client_software { # build return message with twisted target and source my $out_hash = &main::create_xml_hash("answer_$header", $target, $source); - &add_content2xml_hash($out_hash, "session_id", $session_id); if (defined $forward_to_gosa) { &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa); @@ -542,7 +537,7 @@ sub opsi_get_client_software { }; my $res = $main::opsi_client->call($main::opsi_url, $callobj); - if (check_res($res)){ + if (&main::check_opsi_res($res)){ my $result= $res->result; } @@ -567,7 +562,6 @@ sub opsi_get_local_products { # build return message with twisted target and source my $out_hash = &main::create_xml_hash("answer_$header", $target, $source); - &add_content2xml_hash($out_hash, "session_id", $session_id); # Get hostID if defined if (defined @{$msg_hash->{'hostId'}}[0]){ @@ -593,7 +587,7 @@ sub opsi_get_local_products { my %r = (); for (@{$res->result}) { $r{$_} = 1 } - if (check_res($res)){ + if (&main::check_opsi_res($res)){ if (defined $hostId){ $callobj = { @@ -603,7 +597,7 @@ sub opsi_get_local_products { }; my $hres = $main::opsi_client->call($main::opsi_url, $callobj); - if (check_res($hres)){ + if (&main::check_opsi_res($hres)){ my $htmp= $hres->result->{$hostId}; # check state != not_installed or action == setup -> load and add @@ -625,7 +619,7 @@ sub opsi_get_local_products { }; my $sres = $main::opsi_client->call($main::opsi_url, $callobj); - if (check_res($sres)){ + if (&main::check_opsi_res($sres)){ my $tres= $sres->result; my $name= xml_quote($tres->{'name'}); @@ -650,7 +644,7 @@ sub opsi_get_local_products { }; my $sres = $main::opsi_client->call($main::opsi_url, $callobj); - if (check_res($sres)){ + if (&main::check_opsi_res($sres)){ my $tres= $sres->result; my $name= xml_quote($tres->{'name'}); @@ -689,7 +683,7 @@ sub opsi_get_local_products { # }; # # my $hres = $main::opsi_client->call($main::opsi_url, $callobj); -# if (check_res($hres)){ +# if (&main::check_opsi_res($hres)){ # my $htmp= $hres->result->{$hostId}; # # # check state != not_installed or action == setup -> load and add @@ -741,7 +735,6 @@ sub opsi_del_client { # build return message with twisted target and source my $out_hash = &main::create_xml_hash("answer_$header", $target, $source); - &add_content2xml_hash($out_hash, "session_id", $session_id); if (defined $forward_to_gosa) { &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa); @@ -777,7 +770,6 @@ sub opsi_install_client { # build return message with twisted target and source my $out_hash = &main::create_xml_hash("answer_$header", $target, $source); - &add_content2xml_hash($out_hash, "session_id", $session_id); if (defined $forward_to_gosa) { &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa); @@ -793,7 +785,7 @@ sub opsi_install_client { }; my $hres = $main::opsi_client->call($main::opsi_url, $callobj); - if (check_res($hres)){ + if (&main::check_opsi_res($hres)){ my $htmp= $hres->result->{$hostId}; # check state != not_installed or action == setup -> load and add @@ -810,7 +802,7 @@ sub opsi_install_client { id => 1, }; my $res = $main::opsi_client->call($main::opsi_url, $callobj); - if (!check_res($res)){ + if (!&main::check_opsi_res($res)){ &main::daemon_log("ERROR: cannot set product action request for $hostId!", 1); } else { &main::daemon_log("INFO: requesting 'setup' for '".$product->{'productId'}."' on $hostId", 1); diff --git a/gosa-si/tests/client.php b/gosa-si/tests/client.php index ecc70afb1..b445c0394 100755 --- a/gosa-si/tests/client.php +++ b/gosa-si/tests/client.php @@ -118,22 +118,22 @@ for($count = 1; $count <= $zahl; $count++) # Opsi testing # Get all netboot products - #$data = "
gosa_opsi_get_netboot_products
GOSA 00:01:6c:9d:aa:16
"; + #$data = "
gosa_opsi_get_netboot_products
GOSA GOSA
"; # Get netboot product for specific host - #$data = "
gosa_opsi_get_netboot_products
GOSA 00:01:6c:9d:aa:16 limux-cl-2.intranet.gonicus.de
"; + #$data = "
gosa_opsi_get_netboot_products
GOSA GOSA limux-cl-2.intranet.gonicus.de
"; # Get all localboot products - #$data = "
gosa_opsi_get_local_products
GOSA 00:01:6c:9d:aa:16
"; + #$data = "
gosa_opsi_get_local_products
GOSA GOSA
"; # Get localboot product for specific host - #$data = "
gosa_opsi_get_local_products
GOSA 00:01:6c:9d:aa:16 limux-cl-2.intranet.gonicus.de
"; + #$data = "
gosa_opsi_get_local_products
GOSA GOSA limux-cl-2.intranet.gonicus.de
"; # Get product properties - global - #$data = "
gosa_opsi_get_product_properties
GOSA 00:01:6c:9d:aa:16 winxppro
"; + #$data = "
gosa_opsi_get_product_properties
GOSA GOSA winxppro
"; # Get product properties - per host - #$data = "
gosa_opsi_get_product_properties
GOSA 00:01:6c:9d:aa:16 firefox limux-cl-2.intranet.gonicus.de
"; + #$data = "
gosa_opsi_get_product_properties
GOSA GOSA firefox limux-cl-2.intranet.gonicus.de
"; # Set product properties - global #$data = "
gosa_opsi_set_product_properties
GOSA 00:01:6c:9d:aa:16 winxppro askbeforeinstfalse
"; @@ -142,19 +142,19 @@ for($count = 1; $count <= $zahl; $count++) #$data = "
gosa_opsi_set_product_properties
GOSA 00:01:6c:9d:aa:16 limux-cl-2.intranet.gonicus.de winxppro askbeforeinstfalse
"; # Get hardware inventory - #$data = "
gosa_opsi_get_client_hardware
GOSA 00:01:6c:9d:aa:16 limux-cl-2.intranet.gonicus.de
"; + #$data = "
gosa_opsi_get_client_hardware
GOSA GOSA limux-cl-2.intranet.gonicus.de
"; # Get software inventory - #$data = "
gosa_opsi_get_client_software
GOSA 00:01:6c:9d:aa:16 limux-cl-2.intranet.gonicus.de
"; + #$data = "
gosa_opsi_get_client_software
GOSA GOSA limux-cl-2.intranet.gonicus.de
"; # List Opsi clients - #$data = "
gosa_opsi_list_clients
GOSA 00:01:6c:9d:aa:16
"; + #$data = "
gosa_opsi_list_clients
GOSA GOSA
"; # Delete Opsi client #$data = "
gosa_opsi_del_client
GOSA 00:01:6c:9d:aa:16 sdfgsg.intranet.gonicus.de
"; # Install Opsi client - $data = "
gosa_opsi_install_client
GOSA 00:01:6c:9d:b9:fa limux-cl-2.intranet.gonicus.de 11:22:33:44:55:66
"; + $data = "
job_opsi_install_client
GOSA 00:01:6c:9d:b9:fa limux-cl-2.intranet.gonicus.de 11:22:33:44:55:66
"; ######################### -- 2.30.2