[LON-CAPA-cvs] cvs: loncom /cgi takeoffline.pl

raeburn raeburn@source.lon-capa.org
Tue, 06 Jan 2009 08:08:44 -0000


This is a MIME encoded message

--raeburn1231229324
Content-Type: text/plain

raeburn		Tue Jan  6 08:08:44 2009 EDT

  Modified files:              
    /loncom/cgi	takeoffline.pl 
  Log:
  - Replace <html><body> etc with standard loncommon::start_page() and loncommon::end_page().
  - Make taking offline a two step process.
  - Provide information about how to establish access to /cgi-bin/takeonline.pl to reduce the risk of DCs using takeoffline.pl to replace the log-in page with a rerouting page and then being unable to use takeonline.pl to restore the standard page.
  
  
--raeburn1231229324
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20090106080844.txt"

Index: loncom/cgi/takeoffline.pl
diff -u loncom/cgi/takeoffline.pl:1.4 loncom/cgi/takeoffline.pl:1.5
--- loncom/cgi/takeoffline.pl:1.4	Thu Dec 25 01:56:03 2008
+++ loncom/cgi/takeoffline.pl	Tue Jan  6 08:08:44 2009
@@ -2,7 +2,7 @@
 $|=1;
 # Take machine offline, reroute traffic
 #
-# $Id: takeoffline.pl,v 1.4 2008/12/25 01:56:03 raeburn Exp $
+# $Id: takeoffline.pl,v 1.5 2009/01/06 08:08:44 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -30,6 +30,7 @@
 use strict;
 use lib '/home/httpd/lib/perl/';
 use Apache::lonlocal;
+use Apache::loncommon;
 use LONCAPA::loncgi;
 use LONCAPA::lonauthcgi;
 
@@ -51,13 +52,129 @@
             return;
         }
     }
-
     &Apache::lonlocal::get_language_handle();
 
-    print '<html><body bgcolor="#FFFFFF"><h1>'.&Apache::lonlocal::mt('Take Offline').'</h1>';
-    my $statusmsg;
+    if ($ENV{'QUERY_STRING'} =~ /^phase\=two\&?(.*)$/) {
+        my $reroute = $1;
+        print &Apache::loncommon::start_page('Take Offline','',
+                                             {'no_inline_link'   => 1,});
+        &phasetwo($reroute);
+    } else {
+        my $js = <<ENDJS;
+<script type="text/javascript">
+
+function setphase(form) {
+    var subaction = '/cgi-bin/takeoffline.pl?phase=two';
+    form.action = subaction+'&'+form.rerouteto.options[form.rerouteto.selectedIndex].value;
+    form.submit();
+}
+
+</script>
+ENDJS
+        print &Apache::loncommon::start_page('Take Offline',$js,
+                                             {'no_inline_link'   => 1,});
+        &phaseone();
+    }
+    print &Apache::loncommon::end_page();
+    return;
+}
+
+sub phaseone {
+#    print '<html><body bgcolor="#FFFFFF">
+    print '<h2>'.&Apache::lonlocal::mt('Take Offline').'</h2>';
+    my ($is_dc,@okmachines,%servers);
+    my @poss_domains = &Apache::lonnet::current_machine_domains();
+    foreach my $dom (@poss_domains) {
+        if ($Apache::lonnet::env{'request.role'} eq "dc./$dom/") {
+            $is_dc = 1;
+        }
+        my %domconfig = &Apache::lonnet::get_dom('configuration',['serverstatuses'],$dom);
+        if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
+            if (ref($domconfig{'serverstatuses'}{'takeonline'}) eq 'HASH') {
+                if ($domconfig{'serverstatuses'}{'takeonline'}{'machines'} ne '') {
+                    @okmachines = split(/,/,$domconfig{'serverstatuses'}{'takeonline'}{'machines'});
+                }
+            }
+        }
+        %{$servers{$dom}} = &Apache::lonnet::get_servers($dom);
+    }
+    my $ip = $ENV{'REMOTE_ADDR'};
+    my $lonhost = $Apache::lonnet::perlvar{'lonHostID'};
+    print '<p>'.&mt("When a LON-CAPA server is 'taken offline' using this utility, the standard log-in page will be replaced with one of the following:").
+          '<ul><li>'.&mt('A page which automatically performs a redirect to another server in your domain - the server must be specified below.').'</li>'.
+              '<li>'.&mt('A page which announces that this LON-CAPA server is offline.').'</li></ul></p>';
+    print '<p>'.&mt("Once a server is offline, the [_1]'take online'[_2] utility can be used to reverse this, but you should verify that you will still be able to access that script once the server is in an offline state.",'<a href="/cgi-bin/takeonline.pl">','</a>').'</p>';
+    print '<p>'.&mt("In common with other Domain Status pages the 'take online' script is accessible from certain IP addresses:").'<ul>'.
+          '<li>'.&mt('The loopback device - 127.0.0.1 (localhost) - for times when you are web browsing from the server itself.').'</li>'.
+          '<li>'.&mt("Specified IP addresses set via the configuration for [_1]Access to Server Status Pages[_2] for domain(s) hosted on the server.",'<a href="/adm/domainprefs?phase=display&actions=serverstatuses">','</a>').'</li></ul><p>'.
+          &mt("Once the server is offline you will not be able to log-in directly to select a Domain Coordinator role to use the 'Take online' script, unless IP-based controls provide your access.").'<br />'.&mt("However, you will be able to log-in to a different server in the LON-CAPA network, select a Domain Coordinator role in this server's domain, and then use: [_1] to migrate you session to this machine.","<br /><tt>/adm/switchserver?otherserver=$lonhost</tt>").'<br /></p><p><hr /></p>';
+    if ($ip eq '127.0.0.1') {
+        print '<div class="LC_info">'.&mt('You are accessing this page from the loopback device.').'</div>';
+    } elsif (!@okmachines) {
+        print '<div class="LC_warning">'.&mt("IP-based access to the 'take online' page is currently unavailable, as no IP addresses have been specified.").'</div>';
+    } elsif (!grep(/^\Q$ip\E$/,@okmachines)) {
+        print '<div class="LC_warning">'.&mt("Your current IP [_1] is not among those configured to have IP-based access to the 'take online' utility.",$ip).'</div>';
+    } else {
+        print '<div class="LC_info">'.&mt("Your current IP [_1] is configured to have IP-based access to the 'take online' utility.",$ip).'</div>';
+    }
+    my (%allhosts,$otherserver,$domain);
     if ($ENV{'QUERY_STRING'}) {
-        my ($otherserver,$domain)=split(/\&/,$ENV{'QUERY_STRING'});
+        ($otherserver,$domain)=split(/\&/,$ENV{'QUERY_STRING'});
+    }
+    print '<p><hr /></p><p><form method="post" name="takeoffline" onSubmit="javascript:setphase(this);">';
+
+    my $options;
+    if ($domain) {
+        if (grep(/^\Q$domain\E$/,keys(%servers))) {
+            if (ref($servers{$domain}) eq 'HASH') {
+                foreach my $hostid (sort(keys(%{$servers{$domain}}))) {
+                    next if ($hostid eq $lonhost);
+                    my $selchk = '';
+                    if ($otherserver eq $hostid) {
+                        $selchk = 'selected="selected"';
+                    }
+                    $options .=
+   "<option value=\"$hostid&$domain\" $selchk>"."\n".
+   "$hostid ".&mt('Domain: [_1], Hostname: [_2]',$domain,$servers{$domain}{$hostid}).'</option>'."\n";
+                }
+            }
+        }
+    }
+    foreach my $dom (sort(keys(%servers))) {
+        next if (($domain) && ($dom eq $domain));
+        if (ref($servers{$dom}) eq 'HASH') {
+            foreach my $hostid (sort(keys(%{$servers{$dom}}))) {
+                next if ($hostid eq $lonhost);
+                next if(defined($allhosts{$hostid}));
+                $allhosts{$hostid} = 1;
+                my $selchk = '';
+                if ($otherserver eq $hostid) {
+                    $selchk = 'selected="selected"';
+                }
+                $options .=  
+               "<option value=\"$hostid&$dom\" $selchk>".
+               $hostid.'</option>';
+            }
+        }
+    }
+    if ($options) {
+        print &mt('Optional: choose another server in your domain, to which to redirect log-ins: ').
+              '<select name="rerouteto">'."\n".
+              ' <option value="">'.&mt('Please select').'</option>'."\n".
+              $options.'</select>'."\n";
+    } else {
+        print &mt('There are no other servers in your domain to which to redirect logins.');
+    }
+    print '<br /><br /><input type="submit" name="offline" value="Take Offline" />'."\n".
+          '</form>'."\n";
+}
+
+sub phasetwo {
+    my ($reroute) = @_;
+    print '<h2>'.&Apache::lonlocal::mt('Take Offline').'</h2>';
+    my $statusmsg;
+    if ($reroute) {
+        my ($otherserver,$domain)=split(/\&/,$reroute);
         if (&reroute($otherserver,$domain)) {
             print &Apache::lonlocal::mt('Rerouting to [_1]',$otherserver);
             $statusmsg = "status=rerouting&server=$otherserver&domain=$domain&time=".time.
@@ -69,13 +186,12 @@
             $statusmsg = 'status=offline&time='.time."by=$Apache::lonnet::env{'user.name'}:$Apache::lonnet::env{'user.domain'}"
         }
     }
-    print "</body></html>";
     if ($statusmsg) {
         if (open (STATUS,'>/home/httpd/html/lon-status/reroute.txt')) {
             print STATUS "$statusmsg\n";
             close(STATUS);
         } else {
-            print &Apache::lonlocal::mt('Logging of status change to [_1] failed.','reroute.txt'); 
+            print &Apache::lonlocal::mt('Logging of status change to [_1] failed.','<tt>/home/httpd/html/lon-status/reroute.txt</tt>'); 
         }
     }
 }

--raeburn1231229324--