Annotation of loncom/lonManage, revision 1.8

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

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