Code

bugfix: correct update of FAIstate by CLMSG_TASKBEGIN finish and faiend
[gosa.git] / gosa-si / server / events / clMessages.pm
index 686c9a0419ac72470d8e5ee1a0810c76abc40b99..3415ea14c0ea083d55d05a5003e84c7c9adb4823 100644 (file)
@@ -12,6 +12,8 @@ my @events = (
     "GOTOACTIVATION",
     "LOGIN",
     "LOGOUT",
+    "CURRENTLY_LOGGED_IN",
+    "save_fai_log",
     );
 @EXPORT = @events;
 
@@ -19,7 +21,7 @@ use strict;
 use warnings;
 use Data::Dumper;
 use GOSA::GosaSupportDaemon;
-use utf8;
+use MIME::Base64;
 
 
 BEGIN {}
@@ -28,13 +30,27 @@ END {}
 
 ### Start ######################################################################
 
-#&read_configfile($main::cfg_file, %cfg_defaults);
+my $ldap_uri;
+my $ldap_base;
+my $ldap_admin_dn;
+my $ldap_admin_password;
+
+my %cfg_defaults = (
+"server" => {
+   "ldap-uri" => [\$ldap_uri, ""],
+   "ldap-base" => [\$ldap_base, ""],
+   "ldap-admin-dn" => [\$ldap_admin_dn, ""],
+   "ldap-admin-password" => [\$ldap_admin_password, ""],
+   },
+);
+&read_configfile($main::cfg_file, %cfg_defaults);
 
 
 sub get_events {
     return \@events;
 }
 
+
 sub read_configfile {
     my ($cfg_file, %cfg_defaults) = @_;
     my $cfg;
@@ -56,41 +72,67 @@ sub read_configfile {
     }
 }
 
+
+sub save_fai_log {
+    my ($msg, $msg_hash, $session_id) = @_;
+    my $header = @{$msg_hash->{'header'}}[0];
+    my $source = @{$msg_hash->{'source'}}[0];
+    my $macaddress = @{$msg_hash->{'macaddress'}}[0];
+    my $all_logs = @{$msg_hash->{$header}}[0];
+
+    # if there is nothing to log
+    if( ref($all_logs) eq "HASH" ) { return; }
+        
+    my $client_fai_log_dir = $main::client_fai_log_dir;
+    if (not -d $client_fai_log_dir) {
+        mkdir($client_fai_log_dir, 0755)
+    }
+
+    $client_fai_log_dir = File::Spec->catfile( $client_fai_log_dir, $macaddress );
+    if (not -d $client_fai_log_dir) {
+        mkdir($client_fai_log_dir, 0755)
+    }
+
+    my $time = &get_time;
+    $time = substr($time, 0, 8)."_".substr($time, 8, 6);
+    $client_fai_log_dir = File::Spec->catfile( $client_fai_log_dir, "install-$time" );
+    mkdir($client_fai_log_dir, 0755);
+
+    my @all_logs = split(/log_file:/, $all_logs); 
+    foreach my $log (@all_logs) {
+        if (length $log == 0) { next; };
+        my ($log_file, $log_string) = split("\n", $log, 2);
+        my $client_fai_log_file = File::Spec->catfile( $client_fai_log_dir, $log_file);
+        open(my $LOG_FILE, ">$client_fai_log_file"); 
+        print $LOG_FILE &decode_base64($log_string);
+        close($LOG_FILE);
+    }
+    return;
+}
+
+
 sub LOGIN {
     my ($msg, $msg_hash, $session_id) = @_;
     my $header = @{$msg_hash->{'header'}}[0];
     my $source = @{$msg_hash->{'source'}}[0];
     my $login = @{$msg_hash->{$header}}[0];
 
-    my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$source'";
-    my $res = $main::known_clients_db->select_dbentry($sql_statement);
-    if( 1 != keys(%$res) ) {
-        &main::daemon_log("DEBUG: clMessages.pm: LOGIN: no or more hits found in known_clients_db for host '$source'");
+    my %add_hash = ( table=>$main::login_users_tn, 
+        primkey=> ['client', 'user'],
+        client=>$source,
+        user=>$login,
+        timestamp=>&get_time,
+        ); 
+    my ($res, $error_str) = $main::login_users_db->add_dbentry( \%add_hash );
+    if ($res != 0)  {
+        &main::daemon_log("$session_id ERROR: cannot add entry to known_clients: $error_str");
         return;
     }
 
-    my $act_login = $res->{'1'}->{'login'};
-    if( $act_login eq "nobody" ) {
-        $act_login = "";
-    }
-
-    $act_login =~ s/$login,?//gi;
-    my @act_login = split(",", $act_login);
-    unshift(@act_login, $login);
-    $act_login = join(",", @act_login);
-
-#print STDERR "source: $source\n";
-#print STDERR "login: $login\n";
-#print STDERR "act_login: $act_login\n";
-#print STDERR "dbres: ".Dumper($res)."\n";
-    $sql_statement = "UPDATE known_clients ".
-                "SET login='$act_login' ".
-                "WHERE hostname='$source'";
-    $res = $main::known_clients_db->update_dbentry($sql_statement);
     return;   
 }
 
-
+# TODO umstellen wie bei LOGIN
 sub LOGOUT {
     my ($msg, $msg_hash, $session_id) = @_;
     my $header = @{$msg_hash->{'header'}}[0];
@@ -109,203 +151,293 @@ sub LOGOUT {
 
     if( $act_login eq "" ){ $act_login = "nobody"; }
 
-    $sql_statement = "UPDATE known_clients ".
-                "SET login='$act_login' ".
-                "WHERE hostname='$source'";
+    $sql_statement = "UPDATE known_clients SET login='$act_login' WHERE hostname='$source'";
     $res = $main::known_clients_db->update_dbentry($sql_statement);
     
     return;
 }
 
 
+sub CURRENTLY_LOGGED_IN {
+    my ($msg, $msg_hash, $session_id) = @_;
+    my ($sql_statement, $db_res);
+    my $header = @{$msg_hash->{'header'}}[0];
+    my $source = @{$msg_hash->{'source'}}[0];
+    my $login = @{$msg_hash->{$header}}[0];
+
+    # fetch all user currently assigned to the client at login_users_db
+    my %currently_logged_in_user = (); 
+    $sql_statement = "SELECT * FROM $main::login_users_tn WHERE client='$source'"; 
+    $db_res = $main::login_users_db->select_dbentry($sql_statement);
+    while( my($hit_id, $hit) = each(%{$db_res}) ) {
+        $currently_logged_in_user{$hit->{'user'}} = 1;
+    }
+    &main::daemon_log("$session_id DEBUG: logged in users from login_user_db: ".join(", ", keys(%currently_logged_in_user)), 7); 
+
+    # 
+    my @logged_in_user = split(/\s+/, $login);
+    &main::daemon_log("$session_id DEBUG: logged in users reported from client: ".join(", ", @logged_in_user), 7); 
+    foreach my $user (@logged_in_user) {
+        my %add_hash = ( table=>$main::login_users_tn, 
+                primkey=> ['client', 'user'],
+                client=>$source,
+                user=>$user,
+                timestamp=>&get_time,
+                ); 
+        my ($res, $error_str) = $main::login_users_db->add_dbentry( \%add_hash );
+        if ($res != 0)  {
+            &main::daemon_log("$session_id ERROR: cannot add entry to known_clients: $error_str");
+            return;
+        }
+
+        delete $currently_logged_in_user{$user};
+    }
+
+    # if there is still a user in %currently_logged_in_user 
+    # although he is not reported by client 
+    # then delete it from $login_user_db
+    foreach my $obsolete_user (keys(%currently_logged_in_user)) {
+        &main::daemon_log("$session_id WARNING: user '$obsolete_user' is currently not logged ".
+                "in at client '$source' but still found at login_user_db", 3); 
+        my $sql_statement = "DELETE FROM $main::login_users_tn WHERE client='$source' AND user='$obsolete_user'"; 
+        my $res =  $main::login_users_db->del_dbentry($sql_statement);
+        &main::daemon_log("$session_id WARNING: delete user '$obsolete_user' at client '$source' from login_user_db", 3); 
+    }
+
+    return;
+}
+
 
-# echo "GOTOACTIVATION" > /var/run/gosa-si-client.socket
 sub GOTOACTIVATION {
     my ($msg, $msg_hash, $session_id) = @_;
-    my $out_msg = &build_result_update_msg($msg_hash);
-    my @out_msg_l = ($out_msg);  
-    return @out_msg_l; 
+    my $header = @{$msg_hash->{'header'}}[0];
+    my $source = @{$msg_hash->{'target'}}[0];
+    my $macaddress = @{$msg_hash->{'macaddress'}}[0];
+
+    # test whether content is an empty hash or a string which is required
+    my $content = @{$msg_hash->{$header}}[0];
+    eval{ if( 0 == keys(%$content) ) { $content = ""; } };
+    if( $@ ) { $content = "$content"; }
+
+    # clean up header
+    $header =~ s/CLMSG_//g;
+
+    my $sql_statement = "UPDATE $main::job_queue_tn ".
+            "SET status='processing', result='$header"."$content' ".
+            "WHERE status='processing' AND macaddress LIKE '$macaddress'"; 
+    &main::daemon_log("$session_id DEBUG: $sql_statement", 7);         
+    my $res = $main::job_db->update_dbentry($sql_statement);
+    &main::daemon_log("$session_id INFO: $header at '$macaddress'", 5); 
+    return; 
 }
 
 
-# echo "PROGRESS 15" > /var/run/gosa-si-client.socket
 sub PROGRESS {
     my ($msg, $msg_hash, $session_id) = @_;
-    my $out_msg = &build_progress_update_msg($msg_hash);
-    my @out_msg_l = ($out_msg);  
-    return @out_msg_l; 
+    my $header = @{$msg_hash->{'header'}}[0];
+    my $source = @{$msg_hash->{'target'}}[0];
+    my $macaddress = @{$msg_hash->{'macaddress'}}[0];
+
+    # test whether content is an empty hash or a string which is required
+    my $content;
+    my $cont = @{$msg_hash->{$header}}[0];
+    eval{ if( 0 == keys(%$cont) ) { $content = ""; } };
+    if( $@ ) { $content = "$cont"; }
+
+    # clean up header
+    $header =~ s/CLMSG_//g;
+
+    my $sql_statement = "UPDATE $main::job_queue_tn ".
+        "SET progress='$content' ".
+        "WHERE status='processing' AND macaddress LIKE '$macaddress'";
+    &main::daemon_log("$session_id DEBUG: $sql_statement", 7);         
+    my $res = $main::job_db->update_dbentry($sql_statement);
+    &main::daemon_log("$session_id INFO: $header at '$macaddress' - $content%", 5); 
+
+    return;
 }
 
-# echo "FAIREBOOT" > /tmp/gosa-si-client-fifo
+
 sub FAIREBOOT {
     my ($msg, $msg_hash, $session_id) = @_;
-    my $out_msg = &build_status_result_update_msg($msg_hash);
-    my @out_msg_l = ($out_msg);  
-    return @out_msg_l; 
+    my $header = @{$msg_hash->{'header'}}[0];
+    my $source = @{$msg_hash->{'target'}}[0];
+    my $macaddress = @{$msg_hash->{'macaddress'}}[0];
+
+    # test whether content is an empty hash or a string which is required
+    my $content = @{$msg_hash->{$header}}[0];
+    eval{ if( 0 == keys(%$content) ) { $content = ""; } };
+    if( $@ ) { $content = "$content"; }
+
+    # clean up header
+    $header =~ s/CLMSG_//g;
+
+    my $sql_statement = "UPDATE $main::job_queue_tn ".
+            "SET status='processing', result='$header "."$content' ".
+            "WHERE status='processing' AND macaddress LIKE '$macaddress'"; 
+    &main::daemon_log("$session_id DEBUG: $sql_statement", 7);         
+    my $res = $main::job_db->update_dbentry($sql_statement);
+    &main::daemon_log("$session_id INFO: $header at '$macaddress' - '$content'", 5); 
+
+    return; 
 }
 
-# echo "TASKSKIP hallo welt" > /tmp/gosa-si-client-fifo
+
 sub TASKSKIP {
     my ($msg, $msg_hash, $session_id) = @_;
-    my $out_msg = &build_status_result_update_msg($msg_hash);
-    my @out_msg_l = ($out_msg);  
-    return @out_msg_l; 
+    my $header = @{$msg_hash->{'header'}}[0];
+    my $source = @{$msg_hash->{'target'}}[0];
+    my $macaddress = @{$msg_hash->{'macaddress'}}[0];
+
+    # test whether content is an empty hash or a string which is required
+    my $content = @{$msg_hash->{$header}}[0];
+    eval{ if( 0 == keys(%$content) ) { $content = ""; } };
+    if( $@ ) { $content = "$content"; }
+
+    # clean up header
+    $header =~ s/CLMSG_//g;
+
+    my $sql_statement = "UPDATE $main::job_queue_tn ".
+            "SET status='processing', result='$header "."$content' ".
+            "WHERE status='processing' AND macaddress LIKE '$macaddress'"; 
+    &main::daemon_log("$session_id DEBUG: $sql_statement", 7);         
+    my $res = $main::job_db->update_dbentry($sql_statement);
+    &main::daemon_log("$session_id INFO: $header at '$macaddress' - '$content'", 5); 
+
+    return; 
 }
 
 
-# echo "TASKBEGIN hallo welt" > /tmp/gosa-si-client-fifo
 sub TASKBEGIN {
     my ($msg, $msg_hash, $session_id) = @_;
-    my $out_msg = &build_status_result_update_msg($msg_hash);
-    my @out_msg_l = ($out_msg);  
-    return @out_msg_l; 
-}
+    my $header = @{$msg_hash->{'header'}}[0];
+    my $source = @{$msg_hash->{'target'}}[0];
+    my $macaddress = @{$msg_hash->{'macaddress'}}[0];
+    my $content = @{$msg_hash->{$header}}[0];
 
-# echo "TASKEND hallo welt" > /tmp/gosa-si-client-fifo
-sub TASKEND {
-    my ($msg, $msg_hash, $session_id) = @_;
-    my $out_msg = &build_status_result_update_msg($msg_hash);
-    my @out_msg_l = ($out_msg);  
-    return @out_msg_l; 
-}
+    # test whether content is an empty hash or a string which is required
+    eval{ if( 0 == keys(%$content) ) { $content = ""; } };
+    if( $@ ) { $content = "$content"; }
 
+    # clean up header
+    $header =~ s/CLMSG_//g;
 
-# echo "TASKERROR hallo welt" > /tmp/gosa-si-client-fifo
-sub TASKERROR {
-    my ($msg, $msg_hash, $session_id) = @_;
-    my $out_msg = &build_status_result_update_msg($msg_hash);
-    my @out_msg_l = ($out_msg);  
-    return @out_msg_l; 
-}
+    # check if installation finished
+    if (($content eq 'finish') || ($content eq 'faiend')){
+        my $sql_statement = "UPDATE $main::job_queue_tn ".
+            "SET status='done', result='$header "."$content' ".
+            "WHERE status='processing' AND macaddress LIKE '$macaddress'"; 
+        &main::daemon_log("$session_id DEBUG: $sql_statement", 7);         
+        my $res = $main::job_db->update_dbentry($sql_statement);
+        &main::daemon_log("$session_id INFO: $header at '$macaddress' - '$content'", 5); 
+        
+        # set fai_state to localboot
+        &main::change_fai_state('localboot', \@{$msg_hash->{'macaddress'}}, $session_id);
 
+    } else {
+        my $sql_statement = "UPDATE $main::job_queue_tn ".
+            "SET status='processing', result='$header "."$content' ".
+            "WHERE status='processing' AND macaddress LIKE '$macaddress'"; 
+        &main::daemon_log("$session_id DEBUG: $sql_statement", 7);         
+        my $res = $main::job_db->update_dbentry($sql_statement);
+        &main::daemon_log("$session_id INFO: $header at '$macaddress' - '$content'", 5); 
+
+
+# -----------------------> Update hier
+#  <CLMSG_TASKBEGIN>finish</CLMSG_TASKBEGIN>
+#  <header>CLMSG_TASKBEGIN</header>
+# macaddress auslesen, Client im LDAP lokalisieren
+# FAIstate auf "localboot" setzen, wenn FAIstate "install" oder "softupdate" war
+    }
 
-# echo "HOOK hallo welt" > /tmp/gosa-si-client-fifo
-sub HOOK {
-    my ($msg, $msg_hash, $session_id) = @_;
-    my $out_msg = &build_status_result_update_msg($msg_hash);
-    my @out_msg_l = ($out_msg);  
-    return @out_msg_l; 
+    return; 
 }
 
 
-sub build_status_result_update_msg {
-    my ($msg_hash) = @_;
-
+sub TASKEND {
+    my ($msg, $msg_hash, $session_id) = @_;
     my $header = @{$msg_hash->{'header'}}[0];
     my $source = @{$msg_hash->{'target'}}[0];
-    my $target = @{$msg_hash->{'target'}}[0];
     my $macaddress = @{$msg_hash->{'macaddress'}}[0];
 
     # test whether content is an empty hash or a string which is required
     my $content = @{$msg_hash->{$header}}[0];
-    eval{
-        if( 0 == keys(%$content) ) {
-            $content = "";
-        }
-    };
-    if( $@ ) {
-        $content = " $content";
-    }
+    eval{ if( 0 == keys(%$content) ) { $content = ""; } };
+    if( $@ ) { $content = "$content"; }
 
+    # clean up header
     $header =~ s/CLMSG_//g;
-    my $out_msg = sprintf("<xml> ".  
-        "<header>gosa_update_status_jobdb_entry</header> ".
-        "<source>%s</source> ".
-        "<target>%s</target>".
-        "<where> ".
-            "<clause> ".
-                "<phrase> ".
-                    "<status>processing</status> ".
-                    "<macaddress>%s</macaddress> ".
-                "</phrase> ".
-            "</clause> ".
-        "</where> ".
-        "<update> ".
-            "<status>processing</status> ".
-            "<result>%s</result> ".
-        "</update> ".
-        "</xml>", $source, "JOBDB", $macaddress, $header.$content);
-    return $out_msg;
-}   
-
-
-sub build_progress_update_msg {
-    my ($msg_hash) = @_;
 
+    my $sql_statement = "UPDATE $main::job_queue_tn ".
+            "SET status='processing', result='$header "."$content' ".
+            "WHERE status='processing' AND macaddress LIKE '$macaddress'"; 
+    &main::daemon_log("$session_id DEBUG: $sql_statement", 7);         
+    my $res = $main::job_db->update_dbentry($sql_statement);
+    &main::daemon_log("$session_id INFO: $header at '$macaddress' - '$content'", 5); 
+
+# -----------------------> Update hier
+#  <CLMSG_TASKBEGIN>finish</CLMSG_TASKBEGIN>
+#  <header>CLMSG_TASKBEGIN</header>
+# macaddress auslesen, Client im LDAP lokalisieren
+# FAIstate auf "error" setzen
+
+    return; 
+}
+
+
+sub TASKERROR {
+    my ($msg, $msg_hash, $session_id) = @_;
     my $header = @{$msg_hash->{'header'}}[0];
     my $source = @{$msg_hash->{'target'}}[0];
-    my $target = @{$msg_hash->{'target'}}[0];
     my $macaddress = @{$msg_hash->{'macaddress'}}[0];
 
+    # clean up header
+    $header =~ s/CLMSG_//g;
+
     # test whether content is an empty hash or a string which is required
     my $content = @{$msg_hash->{$header}}[0];
-    eval{
-        if( 0 == keys(%$content) ) {
-            $content = "";
-        }
-    };
-    if( $@ ) {
-        $content = "$content";
-    }
-
-    $header =~ s/CLMSG_//g;
-    my $out_msg = sprintf("<xml> ".  
-        "<header>gosa_update_status_jobdb_entry</header> ".
-        "<source>%s</source> ".
-        "<target>%s</target>".
-        "<where> ".
-            "<clause> ".
-                "<phrase> ".
-                    "<status>processing</status> ".
-                    "<macaddress>%s</macaddress> ".
-                "</phrase> ".
-            "</clause> ".
-        "</where> ".
-        "<update> ".
-            "<progress>%s</progress> ".
-        "</update> ".
-        "</xml>", $source, "JOBDB", $macaddress, $content);
-    return $out_msg;
+    eval{ if( 0 == keys(%$content) ) { $content = ""; } };
+    if( $@ ) { $content = "$content"; }
+
+    my $sql_statement = "UPDATE $main::job_queue_tn ".
+            "SET status='processing', result='$header "."$content' ".
+            "WHERE status='processing' AND macaddress LIKE '$macaddress'"; 
+    &main::daemon_log("$session_id DEBUG: $sql_statement", 7);         
+    my $res = $main::job_db->update_dbentry($sql_statement);
+    &main::daemon_log("$session_id INFO: $header at '$macaddress' - '$content'", 5); 
+
+# -----------------------> Update hier
+#  <CLMSG_TASKBEGIN>finish</CLMSG_TASKBEGIN>
+#  <header>CLMSG_TASKBEGIN</header>
+# macaddress auslesen, Client im LDAP lokalisieren
+# FAIstate auf "error" setzen
+
+    return; 
 }
 
 
-sub build_result_update_msg {
-    my ($msg_hash) = @_;
-
+sub HOOK {
+    my ($msg, $msg_hash, $session_id) = @_;
     my $header = @{$msg_hash->{'header'}}[0];
     my $source = @{$msg_hash->{'target'}}[0];
-    my $target = @{$msg_hash->{'target'}}[0];
     my $macaddress = @{$msg_hash->{'macaddress'}}[0];
 
+    # clean up header
+    $header =~ s/CLMSG_//g;
+
     # test whether content is an empty hash or a string which is required
     my $content = @{$msg_hash->{$header}}[0];
-    eval{
-        if( 0 == keys(%$content) ) {
-            $content = "";
-        }
-    };
-    if( $@ ) {
-        $content = " $content";
-    }
+    eval{ if( 0 == keys(%$content) ) { $content = ""; } };
+    if( $@ ) { $content = "$content"; }
 
-    $header =~ s/CLMSG_//g;
-    my $out_msg = sprintf("<xml> ".  
-        "<header>gosa_update_status_jobdb_entry</header> ".
-        "<source>%s</source> ".
-        "<target>%s</target>".
-        "<where> ".
-            "<clause> ".
-                "<phrase> ".
-                    "<status>processing</status> ".
-                    "<macaddress>%s</macaddress> ".
-                "</phrase> ".
-            "</clause> ".
-        "</where> ".
-        "<update> ".
-            "<result>%s</result> ".
-        "</update> ".
-        "</xml>", $source, "JOBDB", $macaddress, $header.$content);
-    return $out_msg;
+    my $sql_statement = "UPDATE $main::job_queue_tn ".
+            "SET status='processing', result='$header "."$content' ".
+            "WHERE status='processing' AND macaddress LIKE '$macaddress'"; 
+    &main::daemon_log("$session_id DEBUG: $sql_statement", 7);         
+    my $res = $main::job_db->update_dbentry($sql_statement);
+    &main::daemon_log("$session_id INFO: $header at '$macaddress' - '$content'", 5); 
+
+    return;
 }