Code

Some more checks.
authorjanw <janw@594d385d-05f5-0310-b6e9-bd551577e9d8>
Wed, 10 Sep 2008 07:09:20 +0000 (07:09 +0000)
committerjanw <janw@594d385d-05f5-0310-b6e9-bd551577e9d8>
Wed, 10 Sep 2008 07:09:20 +0000 (07:09 +0000)
Removed eval.

git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@12385 594d385d-05f5-0310-b6e9-bd551577e9d8

gosa-si/server/events/gosaTriggered.pm

index 0823eb0a967e286ec84748473871cf61eb79ec5e..96413597ab5e5f52b46295c31304a9f58cd5f46a 100644 (file)
@@ -802,149 +802,160 @@ sub trigger_activate_new {
        my $ldap_entry;
        my $ogroup_entry;
        my $changed_attributes_counter = 0;
+
+    my $activate_client = 0;
        
-       eval {
-               my $ldap_mesg= $ldap_handle->search(
-                       base => $main::ldap_base,
-                       scope => 'sub',
-                       filter => "(&(objectClass=gosaGroupOfnames)(cn=$ogroup))",
-               );
-               if($ldap_mesg->count == 1) {
-                       $ogroup_entry= $ldap_mesg->pop_entry();
-               } elsif ($ldap_mesg->count == 0) {
-                       &main::daemon_log("ERROR: A GosaGroupOfNames with cn '$ogroup' was not found in base '".$main::ldap_base."'!", 1);
-               } else {
-                       &main::daemon_log("ERROR: More than one ObjectGroups with cn '$ogroup' was found in base '".$main::ldap_base."'!", 1);
-               }
-
-               # build the base, use optional base parameter or take it from ogroup
-               if(!(defined($base) && (length($base) > 0))) {
-                               # Subtract the ObjectGroup cn
-                               $base = $1 if $ogroup_entry->dn =~ /cn=$ogroup,ou=groups,(.*)$/;
-               }
-
-               # prepend ou=systems (configurable through config)
-               $base = $main::new_systems_ou.",".$base;
-
-               # Search for an existing entry (should be in ou=incoming)
-               $ldap_mesg= $ldap_handle->search(
-                       base => $main::ldap_base,
-                       scope => 'sub',
-                       filter => "(&(objectClass=GOhard)(|(macAddress=$mac)(dhcpHWaddress=$mac)))",
-               );
-
-               # TODO: Find a way to guess an ip address for hosts with no ldap entry (MAC->ARP->IP)
-               if($ldap_mesg->count == 1) {
-                       &main::daemon_log("DEBUG: One system with mac address '$mac' was found in base '".$main::ldap_base."'!", 6);
-                       # Get the entry from LDAP
-                       $ldap_entry= $ldap_mesg->pop_entry();
-
-                       if(!($ldap_entry->dn() eq "cn=".$ldap_entry->get_value('cn').",$base")) {
-                               # Move the entry to the new ou
-                               $ldap_entry->changetype('moddn');
-                               $ldap_entry->add(
-                                       newrdn => "cn=".$ldap_entry->get_value('cn'),
-                                       deleteoldrdn => 1,
-                                       newsuperior => $base,
-                               );
-                       }
-
-               } 
-
-               $ldap_mesg= $ldap_handle->search(
-                       base => $main::ldap_base,
-                       scope => 'sub',
-                       filter => "(&(objectClass=GOhard)(|(macAddress=$mac)(dhcpHWaddress=$mac)))",
-               );
-
-               # TODO: Find a way to guess an ip address for hosts with no ldap entry (MAC->ARP->IP)
-               if($ldap_mesg->count == 1) {
-                       $ldap_entry= $ldap_mesg->pop_entry();
-                       # Check for needed objectClasses
-                       my $oclasses = $ldap_entry->get_value('objectClass', asref => 1);
-                       foreach my $oclass ("FAIobject", "GOhard") {
-                               if(!(scalar grep $_ eq $oclass, map {$_ => 1} @$oclasses)) {
-                                       &main::daemon_log("Adding objectClass $oclass", 1);
-                                       $ldap_entry->add(
-                                               objectClass => $oclass,
-                                       );
-                                       my $oclass_result = $ldap_entry->update($ldap_handle);
-                               }
-                       }
-
-                       # Set FAIstate
-                       if(defined($ldap_entry->get_value('FAIstate'))) {
-                               if(!($ldap_entry->get_value('FAIstate') eq 'install')) {
-                                       $ldap_entry->replace(
-                                               'FAIstate' => 'install'
-                                       );
-                                       my $replace_result = $ldap_entry->update($ldap_handle);
-                               }
-                       } else {
-                               $ldap_entry->add(
-                                       'FAIstate' => 'install'
-                               );
-                               my $add_result = $ldap_entry->update($ldap_handle);
-                       }
-
-
-               } elsif ($ldap_mesg->count == 0) {
-                       # TODO: Create a new entry
-                       # $ldap_entry = Net::LDAP::Entry->new();
-                       # $ldap_entry->dn("cn=$mac,$base");
-                       &main::daemon_log("WARNING: No System with mac address '$mac' was found in base '".$main::ldap_base."'! Re-queuing job.", 4);
-                       $main::job_db->exec_statement("UPDATE jobs SET status = 'waiting' WHERE id = $jobdb_id");
-                       $main::job_db->exec_statement("UPDATE jobs SET timestamp = '".&get_time()."' WHERE id = $jobdb_id");
-            return;
-               } else {
-                       &main::daemon_log("ERROR: More than one system with mac address '$mac' was found in base '".$main::ldap_base."'!", 1);
-               }
-
-               # Add to ObjectGroup
-               if(!(scalar grep $_, map {$_ => 1} $ogroup_entry->get_value('member', asref => 1))) {
-                       $ogroup_entry->add (
-                               'member' => $ldap_entry->dn(),
-                       );
-                       my $ogroup_result = $ogroup_entry->update($ldap_handle);
-                       if ($ogroup_result->code() != 0) {
-                               &main::daemon_log("ERROR: Updating the ObjectGroup '$ogroup' failed (code '".$ogroup_result->code()."') with '".$ogroup_result->{'errorMessage'}."'!", 1);
-                       }
-               }
-
-               # Finally set gotoMode to active
-               if(defined($ldap_entry->get_value('gotoMode'))) {
-                       if(!($ldap_entry->get_value('gotoMode') eq 'active')) {
-                               $ldap_entry->replace(
-                                       'gotoMode' => 'active'
-                               );
-                               my $activate_result = $ldap_entry->update($ldap_handle);
-                               if ($activate_result->code() != 0) {
-                                       &main::daemon_log("ERROR: Activating system '".$ldap_entry->dn()."' failed (code '".$activate_result->code()."') with '".$activate_result->{'errorMessage'}."'!", 1);
-                               }
-                       }
-               } else {
-                       $ldap_entry->add(
-                               'gotoMode' => 'active'
-                       );
-                       my $activate_result = $ldap_entry->update($ldap_handle);
-                       if ($activate_result->code() != 0) {
-                               &main::daemon_log("ERROR: Activating system '".$ldap_entry->dn()."' failed (code '".$activate_result->code()."') with '".$activate_result->{'errorMessage'}."'!", 1);
-                       }
-               }
-       };
-       if($@) {
-               &main::daemon_log("ERROR: activate_new failed with '$@'!", 1);
-       }
+    if(defined($ogroup)) {
+      my $ldap_mesg= $ldap_handle->search(
+        base => $main::ldap_base,
+        scope => 'sub',
+        filter => "(&(objectClass=gosaGroupOfnames)(cn=$ogroup))",
+      );
+      if($ldap_mesg->count == 1) {
+        $ogroup_entry= $ldap_mesg->pop_entry();
+      } elsif ($ldap_mesg->count == 0) {
+        &main::daemon_log("ERROR: A GosaGroupOfNames with cn '$ogroup' was not found in base '".$main::ldap_base."'!", 1);
+      } else {
+        &main::daemon_log("ERROR: More than one ObjectGroups with cn '$ogroup' was found in base '".$main::ldap_base."'!", 1);
+      }
+
+      # build the base, use optional base parameter or take it from ogroup
+      if(!(defined($base) && (length($base) > 0))) {
+        # Subtract the ObjectGroup cn
+        $base = $1 if $ogroup_entry->dn =~ /cn=$ogroup,ou=groups,(.*)$/;
+      }
+    }
 
-       # Set job to done
-       $main::job_db->exec_statement("UPDATE jobs SET status = 'done' WHERE id = $jobdb_id");
+    # prepend ou=systems (configurable through config)
+    $base = $main::new_systems_ou.",".$base;
 
-       # create set_activated_for_installation message for delivery
-    my $out_hash = &create_xml_hash("set_activated_for_installation", $source, $target);
-    my $out_msg = &create_xml_string($out_hash);
-       my @out_msg_l = ($out_msg);
+    # Search for an existing entry (should be in ou=incoming)
+    my $ldap_mesg= $ldap_handle->search(
+      base => $main::ldap_base,
+      scope => 'sub',
+      filter => "(&(objectClass=GOhard)(|(macAddress=$mac)(dhcpHWaddress=$mac)))",
+    );
 
-    return @out_msg_l;
+    # TODO: Find a way to guess an ip address for hosts with no ldap entry (MAC->ARP->IP)
+    if($ldap_mesg->count == 1) {
+      &main::daemon_log("DEBUG: One system with mac address '$mac' was found in base '".$main::ldap_base."'!", 6);
+      # Get the entry from LDAP
+      $ldap_entry= $ldap_mesg->pop_entry();
+
+      if(!($ldap_entry->dn() eq "cn=".$ldap_entry->get_value('cn').",$base")) {
+        # Move the entry to the new ou
+        $ldap_entry->changetype('moddn');
+        $ldap_entry->add(
+          newrdn => "cn=".$ldap_entry->get_value('cn'),
+          deleteoldrdn => 1,
+          newsuperior => $base,
+        );
+      }
+
+    } elsif ($ldap_mesg->count == 0) {
+      &main::daemon_log("WARNING: No System with mac address '$mac' was found in base '".$main::ldap_base."'! Re-queuing job.", 4);
+      $main::job_db->exec_statement("UPDATE ".$main::job_queue_tn." SET status = 'waiting' WHERE id = $jobdb_id");
+      $main::job_db->exec_statement("UPDATE ".$main::job_queue_tn." SET timestamp = '".&get_time()."' WHERE id = $jobdb_id");
+      return undef;
+    }
+
+    $ldap_mesg= $ldap_handle->search(
+      base => $main::ldap_base,
+      scope => 'sub',
+      filter => "(&(objectClass=GOhard)(|(macAddress=$mac)(dhcpHWaddress=$mac)))",
+    );
+
+    # TODO: Find a way to guess an ip address for hosts with no ldap entry (MAC->ARP->IP)
+    if($ldap_mesg->count == 1) {
+      $ldap_entry= $ldap_mesg->pop_entry();
+      # Check for needed objectClasses
+      my $oclasses = $ldap_entry->get_value('objectClass', asref => 1);
+      foreach my $oclass ("FAIobject", "GOhard") {
+        if(!(scalar grep $_ eq $oclass, map {$_ => 1} @$oclasses)) {
+          &main::daemon_log("Adding objectClass $oclass", 1);
+          $ldap_entry->add(
+            objectClass => $oclass,
+          );
+          my $oclass_result = $ldap_entry->update($ldap_handle);
+        }
+      }
+
+      # Set FAIstate
+      if(defined($ldap_entry->get_value('FAIstate'))) {
+        if(!($ldap_entry->get_value('FAIstate') eq 'install')) {
+          $ldap_entry->replace(
+            'FAIstate' => 'install'
+          );
+          my $replace_result = $ldap_entry->update($ldap_handle);
+        }
+      } else {
+        $ldap_entry->add(
+          'FAIstate' => 'install'
+        );
+        my $add_result = $ldap_entry->update($ldap_handle);
+      }
+
+
+    } elsif ($ldap_mesg->count == 0) {
+      # TODO: Create a new entry
+      # $ldap_entry = Net::LDAP::Entry->new();
+      # $ldap_entry->dn("cn=$mac,$base");
+      &main::daemon_log("WARNING: No System with mac address '$mac' was found in base '".$main::ldap_base."'! Re-queuing job.", 4);
+      $main::job_db->exec_statement("UPDATE ".$main::job_queue_tn." SET status = 'waiting' WHERE id = $jobdb_id");
+      $main::job_db->exec_statement("UPDATE ".$main::job_queue_tn." SET timestamp = '".&get_time()."' WHERE id = $jobdb_id");
+      return undef;
+    } else {
+      &main::daemon_log("ERROR: More than one system with mac address '$mac' was found in base '".$main::ldap_base."'!", 1);
+    }
+
+    # Add to ObjectGroup
+    if(!(scalar grep $_, map {$_ => 1} $ogroup_entry->get_value('member', asref => 1))) {
+      $ogroup_entry->add (
+        'member' => $ldap_entry->dn(),
+      );
+      my $ogroup_result = $ogroup_entry->update($ldap_handle);
+      if ($ogroup_result->code() != 0) {
+        &main::daemon_log("ERROR: Updating the ObjectGroup '$ogroup' failed (code '".$ogroup_result->code()."') with '".$ogroup_result->{'errorMessage'}."'!", 1);
+      }
+    }
+
+    # Finally set gotoMode to active
+    if(defined($ldap_entry->get_value('gotoMode'))) {
+      if(!($ldap_entry->get_value('gotoMode') eq 'active')) {
+        $ldap_entry->replace(
+          'gotoMode' => 'active'
+        );
+        my $activate_result = $ldap_entry->update($ldap_handle);
+        if ($activate_result->code() != 0) {
+          &main::daemon_log("ERROR: Activating system '".$ldap_entry->dn()."' failed (code '".$activate_result->code()."') with '".$activate_result->{'errorMessage'}."'!", 1);
+        } else {
+          $activate_client = 1;
+        }
+      }
+    } else {
+      $ldap_entry->add(
+        'gotoMode' => 'active'
+      );
+      my $activate_result = $ldap_entry->update($ldap_handle);
+      if ($activate_result->code() != 0) {
+        &main::daemon_log("ERROR: Activating system '".$ldap_entry->dn()."' failed (code '".$activate_result->code()."') with '".$activate_result->{'errorMessage'}."'!", 1);
+      } else {
+        $activate_client = 1;
+      }
+    }
+
+    if($activate_client == 1) {
+      # Set job to done
+      $main::job_db->exec_statement("UPDATE jobs SET status = 'done' WHERE id = $jobdb_id");
+
+      # create set_activated_for_installation message for delivery
+      my $out_hash = &create_xml_hash("set_activated_for_installation", $source, $target);
+      my $out_msg = &create_xml_string($out_hash);
+      my @out_msg_l = ($out_msg);
+
+      return @out_msg_l;
+    } 
+    return undef;
 }