File:  [LON-CAPA] / loncom / Attic / lonManage
Revision 1.11: download - view: text, annotated - select for diffs
Mon Aug 18 10:45:32 2003 UTC (20 years, 9 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
Felt strongly enough about hoisting ReadConfiguration into a separate sub
that I did it now before I forgot.

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

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