Annotation of loncom/cgi/takeoffline.pl, revision 1.8

1.1       www         1: #!/usr/bin/perl
1.3       raeburn     2: $|=1;
1.1       www         3: # Take machine offline, reroute traffic
                      4: #
1.8     ! raeburn     5: # $Id: takeoffline.pl,v 1.7 2010/03/22 20:11:15 droeschl Exp $
1.1       www         6: #
                      7: # Copyright Michigan State University Board of Trustees
                      8: #
                      9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                     10: #
                     11: # LON-CAPA is free software; you can redistribute it and/or modify
                     12: # it under the terms of the GNU General Public License as published by
                     13: # the Free Software Foundation; either version 2 of the License, or
                     14: # (at your option) any later version.
                     15: #
                     16: # LON-CAPA is distributed in the hope that it will be useful,
                     17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     19: # GNU General Public License for more details.
                     20: #
                     21: # You should have received a copy of the GNU General Public License
                     22: # along with LON-CAPA; if not, write to the Free Software
                     23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     24: #
                     25: # /home/httpd/html/adm/gpl.txt
                     26: #
                     27: # http://www.lon-capa.org/
                     28: #
                     29: 
1.3       raeburn    30: use strict;
                     31: use lib '/home/httpd/lib/perl/';
                     32: use Apache::lonlocal;
1.5       raeburn    33: use Apache::loncommon;
1.8     ! raeburn    34: use Apache::lonnet;
1.3       raeburn    35: use LONCAPA::loncgi;
1.4       raeburn    36: use LONCAPA::lonauthcgi;
1.3       raeburn    37: 
                     38: print "Content-type: text/html\n\n";
                     39: 
                     40: &main();
                     41: 
                     42: sub main {
1.4       raeburn    43:     if (!&LONCAPA::lonauthcgi::check_ipbased_access('takeoffline')) {
1.3       raeburn    44:         if (!&LONCAPA::loncgi::check_cookie_and_load_env()) {
                     45:             &Apache::lonlocal::get_language_handle();
                     46:             print(&LONCAPA::loncgi::missing_cookie_msg());
                     47:             return;
                     48:         }
                     49: 
1.4       raeburn    50:         if (!&LONCAPA::lonauthcgi::can_view('takeoffline')) {
1.3       raeburn    51:             &Apache::lonlocal::get_language_handle();
1.4       raeburn    52:             print(&LONCAPA::lonauthcgi::unauthorized_msg('takeoffline'));
1.3       raeburn    53:             return;
                     54:         }
                     55:     }
1.5       raeburn    56:     &Apache::lonlocal::get_language_handle();
                     57: 
                     58:     if ($ENV{'QUERY_STRING'} =~ /^phase\=two\&?(.*)$/) {
                     59:         my $reroute = $1;
1.7       droeschl   60:         print &Apache::loncommon::start_page('Take Offline');
1.5       raeburn    61:         &phasetwo($reroute);
                     62:     } else {
                     63:         my $js = <<ENDJS;
                     64: <script type="text/javascript">
1.8     ! raeburn    65: // <![CDATA[
1.5       raeburn    66: 
                     67: function setphase(form) {
                     68:     var subaction = '/cgi-bin/takeoffline.pl?phase=two';
                     69:     form.action = subaction+'&'+form.rerouteto.options[form.rerouteto.selectedIndex].value;
                     70:     form.submit();
                     71: }
                     72: 
1.8     ! raeburn    73: // ]]>
1.5       raeburn    74: </script>
                     75: ENDJS
1.7       droeschl   76:         print &Apache::loncommon::start_page('Take Offline',$js);
1.5       raeburn    77:         &phaseone();
                     78:     }
                     79:     print &Apache::loncommon::end_page();
                     80:     return;
                     81: }
                     82: 
                     83: sub phaseone {
                     84:     print '<h2>'.&Apache::lonlocal::mt('Take Offline').'</h2>';
                     85:     my ($is_dc,@okmachines,%servers);
                     86:     my @poss_domains = &Apache::lonnet::current_machine_domains();
                     87:     foreach my $dom (@poss_domains) {
                     88:         if ($Apache::lonnet::env{'request.role'} eq "dc./$dom/") {
                     89:             $is_dc = 1;
                     90:         }
                     91:         my %domconfig = &Apache::lonnet::get_dom('configuration',['serverstatuses'],$dom);
                     92:         if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
                     93:             if (ref($domconfig{'serverstatuses'}{'takeonline'}) eq 'HASH') {
                     94:                 if ($domconfig{'serverstatuses'}{'takeonline'}{'machines'} ne '') {
                     95:                     @okmachines = split(/,/,$domconfig{'serverstatuses'}{'takeonline'}{'machines'});
                     96:                 }
                     97:             }
                     98:         }
                     99:         %{$servers{$dom}} = &Apache::lonnet::get_servers($dom);
                    100:     }
                    101:     my $ip = $ENV{'REMOTE_ADDR'};
                    102:     my $lonhost = $Apache::lonnet::perlvar{'lonHostID'};
1.8     ! raeburn   103:     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:").'</p>'.
1.5       raeburn   104:           '<ul><li>'.&mt('A page which automatically performs a redirect to another server in your domain - the server must be specified below.').'</li>'.
1.8     ! raeburn   105:               '<li>'.&mt('A page which announces that this LON-CAPA server is offline.').'</li></ul>';
1.5       raeburn   106:     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>';
1.8     ! raeburn   107:     print '<p>'.&mt("In common with other Domain Status pages the 'take online' script is accessible from certain IP addresses:").'</p><ul>'.
1.5       raeburn   108:           '<li>'.&mt('The loopback device - 127.0.0.1 (localhost) - for times when you are web browsing from the server itself.').'</li>'.
1.8     ! raeburn   109:           '<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&amp;actions=serverstatuses">','</a>').'</li></ul><p>'.
        !           110:           &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><hr />';
1.5       raeburn   111:     if ($ip eq '127.0.0.1') {
                    112:         print '<div class="LC_info">'.&mt('You are accessing this page from the loopback device.').'</div>';
                    113:     } elsif (!@okmachines) {
                    114:         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>';
                    115:     } elsif (!grep(/^\Q$ip\E$/,@okmachines)) {
                    116:         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>';
                    117:     } else {
                    118:         print '<div class="LC_info">'.&mt("Your current IP [_1] is configured to have IP-based access to the 'take online' utility.",$ip).'</div>';
                    119:     }
                    120:     my (%allhosts,$otherserver,$domain);
                    121:     if ($ENV{'QUERY_STRING'}) {
                    122:         ($otherserver,$domain)=split(/\&/,$ENV{'QUERY_STRING'});
                    123:     }
1.8     ! raeburn   124:     print '<hr /><form method="post" name="takeoffline" onsubmit="javascript:setphase(this);" action="">';
1.3       raeburn   125: 
1.5       raeburn   126:     my $options;
                    127:     if ($domain) {
                    128:         if (grep(/^\Q$domain\E$/,keys(%servers))) {
                    129:             if (ref($servers{$domain}) eq 'HASH') {
                    130:                 foreach my $hostid (sort(keys(%{$servers{$domain}}))) {
                    131:                     next if ($hostid eq $lonhost);
                    132:                     my $selchk = '';
                    133:                     if ($otherserver eq $hostid) {
1.8     ! raeburn   134:                         $selchk = ' selected="selected"';
1.5       raeburn   135:                     }
                    136:                     $options .=
1.8     ! raeburn   137:                         "<option value=\"$hostid&amp;$domain\"$selchk>$servers{$domain}{$hostid}</option>\n";
1.5       raeburn   138:                 }
                    139:             }
                    140:         }
                    141:     }
                    142:     foreach my $dom (sort(keys(%servers))) {
                    143:         next if (($domain) && ($dom eq $domain));
                    144:         if (ref($servers{$dom}) eq 'HASH') {
                    145:             foreach my $hostid (sort(keys(%{$servers{$dom}}))) {
                    146:                 next if ($hostid eq $lonhost);
                    147:                 next if(defined($allhosts{$hostid}));
                    148:                 $allhosts{$hostid} = 1;
                    149:                 my $selchk = '';
                    150:                 if ($otherserver eq $hostid) {
1.8     ! raeburn   151:                     $selchk = ' selected="selected"';
1.5       raeburn   152:                 }
1.8     ! raeburn   153:                 $options .=
        !           154:                     "<option value=\"$hostid&amp;$dom\"$selchk>$servers{$dom}{$hostid}</option>\n";
1.5       raeburn   155:             }
                    156:         }
                    157:     }
                    158:     if ($options) {
                    159:         print &mt('Optional: choose another server in your domain, to which to redirect log-ins: ').
                    160:               '<select name="rerouteto">'."\n".
                    161:               ' <option value="">'.&mt('Please select').'</option>'."\n".
                    162:               $options.'</select>'."\n";
                    163:     } else {
                    164:         print &mt('There are no other servers in your domain to which to redirect logins.');
                    165:     }
                    166:     print '<br /><br /><input type="submit" name="offline" value="Take Offline" />'."\n".
                    167:           '</form>'."\n";
                    168: }
1.3       raeburn   169: 
1.5       raeburn   170: sub phasetwo {
                    171:     my ($reroute) = @_;
                    172:     print '<h2>'.&Apache::lonlocal::mt('Take Offline').'</h2>';
1.3       raeburn   173:     my $statusmsg;
1.8     ! raeburn   174:     if ($reroute ne '') {
1.5       raeburn   175:         my ($otherserver,$domain)=split(/\&/,$reroute);
1.8     ! raeburn   176:         my $otherhostname = &reroute($otherserver,$domain);
        !           177:         if ($otherhostname ne '') {
        !           178:             print &Apache::lonlocal::mt('Rerouting to [_1]',$otherhostname);
1.3       raeburn   179:             $statusmsg = "status=rerouting&server=$otherserver&domain=$domain&time=".time.
                    180:                          "by=$Apache::lonnet::env{'user.name'}:$Apache::lonnet::env{'user.domain'}";
                    181:         }
                    182:     } else {
                    183:         if (&dead()) {
                    184:             print &Apache::lonlocal::mt('No reroute server given, taking completely offline.');
                    185:             $statusmsg = 'status=offline&time='.time."by=$Apache::lonnet::env{'user.name'}:$Apache::lonnet::env{'user.domain'}"
                    186:         }
                    187:     }
                    188:     if ($statusmsg) {
                    189:         if (open (STATUS,'>/home/httpd/html/lon-status/reroute.txt')) {
                    190:             print STATUS "$statusmsg\n";
                    191:             close(STATUS);
                    192:         } else {
1.5       raeburn   193:             print &Apache::lonlocal::mt('Logging of status change to [_1] failed.','<tt>/home/httpd/html/lon-status/reroute.txt</tt>'); 
1.3       raeburn   194:         }
                    195:     }
1.1       www       196: }
                    197: 
                    198: sub dead {
1.3       raeburn   199:     if (open (OUT,'>/home/httpd/html/index.html')) {
                    200:         print OUT &Apache::lonlocal::mt('This LON-CAPA server is currently offline.');
                    201:         close(OUT);
                    202:         return 'ok';
                    203:     }
                    204:     return;
1.1       www       205: }
                    206: 
                    207: sub reroute {
1.3       raeburn   208:     my ($otherserver,$domain) = @_;
1.8     ! raeburn   209:     my $otherhostname = &Apache::lonnet::hostname($otherserver);
        !           210:     my $protocol = $Apache::lonnet::protocol{$otherserver};
        !           211:     $protocol = 'http' if ($protocol ne 'https');
        !           212:     if ($otherhostname ne '') {
        !           213:         if (open (OUT,'>/home/httpd/html/index.html')) {
        !           214:             print OUT (<<ENDNEWINDEX);
        !           215: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        !           216: <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
1.1       www       217:  <head>
                    218:   <title>Welcome to the LearningOnline Network with CAPA</title>
1.8     ! raeburn   219:   <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
        !           220:   <meta http-equiv="refresh" content="0.5; url=$protocol://$otherhostname/adm/login?domain=$domain">
        !           221:   <meta http-equiv="pragma" content="no-cache" />
1.1       www       222:  </head>
1.8     ! raeburn   223:  <body style="background-color:#004400; margin: 0px;">
        !           224:   <img src="/adm/lonIcons/header.gif" alt="loncapa banner" />
        !           225: <p style="color:#ffffff">Connecting to 
        !           226: <a href="$protocol://$otherhostname/adm/login?domain=$domain" style="color:#ffffff">$protocol://$otherhostname/</a></p>
1.1       www       227: </body>
                    228: </html>
                    229: ENDNEWINDEX
1.8     ! raeburn   230:            close(OUT);
        !           231:            return $otherhostname;
        !           232:         }
1.3       raeburn   233:     }
                    234:     return;
1.1       www       235: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>