Annotation of loncom/cgi/takeoffline.pl, revision 1.7
1.1 www 1: #!/usr/bin/perl
1.3 raeburn 2: $|=1;
1.1 www 3: # Take machine offline, reroute traffic
4: #
1.7 ! droeschl 5: # $Id: takeoffline.pl,v 1.6 2009/04/23 17:29:58 bisitz 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.3 raeburn 34: use LONCAPA::loncgi;
1.4 raeburn 35: use LONCAPA::lonauthcgi;
1.3 raeburn 36:
37: print "Content-type: text/html\n\n";
38:
39: &main();
40:
41: sub main {
1.4 raeburn 42: if (!&LONCAPA::lonauthcgi::check_ipbased_access('takeoffline')) {
1.3 raeburn 43: if (!&LONCAPA::loncgi::check_cookie_and_load_env()) {
44: &Apache::lonlocal::get_language_handle();
45: print(&LONCAPA::loncgi::missing_cookie_msg());
46: return;
47: }
48:
1.4 raeburn 49: if (!&LONCAPA::lonauthcgi::can_view('takeoffline')) {
1.3 raeburn 50: &Apache::lonlocal::get_language_handle();
1.4 raeburn 51: print(&LONCAPA::lonauthcgi::unauthorized_msg('takeoffline'));
1.3 raeburn 52: return;
53: }
54: }
1.5 raeburn 55: &Apache::lonlocal::get_language_handle();
56:
57: if ($ENV{'QUERY_STRING'} =~ /^phase\=two\&?(.*)$/) {
58: my $reroute = $1;
1.7 ! droeschl 59: print &Apache::loncommon::start_page('Take Offline');
1.5 raeburn 60: &phasetwo($reroute);
61: } else {
62: my $js = <<ENDJS;
63: <script type="text/javascript">
64:
65: function setphase(form) {
66: var subaction = '/cgi-bin/takeoffline.pl?phase=two';
67: form.action = subaction+'&'+form.rerouteto.options[form.rerouteto.selectedIndex].value;
68: form.submit();
69: }
70:
71: </script>
72: ENDJS
1.7 ! droeschl 73: print &Apache::loncommon::start_page('Take Offline',$js);
1.5 raeburn 74: &phaseone();
75: }
76: print &Apache::loncommon::end_page();
77: return;
78: }
79:
80: sub phaseone {
81: # print '<html><body bgcolor="#FFFFFF">
82: print '<h2>'.&Apache::lonlocal::mt('Take Offline').'</h2>';
83: my ($is_dc,@okmachines,%servers);
84: my @poss_domains = &Apache::lonnet::current_machine_domains();
85: foreach my $dom (@poss_domains) {
86: if ($Apache::lonnet::env{'request.role'} eq "dc./$dom/") {
87: $is_dc = 1;
88: }
89: my %domconfig = &Apache::lonnet::get_dom('configuration',['serverstatuses'],$dom);
90: if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
91: if (ref($domconfig{'serverstatuses'}{'takeonline'}) eq 'HASH') {
92: if ($domconfig{'serverstatuses'}{'takeonline'}{'machines'} ne '') {
93: @okmachines = split(/,/,$domconfig{'serverstatuses'}{'takeonline'}{'machines'});
94: }
95: }
96: }
97: %{$servers{$dom}} = &Apache::lonnet::get_servers($dom);
98: }
99: my $ip = $ENV{'REMOTE_ADDR'};
100: my $lonhost = $Apache::lonnet::perlvar{'lonHostID'};
101: 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:").
102: '<ul><li>'.&mt('A page which automatically performs a redirect to another server in your domain - the server must be specified below.').'</li>'.
103: '<li>'.&mt('A page which announces that this LON-CAPA server is offline.').'</li></ul></p>';
104: 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>';
105: print '<p>'.&mt("In common with other Domain Status pages the 'take online' script is accessible from certain IP addresses:").'<ul>'.
106: '<li>'.&mt('The loopback device - 127.0.0.1 (localhost) - for times when you are web browsing from the server itself.').'</li>'.
107: '<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>'.
108: &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>';
109: if ($ip eq '127.0.0.1') {
110: print '<div class="LC_info">'.&mt('You are accessing this page from the loopback device.').'</div>';
111: } elsif (!@okmachines) {
112: 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>';
113: } elsif (!grep(/^\Q$ip\E$/,@okmachines)) {
114: 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>';
115: } else {
116: print '<div class="LC_info">'.&mt("Your current IP [_1] is configured to have IP-based access to the 'take online' utility.",$ip).'</div>';
117: }
118: my (%allhosts,$otherserver,$domain);
119: if ($ENV{'QUERY_STRING'}) {
120: ($otherserver,$domain)=split(/\&/,$ENV{'QUERY_STRING'});
121: }
122: print '<p><hr /></p><p><form method="post" name="takeoffline" onSubmit="javascript:setphase(this);">';
1.3 raeburn 123:
1.5 raeburn 124: my $options;
125: if ($domain) {
126: if (grep(/^\Q$domain\E$/,keys(%servers))) {
127: if (ref($servers{$domain}) eq 'HASH') {
128: foreach my $hostid (sort(keys(%{$servers{$domain}}))) {
129: next if ($hostid eq $lonhost);
130: my $selchk = '';
131: if ($otherserver eq $hostid) {
132: $selchk = 'selected="selected"';
133: }
134: $options .=
135: "<option value=\"$hostid&$domain\" $selchk>"."\n".
136: "$hostid ".&mt('Domain: [_1], Hostname: [_2]',$domain,$servers{$domain}{$hostid}).'</option>'."\n";
137: }
138: }
139: }
140: }
141: foreach my $dom (sort(keys(%servers))) {
142: next if (($domain) && ($dom eq $domain));
143: if (ref($servers{$dom}) eq 'HASH') {
144: foreach my $hostid (sort(keys(%{$servers{$dom}}))) {
145: next if ($hostid eq $lonhost);
146: next if(defined($allhosts{$hostid}));
147: $allhosts{$hostid} = 1;
148: my $selchk = '';
149: if ($otherserver eq $hostid) {
150: $selchk = 'selected="selected"';
151: }
152: $options .=
153: "<option value=\"$hostid&$dom\" $selchk>".
154: $hostid.'</option>';
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.5 raeburn 174: if ($reroute) {
175: my ($otherserver,$domain)=split(/\&/,$reroute);
1.3 raeburn 176: if (&reroute($otherserver,$domain)) {
177: print &Apache::lonlocal::mt('Rerouting to [_1]',$otherserver);
178: $statusmsg = "status=rerouting&server=$otherserver&domain=$domain&time=".time.
179: "by=$Apache::lonnet::env{'user.name'}:$Apache::lonnet::env{'user.domain'}";
180: }
181: } else {
182: if (&dead()) {
183: print &Apache::lonlocal::mt('No reroute server given, taking completely offline.');
184: $statusmsg = 'status=offline&time='.time."by=$Apache::lonnet::env{'user.name'}:$Apache::lonnet::env{'user.domain'}"
185: }
186: }
187: if ($statusmsg) {
188: if (open (STATUS,'>/home/httpd/html/lon-status/reroute.txt')) {
189: print STATUS "$statusmsg\n";
190: close(STATUS);
191: } else {
1.5 raeburn 192: print &Apache::lonlocal::mt('Logging of status change to [_1] failed.','<tt>/home/httpd/html/lon-status/reroute.txt</tt>');
1.3 raeburn 193: }
194: }
1.1 www 195: }
196:
197: sub dead {
1.3 raeburn 198: if (open (OUT,'>/home/httpd/html/index.html')) {
199: print OUT &Apache::lonlocal::mt('This LON-CAPA server is currently offline.');
200: close(OUT);
201: return 'ok';
202: }
203: return;
1.1 www 204: }
205:
206: sub reroute {
1.3 raeburn 207: my ($otherserver,$domain) = @_;
208: if (open (OUT,'>/home/httpd/html/index.html')) {
209: print OUT (<<ENDNEWINDEX);
1.1 www 210: <html>
211: <head>
212: <title>Welcome to the LearningOnline Network with CAPA</title>
1.2 www 213: <meta HTTP-EQUIV="Refresh" CONTENT="0.5; url=http://$otherserver/adm/login?domain=$domain">
1.1 www 214: </head>
1.6 bisitz 215: <body style="background-color:"#004400"; margin: 0px;">
1.1 www 216: <img src="/adm/lonIcons/header.gif" />
217: <p><font color="#FFFFFF">Connecting to
1.2 www 218: <a href="http://$otherserver/adm/login?domain=$domain"><font color="#FFFFFF">http://$otherserver/</font></a></font></p>
1.1 www 219: </body>
220: </html>
221: ENDNEWINDEX
1.3 raeburn 222: close(OUT);
223: return 'ok';
224: }
225: return;
1.1 www 226: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>