Annotation of loncom/lonManage, revision 1.25
1.1 foxr 1: #!/usr/bin/perl
2: # The LearningOnline Network with CAPA
3: #
4: # lonManage supports remote management of nodes in a LonCAPA cluster.
5: #
1.25 ! foxr 6: # $Id: lonManage,v 1.24 2003/11/04 11:36:04 foxr Exp $
1.1 foxr 7: #
1.25 ! foxr 8: # $Id: lonManage,v 1.24 2003/11/04 11:36:04 foxr Exp $
1.1 foxr 9: #
10: # Copyright Michigan State University Board of Trustees
11: #
12: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
13: ## LON-CAPA is free software; you can redistribute it and/or modify
14: # it under the terms of the GNU General Public License as published by
15: # the Free Software Foundation; either version 2 of the License, or
16: # (at your option) any later version.
17: #
18: # LON-CAPA is distributed in the hope that it will be useful,
19: # but WITHOUT ANY WARRANTY; without even the implied warranty of
20: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21: # GNU General Public License for more details.
22: #
23: # You should have received a copy of the GNU General Public License
24: # along with LON-CAPA; if not, write to the Free Software
25: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26: #
27: # /home/httpd/html/adm/gpl.txt
28: #
29: # http://www.lon-capa.org/
30: #
31: #
32: # lonManage supports management of remot nodes in a lonCAPA cluster.
33: # it is a command line tool. The following command line syntax (usage)
34: # is supported:
35: #
1.16 foxr 36: # lonManage -push <tablename> newfile [host]
1.1 foxr 37: # Push <tablename> to the lonTabs directory. Note that
38: # <tablename> must be one of:
1.15 foxr 39: # host (hosts.tab)
1.1 foxr 40: # domain (domain.tab)
41: #
1.16 foxr 42: # lonManage -reinit lonc [host]
1.1 foxr 43: # Sends a HUP signal to the remote systems's lond.
44: #
1.16 foxr 45: # lonmanage -reinit lond [host]
1.1 foxr 46: # Requests the remote system's lond perform the same action as if
47: # it had received a HUP signal.
48: #
49: # In the above syntax, the host above is the hosts.tab name of a host,
1.16 foxr 50: # not the IP address of the host
51: #
52: # If [host] is not supplied, every host in the client's hosts.tab
53: # table is iterated through and procesed..
1.1 foxr 54: #
1.3 foxr 55: #
1.10 foxr 56:
1.14 foxr 57:
1.13 foxr 58:
1.10 foxr 59: # Modules required:
60:
1.17 foxr 61: use lib ".";
62:
1.7 foxr 63: use strict; # Because it's good practice.
64: use English; # Cause I like meaningful names.
1.3 foxr 65: use Getopt::Long;
1.17 foxr 66: use LondConnection;
1.23 foxr 67: use IO::Poll qw(POLLRDNORM POLLWRNORM POLLIN POLLHUP POLLOUT);
1.10 foxr 68:
69: # File scoped variables:
70:
71: my %perlvar; # Perl variable defs from apache config.
72: my %hostshash; # Host table as a host indexed hash.
1.2 foxr 73:
1.19 foxr 74: my $MyHost=""; # Host name to use as me.
75: my $ForeignHostTab=""; # Name of foreign hosts table.
1.23 foxr 76:
77: my $DefaultServerPort = 5663; # Default server port if standalone.
1.21 foxr 78: my $ServerPort; # Port used to connect to lond.
1.18 foxr 79:
1.23 foxr 80: my $TransitionTimeout = 5; # Poll timeout in seconds.
81:
82:
1.24 foxr 83: # LondConnection::SetDebug(10);
1.23 foxr 84:
85:
1.13 foxr 86: #
87: # prints out utility's command usage info.
88: #
1.3 foxr 89: sub Usage {
1.2 foxr 90: print "Usage:";
91: print <<USAGE;
1.18 foxr 92: lonManage [--myname=host --hosts=table] --push=<tablename> newfile [host]
1.2 foxr 93: Push <tablename> to the lonTabs directory. Note that
94: <tablename> must be one of:
1.15 foxr 95: host (hosts.tab)
1.2 foxr 96: domain (domain.tab)
97:
1.18 foxr 98: lonManage [--myname=host --hosts=table] --reinit=lonc [host]
1.15 foxr 99: Causes lonc in the remote system to reread hosts.tab and
100: adjust the set of clients that are being maintained to match
101: the new file.
102:
1.2 foxr 103:
1.18 foxr 104: lonManage [--myname=host --hosts=table] --reinit=lond [host]
1.15 foxr 105: Causes lond in the remote system to reread the hosts.tab file
106: and adjust the set of servers to match changes in that file.
1.2 foxr 107:
108: In the above syntax, the host above is the hosts.tab name of a host,
109: not the IP address of the host.
1.16 foxr 110:
111: If [host] is omitted, all hosts in the hosts.tab file are iterated
112: over.
113:
1.18 foxr 114: For all of the above syntaxes if --myname=host and --hosts=table are
115: supplied (both must be present), the utility runs in standalone mode
116: presenting itself to the world as 'host' and using the hosts.tab file
117: specified in the --hosts switch.
1.2 foxr 118: USAGE
119:
120:
121: }
1.21 foxr 122:
1.23 foxr 123: #
124: # Make a direct connection to the lond in 'host'. The port is
125: # gotten from the global variable: ServerPort.
126: # Returns:
127: # The connection or undef if one could not be formed.
128: #
1.21 foxr 129: sub MakeLondConnection {
130: my $host = shift;
1.22 foxr 131:
132: my $Connection = LondConnection->new($host, $ServerPort);
133: return return $Connection;
1.21 foxr 134: }
1.23 foxr 135: #
1.25 ! foxr 136: # Process the connection state machine until the connection
! 137: # becomes idle. This is used both to negotiate the initial
! 138: # connection, during which the LondConnection sequences a rather
! 139: # complex state machine and during the transaction itself
! 140: # for a simpler set of transitions.
! 141: # All we really need to be concerned with is whether or not
! 142: # we're readable or writable and the final state:
! 143: #
! 144: # Parameter:
! 145: # connection - Represents the LondConnection to be sequenced.
! 146: # timeout - Maximum time to wait for readable/writable sockets.
! 147: # in seconds. < 0 waits forever.
! 148: # Return:
! 149: # 'ok' - We got to idle ok.
! 150: # 'error:msg' - An error occured. msg describes the error.
! 151: #
! 152: sub SequenceStateMachine {
! 153: my $connection = shift;
! 154: my $timeout = shift;
! 155:
! 156: my $Socket = $connection->GetSocket;
! 157: my $returnstatus = "ok"; # optimist!!!
! 158: my $error = 0; # Used to force early loop termination
! 159: # damned perl has no break!!.
! 160: my $state = $connection->GetState;
! 161:
! 162: while(($connection->GetState ne "Idle") && (!$error)) {
! 163: #
! 164: # Figure out what the connection wants. read/write and wait for it
! 165: # or for the timeout.
! 166: #
! 167: my $wantread = $connection->WantReadable;
! 168: my $poll = new IO::Poll;
! 169: $poll->mask($Socket, => $wantread ? POLLIN : POLLOUT);
! 170: $poll->poll($timeout);
! 171: my $done = $poll->handles();
! 172: if(scalar($done) == 0) { # no handles ready... timeout!!
! 173: $returnstatus = "error:";
! 174: $returnstatus .= "Timeout in state $state\n";
! 175: $error = 1;
! 176: } else {
! 177: my $status;
! 178: $status = $wantread ? $connection->Readable :
! 179: $connection->Writable;
! 180: if($status != 0) {
! 181: $returnstatus = "error:";
! 182: $returnstatus .= " I/O failed in state $state\n";
! 183: $error = 1;
! 184: }
! 185: }
! 186: $state = $connection->GetState;
! 187: }
! 188: return $returnstatus;
! 189: }
! 190:
! 191: #
1.23 foxr 192: # This function runs through the section of the connection
193: # state machine that has to do with negotiating the startup
194: # sequence with lond. The general strategy is to loop
195: # until the connection state becomes idle or disconnected.
196: # Disconnected indicates an error or rejection of the
197: # connection at some point in the negotiation.
198: # idle indicates a connection ready for a request.
199: # The main loop consults the object to determine if it
200: # wants to be writeable or readable, waits for that
201: # condition on the socket (with timeout) and then issues
202: # the appropriate LondConnection call. Note that
203: # LondConnection is capable of doing everything necessary
204: # to get to the initial idle state.
205: #
206: #
207: # Parameters:
208: # connection - A connection that has been created with
209: # the remote lond. This connection should
210: # be in the Connected state ready to send
211: # the init sequence.
212: #
1.21 foxr 213: sub NegotiateStartup {
214: my $connection = shift;
1.23 foxr 215: my $returnstatus = "ok"; # Optimistic!!.
216:
217: my $state = $connection->GetState;
218: if($state ne "Connected") {
219: print "Error: Initial lond connection state: $state should be Connected\n";
220: return "error";
221: }
222:
1.25 ! foxr 223: return SequenceStateMachine($connection, $TransitionTimeout);
1.21 foxr 224: }
1.24 foxr 225: #
226: # Perform a transaction with the remote lond.
227: # Paramters:
228: # connection - the connection object that represents
229: # a LondConnection to the remote lond.
230: # command - The request to send to the remote system.
231: # Returns:
232: # The 'reaction' of the lond to this command.
233: # However if the connection to lond is lost during the transaction
234: # or some other error occurs, the text "error:con_lost" is returned.
235: #
1.21 foxr 236: sub PerformTransaction {
237: my $connection = shift;
238: my $command = shift;
1.24 foxr 239: my $retval; # What we'll returnl.
1.25 ! foxr 240:
1.24 foxr 241:
242: # Set up the connection to do the transaction then
243: # do the I/O until idle or error.
244: #
245: $connection->InitiateTransaction($command);
246:
1.25 ! foxr 247: my $status = SequenceStateMachine($connection, $TransitionTimeout);
! 248: if($status eq "ok") {
1.24 foxr 249: $retval = $connection->GetReply;
1.25 ! foxr 250: } else {
! 251: $retval = $status;
1.24 foxr 252: }
1.21 foxr 253:
1.24 foxr 254: return $retval;
1.21 foxr 255: }
1.13 foxr 256: #
1.21 foxr 257: # Performs a transaction direct to a remote lond.
1.13 foxr 258: # Parameter:
259: # cmd - The text of the request.
260: # host - The host to which the request ultimately goes.
261: # Returns:
262: # The text of the reply from the lond or con_lost if not able to contact
263: # lond/lonc etc.
264: #
265: sub subreply {
1.21 foxr 266: my $cmd = shift;
267: my $host = shift;
268:
269:
270: my $connection = MakeLondConnection($host);
271: if ($connection eq undef) {
272: return "Connect Failed";
273: }
274: my $reply = NegotiateStartup($connection);
1.23 foxr 275: if($reply ne "ok") {
1.21 foxr 276: return "connection negotiation failed";
277: }
278: my $reply = PerformTransaction($connection, $cmd);
279: return $reply;
280:
281:
282: # my ($cmd,$server)=@_;
283: # my $peerfile="$perlvar{'lonSockDir'}/$server";
284: # my $client=IO::Socket::UNIX->new(Peer =>"$peerfile",
285: # Type => SOCK_STREAM,
286: # Timeout => 10)
287: # or return "con_lost";
288: # print $client "$cmd\n";
289: # my $answer=<$client>;
290: # if (!$answer) { $answer="con_lost"; }
291: # chomp($answer);
292: # return $answer;
1.13 foxr 293: }
294: # >>> BUGBUG <<<
1.2 foxr 295: #
1.3 foxr 296: # Use Getopt::Long to parse the parameters of the program.
297: #
298: # Return value is a list consisting of:
299: # A 'command' which is one of:
300: # push - table push requested.
301: # reinit - reinit requested.
302: # Additional parameters as follows:
303: # for push: Tablename, hostname
304: # for reinit: Appname hostname
305: #
306: # This function does not validation of the parameters of push and
307: # reinit.
1.4 foxr 308: #
309: # returns a list. The first element of the list is the operation name
310: # (e.g. reinit or push). The second element is the switch parameter.
311: # for push, this is the table name, for reinit, this is the process name.
312: # Additional elements of the list are the command argument. The count of
313: # command arguments is validated, but not their semantics.
314: #
1.3 foxr 315: # returns an empty list if the parse fails.
316: #
317:
1.18 foxr 318:
1.3 foxr 319: sub ParseArgs {
1.4 foxr 320: my $pushing = '';
1.7 foxr 321: my $reinitting = '';
1.5 foxr 322:
1.4 foxr 323: if(!GetOptions('push=s' => \$pushing,
1.18 foxr 324: 'reinit=s' => \$reinitting,
325: 'myname=s' => \$MyHost,
326: 'hosts=s' => \$ForeignHostTab)) {
327: return ();
328: }
329: # The --myname and --hosts switch must have values and
330: # most both appear if either appears:
331:
332: if(($MyHost ne "") && ($ForeignHostTab eq "")) {
333: return ();
334: }
335: if(($ForeignHostTab ne "") && ($MyHost eq "")) {
1.4 foxr 336: return ();
337: }
338:
339: # Require exactly one of --push and --reinit
340:
1.5 foxr 341: my $command = '';
1.4 foxr 342: my $commandarg = '';
1.5 foxr 343: my $paramcount = @ARGV; # Number of additional arguments.
344:
345:
1.4 foxr 346: if($pushing ne '') {
1.5 foxr 347:
1.16 foxr 348: # --push takes in addition a table, and an optional host:
1.5 foxr 349: #
1.16 foxr 350: if(($paramcount != 2) && ($paramcount != 1)) {
1.5 foxr 351: return (); # Invalid parameter count.
352: }
1.4 foxr 353: if($command ne '') {
354: return ();
355: } else {
1.5 foxr 356:
1.4 foxr 357: $command = 'push';
358: $commandarg = $pushing;
359: }
360: }
1.5 foxr 361:
1.4 foxr 362: if ($reinitting ne '') {
1.5 foxr 363:
1.16 foxr 364: # --reinit takes in addition just an optional host name
1.5 foxr 365:
1.16 foxr 366: if($paramcount > 1) {
1.5 foxr 367: return ();
368: }
1.4 foxr 369: if($command ne '') {
370: return ();
371: } else {
372: $command = 'reinit';
373: $commandarg = $reinitting;
374: }
375: }
376:
1.5 foxr 377: # Build the result list:
378:
379: my @result = ($command, $commandarg);
380: my $i;
381: for($i = 0; $i < $paramcount; $i++) {
382: push(@result, $ARGV[$i]);
383: }
384:
385: return @result;
1.3 foxr 386: }
1.10 foxr 387: #
1.19 foxr 388: # Read the loncapa configuration stuff. If ForeignHostTab is empty,
389: # assume we are part of a loncapa cluster and read the hosts.tab
390: # file from the config directory. Otherwise, ForeignHossTab
391: # is the name of an alternate configuration file to read in
392: # standalone mode.
1.11 foxr 393: #
394: sub ReadConfig {
1.19 foxr 395:
396: if($ForeignHostTab eq "") {
397: my $perlvarref = LondConnection::read_conf('loncapa.conf');
398: %perlvar = %{$perlvarref};
399: my $hoststab = LondConnection::read_hosts(
400: "$perlvar{'lonTabDir'}/hosts.tab");
401: %hostshash = %{$hoststab};
1.20 foxr 402: $MyHost = $perlvar{lonHostID}; # Set hostname from vars.
1.21 foxr 403: $ServerPort = $perlvar{londPort};
1.19 foxr 404: } else {
1.23 foxr 405:
406: LondConnection::ReadForeignConfig($MyHost, $ForeignHostTab);
407: my $hoststab = LondConnection::read_hosts($ForeignHostTab); # we need to know too.
408: %hostshash = %{$hoststab};
409: $ServerPort = $DefaultServerPort;
410: }
411:
1.11 foxr 412: }
413: #
1.10 foxr 414: # Determine if the target host is valid.
415: # This is done by reading the current hosts.tab file.
416: # For the host to be valid, it must be inthe file.
417: #
418: # Parameters:
419: # host - Name of host to check on.
420: # Returns:
421: # true if host is valid.
422: # false if host is invalid.
423: #
1.8 foxr 424: sub ValidHost {
1.10 foxr 425: my $host = shift;
1.11 foxr 426:
1.10 foxr 427:
428: return defined $hostshash{$host};
429:
1.8 foxr 430: }
1.13 foxr 431:
432:
433:
1.12 foxr 434: #
435: # Performs a transaction with lonc.
436: # By the time this is called, the transaction has already been
437: # validated by the caller.
438: #
439: # Parameters:
440: #
441: # host - hosts.tab name of the host whose lonc we'll be talking to.
442: # command - The base command we'll be asking lond to execute.
443: # body - [optional] If supplied, this is a command body that is a ref.
444: # to an array of lines that will be appended to the
445: # command.
446: #
447: # NOTE:
448: # The command will be done as an encrypted operation.
449: #
1.8 foxr 450: sub Transact {
1.12 foxr 451: my $host = shift;
452: my $command = shift;
453: my $haveBody= 0;
454: my $body;
455: my $i;
456:
457: if(scalar @ARG) {
458: $body = shift;
459: $haveBody = 1;
460: }
461: # Construct the command to send to the server:
462:
463: my $request = "encrypt\:"; # All requests are encrypted.
464: $request .= $command;
465: if($haveBody) {
466: $request .= "\:";
467: my $bodylines = scalar @$body;
468: for($i = 0; $i < $bodylines; $i++) {
469: $request .= $$body[$i];
470: }
471: } else {
472: $request .= "\n";
473: }
1.13 foxr 474: # Body is now built... transact with lond..
475:
476: my $answer = subreply($request, $host);
477:
478: print "$answer\n";
1.10 foxr 479:
1.8 foxr 480: }
1.7 foxr 481: #
482: # Called to push a file to the remote system.
483: # The only legal files to push are hosts.tab and domain.tab.
484: # Security is somewhat improved by
485: #
486: # - Requiring the user run as root.
487: # - Connecting with lonc rather than lond directly ensuring this is a loncapa
488: # host
489: # - We must appear in the remote host's hosts.tab file.
490: # - The host must appear in our hosts.tab file.
491: #
492: # Parameters:
493: # tablename - must be one of hosts or domain.
494: # tablefile - name of the file containing the table to push.
495: # host - name of the host to push this file to.
496: #
1.13 foxr 497: # >>>BUGBUG<<< This belongs in lonnet.pm.
498: #
1.7 foxr 499: sub PushFile {
500: my $tablename = shift;
501: my $tablefile = shift;
502: my $host = shift;
503:
1.8 foxr 504: # Open the table file:
505:
506: if(!open(TABLEFILE, "<$tablefile")) {
507: die "ENOENT - No such file or directory $tablefile";
508: }
509:
510: # Require that the host be valid:
511:
512: if(!ValidHost($host)) {
513: die "EHOSTINVAL - Invalid host $host"; # Ok so I invented this 'errno'.
514: }
515: # Read in the file. If the table name is valid, push it.
516:
517: my @table = <TABLEFILE>; # These files are pretty small.
518: close TABLEFILE;
519:
520: if( ($tablename eq "host") ||
521: ($tablename eq "domain")) {
1.16 foxr 522: print("Pushing $tablename to $host\n");
1.12 foxr 523: Transact($host, "pushfile:$tablename",\@table);
1.8 foxr 524: } else {
525: die "EINVAL - Invalid parameter. tablename: $tablename must be host or domain";
526: }
1.7 foxr 527: }
1.9 foxr 528: #
529: # This function is called to reinitialize a server in a remote host.
530: # The servers that can be reinitialized are:
531: # - lonc - The lonc client process.
532: # - lond - The lond daemon.
533: # NOTE:
534: # Reinitialization in this case means re-scanning the hosts table,
535: # starting new lond/lonc's as approprate and stopping existing lonc/lond's.
536: #
537: # Parameters:
538: # process - The name of the process to reinit (lonc or lond).
539: # host - The host in which this reinit will happen.
540: #
1.13 foxr 541: # >>>BUGBUG<<<< This belongs in lonnet.pm
542: #
1.9 foxr 543: sub ReinitProcess {
544: my $process = shift;
545: my $host = shift;
1.3 foxr 546:
1.9 foxr 547: # Ensure the host is valid:
548:
549: if(!ValidHost($host)) {
550: die "EHOSTINVAL - Invalid host $host";
551: }
552: # Ensure target process selector is valid:
553:
554: if(($process eq "lonc") ||
555: ($process eq "lond")) {
1.16 foxr 556: print("Reinitializing $process in $host\n");
1.9 foxr 557: Transact($host, "reinit:$process");
558: } else {
559: die "EINVAL -Invalid parameter. Process $process must be lonc or lond";
560: }
1.7 foxr 561: }
1.6 foxr 562: #--------------------------- Entry point: --------------------------
563:
1.16 foxr 564:
565:
1.6 foxr 566: # Parse the parameters
567: # If command parsing failed, then print usage:
1.2 foxr 568:
1.7 foxr 569: my @params = ParseArgs;
570: my $nparam = @params;
1.3 foxr 571:
572: if($nparam == 0) {
1.2 foxr 573: Usage;
1.4 foxr 574: exit -1;
1.2 foxr 575: }
1.7 foxr 576: #
577: # Next, ensure we are running as EID root.
578: #
579: if ($EUID != 0) {
580: die "ENOPRIV - No privilege for requested operation"
1.6 foxr 581: }
582:
1.19 foxr 583: #
584: # Read the configuration file.
585: #
586:
587: ReadConfig; # Read the configuration info (incl.hosts).
1.4 foxr 588:
1.6 foxr 589: # Based on the operation requested invoke the appropriate function:
590:
1.7 foxr 591: my $operation = shift @params;
1.6 foxr 592:
593: if($operation eq "push") { # push tablename filename host
1.7 foxr 594: my $tablename = shift @params;
595: my $tablefile = shift @params;
596: my $host = shift @params;
1.16 foxr 597: if($host) {
598: PushFile($tablename, $tablefile, $host);
599: } else { # Push to whole cluster.
600: foreach my $host (keys %hostshash) {
601: PushFile($tablename, $tablefile, $host);
602: }
603: }
1.6 foxr 604:
1.7 foxr 605: } elsif($operation eq "reinit") { # reinit processname host.
606: my $process = shift @params;
607: my $host = shift @params;
1.16 foxr 608: if ($host) {
609: ReinitProcess($process, $host);
610: } else { # Reinit whole cluster.
611: foreach my $host (keys %hostshash) {
612: ReinitProcess($process,$host);
613: }
614: }
615: }
1.7 foxr 616: else {
617: Usage;
1.6 foxr 618: }
1.4 foxr 619: exit 0;
1.2 foxr 620:
621: =head1 NAME
622: lonManage - Command line utility for remote management of lonCAPA
623: cluster nodes.
624:
625: =head1 SYNOPSIS
626:
627: Usage:
1.3 foxr 628: B<lonManage --push=<tablename> newfile host>
1.2 foxr 629: Push <tablename> to the lonTabs directory. Note that
630: <tablename> must be one of:
631: hosts (hosts.tab)
632: domain (domain.tab)
633:
1.3 foxr 634: B<lonManage --reinit=lonc host>
1.2 foxr 635: Sends a HUP signal to the remote systems's lond.
636:
1.3 foxr 637: B<lonmanage --reinit=lond host>
1.2 foxr 638: Requests the remote system's lond perform the same action as if
639: it had received a HUP signal.
640:
641: In the above syntax, the host above is the hosts.tab name of a host,
642: not the IP address of the host.
643:
644:
645: =head1 DESCRIPTION
646:
647: =head1 PREREQUISITES
1.3 foxr 648:
1.7 foxr 649: =item strict
1.3 foxr 650: =item Getopt::Long
1.7 foxr 651: =item English
1.13 foxr 652: =item IO::Socket::UNIX
653:
654: =head1 KEY Subroutines.
1.2 foxr 655:
656: =head1 CATEGORIES
657: Command line utility
658:
659: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>