Annotation of loncom/lonManage, revision 1.7

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

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