File:  [LON-CAPA] / loncom / Attic / lonManage
Revision 1.21: download - view: text, annotated - select for diffs
Mon Nov 3 10:39:24 2003 UTC (20 years, 6 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
Block out top level logic for standalone operation.. NOTE:  lonManage
will not work until further notice (the logic is all stubs for now).

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

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