File:  [LON-CAPA] / loncom / Attic / lonManage
Revision 1.8: download - view: text, annotated - select for diffs
Mon Aug 18 10:18:21 2003 UTC (20 years, 9 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
Completed PushFile function in terms of
- ValidHost - Determines if target host is valid.
- Transact  - Performs one of the valid transactions with the
              appropriate lonc<-->lond client/server pairs.

    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.8 2003/08/18 10:18:21 foxr Exp $
    7: #
    8: # $Id: lonManage,v 1.8 2003/08/18 10:18:21 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.8  2003/08/18 10:18:21  foxr
   54: #  Completed PushFile function in terms of
   55: #  - ValidHost - Determines if target host is valid.
   56: #  - Transact  - Performs one of the valid transactions with the
   57: #                appropriate lonc<-->lond client/server pairs.
   58: #
   59: #  Revision 1.7  2003/08/18 09:56:01  foxr
   60: #  1. Require to be run as root.
   61: #  2. Catch case where no operation switch is supplied and put out usage.
   62: #  3. skeleton/comments for PushFile function.
   63: #
   64: #  Revision 1.6  2003/08/12 11:02:59  foxr
   65: #  Implement command switch dispatching.
   66: #
   67: #  Revision 1.5  2003/08/12 10:55:42  foxr
   68: #  Complete command line parsing (tested)
   69: #
   70: #  Revision 1.4  2003/08/12 10:40:44  foxr
   71: #  Get switch parsing right.
   72: #
   73: #  Revision 1.3  2003/08/12 10:22:35  foxr
   74: #  Put in parameter parsing infrastructure
   75: #
   76: #  Revision 1.2  2003/08/12 09:58:49  foxr
   77: #  Add usage and skeleton documentation.
   78: #
   79: #
   80: use strict;			# Because it's good practice.
   81: use English;			# Cause I like meaningful names.
   82: use Getopt::Long;
   83: 
   84: sub Usage  {
   85:     print "Usage:";
   86:     print <<USAGE;
   87:     lonManage  --push=<tablename>  newfile  host
   88:         Push <tablename> to the lonTabs directory.  Note that
   89:         <tablename> must be one of:
   90:            hosts  (hosts.tab)
   91:            domain (domain.tab)
   92: 
   93:     lonManage  --reinit=lonc host
   94:            Sends a HUP signal to the remote systems's lond.
   95: 
   96:     lonManage  --reinit=lond host
   97:           Requests the remote system's lond perform the same action as if
   98:           it had received a HUP signal.
   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: USAGE
  103: 
  104: 
  105: }
  106: 
  107: #
  108: #  Use Getopt::Long to parse the parameters of the program.
  109: #
  110: #  Return value is a list consisting of:
  111: #    A 'command' which is one of:
  112: #       push   - table push requested.
  113: #       reinit - reinit requested.
  114: #   Additional parameters as follows:
  115: #       for push: Tablename, hostname
  116: #       for reinit: Appname  hostname
  117: #
  118: #   This function does not validation of the parameters of push and
  119: #   reinit.
  120: #
  121: #   returns a list.  The first element of the list is the operation name
  122: #   (e.g. reinit or push).  The second element is the switch parameter.
  123: #   for push, this is the table name, for reinit, this is the process name.
  124: #   Additional elements of the list are the command argument.  The count of
  125: #   command arguments is validated, but not their semantics.
  126: #
  127: #   returns an empty list if the parse fails.
  128: #
  129: 
  130: sub ParseArgs {
  131:     my $pushing   = '';
  132:     my $reinitting = '';
  133: 
  134:     if(!GetOptions('push=s'    => \$pushing,
  135: 	           'reinit=s'  => \$reinitting)) {
  136: 	return ();
  137:     }
  138: 
  139:     #  Require exactly   one of --push and --reinit
  140: 
  141:     my $command    = '';
  142:     my $commandarg = '';
  143:     my $paramcount = @ARGV; 	# Number of additional arguments.
  144:     
  145: 
  146:     if($pushing ne '') {
  147: 
  148:         # --push takes in addition a table, and a host:
  149:         #
  150: 	if($paramcount != 2) {
  151: 	    return ();		# Invalid parameter count.
  152: 	}
  153: 	if($command ne '') {
  154: 	    return ();
  155: 	} else {
  156: 	    
  157: 	    $command    = 'push';
  158: 	    $commandarg = $pushing;
  159: 	}
  160:     }
  161: 
  162:     if ($reinitting ne '') {
  163: 
  164: 	# --reinit takes in addition just a host name
  165: 
  166: 	if($paramcount != 1) {
  167: 	    return ();
  168: 	}
  169: 	if($command ne '') {
  170: 	    return ();
  171: 	} else {
  172: 	    $command    = 'reinit';
  173: 	    $commandarg = $reinitting; 
  174: 	}
  175:     }
  176: 
  177:     #  Build the result list:
  178: 
  179:     my @result = ($command, $commandarg);
  180:     my $i;
  181:     for($i = 0; $i < $paramcount; $i++) {
  182: 	push(@result, $ARGV[$i]);
  183:     }
  184:     
  185:     return @result;
  186: }
  187: sub ValidHost {
  188:     return 1;
  189: }
  190: sub Transact {
  191: }
  192: #
  193: #   Called to push a file to the remote system.
  194: #   The only legal files to push are hosts.tab and domain.tab.
  195: #   Security is somewhat improved by
  196: #   
  197: #   - Requiring the user run as root.
  198: #   - Connecting with lonc rather than lond directly ensuring this is a loncapa
  199: #     host
  200: #   - We must appear in the remote host's hosts.tab file.
  201: #   - The host must appear in our hosts.tab file.
  202: #
  203: #  Parameters:
  204: #     tablename - must be one of hosts or domain.
  205: #     tablefile - name of the file containing the table to push.
  206: #     host      - name of the host to push this file to.     
  207: #
  208: sub PushFile {
  209:     my $tablename = shift;
  210:     my $tablefile = shift;
  211:     my $host      = shift;
  212:     
  213:     # Open the table file:
  214: 
  215:     if(!open(TABLEFILE, "<$tablefile")) {
  216: 	die "ENOENT - No such file or directory $tablefile";
  217:     }
  218:   
  219:     # Require that the host be valid:
  220: 
  221:     if(!ValidHost($host)) {
  222: 	die "EHOSTINVAL - Invalid host $host"; # Ok so I invented this 'errno'.
  223:     }
  224:     # Read in the file.  If the table name is valid, push it.
  225: 
  226:     my @table = <TABLEFILE>;	#  These files are pretty small.
  227:     close TABLEFILE;
  228: 
  229:     if( ($tablename eq "host")    ||
  230: 	($tablename eq "domain")) {
  231: 	Transact($host, "pushfile:$tablename:",\@table);
  232:     } else {
  233: 	die "EINVAL - Invalid parameter. tablename: $tablename must be host or domain";
  234:     }
  235: }
  236: 
  237: sub ReinitProcess {
  238:     print "Reinitializing a process\n";
  239: }
  240: #--------------------------- Entry point: --------------------------
  241: 
  242: #  Parse the parameters
  243: #  If command parsing failed, then print usage:
  244: 
  245: my @params   = ParseArgs;
  246: my $nparam   = @params;
  247: 
  248: if($nparam == 0) {
  249:     Usage;
  250:     exit -1;
  251: }
  252: #
  253: #   Next, ensure we are running as EID root.
  254: #
  255: if ($EUID != 0) {
  256:     die "ENOPRIV - No privilege for requested operation"
  257: }
  258: 
  259: 
  260: #   Based on the operation requested invoke the appropriate function:
  261: 
  262: my $operation = shift @params;
  263: 
  264: if($operation eq "push") {  # push tablename filename host
  265:     my $tablename = shift @params;
  266:     my $tablefile = shift @params;
  267:     my $host      = shift @params;
  268:     PushFile($tablename, $tablefile, $host);
  269: 
  270: } elsif($operation eq "reinit") {	# reinit processname host.
  271:     my $process   = shift @params;
  272:     my $host      = shift @params;
  273:     ReinitProcess($process, $host);
  274: }
  275: else {
  276:     Usage;
  277: }
  278: exit 0;
  279: 
  280: =head1 NAME
  281:     lonManage - Command line utility for remote management of lonCAPA
  282:     cluster nodes.
  283: 
  284: =head1 SYNOPSIS
  285: 
  286: Usage:
  287:     B<lonManage  --push=<tablename>  newfile  host>
  288:         Push <tablename> to the lonTabs directory.  Note that
  289:         <tablename> must be one of:
  290:            hosts  (hosts.tab)
  291:            domain (domain.tab)
  292: 
  293:     B<lonManage  --reinit=lonc host>
  294:            Sends a HUP signal to the remote systems's lond.
  295: 
  296:     B<lonmanage  --reinit=lond host>
  297:           Requests the remote system's lond perform the same action as if
  298:           it had received a HUP signal.
  299: 
  300:     In the above syntax, the host above is the hosts.tab name of a host,
  301:     not the IP address of the host.
  302: 
  303: 
  304: =head1 DESCRIPTION
  305: 
  306: =head1 PREREQUISITES
  307: 
  308: =item strict
  309: =item Getopt::Long
  310: =item English
  311: 
  312: =head1  CATEGORIES
  313:     Command line utility
  314: 
  315: =cut

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