File:
[LON-CAPA] /
loncom /
cgi /
takeoffline.pl
Revision
1.6.12.1:
download - view:
text,
annotated -
select for diffs
Wed Mar 11 03:33:22 2015 UTC (9 years, 5 months ago) by
raeburn
Branches:
version_2_11_X
CVS tags:
version_2_11_5_msu,
version_2_11_5,
version_2_11_4_uiuc,
version_2_11_4_msu,
version_2_11_4,
version_2_11_3_uiuc,
version_2_11_3_msu,
version_2_11_3,
version_2_11_2_uiuc,
version_2_11_2_msu,
version_2_11_2_educog,
version_2_11_2,
version_2_11_1
Diff to branchpoint 1.6:
preferred,
unified
- For 2.11
Backport 1.8
1: #!/usr/bin/perl
2: $|=1;
3: # Take machine offline, reroute traffic
4: #
5: # $Id: takeoffline.pl,v 1.6.12.1 2015/03/11 03:33:22 raeburn Exp $
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:
30: use strict;
31: use lib '/home/httpd/lib/perl/';
32: use Apache::lonlocal;
33: use Apache::loncommon;
34: use Apache::lonnet;
35: use LONCAPA::loncgi;
36: use LONCAPA::lonauthcgi;
37:
38: print "Content-type: text/html\n\n";
39:
40: &main();
41:
42: sub main {
43: if (!&LONCAPA::lonauthcgi::check_ipbased_access('takeoffline')) {
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:
50: if (!&LONCAPA::lonauthcgi::can_view('takeoffline')) {
51: &Apache::lonlocal::get_language_handle();
52: print(&LONCAPA::lonauthcgi::unauthorized_msg('takeoffline'));
53: return;
54: }
55: }
56: &Apache::lonlocal::get_language_handle();
57:
58: if ($ENV{'QUERY_STRING'} =~ /^phase\=two\&?(.*)$/) {
59: my $reroute = $1;
60: print &Apache::loncommon::start_page('Take Offline','',
61: {'no_inline_link' => 1,});
62: &phasetwo($reroute);
63: } else {
64: my $js = <<ENDJS;
65: <script type="text/javascript">
66: // <![CDATA[
67:
68: function setphase(form) {
69: var subaction = '/cgi-bin/takeoffline.pl?phase=two';
70: form.action = subaction+'&'+form.rerouteto.options[form.rerouteto.selectedIndex].value;
71: form.submit();
72: }
73:
74: // ]]>
75: </script>
76: ENDJS
77: print &Apache::loncommon::start_page('Take Offline',$js,
78: {'no_inline_link' => 1,});
79: &phaseone();
80: }
81: print &Apache::loncommon::end_page();
82: return;
83: }
84:
85: sub phaseone {
86: print '<h2>'.&Apache::lonlocal::mt('Take Offline').'</h2>';
87: my ($is_dc,@okmachines,%servers);
88: my @poss_domains = &Apache::lonnet::current_machine_domains();
89: foreach my $dom (@poss_domains) {
90: if ($Apache::lonnet::env{'request.role'} eq "dc./$dom/") {
91: $is_dc = 1;
92: }
93: my %domconfig = &Apache::lonnet::get_dom('configuration',['serverstatuses'],$dom);
94: if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
95: if (ref($domconfig{'serverstatuses'}{'takeonline'}) eq 'HASH') {
96: if ($domconfig{'serverstatuses'}{'takeonline'}{'machines'} ne '') {
97: @okmachines = split(/,/,$domconfig{'serverstatuses'}{'takeonline'}{'machines'});
98: }
99: }
100: }
101: %{$servers{$dom}} = &Apache::lonnet::get_servers($dom);
102: }
103: my $ip = $ENV{'REMOTE_ADDR'};
104: my $lonhost = $Apache::lonnet::perlvar{'lonHostID'};
105: 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>'.
106: '<ul><li>'.&mt('A page which automatically performs a redirect to another server in your domain - the server must be specified below.').'</li>'.
107: '<li>'.&mt('A page which announces that this LON-CAPA server is offline.').'</li></ul>';
108: 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>';
109: print '<p>'.&mt("In common with other Domain Status pages the 'take online' script is accessible from certain IP addresses:").'</p><ul>'.
110: '<li>'.&mt('The loopback device - 127.0.0.1 (localhost) - for times when you are web browsing from the server itself.').'</li>'.
111: '<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>'.
112: &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 />';
113: if ($ip eq '127.0.0.1') {
114: print '<div class="LC_info">'.&mt('You are accessing this page from the loopback device.').'</div>';
115: } elsif (!@okmachines) {
116: 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>';
117: } elsif (!grep(/^\Q$ip\E$/,@okmachines)) {
118: 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>';
119: } else {
120: print '<div class="LC_info">'.&mt("Your current IP [_1] is configured to have IP-based access to the 'take online' utility.",$ip).'</div>';
121: }
122: my (%allhosts,$otherserver,$domain);
123: if ($ENV{'QUERY_STRING'}) {
124: ($otherserver,$domain)=split(/\&/,$ENV{'QUERY_STRING'});
125: }
126: print '<hr /><form method="post" name="takeoffline" onsubmit="javascript:setphase(this);" action="">';
127:
128: my $options;
129: if ($domain) {
130: if (grep(/^\Q$domain\E$/,keys(%servers))) {
131: if (ref($servers{$domain}) eq 'HASH') {
132: foreach my $hostid (sort(keys(%{$servers{$domain}}))) {
133: next if ($hostid eq $lonhost);
134: my $selchk = '';
135: if ($otherserver eq $hostid) {
136: $selchk = ' selected="selected"';
137: }
138: $options .=
139: "<option value=\"$hostid&$domain\"$selchk>$servers{$domain}{$hostid}</option>\n";
140: }
141: }
142: }
143: }
144: foreach my $dom (sort(keys(%servers))) {
145: next if (($domain) && ($dom eq $domain));
146: if (ref($servers{$dom}) eq 'HASH') {
147: foreach my $hostid (sort(keys(%{$servers{$dom}}))) {
148: next if ($hostid eq $lonhost);
149: next if(defined($allhosts{$hostid}));
150: $allhosts{$hostid} = 1;
151: my $selchk = '';
152: if ($otherserver eq $hostid) {
153: $selchk = ' selected="selected"';
154: }
155: $options .=
156: "<option value=\"$hostid&$dom\"$selchk>$servers{$dom}{$hostid}</option>\n";
157: }
158: }
159: }
160: if ($options) {
161: print &mt('Optional: choose another server in your domain, to which to redirect log-ins: ').
162: '<select name="rerouteto">'."\n".
163: ' <option value="">'.&mt('Please select').'</option>'."\n".
164: $options.'</select>'."\n";
165: } else {
166: print &mt('There are no other servers in your domain to which to redirect logins.');
167: }
168: print '<br /><br /><input type="submit" name="offline" value="Take Offline" />'."\n".
169: '</form>'."\n";
170: }
171:
172: sub phasetwo {
173: my ($reroute) = @_;
174: print '<h2>'.&Apache::lonlocal::mt('Take Offline').'</h2>';
175: my $statusmsg;
176: if ($reroute ne '') {
177: my ($otherserver,$domain)=split(/\&/,$reroute);
178: my $otherhostname = &reroute($otherserver,$domain);
179: if ($otherhostname ne '') {
180: print &Apache::lonlocal::mt('Rerouting to [_1]',$otherhostname);
181: $statusmsg = "status=rerouting&server=$otherserver&domain=$domain&time=".time.
182: "by=$Apache::lonnet::env{'user.name'}:$Apache::lonnet::env{'user.domain'}";
183: }
184: } else {
185: if (&dead()) {
186: print &Apache::lonlocal::mt('No reroute server given, taking completely offline.');
187: $statusmsg = 'status=offline&time='.time."by=$Apache::lonnet::env{'user.name'}:$Apache::lonnet::env{'user.domain'}"
188: }
189: }
190: if ($statusmsg) {
191: if (open (STATUS,'>/home/httpd/html/lon-status/reroute.txt')) {
192: print STATUS "$statusmsg\n";
193: close(STATUS);
194: } else {
195: print &Apache::lonlocal::mt('Logging of status change to [_1] failed.','<tt>/home/httpd/html/lon-status/reroute.txt</tt>');
196: }
197: }
198: }
199:
200: sub dead {
201: if (open (OUT,'>/home/httpd/html/index.html')) {
202: print OUT &Apache::lonlocal::mt('This LON-CAPA server is currently offline.');
203: close(OUT);
204: return 'ok';
205: }
206: return;
207: }
208:
209: sub reroute {
210: my ($otherserver,$domain) = @_;
211: my $otherhostname = &Apache::lonnet::hostname($otherserver);
212: my $protocol = $Apache::lonnet::protocol{$otherserver};
213: $protocol = 'http' if ($protocol ne 'https');
214: if ($otherhostname ne '') {
215: if (open (OUT,'>/home/httpd/html/index.html')) {
216: print OUT (<<ENDNEWINDEX);
217: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
218: <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
219: <head>
220: <title>Welcome to the LearningOnline Network with CAPA</title>
221: <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
222: <meta http-equiv="refresh" content="0.5; url=$protocol://$otherhostname/adm/login?domain=$domain">
223: <meta http-equiv="pragma" content="no-cache" />
224: </head>
225: <body style="background-color:#004400; margin: 0px;">
226: <img src="/adm/lonIcons/header.gif" alt="loncapa banner" />
227: <p style="color:#ffffff">Connecting to
228: <a href="$protocol://$otherhostname/adm/login?domain=$domain" style="color:#ffffff">$protocol://$otherhostname/</a></p>
229: </body>
230: </html>
231: ENDNEWINDEX
232: close(OUT);
233: return $otherhostname;
234: }
235: }
236: return;
237: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>