File:  [LON-CAPA] / loncom / Attic / lonManage
Revision 1.10: download - view: text, annotated - select for diffs
Mon Aug 18 10:43:31 2003 UTC (20 years, 8 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
Code/test ValidHost.  The hosts.tab and the perl variables are read in as
global hashes as a side effect.  May later want to clean this up by making
a separate getconfig function and hoisting the config reads into 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.10 2003/08/18 10:43:31 foxr Exp $
    7: #
    8: # $Id: lonManage,v 1.10 2003/08/18 10:43:31 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: #           hosts  (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: #  $Log: lonManage,v $
   53: #  Revision 1.10  2003/08/18 10:43:31  foxr
   54: #  Code/test ValidHost.  The hosts.tab and the perl variables are read in as
   55: #  global hashes as a side effect.  May later want to clean this up by making
   56: #  a separate getconfig function and hoisting the config reads into that.
   57: #
   58: #  Revision 1.9  2003/08/18 10:25:46  foxr
   59: #  Write ReinitProcess function in terms of ValidHost and Transact.
   60: #
   61: #  Revision 1.8  2003/08/18 10:18:21  foxr
   62: #  Completed PushFile function in terms of
   63: #  - ValidHost - Determines if target host is valid.
   64: #  - Transact  - Performs one of the valid transactions with the
   65: #                appropriate lonc<-->lond client/server pairs.
   66: #
   67: #  Revision 1.7  2003/08/18 09:56:01  foxr
   68: #  1. Require to be run as root.
   69: #  2. Catch case where no operation switch is supplied and put out usage.
   70: #  3. skeleton/comments for PushFile function.
   71: #
   72: #  Revision 1.6  2003/08/12 11:02:59  foxr
   73: #  Implement command switch dispatching.
   74: #
   75: #  Revision 1.5  2003/08/12 10:55:42  foxr
   76: #  Complete command line parsing (tested)
   77: #
   78: #  Revision 1.4  2003/08/12 10:40:44  foxr
   79: #  Get switch parsing right.
   80: #
   81: #  Revision 1.3  2003/08/12 10:22:35  foxr
   82: #  Put in parameter parsing infrastructure
   83: #
   84: #  Revision 1.2  2003/08/12 09:58:49  foxr
   85: #  Add usage and skeleton documentation.
   86: #
   87: #
   88: 
   89: # Modules required:
   90: 
   91: use strict;			# Because it's good practice.
   92: use English;			# Cause I like meaningful names.
   93: use Getopt::Long;
   94: use LONCAPA::Configuration;	# To handle configuration I/O.
   95: 
   96: # File scoped variables:
   97: 
   98: my %perlvar;			# Perl variable defs from apache config.
   99: my %hostshash;			# Host table as a host indexed hash.
  100: 
  101: sub Usage  {
  102:     print "Usage:";
  103:     print <<USAGE;
  104:     lonManage  --push=<tablename>  newfile  host
  105:         Push <tablename> to the lonTabs directory.  Note that
  106:         <tablename> must be one of:
  107:            hosts  (hosts.tab)
  108:            domain (domain.tab)
  109: 
  110:     lonManage  --reinit=lonc host
  111:            Sends a HUP signal to the remote systems's lond.
  112: 
  113:     lonManage  --reinit=lond host
  114:           Requests the remote system's lond perform the same action as if
  115:           it had received a HUP signal.
  116: 
  117:     In the above syntax, the host above is the hosts.tab name of a host,
  118:     not the IP address of the host.
  119: USAGE
  120: 
  121: 
  122: }
  123: 
  124: #
  125: #  Use Getopt::Long to parse the parameters of the program.
  126: #
  127: #  Return value is a list consisting of:
  128: #    A 'command' which is one of:
  129: #       push   - table push requested.
  130: #       reinit - reinit requested.
  131: #   Additional parameters as follows:
  132: #       for push: Tablename, hostname
  133: #       for reinit: Appname  hostname
  134: #
  135: #   This function does not validation of the parameters of push and
  136: #   reinit.
  137: #
  138: #   returns a list.  The first element of the list is the operation name
  139: #   (e.g. reinit or push).  The second element is the switch parameter.
  140: #   for push, this is the table name, for reinit, this is the process name.
  141: #   Additional elements of the list are the command argument.  The count of
  142: #   command arguments is validated, but not their semantics.
  143: #
  144: #   returns an empty list if the parse fails.
  145: #
  146: 
  147: sub ParseArgs {
  148:     my $pushing   = '';
  149:     my $reinitting = '';
  150: 
  151:     if(!GetOptions('push=s'    => \$pushing,
  152: 	           'reinit=s'  => \$reinitting)) {
  153: 	return ();
  154:     }
  155: 
  156:     #  Require exactly   one of --push and --reinit
  157: 
  158:     my $command    = '';
  159:     my $commandarg = '';
  160:     my $paramcount = @ARGV; 	# Number of additional arguments.
  161:     
  162: 
  163:     if($pushing ne '') {
  164: 
  165:         # --push takes in addition a table, and a host:
  166:         #
  167: 	if($paramcount != 2) {
  168: 	    return ();		# Invalid parameter count.
  169: 	}
  170: 	if($command ne '') {
  171: 	    return ();
  172: 	} else {
  173: 	    
  174: 	    $command    = 'push';
  175: 	    $commandarg = $pushing;
  176: 	}
  177:     }
  178: 
  179:     if ($reinitting ne '') {
  180: 
  181: 	# --reinit takes in addition just a host name
  182: 
  183: 	if($paramcount != 1) {
  184: 	    return ();
  185: 	}
  186: 	if($command ne '') {
  187: 	    return ();
  188: 	} else {
  189: 	    $command    = 'reinit';
  190: 	    $commandarg = $reinitting; 
  191: 	}
  192:     }
  193: 
  194:     #  Build the result list:
  195: 
  196:     my @result = ($command, $commandarg);
  197:     my $i;
  198:     for($i = 0; $i < $paramcount; $i++) {
  199: 	push(@result, $ARGV[$i]);
  200:     }
  201:     
  202:     return @result;
  203: }
  204: #
  205: #  Determine if the target host is valid.
  206: #  This is done by reading the current hosts.tab file.
  207: #  For the host to be valid, it must be inthe file.
  208: #
  209: #  Parameters:
  210: #     host   - Name of host to check on.
  211: #  Returns:
  212: #     true   if host is valid.
  213: #     false  if host is invalid.
  214: #
  215: sub ValidHost {
  216:     my $host       = shift;
  217: 
  218:     my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf');
  219:     %perlvar       = %{$perlvarref};
  220:     my $hoststab   = LONCAPA::Configuration::read_hosts(
  221: 					"$perlvar{'lonTabDir'}/hosts.tab");
  222:     %hostshash     = %{$hoststab};
  223: 
  224:     return defined $hostshash{$host};
  225: 
  226: }
  227: sub Transact {
  228: 
  229: }
  230: #
  231: #   Called to push a file to the remote system.
  232: #   The only legal files to push are hosts.tab and domain.tab.
  233: #   Security is somewhat improved by
  234: #   
  235: #   - Requiring the user run as root.
  236: #   - Connecting with lonc rather than lond directly ensuring this is a loncapa
  237: #     host
  238: #   - We must appear in the remote host's hosts.tab file.
  239: #   - The host must appear in our hosts.tab file.
  240: #
  241: #  Parameters:
  242: #     tablename - must be one of hosts or domain.
  243: #     tablefile - name of the file containing the table to push.
  244: #     host      - name of the host to push this file to.     
  245: #
  246: sub PushFile {
  247:     my $tablename = shift;
  248:     my $tablefile = shift;
  249:     my $host      = shift;
  250:     
  251:     # Open the table file:
  252: 
  253:     if(!open(TABLEFILE, "<$tablefile")) {
  254: 	die "ENOENT - No such file or directory $tablefile";
  255:     }
  256:   
  257:     # Require that the host be valid:
  258: 
  259:     if(!ValidHost($host)) {
  260: 	die "EHOSTINVAL - Invalid host $host"; # Ok so I invented this 'errno'.
  261:     }
  262:     # Read in the file.  If the table name is valid, push it.
  263: 
  264:     my @table = <TABLEFILE>;	#  These files are pretty small.
  265:     close TABLEFILE;
  266: 
  267:     if( ($tablename eq "host")    ||
  268: 	($tablename eq "domain")) {
  269: 	Transact($host, "pushfile:$tablename:",\@table);
  270:     } else {
  271: 	die "EINVAL - Invalid parameter. tablename: $tablename must be host or domain";
  272:     }
  273: }
  274: #
  275: #   This function is called to reinitialize a server in a remote host.
  276: #   The servers that can be reinitialized are:
  277: #   - lonc   - The lonc client process.
  278: #   - lond   - The lond daemon.
  279: #  NOTE:
  280: #    Reinitialization in this case means re-scanning the hosts table,
  281: #    starting new lond/lonc's as approprate and stopping existing lonc/lond's.
  282: #
  283: #  Parameters:
  284: #     process - The name of the process to reinit (lonc or lond).
  285: #     host    - The host in which this reinit will happen.
  286: #
  287: sub ReinitProcess {
  288:     my $process = shift;
  289:     my $host    = shift;
  290: 
  291:     #  Ensure the host is valid:
  292:     
  293:     if(!ValidHost($host)) {
  294: 	die "EHOSTINVAL - Invalid host $host";
  295:     }
  296:     # Ensure target process selector is valid:
  297: 
  298:     if(($process eq "lonc") ||
  299:        ($process eq "lond")) {
  300: 	Transact($host, "reinit:$process");
  301:     } else {
  302: 	die "EINVAL -Invalid parameter. Process $process must be lonc or lond";
  303:     }
  304: }
  305: #--------------------------- Entry point: --------------------------
  306: 
  307: #  Parse the parameters
  308: #  If command parsing failed, then print usage:
  309: 
  310: my @params   = ParseArgs;
  311: my $nparam   = @params;
  312: 
  313: if($nparam == 0) {
  314:     Usage;
  315:     exit -1;
  316: }
  317: #
  318: #   Next, ensure we are running as EID root.
  319: #
  320: if ($EUID != 0) {
  321:     die "ENOPRIV - No privilege for requested operation"
  322: }
  323: 
  324: 
  325: #   Based on the operation requested invoke the appropriate function:
  326: 
  327: my $operation = shift @params;
  328: 
  329: if($operation eq "push") {  # push tablename filename host
  330:     my $tablename = shift @params;
  331:     my $tablefile = shift @params;
  332:     my $host      = shift @params;
  333:     PushFile($tablename, $tablefile, $host);
  334: 
  335: } elsif($operation eq "reinit") {	# reinit processname host.
  336:     my $process   = shift @params;
  337:     my $host      = shift @params;
  338:     ReinitProcess($process, $host);
  339: }
  340: else {
  341:     Usage;
  342: }
  343: exit 0;
  344: 
  345: =head1 NAME
  346:     lonManage - Command line utility for remote management of lonCAPA
  347:     cluster nodes.
  348: 
  349: =head1 SYNOPSIS
  350: 
  351: Usage:
  352:     B<lonManage  --push=<tablename>  newfile  host>
  353:         Push <tablename> to the lonTabs directory.  Note that
  354:         <tablename> must be one of:
  355:            hosts  (hosts.tab)
  356:            domain (domain.tab)
  357: 
  358:     B<lonManage  --reinit=lonc host>
  359:            Sends a HUP signal to the remote systems's lond.
  360: 
  361:     B<lonmanage  --reinit=lond host>
  362:           Requests the remote system's lond perform the same action as if
  363:           it had received a HUP signal.
  364: 
  365:     In the above syntax, the host above is the hosts.tab name of a host,
  366:     not the IP address of the host.
  367: 
  368: 
  369: =head1 DESCRIPTION
  370: 
  371: =head1 PREREQUISITES
  372: 
  373: =item strict
  374: =item Getopt::Long
  375: =item English
  376: 
  377: =head1  CATEGORIES
  378:     Command line utility
  379: 
  380: =cut

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