Annotation of loncom/lonManage, revision 1.9

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.9     ! foxr        6: #  $Id: lonManage,v 1.8 2003/08/18 10:18:21 foxr Exp $
1.1       foxr        7: #
1.9     ! foxr        8: # $Id: lonManage,v 1.8 2003/08/18 10:18:21 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.9     ! foxr       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: #
1.8       foxr       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: #
1.7       foxr       64: #  Revision 1.6  2003/08/12 11:02:59  foxr
                     65: #  Implement command switch dispatching.
                     66: #
1.6       foxr       67: #  Revision 1.5  2003/08/12 10:55:42  foxr
                     68: #  Complete command line parsing (tested)
                     69: #
1.5       foxr       70: #  Revision 1.4  2003/08/12 10:40:44  foxr
                     71: #  Get switch parsing right.
                     72: #
1.4       foxr       73: #  Revision 1.3  2003/08/12 10:22:35  foxr
                     74: #  Put in parameter parsing infrastructure
                     75: #
1.3       foxr       76: #  Revision 1.2  2003/08/12 09:58:49  foxr
                     77: #  Add usage and skeleton documentation.
1.2       foxr       78: #
1.3       foxr       79: #
1.7       foxr       80: use strict;			# Because it's good practice.
                     81: use English;			# Cause I like meaningful names.
1.3       foxr       82: use Getopt::Long;
1.2       foxr       83: 
1.3       foxr       84: sub Usage  {
1.2       foxr       85:     print "Usage:";
                     86:     print <<USAGE;
1.3       foxr       87:     lonManage  --push=<tablename>  newfile  host
1.2       foxr       88:         Push <tablename> to the lonTabs directory.  Note that
                     89:         <tablename> must be one of:
                     90:            hosts  (hosts.tab)
                     91:            domain (domain.tab)
                     92: 
1.3       foxr       93:     lonManage  --reinit=lonc host
1.2       foxr       94:            Sends a HUP signal to the remote systems's lond.
                     95: 
1.7       foxr       96:     lonManage  --reinit=lond host
1.2       foxr       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: #
1.3       foxr      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.
1.4       foxr      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: #
1.3       foxr      127: #   returns an empty list if the parse fails.
                    128: #
                    129: 
                    130: sub ParseArgs {
1.4       foxr      131:     my $pushing   = '';
1.7       foxr      132:     my $reinitting = '';
1.5       foxr      133: 
1.4       foxr      134:     if(!GetOptions('push=s'    => \$pushing,
                    135: 	           'reinit=s'  => \$reinitting)) {
                    136: 	return ();
                    137:     }
                    138: 
                    139:     #  Require exactly   one of --push and --reinit
                    140: 
1.5       foxr      141:     my $command    = '';
1.4       foxr      142:     my $commandarg = '';
1.5       foxr      143:     my $paramcount = @ARGV; 	# Number of additional arguments.
                    144:     
                    145: 
1.4       foxr      146:     if($pushing ne '') {
1.5       foxr      147: 
                    148:         # --push takes in addition a table, and a host:
                    149:         #
                    150: 	if($paramcount != 2) {
                    151: 	    return ();		# Invalid parameter count.
                    152: 	}
1.4       foxr      153: 	if($command ne '') {
                    154: 	    return ();
                    155: 	} else {
1.5       foxr      156: 	    
1.4       foxr      157: 	    $command    = 'push';
                    158: 	    $commandarg = $pushing;
                    159: 	}
                    160:     }
1.5       foxr      161: 
1.4       foxr      162:     if ($reinitting ne '') {
1.5       foxr      163: 
                    164: 	# --reinit takes in addition just a host name
                    165: 
                    166: 	if($paramcount != 1) {
                    167: 	    return ();
                    168: 	}
1.4       foxr      169: 	if($command ne '') {
                    170: 	    return ();
                    171: 	} else {
                    172: 	    $command    = 'reinit';
                    173: 	    $commandarg = $reinitting; 
                    174: 	}
                    175:     }
                    176: 
1.5       foxr      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;
1.3       foxr      186: }
1.8       foxr      187: sub ValidHost {
                    188:     return 1;
                    189: }
                    190: sub Transact {
                    191: }
1.7       foxr      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:     
1.8       foxr      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:     }
1.7       foxr      235: }
1.9     ! foxr      236: #
        !           237: #   This function is called to reinitialize a server in a remote host.
        !           238: #   The servers that can be reinitialized are:
        !           239: #   - lonc   - The lonc client process.
        !           240: #   - lond   - The lond daemon.
        !           241: #  NOTE:
        !           242: #    Reinitialization in this case means re-scanning the hosts table,
        !           243: #    starting new lond/lonc's as approprate and stopping existing lonc/lond's.
        !           244: #
        !           245: #  Parameters:
        !           246: #     process - The name of the process to reinit (lonc or lond).
        !           247: #     host    - The host in which this reinit will happen.
        !           248: #
        !           249: sub ReinitProcess {
        !           250:     my $process = shift;
        !           251:     my $host    = shift;
1.3       foxr      252: 
1.9     ! foxr      253:     #  Ensure the host is valid:
        !           254:     
        !           255:     if(!ValidHost($host)) {
        !           256: 	die "EHOSTINVAL - Invalid host $host";
        !           257:     }
        !           258:     # Ensure target process selector is valid:
        !           259: 
        !           260:     if(($process eq "lonc") ||
        !           261:        ($process eq "lond")) {
        !           262: 	Transact($host, "reinit:$process");
        !           263:     } else {
        !           264: 	die "EINVAL -Invalid parameter. Process $process must be lonc or lond";
        !           265:     }
1.7       foxr      266: }
1.6       foxr      267: #--------------------------- Entry point: --------------------------
                    268: 
                    269: #  Parse the parameters
                    270: #  If command parsing failed, then print usage:
1.2       foxr      271: 
1.7       foxr      272: my @params   = ParseArgs;
                    273: my $nparam   = @params;
1.3       foxr      274: 
                    275: if($nparam == 0) {
1.2       foxr      276:     Usage;
1.4       foxr      277:     exit -1;
1.2       foxr      278: }
1.7       foxr      279: #
                    280: #   Next, ensure we are running as EID root.
                    281: #
                    282: if ($EUID != 0) {
                    283:     die "ENOPRIV - No privilege for requested operation"
1.6       foxr      284: }
                    285: 
1.4       foxr      286: 
1.6       foxr      287: #   Based on the operation requested invoke the appropriate function:
                    288: 
1.7       foxr      289: my $operation = shift @params;
1.6       foxr      290: 
                    291: if($operation eq "push") {  # push tablename filename host
1.7       foxr      292:     my $tablename = shift @params;
                    293:     my $tablefile = shift @params;
                    294:     my $host      = shift @params;
1.6       foxr      295:     PushFile($tablename, $tablefile, $host);
                    296: 
1.7       foxr      297: } elsif($operation eq "reinit") {	# reinit processname host.
                    298:     my $process   = shift @params;
                    299:     my $host      = shift @params;
                    300:     ReinitProcess($process, $host);
1.6       foxr      301: }
1.7       foxr      302: else {
                    303:     Usage;
1.6       foxr      304: }
1.4       foxr      305: exit 0;
1.2       foxr      306: 
                    307: =head1 NAME
                    308:     lonManage - Command line utility for remote management of lonCAPA
                    309:     cluster nodes.
                    310: 
                    311: =head1 SYNOPSIS
                    312: 
                    313: Usage:
1.3       foxr      314:     B<lonManage  --push=<tablename>  newfile  host>
1.2       foxr      315:         Push <tablename> to the lonTabs directory.  Note that
                    316:         <tablename> must be one of:
                    317:            hosts  (hosts.tab)
                    318:            domain (domain.tab)
                    319: 
1.3       foxr      320:     B<lonManage  --reinit=lonc host>
1.2       foxr      321:            Sends a HUP signal to the remote systems's lond.
                    322: 
1.3       foxr      323:     B<lonmanage  --reinit=lond host>
1.2       foxr      324:           Requests the remote system's lond perform the same action as if
                    325:           it had received a HUP signal.
                    326: 
                    327:     In the above syntax, the host above is the hosts.tab name of a host,
                    328:     not the IP address of the host.
                    329: 
                    330: 
                    331: =head1 DESCRIPTION
                    332: 
                    333: =head1 PREREQUISITES
1.3       foxr      334: 
1.7       foxr      335: =item strict
1.3       foxr      336: =item Getopt::Long
1.7       foxr      337: =item English
1.2       foxr      338: 
                    339: =head1  CATEGORIES
                    340:     Command line utility
                    341: 
                    342: =cut

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