File:  [LON-CAPA] / loncom / Attic / lonManage
Revision 1.18: download - view: text, annotated - select for diffs
Tue Oct 28 11:55:58 2003 UTC (20 years, 6 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
Add switch processing and usage for standalone mode; To invoke standalone mode
>both< of the following switches will need to be present with values:

--myname=hostname - hostname is the name lonManage will use to negotiate the
   host key.  This must match the hostname given to this system in
   the manager table read by the associated lond.

--hosts=filename - filename will be read as the hosts.tab for this invocation
   of lonc.

This will allow the program to run standalone as well as to manage nodes in a
cluster other than the one it is a member of (assuming the remote management
table allows that).

    1: #!/usr/bin/perl
    2: # The LearningOnline Network with CAPA
    3: #
    4: #  lonManage supports remote management of nodes in a LonCAPA cluster.
    5: #
    6: #  $Id: lonManage,v 1.18 2003/10/28 11:55:58 foxr Exp $
    7: #
    8: # $Id: lonManage,v 1.18 2003/10/28 11:55:58 foxr Exp $
    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: #
   36: #    lonManage  -push   <tablename>  newfile  [host]
   37: #        Push <tablename> to the lonTabs directory.  Note that
   38: #        <tablename> must be one of:
   39: #           host  (hosts.tab)
   40: #           domain (domain.tab)
   41: #
   42: #    lonManage  -reinit lonc [host]
   43: #           Sends a HUP signal to the remote systems's lond.
   44: #
   45: #    lonmanage  -reinit lond [host]
   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,
   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..
   54: #
   55: #
   56: 
   57: 
   58: 
   59: # Modules required:
   60: 
   61: use lib ".";
   62: 
   63: use strict;			# Because it's good practice.
   64: use English;			# Cause I like meaningful names.
   65: use Getopt::Long;
   66: use IO::Socket::UNIX;		# To communicate with lonc.
   67: use LondConnection;
   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.
   73: 
   74: my $MyHost;			# Host name to use as me.
   75: my $ForeignHostTab;		# Name of foreign hosts table.
   76: 
   77: #
   78: #   prints out utility's command usage info.
   79: #
   80: sub Usage  {
   81:     print "Usage:";
   82:     print <<USAGE;
   83:  lonManage  [--myname=host --hosts=table] --push=<tablename>  newfile  [host]
   84:         Push <tablename> to the lonTabs directory.  Note that
   85:         <tablename> must be one of:
   86:            host  (hosts.tab)
   87:            domain (domain.tab)
   88: 
   89:  lonManage [--myname=host --hosts=table] --reinit=lonc [host]
   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:        
   94: 
   95:  lonManage [--myname=host --hosts=table] --reinit=lond [host]
   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.
   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.
  101: 
  102:     If [host] is omitted, all hosts in the hosts.tab file are iterated
  103:     over.
  104: 
  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.
  109: USAGE
  110: 
  111: 
  112: }
  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 <<< 
  137: #
  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.
  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: #
  157: #   returns an empty list if the parse fails.
  158: #
  159: 
  160: 
  161: sub ParseArgs {
  162:     my $pushing   = '';
  163:     my $reinitting = '';
  164: 
  165:     if(!GetOptions('push=s'    => \$pushing,
  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 "")) {
  178: 	return ();
  179:     }
  180: 
  181:     #  Require exactly   one of --push and --reinit
  182: 
  183:     my $command    = '';
  184:     my $commandarg = '';
  185:     my $paramcount = @ARGV; 	# Number of additional arguments.
  186:     
  187: 
  188:     if($pushing ne '') {
  189: 
  190:         # --push takes in addition a table, and an optional  host:
  191:         #
  192: 	if(($paramcount != 2) && ($paramcount != 1)) {
  193: 	    return ();		# Invalid parameter count.
  194: 	}
  195: 	if($command ne '') {
  196: 	    return ();
  197: 	} else {
  198: 	    
  199: 	    $command    = 'push';
  200: 	    $commandarg = $pushing;
  201: 	}
  202:     }
  203: 
  204:     if ($reinitting ne '') {
  205: 
  206: 	# --reinit takes in addition just an optional  host name
  207: 
  208: 	if($paramcount > 1) {
  209: 	    return ();
  210: 	}
  211: 	if($command ne '') {
  212: 	    return ();
  213: 	} else {
  214: 	    $command    = 'reinit';
  215: 	    $commandarg = $reinitting; 
  216: 	}
  217:     }
  218: 
  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;
  228: }
  229: #
  230: #  Read the loncapa configuration stuff.
  231: #
  232: sub ReadConfig {
  233:     my $perlvarref = LondConnection::read_conf('loncapa.conf');
  234:     %perlvar       = %{$perlvarref};
  235:     my $hoststab   = LondConnection::read_hosts(
  236: 					"$perlvar{'lonTabDir'}/hosts.tab");
  237:     %hostshash     = %{$hoststab};
  238: 
  239: }
  240: #
  241: #  Determine if the target host is valid.
  242: #  This is done by reading the current hosts.tab file.
  243: #  For the host to be valid, it must be inthe file.
  244: #
  245: #  Parameters:
  246: #     host   - Name of host to check on.
  247: #  Returns:
  248: #     true   if host is valid.
  249: #     false  if host is invalid.
  250: #
  251: sub ValidHost {
  252:     my $host       = shift;
  253:    
  254: 
  255:     return defined $hostshash{$host};
  256: 
  257: }
  258: 
  259: 
  260: 
  261: #
  262: #  Performs a transaction with lonc.
  263: #  By the time this is called, the transaction has already been
  264: #  validated by the caller.
  265: #
  266: #   Parameters:
  267: #
  268: #   host    - hosts.tab name of the host whose lonc we'll be talking to.
  269: #   command - The base command we'll be asking lond to execute.
  270: #   body    - [optional] If supplied, this is a command body that is a ref.
  271: #             to an array of lines that will be appended to the 
  272: #             command.
  273: #
  274: #  NOTE:
  275: #    The command will be done as an encrypted operation.
  276: #
  277: sub Transact {
  278:     my $host    = shift;
  279:     my $command = shift;
  280:     my $haveBody= 0;
  281:     my $body;
  282:     my $i;
  283: 
  284:     if(scalar @ARG) {
  285: 	$body = shift;
  286: 	$haveBody = 1;
  287:     }
  288:     #  Construct the command to send to the server:
  289:     
  290:     my $request = "encrypt\:";	# All requests are encrypted.
  291:     $request   .= $command;
  292:     if($haveBody) {
  293: 	$request .= "\:";
  294: 	my $bodylines = scalar @$body;
  295: 	for($i = 0; $i < $bodylines; $i++) {
  296: 	    $request .= $$body[$i];
  297: 	}
  298:     } else {
  299: 	$request .= "\n";
  300:     }
  301:     # Body is now built... transact with lond..
  302:     
  303:     my $answer = subreply($request, $host);
  304: 
  305:     print "$answer\n";
  306: 
  307: }
  308: #
  309: #   Called to push a file to the remote system.
  310: #   The only legal files to push are hosts.tab and domain.tab.
  311: #   Security is somewhat improved by
  312: #   
  313: #   - Requiring the user run as root.
  314: #   - Connecting with lonc rather than lond directly ensuring this is a loncapa
  315: #     host
  316: #   - We must appear in the remote host's hosts.tab file.
  317: #   - The host must appear in our hosts.tab file.
  318: #
  319: #  Parameters:
  320: #     tablename - must be one of hosts or domain.
  321: #     tablefile - name of the file containing the table to push.
  322: #     host      - name of the host to push this file to.     
  323: #
  324: #    >>>BUGBUG<<< This belongs in lonnet.pm.
  325: #
  326: sub PushFile {
  327:     my $tablename = shift;
  328:     my $tablefile = shift;
  329:     my $host      = shift;
  330:     
  331:     # Open the table file:
  332: 
  333:     if(!open(TABLEFILE, "<$tablefile")) {
  334: 	die "ENOENT - No such file or directory $tablefile";
  335:     }
  336:   
  337:     # Require that the host be valid:
  338: 
  339:     if(!ValidHost($host)) {
  340: 	die "EHOSTINVAL - Invalid host $host"; # Ok so I invented this 'errno'.
  341:     }
  342:     # Read in the file.  If the table name is valid, push it.
  343: 
  344:     my @table = <TABLEFILE>;	#  These files are pretty small.
  345:     close TABLEFILE;
  346: 
  347:     if( ($tablename eq "host")    ||
  348: 	($tablename eq "domain")) {
  349: 	print("Pushing $tablename to $host\n");
  350: 	Transact($host, "pushfile:$tablename",\@table);
  351:     } else {
  352: 	die "EINVAL - Invalid parameter. tablename: $tablename must be host or domain";
  353:     }
  354: }
  355: #
  356: #   This function is called to reinitialize a server in a remote host.
  357: #   The servers that can be reinitialized are:
  358: #   - lonc   - The lonc client process.
  359: #   - lond   - The lond daemon.
  360: #  NOTE:
  361: #    Reinitialization in this case means re-scanning the hosts table,
  362: #    starting new lond/lonc's as approprate and stopping existing lonc/lond's.
  363: #
  364: #  Parameters:
  365: #     process - The name of the process to reinit (lonc or lond).
  366: #     host    - The host in which this reinit will happen.
  367: #
  368: #   >>>BUGBUG<<<< This belongs  in lonnet.pm
  369: #
  370: sub ReinitProcess {
  371:     my $process = shift;
  372:     my $host    = shift;
  373: 
  374:     #  Ensure the host is valid:
  375:     
  376:     if(!ValidHost($host)) {
  377: 	die "EHOSTINVAL - Invalid host $host";
  378:     }
  379:     # Ensure target process selector is valid:
  380: 
  381:     if(($process eq "lonc") ||
  382:        ($process eq "lond")) {
  383: 	print("Reinitializing $process in $host\n");
  384: 	Transact($host, "reinit:$process");
  385:     } else {
  386: 	die "EINVAL -Invalid parameter. Process $process must be lonc or lond";
  387:     }
  388: }
  389: #--------------------------- Entry point: --------------------------
  390: 
  391: ReadConfig;			# Read the configuration info (incl.hosts).
  392: 
  393: 
  394: #  Parse the parameters
  395: #  If command parsing failed, then print usage:
  396: 
  397: my @params   = ParseArgs;
  398: my $nparam   = @params;
  399: 
  400: if($nparam == 0) {
  401:     Usage;
  402:     exit -1;
  403: }
  404: #
  405: #   Next, ensure we are running as EID root.
  406: #
  407: if ($EUID != 0) {
  408:     die "ENOPRIV - No privilege for requested operation"
  409: }
  410: 
  411: 
  412: #   Based on the operation requested invoke the appropriate function:
  413: 
  414: my $operation = shift @params;
  415: 
  416: if($operation eq "push") {  # push tablename filename host
  417:     my $tablename = shift @params;
  418:     my $tablefile = shift @params;
  419:     my $host      = shift @params;
  420:     if($host) {
  421: 	PushFile($tablename, $tablefile, $host);
  422:     } else {			# Push to whole cluster.
  423: 	foreach my $host (keys %hostshash) {
  424: 	    PushFile($tablename, $tablefile, $host);
  425: 	}
  426:     }
  427: 
  428: } elsif($operation eq "reinit") {	# reinit processname host.
  429:     my $process   = shift @params;
  430:     my $host      = shift @params;
  431:     if ($host) {
  432: 	ReinitProcess($process, $host);
  433:     } else {			# Reinit whole cluster.
  434: 	foreach my $host (keys %hostshash) {
  435: 	    ReinitProcess($process,$host);
  436: 	}
  437:     }
  438: } 
  439: else {
  440:     Usage;
  441: }
  442: exit 0;
  443: 
  444: =head1 NAME
  445:     lonManage - Command line utility for remote management of lonCAPA
  446:     cluster nodes.
  447: 
  448: =head1 SYNOPSIS
  449: 
  450: Usage:
  451:     B<lonManage  --push=<tablename>  newfile  host>
  452:         Push <tablename> to the lonTabs directory.  Note that
  453:         <tablename> must be one of:
  454:            hosts  (hosts.tab)
  455:            domain (domain.tab)
  456: 
  457:     B<lonManage  --reinit=lonc host>
  458:            Sends a HUP signal to the remote systems's lond.
  459: 
  460:     B<lonmanage  --reinit=lond host>
  461:           Requests the remote system's lond perform the same action as if
  462:           it had received a HUP signal.
  463: 
  464:     In the above syntax, the host above is the hosts.tab name of a host,
  465:     not the IP address of the host.
  466: 
  467: 
  468: =head1 DESCRIPTION
  469: 
  470: =head1 PREREQUISITES
  471: 
  472: =item strict
  473: =item Getopt::Long
  474: =item English
  475: =item IO::Socket::UNIX
  476: 
  477: =head1 KEY Subroutines.
  478: 
  479: =head1  CATEGORIES
  480:     Command line utility
  481: 
  482: =cut

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