[LON-CAPA-cvs] cvs: loncom / lond

raeburn raeburn@source.lon-capa.org
Fri, 13 May 2011 02:58:02 -0000


raeburn		Fri May 13 02:58:02 2011 EDT

  Modified files:              
    /loncom	lond 
  Log:
  - Routine to check if server is a LON-CAPA "DNS" server.
  - Only log warning about missing managers.tab if server is a DNS server.
  - Limit files which can be modified via PushFile to:
    hosts.tab, domain.tab, dns_hosts.tab and dns_domain.tab
  - Send e-mail to Admin when a remote manager has called "pushfile" to 
    update dns_hosts.tab or dns_domain.tab.    
  
  
Index: loncom/lond
diff -u loncom/lond:1.471 loncom/lond:1.472
--- loncom/lond:1.471	Fri May 13 02:32:40 2011
+++ loncom/lond	Fri May 13 02:58:02 2011
@@ -2,7 +2,7 @@
 # The LearningOnline Network
 # lond "LON Daemon" Server (port "LOND" 5663)
 #
-# $Id: lond,v 1.471 2011/05/13 02:32:40 raeburn Exp $
+# $Id: lond,v 1.472 2011/05/13 02:58:02 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -53,13 +53,14 @@
 use LONCAPA::lonssl;
 use Fcntl qw(:flock);
 use Apache::lonnet;
+use Mail::Send;
 
 my $DEBUG = 0;		       # Non zero to enable debug log entries.
 
 my $status='';
 my $lastlog='';
 
-my $VERSION='$Revision: 1.471 $'; #' stupid emacs
+my $VERSION='$Revision: 1.472 $'; #' stupid emacs
 my $remoteVERSION;
 my $currenthostid="default";
 my $currentdomainid;
@@ -420,8 +421,10 @@
 
    my $tablename = $perlvar{'lonTabDir'}."/managers.tab";
    if (!open (MANAGERS, $tablename)) {
-      logthis('<font color="red">No manager table.  Nobody can manage!!</font>');
-      return;
+       if (&loncapa_dns_server()) {
+           &logthis('<font color="red">No manager table.  Nobody can manage!!</font>');
+       }
+       return;
    }
    while(my $host = <MANAGERS>) {
       chomp($host);
@@ -446,7 +449,7 @@
          }
       } else {
          logthis('<font color="green"> existing host'." $host</font>\n");
-         $managers{&Apache::lonnet::get_host_ip($host)} = $host;  # Use info from cluster tab if clumemeber
+         $managers{&Apache::lonnet::get_host_ip($host)} = $host;  # Use info from cluster tab if cluster memeber
       }
    }
 }
@@ -508,7 +511,8 @@
     my $me        = $perlvar{'lonHostID'};
 
     foreach my $line (split(/\n/,$contents)) {
-	if(!(($line eq "") || ($line =~ /^ *\#/) || ($line =~ /^ *$/))) {
+	if(!(($line eq "") || ($line =~ /^ *\#/) || ($line =~ /^ *$/) ||
+             ($line =~ /^\s*\^/))) {
 	    chomp($line);
 	    my ($id,$domain,$role,$name,$ip,$maxcon,$idleto,$mincon)=split(/:/,$line);
 	    if ($id eq $me) {
@@ -596,11 +600,8 @@
 #
 #   ConfigFileFromSelector: converts a configuration file selector
 #                 into a configuration file pathname.
-#                 It's probably no longer necessary to preserve
-#                 special handling of hosts or domain as those
-#                 files have been superceded by dns_hosts, dns_domain.
-#                 The default action is just to prepend the directory
-#                 and append .tab
+#                 Supports the following file selectors: 
+#                 hosts, domain, dns_hosts, dns_domain  
 #
 #
 #  Parameters:
@@ -613,15 +614,11 @@
     my $tablefile;
 
     my $tabledir = $perlvar{'lonTabDir'}.'/';
-    if ($selector eq "hosts") {
-	$tablefile = $tabledir."hosts.tab";
-    } elsif ($selector eq "domain") {
-	$tablefile = $tabledir."domain.tab";
-    } else {
+    if (($selector eq "hosts") || ($selector eq "domain") || 
+        ($selector eq "dns_hosts") || ($selector eq "dns_domain")) {
 	$tablefile =  $tabledir.$selector.'.tab';
     }
     return $tablefile;
-
 }
 #
 #   PushFile:  Called to do an administrative push of a file.
@@ -647,6 +644,8 @@
     #  supported:
     #   hosts.tab  ($filename eq host).
     #   domain.tab ($filename eq domain).
+    #   dns_hosts.tab ($filename eq dns_host).
+    #   dns_domain.tab ($filename eq dns_domain). 
     # Construct the destination filename or reject the request.
     #
     # lonManage is supposed to ensure this, however this session could be
@@ -679,16 +678,59 @@
     } else {
 	&logthis('<font color="green"> Installed new '.$tablefile
 		 ."</font>");
-
+        my $adminmail = $perlvar{'lonAdmEMail'};
+        my $admindom = &Apache::lonnet::host_domain($perlvar{'lonHostID'});
+        if ($admindom ne '') {
+            my %domconfig =
+                &Apache::lonnet::get_dom('configuration',['contacts'],$admindom);
+            if (ref($domconfig{'contacts'}) eq 'HASH') {
+                if ($domconfig{'contacts'}{'adminemail'} ne '') {
+                    $adminmail = $domconfig{'contacts'}{'adminemail'};
+                }
+            }
+        }
+        if ($adminmail =~ /^[^\@]+\@[^\@]+$/) {
+            my $msg = new Mail::Send;
+            my $senderaddress =  $perlvar{'lonSysEMail'};
+            $msg->to($adminmail);
+            $msg->subject('LON-CAPA DNS update on '.$perlvar{'lonHostID'});
+            if ($senderaddress) {
+                 $msg->add('From',$senderaddress);
+            }
+            $msg->add('Content-type','text/plain; charset=UTF-8');
+            if (my $fh = $msg->open()) {
+                print $fh 'Update to '.$tablefile.' from Cluster Manager '.
+                          $client."\n";
+                $fh->close;
+            }
+        }
     }
 
-
     #  Indicate success:
  
     return "ok";
 
 }
 
+sub loncapa_dns_server {
+    my $lonhost = &Apache::lonnet::get_host_ip($perlvar{'lonHostID'});
+    my $hoststable = "$perlvar{'lonTabDir'}/hosts.tab";
+    my $is_dns_server;
+    if (!open(HOSTS,"<$hoststable")) {
+        &logthis('<font color="yellow">Could not open hosts.tab to check for LON-CAPA DNS servers.</font>');
+        while (my $host = <HOSTS>) {
+            chomp($host);
+            $host =~ s/(^\s+|\s+$)//g;
+            if ($host =~ /^\Q^$lonhost\E/) {
+                $is_dns_server = 1;
+                last;
+            }
+        }
+        close(HOSTS);
+    }
+    return $is_dns_server;
+}
+
 #
 #  Called to re-init either lonc or lond.
 #