Code

Added new event "trigger_activate_new" - activates FAI systems that are waiting for...
authorjanw <janw@594d385d-05f5-0310-b6e9-bd551577e9d8>
Thu, 29 May 2008 07:06:33 +0000 (07:06 +0000)
committerjanw <janw@594d385d-05f5-0310-b6e9-bd551577e9d8>
Thu, 29 May 2008 07:06:33 +0000 (07:06 +0000)
git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@11091 594d385d-05f5-0310-b6e9-bd551577e9d8

gosa-si/server/events/gosaTriggered.pm

index 8f55b3368790ae2e04026774363508f62da5b4e7..164c4913e100a5c6d908b9103a09c94f68150497 100644 (file)
@@ -33,6 +33,7 @@ my @events = (
     "recreate_packages_list_db",
     "send_user_msg", 
     "get_available_kernel",
+       "trigger_activate_new",
     );
 @EXPORT = @events;
 
@@ -782,4 +783,152 @@ sub get_available_kernel {
 }
 
 
+sub trigger_activate_new {
+       my ($msg, $msg_hash, $session_id) = @_;
+
+       my $source = @{$msg_hash->{'source'}}[0];
+       my $target = @{$msg_hash->{'target'}}[0];
+       my $header= @{$msg_hash->{'header'}}[0];
+       &main::daemon_log(Dumper($msg_hash));
+       my $mac= (defined($msg_hash->{'mac'}))?@{$msg_hash->{'mac'}}[0]:undef;
+       my $ogroup= (defined($msg_hash->{'ogroup'}))?@{$msg_hash->{'ogroup'}}[0]:undef;
+       my $timestamp= (defined($msg_hash->{'timestamp'}))?@{$msg_hash->{'timestamp'}}[0]:undef;
+       my $base= (defined($msg_hash->{'base'}))?@{$msg_hash->{'base'}}[0]:undef;
+       my $hostname= (defined($msg_hash->{'fqdn'}))?@{$msg_hash->{'fqdn'}}[0]:undef;
+       my $ip_address= (defined($msg_hash->{'ip'}))?@{$msg_hash->{'ip'}}[0]:undef;
+       my $dhcp_statement= (defined($msg_hash->{'dhcp'}))?@{$msg_hash->{'dhcp'}}[0]:undef;
+       my $jobdb_id= (defined($msg_hash->{'jobdb_id'}))?@{$msg_hash->{'jobdb_id'}}[0]:undef;
+
+       my $ldap_handle = &main::get_ldap_handle();
+       my $ldap_entry;
+       my $ogroup_entry;
+
+       # build the base, use optional base parameter or take it from ogroup
+       if(!(defined($base) && (length($base) > 0))) {
+               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();
+
+                       # Subtract the ObjectGroup cn
+                       $base = $1 if $ogroup_entry->dn =~ /cn=$ogroup,ou=groups,(.*)$/;
+               } 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);
+               }
+       }
+
+       # prepend ou=systems
+       $base = "ou=systems,".$base;
+
+       # 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)))",
+       );
+
+       # 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("NOTICE: One system with mac address '$mac' was found in base '".$main::ldap_base."'!", 1);
+               # 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,
+                               );
+                               my $mesg = $ldap_entry->update($ldap_handle);
+                               if($mesg->code() != 0) {
+                                       &main::daemon_log("ERROR: Updating the dn for system with mac address '$mac' failed (code ".$mesg->code().") with '".$mesg->{'errorMessage'}."'!", 1);
+                               }
+                       }
+               # 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)) {
+                               $ldap_entry->add(
+                                       objectClass => $oclass,
+                               );
+                       }
+               }
+
+               # Set FAIstate
+               if(defined($ldap_entry->get_value('FAIstate'))) {
+                       if(!($ldap_entry->get_value('FAIstate') eq 'install')) {
+                               $ldap_entry->replace(
+                                       'FAIstate' => 'install'
+                               );
+                       }
+               } else {
+                       $ldap_entry->add(
+                               'FAIstate' => 'install'
+                       );
+               }
+
+               my $faistate_mesg = $ldap_entry->update($ldap_handle);
+               if ($faistate_mesg->code() != 0) {
+                       &main::daemon_log("ERROR: Updating the FAIstate for '".$ldap_entry->dn()."' failed (code '".$faistate_mesg->code()."') with '$@'!", 1);
+               }
+
+       } 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->select_dbentry("UPDATE jobs SET state = 'waiting', timestamp = '".&get_time()."' WHERE header = 'trigger_activate_new' AND mac_address LIKE '$mac'");
+       } else {
+               &main::daemon_log("ERROR: More than one system with mac address '$mac' was found in base '".$main::ldap_base."'!", 1);
+       }
+
+       my $update_mesg = $ldap_entry->update($ldap_handle);
+       if ($update_mesg->code() != 0) {
+               &main::daemon_log("ERROR: Updating attributes for '".$ldap_entry->dn()."' failed (code '".$update_mesg->code()."') with '".$update_mesg->{'errorMessage'}."'!", 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);
+                       &main::daemon_log(Dumper($ogroup_result));
+               }
+       }
+
+       # 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'
+                       );
+               }
+       } 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);
+       }
+
+
+       my %data;
+       my $out_msg = &build_msg("activate_new", $target, "GOSA", \%data);
+       return ( $out_msg );
+}
+
+
 1;