File:  [LON-CAPA] / loncom / Attic / lonManage
Revision 1.7: download - view: text, annotated - select for diffs
Mon Aug 18 09:56:01 2003 UTC (20 years, 9 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
1. Require to be run as root.
2. Catch case where no operation switch is supplied and put out usage.
3. skeleton/comments for PushFile function.

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

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