Annotation of loncom/lonManage, revision 1.20

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.20    ! foxr        6: #  $Id: lonManage,v 1.19 2003/11/03 10:18:13 foxr Exp $
1.1       foxr        7: #
1.20    ! foxr        8: # $Id: lonManage,v 1.19 2003/11/03 10:18:13 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.13      foxr       66: use IO::Socket::UNIX;		# To communicate with lonc.
1.17      foxr       67: use LondConnection;
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.18      foxr       76: 
1.13      foxr       77: #
                     78: #   prints out utility's command usage info.
                     79: #
1.3       foxr       80: sub Usage  {
1.2       foxr       81:     print "Usage:";
                     82:     print <<USAGE;
1.18      foxr       83:  lonManage  [--myname=host --hosts=table] --push=<tablename>  newfile  [host]
1.2       foxr       84:         Push <tablename> to the lonTabs directory.  Note that
                     85:         <tablename> must be one of:
1.15      foxr       86:            host  (hosts.tab)
1.2       foxr       87:            domain (domain.tab)
                     88: 
1.18      foxr       89:  lonManage [--myname=host --hosts=table] --reinit=lonc [host]
1.15      foxr       90:        Causes lonc in the remote system to reread hosts.tab and
                     91:        adjust the set of clients that are being maintained to match
                     92:        the new file.
                     93:        
1.2       foxr       94: 
1.18      foxr       95:  lonManage [--myname=host --hosts=table] --reinit=lond [host]
1.15      foxr       96:        Causes lond in the remote system to reread the hosts.tab file
                     97:        and adjust the set of servers to match changes in that file.
1.2       foxr       98: 
                     99:     In the above syntax, the host above is the hosts.tab name of a host,
                    100:     not the IP address of the host.
1.16      foxr      101: 
                    102:     If [host] is omitted, all hosts in the hosts.tab file are iterated
                    103:     over.
                    104: 
1.18      foxr      105:  For all of the above syntaxes if --myname=host and --hosts=table are
                    106:  supplied (both must be present), the utility runs in standalone mode
                    107:  presenting itself to the world as 'host' and using the hosts.tab file
                    108:  specified in the --hosts switch.
1.2       foxr      109: USAGE
                    110: 
                    111: 
                    112: }
1.13      foxr      113: #
                    114: #   Lifted from lonnet.pm - and we need to figure out a way to get it back in.
                    115: #   Performas a transaction with lond via the lonc proxy server.
                    116: #   Parameter:
                    117: #      cmd  - The text of the request.
                    118: #      host - The host to which the request ultimately goes.
                    119: #   Returns:
                    120: #      The text of the reply from the lond or con_lost if not able to contact
                    121: #      lond/lonc etc.
                    122: #
                    123: sub subreply {
                    124:     my ($cmd,$server)=@_;
                    125:     my $peerfile="$perlvar{'lonSockDir'}/$server";
                    126:     my $client=IO::Socket::UNIX->new(Peer    =>"$peerfile",
                    127:                                      Type    => SOCK_STREAM,
                    128:                                      Timeout => 10)
                    129:        or return "con_lost";
                    130:     print $client "$cmd\n";
                    131:     my $answer=<$client>;
                    132:     if (!$answer) { $answer="con_lost"; }
                    133:     chomp($answer);
                    134:     return $answer;
                    135: }
                    136: #   >>> BUGBUG <<< 
1.2       foxr      137: #
1.3       foxr      138: #  Use Getopt::Long to parse the parameters of the program.
                    139: #
                    140: #  Return value is a list consisting of:
                    141: #    A 'command' which is one of:
                    142: #       push   - table push requested.
                    143: #       reinit - reinit requested.
                    144: #   Additional parameters as follows:
                    145: #       for push: Tablename, hostname
                    146: #       for reinit: Appname  hostname
                    147: #
                    148: #   This function does not validation of the parameters of push and
                    149: #   reinit.
1.4       foxr      150: #
                    151: #   returns a list.  The first element of the list is the operation name
                    152: #   (e.g. reinit or push).  The second element is the switch parameter.
                    153: #   for push, this is the table name, for reinit, this is the process name.
                    154: #   Additional elements of the list are the command argument.  The count of
                    155: #   command arguments is validated, but not their semantics.
                    156: #
1.3       foxr      157: #   returns an empty list if the parse fails.
                    158: #
                    159: 
1.18      foxr      160: 
1.3       foxr      161: sub ParseArgs {
1.4       foxr      162:     my $pushing   = '';
1.7       foxr      163:     my $reinitting = '';
1.5       foxr      164: 
1.4       foxr      165:     if(!GetOptions('push=s'    => \$pushing,
1.18      foxr      166: 	           'reinit=s'  => \$reinitting,
                    167: 		   'myname=s' => \$MyHost,
                    168: 		   'hosts=s' => \$ForeignHostTab)) {
                    169: 	return ();
                    170:     }
                    171:     #  The --myname and --hosts switch must have values and
                    172:     #  most both appear if either appears:
                    173: 
                    174:     if(($MyHost ne "") && ($ForeignHostTab eq "")) {
                    175: 	return ();
                    176:     }
                    177:     if(($ForeignHostTab ne "") && ($MyHost eq "")) {
1.4       foxr      178: 	return ();
                    179:     }
                    180: 
                    181:     #  Require exactly   one of --push and --reinit
                    182: 
1.5       foxr      183:     my $command    = '';
1.4       foxr      184:     my $commandarg = '';
1.5       foxr      185:     my $paramcount = @ARGV; 	# Number of additional arguments.
                    186:     
                    187: 
1.4       foxr      188:     if($pushing ne '') {
1.5       foxr      189: 
1.16      foxr      190:         # --push takes in addition a table, and an optional  host:
1.5       foxr      191:         #
1.16      foxr      192: 	if(($paramcount != 2) && ($paramcount != 1)) {
1.5       foxr      193: 	    return ();		# Invalid parameter count.
                    194: 	}
1.4       foxr      195: 	if($command ne '') {
                    196: 	    return ();
                    197: 	} else {
1.5       foxr      198: 	    
1.4       foxr      199: 	    $command    = 'push';
                    200: 	    $commandarg = $pushing;
                    201: 	}
                    202:     }
1.5       foxr      203: 
1.4       foxr      204:     if ($reinitting ne '') {
1.5       foxr      205: 
1.16      foxr      206: 	# --reinit takes in addition just an optional  host name
1.5       foxr      207: 
1.16      foxr      208: 	if($paramcount > 1) {
1.5       foxr      209: 	    return ();
                    210: 	}
1.4       foxr      211: 	if($command ne '') {
                    212: 	    return ();
                    213: 	} else {
                    214: 	    $command    = 'reinit';
                    215: 	    $commandarg = $reinitting; 
                    216: 	}
                    217:     }
                    218: 
1.5       foxr      219:     #  Build the result list:
                    220: 
                    221:     my @result = ($command, $commandarg);
                    222:     my $i;
                    223:     for($i = 0; $i < $paramcount; $i++) {
                    224: 	push(@result, $ARGV[$i]);
                    225:     }
                    226:     
                    227:     return @result;
1.3       foxr      228: }
1.10      foxr      229: #
1.19      foxr      230: #  Read the loncapa configuration stuff.  If ForeignHostTab is empty,
                    231: #  assume we are part of a loncapa cluster and read the hosts.tab
                    232: #  file from the config directory.  Otherwise, ForeignHossTab
                    233: #  is the name of an alternate configuration file to read in 
                    234: #  standalone mode.
1.11      foxr      235: #
                    236: sub ReadConfig {
1.19      foxr      237: 
                    238:     if($ForeignHostTab eq "") {
                    239: 	my $perlvarref = LondConnection::read_conf('loncapa.conf');
                    240: 	%perlvar       = %{$perlvarref};
                    241: 	my $hoststab   = LondConnection::read_hosts(
                    242: 					    "$perlvar{'lonTabDir'}/hosts.tab");
                    243: 	%hostshash     = %{$hoststab};
1.20    ! foxr      244: 	$MyHost        = $perlvar{lonHostID}; # Set hostname from vars.
1.19      foxr      245:     } else {
                    246: 	my $hoststab   = LondConnection::read_hosts($ForeignHostTab);
                    247: 	%hostshash     = %{$hoststab};
                    248: 	$perlvar{londPort}   = 5663;
                    249: 	$perlvar{lonSockDir} = "/home/httpd/sockets";
                    250:     }
1.11      foxr      251: 
                    252: }
                    253: #
1.10      foxr      254: #  Determine if the target host is valid.
                    255: #  This is done by reading the current hosts.tab file.
                    256: #  For the host to be valid, it must be inthe file.
                    257: #
                    258: #  Parameters:
                    259: #     host   - Name of host to check on.
                    260: #  Returns:
                    261: #     true   if host is valid.
                    262: #     false  if host is invalid.
                    263: #
1.8       foxr      264: sub ValidHost {
1.10      foxr      265:     my $host       = shift;
1.11      foxr      266:    
1.10      foxr      267: 
                    268:     return defined $hostshash{$host};
                    269: 
1.8       foxr      270: }
1.13      foxr      271: 
                    272: 
                    273: 
1.12      foxr      274: #
                    275: #  Performs a transaction with lonc.
                    276: #  By the time this is called, the transaction has already been
                    277: #  validated by the caller.
                    278: #
                    279: #   Parameters:
                    280: #
                    281: #   host    - hosts.tab name of the host whose lonc we'll be talking to.
                    282: #   command - The base command we'll be asking lond to execute.
                    283: #   body    - [optional] If supplied, this is a command body that is a ref.
                    284: #             to an array of lines that will be appended to the 
                    285: #             command.
                    286: #
                    287: #  NOTE:
                    288: #    The command will be done as an encrypted operation.
                    289: #
1.8       foxr      290: sub Transact {
1.12      foxr      291:     my $host    = shift;
                    292:     my $command = shift;
                    293:     my $haveBody= 0;
                    294:     my $body;
                    295:     my $i;
                    296: 
                    297:     if(scalar @ARG) {
                    298: 	$body = shift;
                    299: 	$haveBody = 1;
                    300:     }
                    301:     #  Construct the command to send to the server:
                    302:     
                    303:     my $request = "encrypt\:";	# All requests are encrypted.
                    304:     $request   .= $command;
                    305:     if($haveBody) {
                    306: 	$request .= "\:";
                    307: 	my $bodylines = scalar @$body;
                    308: 	for($i = 0; $i < $bodylines; $i++) {
                    309: 	    $request .= $$body[$i];
                    310: 	}
                    311:     } else {
                    312: 	$request .= "\n";
                    313:     }
1.13      foxr      314:     # Body is now built... transact with lond..
                    315:     
                    316:     my $answer = subreply($request, $host);
                    317: 
                    318:     print "$answer\n";
1.10      foxr      319: 
1.8       foxr      320: }
1.7       foxr      321: #
                    322: #   Called to push a file to the remote system.
                    323: #   The only legal files to push are hosts.tab and domain.tab.
                    324: #   Security is somewhat improved by
                    325: #   
                    326: #   - Requiring the user run as root.
                    327: #   - Connecting with lonc rather than lond directly ensuring this is a loncapa
                    328: #     host
                    329: #   - We must appear in the remote host's hosts.tab file.
                    330: #   - The host must appear in our hosts.tab file.
                    331: #
                    332: #  Parameters:
                    333: #     tablename - must be one of hosts or domain.
                    334: #     tablefile - name of the file containing the table to push.
                    335: #     host      - name of the host to push this file to.     
                    336: #
1.13      foxr      337: #    >>>BUGBUG<<< This belongs in lonnet.pm.
                    338: #
1.7       foxr      339: sub PushFile {
                    340:     my $tablename = shift;
                    341:     my $tablefile = shift;
                    342:     my $host      = shift;
                    343:     
1.8       foxr      344:     # Open the table file:
                    345: 
                    346:     if(!open(TABLEFILE, "<$tablefile")) {
                    347: 	die "ENOENT - No such file or directory $tablefile";
                    348:     }
                    349:   
                    350:     # Require that the host be valid:
                    351: 
                    352:     if(!ValidHost($host)) {
                    353: 	die "EHOSTINVAL - Invalid host $host"; # Ok so I invented this 'errno'.
                    354:     }
                    355:     # Read in the file.  If the table name is valid, push it.
                    356: 
                    357:     my @table = <TABLEFILE>;	#  These files are pretty small.
                    358:     close TABLEFILE;
                    359: 
                    360:     if( ($tablename eq "host")    ||
                    361: 	($tablename eq "domain")) {
1.16      foxr      362: 	print("Pushing $tablename to $host\n");
1.12      foxr      363: 	Transact($host, "pushfile:$tablename",\@table);
1.8       foxr      364:     } else {
                    365: 	die "EINVAL - Invalid parameter. tablename: $tablename must be host or domain";
                    366:     }
1.7       foxr      367: }
1.9       foxr      368: #
                    369: #   This function is called to reinitialize a server in a remote host.
                    370: #   The servers that can be reinitialized are:
                    371: #   - lonc   - The lonc client process.
                    372: #   - lond   - The lond daemon.
                    373: #  NOTE:
                    374: #    Reinitialization in this case means re-scanning the hosts table,
                    375: #    starting new lond/lonc's as approprate and stopping existing lonc/lond's.
                    376: #
                    377: #  Parameters:
                    378: #     process - The name of the process to reinit (lonc or lond).
                    379: #     host    - The host in which this reinit will happen.
                    380: #
1.13      foxr      381: #   >>>BUGBUG<<<< This belongs  in lonnet.pm
                    382: #
1.9       foxr      383: sub ReinitProcess {
                    384:     my $process = shift;
                    385:     my $host    = shift;
1.3       foxr      386: 
1.9       foxr      387:     #  Ensure the host is valid:
                    388:     
                    389:     if(!ValidHost($host)) {
                    390: 	die "EHOSTINVAL - Invalid host $host";
                    391:     }
                    392:     # Ensure target process selector is valid:
                    393: 
                    394:     if(($process eq "lonc") ||
                    395:        ($process eq "lond")) {
1.16      foxr      396: 	print("Reinitializing $process in $host\n");
1.9       foxr      397: 	Transact($host, "reinit:$process");
                    398:     } else {
                    399: 	die "EINVAL -Invalid parameter. Process $process must be lonc or lond";
                    400:     }
1.7       foxr      401: }
1.6       foxr      402: #--------------------------- Entry point: --------------------------
                    403: 
1.16      foxr      404: 
                    405: 
1.6       foxr      406: #  Parse the parameters
                    407: #  If command parsing failed, then print usage:
1.2       foxr      408: 
1.7       foxr      409: my @params   = ParseArgs;
                    410: my $nparam   = @params;
1.3       foxr      411: 
                    412: if($nparam == 0) {
1.2       foxr      413:     Usage;
1.4       foxr      414:     exit -1;
1.2       foxr      415: }
1.7       foxr      416: #
                    417: #   Next, ensure we are running as EID root.
                    418: #
                    419: if ($EUID != 0) {
                    420:     die "ENOPRIV - No privilege for requested operation"
1.6       foxr      421: }
                    422: 
1.19      foxr      423: #
                    424: #   Read the configuration file.
                    425: #   
                    426: 
                    427: ReadConfig;			# Read the configuration info (incl.hosts).
1.4       foxr      428: 
1.6       foxr      429: #   Based on the operation requested invoke the appropriate function:
                    430: 
1.7       foxr      431: my $operation = shift @params;
1.6       foxr      432: 
                    433: if($operation eq "push") {  # push tablename filename host
1.7       foxr      434:     my $tablename = shift @params;
                    435:     my $tablefile = shift @params;
                    436:     my $host      = shift @params;
1.16      foxr      437:     if($host) {
                    438: 	PushFile($tablename, $tablefile, $host);
                    439:     } else {			# Push to whole cluster.
                    440: 	foreach my $host (keys %hostshash) {
                    441: 	    PushFile($tablename, $tablefile, $host);
                    442: 	}
                    443:     }
1.6       foxr      444: 
1.7       foxr      445: } elsif($operation eq "reinit") {	# reinit processname host.
                    446:     my $process   = shift @params;
                    447:     my $host      = shift @params;
1.16      foxr      448:     if ($host) {
                    449: 	ReinitProcess($process, $host);
                    450:     } else {			# Reinit whole cluster.
                    451: 	foreach my $host (keys %hostshash) {
                    452: 	    ReinitProcess($process,$host);
                    453: 	}
                    454:     }
                    455: } 
1.7       foxr      456: else {
                    457:     Usage;
1.6       foxr      458: }
1.4       foxr      459: exit 0;
1.2       foxr      460: 
                    461: =head1 NAME
                    462:     lonManage - Command line utility for remote management of lonCAPA
                    463:     cluster nodes.
                    464: 
                    465: =head1 SYNOPSIS
                    466: 
                    467: Usage:
1.3       foxr      468:     B<lonManage  --push=<tablename>  newfile  host>
1.2       foxr      469:         Push <tablename> to the lonTabs directory.  Note that
                    470:         <tablename> must be one of:
                    471:            hosts  (hosts.tab)
                    472:            domain (domain.tab)
                    473: 
1.3       foxr      474:     B<lonManage  --reinit=lonc host>
1.2       foxr      475:            Sends a HUP signal to the remote systems's lond.
                    476: 
1.3       foxr      477:     B<lonmanage  --reinit=lond host>
1.2       foxr      478:           Requests the remote system's lond perform the same action as if
                    479:           it had received a HUP signal.
                    480: 
                    481:     In the above syntax, the host above is the hosts.tab name of a host,
                    482:     not the IP address of the host.
                    483: 
                    484: 
                    485: =head1 DESCRIPTION
                    486: 
                    487: =head1 PREREQUISITES
1.3       foxr      488: 
1.7       foxr      489: =item strict
1.3       foxr      490: =item Getopt::Long
1.7       foxr      491: =item English
1.13      foxr      492: =item IO::Socket::UNIX
                    493: 
                    494: =head1 KEY Subroutines.
1.2       foxr      495: 
                    496: =head1  CATEGORIES
                    497:     Command line utility
                    498: 
                    499: =cut

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