File:  [LON-CAPA] / loncom / configuration / Checksumming.pm
Revision 1.2: download - view: text, annotated - select for diffs
Sat Feb 2 15:20:02 2013 UTC (11 years, 4 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Fix typos in closing table cell tags.
- Closing tags missing for html tables.
- Some minor wording changes.

    1: # The LearningOnline Network with CAPA
    2: # Checksum installed LON-CAPA modules and some configuration files
    3: #
    4: # $Id: Checksumming.pm,v 1.2 2013/02/02 15:20:02 raeburn Exp $
    5: #
    6: # The LearningOnline Network with CAPA
    7: #
    8: # Copyright Michigan State University Board of Trustees
    9: #
   10: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   11: #
   12: # LON-CAPA is free software; you can redistribute it and/or modify
   13: # it under the terms of the GNU General Public License as published by
   14: # the Free Software Foundation; either version 2 of the License, or
   15: # (at your option) any later version.
   16: #
   17: # LON-CAPA is distributed in the hope that it will be useful,
   18: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   19: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   20: # GNU General Public License for more details.
   21: #
   22: # You should have received a copy of the GNU General Public License
   23: # along with LON-CAPA; if not, write to the Free Software
   24: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   25: #
   26: # /home/httpd/html/adm/gpl.txt
   27: #
   28: # http://www.lon-capa.org/
   29: #
   30: 
   31: package LONCAPA::Checksumming;
   32: 
   33: use strict;
   34: use lib '/home/httpd/lib/perl/';
   35: use Apache::lonlocal();
   36: use Apache::loncommon();
   37: 
   38: sub get_checksums {
   39:     my ($distro,$londaemons,$lonlib,$lonincludes,$lontabdir) = @_;
   40:     my $output;
   41:     my (%versions,%chksums);
   42:     my $dirh;
   43:     my $revtag = '$Id:';
   44:     my @paths;
   45:     if ($londaemons) {
   46:         push(@paths,($londaemons.'/*',
   47:                      $londaemons.'/debug/*.pl'));
   48:     }
   49:     if ($lonlib) {
   50:         push(@paths,($lonlib.'/perl/Apache/*.pm',
   51:                      $lonlib.'/perl/Apache/localize/*.pm',
   52:                      $lonlib.'/perl/LONCAPA/*.pm',
   53:                      $lonlib.'/perl/AlgParser.pm',
   54:                      $lonlib.'/perl/LONCAPA.pm',
   55:                      $lonlib.'/perl/Safe.pm',
   56:                      $lonlib.'/perl/*-std.pm',
   57:                      $lonlib.'/perl/HTML/*.pm',
   58:                      $lonlib.'/perl/Safe.pm'));
   59:     }
   60:     if ($lonincludes) {
   61:         push(@paths,$lonincludes.'/*.lcpm');
   62:     }
   63:     push(@paths,('/home/httpd/cgi-bin/*.pl','/home/httpd/cgi-bin/*.png'));
   64:     my $confdir = '/etc/httpd/conf';
   65:     my $sha = 'SHA1';
   66:     if ($distro =~ /^(ubuntu|debian)(\d+)$/) {
   67:         $confdir = '/etc/apache2';
   68:         if (($1 eq 'ubuntu') && ($2 >= 12)) {
   69:             $sha = 'SHA';
   70:         }
   71:     } elsif ($distro =~ /^sles(\d+)$/) {
   72:         if ($1 >= 10) {
   73:             $confdir = '/etc/apache2';
   74:         }
   75:     } elsif ($distro =~ /^suse(\d+\.\d+)$/) {
   76:         if ($1 >= 10.0) {
   77:             $confdir = '/etc/apache2';
   78:         }
   79:     }
   80:     push(@paths,("$confdir/loncapa_apache.conf","$confdir/startup.pl"));
   81:     if ($sha eq 'SHA1') {
   82:         require Digest::SHA1;
   83:     } else {
   84:         require Digest::SHA;
   85:     }
   86:     if (@paths) {
   87:         my $pathstr = join (' ',@paths);
   88:         if (open($dirh,"grep '$revtag' $pathstr |")) {
   89:             while (my $line=<$dirh>) {
   90:                 if ($line =~ m{^([^#]+):#\s\$Id:\s[\w.]+,v\s([\d.]+)\s}) {
   91:                     $versions{$1} = $2;
   92:                 }
   93:             }
   94:             close($dirh);
   95:         }
   96:         foreach my $key (sort(keys(%versions))) {
   97:             next if ($key =~ /\.lpmlsave$/);
   98:             my $sum;
   99:             if (open(my $fh,"<$key")) {
  100:                 binmode $fh;
  101:                 my $sha_obj;
  102:                 if ($sha eq 'SHA') {
  103:                     $sha_obj = Digest::SHA->new();
  104:                 } else {
  105:                     $sha_obj = Digest::SHA1->new();
  106:                 }
  107:                 $sha_obj->addfile($fh);
  108:                 $sum = $sha_obj->hexdigest;
  109:                 close($fh);
  110:                 $chksums{$key} = $sum; 
  111:             }
  112:             $output .= "$key,$versions{$key},$sum\n";
  113:         }
  114:     }
  115:     if ($lontabdir ne '') {
  116:         if (open(my $tabfh,">$lontabdir/lonchksums.tab")) {
  117:             print $tabfh '# Last written: '.localtime(time)."\n";
  118:             print $tabfh "$output\n";
  119:             close($tabfh);
  120:         }
  121:     }
  122:     return (\%chksums,\%versions);
  123: }
  124: 
  125: sub compare_checksums {
  126:     my ($target,$lonhost,$version,$serversums,$serverversions) = @_;
  127:     my ($message,$numchg,$linefeed);
  128:     if ($target = 'web') {
  129:         $linefeed = '<br />';
  130:     } else {
  131:         $linefeed = "\n";
  132:     }
  133:     if ((ref($serversums) eq 'HASH') && (keys(%{$serversums}))) {
  134:         my $checksums = &Apache::lonnet::fetch_dns_checksums();
  135:         my (%extra,%missing,%diffs,%stdsums,%stdversions);
  136:         if (ref($checksums) eq 'HASH') {
  137:             if (ref($checksums->{'sums'}) eq 'HASH') {
  138:                 %stdsums = %{$checksums->{'sums'}};
  139:             }
  140:             if (ref($checksums->{'versions'}) eq 'HASH') {
  141:                 %stdversions = %{$checksums->{'versions'}};
  142:             }
  143:             if (keys(%stdsums)) {
  144:                 foreach my $key (keys(%stdsums)) {
  145:                     if (exists($serversums->{$key})) {
  146:                         if ($serversums->{$key} ne $stdsums{$key}) {
  147:                             $diffs{$key} = 1;
  148:                             $numchg ++;
  149:                         }
  150:                     } else {
  151:                         $missing{$key} = 1;
  152:                         $numchg ++;
  153:                     }
  154:                 }
  155:                 foreach my $key (keys(%{$serversums})) {
  156:                     unless (exists($stdsums{$key})) {
  157:                         $extra{$key} = 1;
  158:                         $numchg ++;
  159:                     }
  160:                 }
  161:             }
  162:             if ($numchg) {
  163:                 $message =
  164:                     &Apache::lonlocal::mt('[quant,_1,difference was,differences were] found'.
  165:                                           ' between LON-CAPA modules installed on your server [_2]'.
  166:                                           ' and those expected for the LON-CAPA version you are'.
  167:                                           ' currently running.',$numchg,"($lonhost)$linefeed");
  168:                 if ($target eq 'web') {
  169:                     $message = '<p>'.$message.'</p>';
  170:                 } else {
  171:                     $message .= "\n\n";
  172:                 }
  173:                 my (@diffversion,@modified);
  174:                 if (keys(%diffs) > 0) {
  175:                     foreach my $file (sort(keys(%diffs))) {
  176:                         if ($serverversions->{$file} ne $stdversions{$file}) {
  177:                             push(@diffversion,$file);
  178:                         } else {
  179:                             push(@modified,$file);
  180:                         }
  181:                     }
  182:                     if (@diffversion > 0) {
  183:                         my $text =
  184:                             &Apache::lonlocal::mt('The following [quant,_1,file is a,files are]'.
  185:                                                   ' different version(s) from that expected for LON-CAPA [_2]:',
  186:                                                   scalar(@diffversion),$version);
  187:                         if ($target eq 'web') {
  188:                             $message .= '<p>'.$text.'</p>'.
  189:                                         &Apache::loncommon::start_data_table().
  190:                                         &Apache::loncommon::start_data_table_header_row()."\n".
  191:                                         '<th>'.&Apache::lonlocal::mt('File').'</th>'."\n".
  192:                                         '<th>'.&Apache::lonlocal::mt('Server version').'</th>'."\n".
  193:                                         '<th>'.&Apache::lonlocal::mt('Expected version').'</th>'."\n".
  194:                                         &Apache::loncommon::end_data_table_header_row()."\n";
  195:                         } else {
  196:                             $message .= "$text\n";
  197:                         }
  198:                         foreach my $file (sort(@diffversion)) {
  199:                             my $revnum = $stdversions{$file};
  200:                             if ($target eq 'web') {
  201:                                 $message .=  &Apache::loncommon::start_data_table_row().
  202:                                              '<td>'.$file.'</td>'."\n".
  203:                                              '<td>'.$serverversions->{$file}.'</td>'."\n".
  204:                                              '<td>'.$revnum.'</td>'."\n".
  205:                                              &Apache::loncommon::end_data_table_row()."\n";
  206:                             } else {
  207:                                 $message .= $file.' '.
  208:                                             &Apache::lonlocal::mt('(local rev: [_1])',
  209:                                                                   $serverversions->{$file});
  210:                                 if ($revnum) {
  211:                                     $message .= '; '.
  212:                                                 &Apache::lonlocal::mt('(expected rev: [_1])',
  213:                                                                       $revnum);
  214:                                 }
  215:                                 $message .= "\n";
  216:                             }
  217:                         }
  218:                         if ($target eq 'web') {
  219:                             $message .= &Apache::loncommon::end_data_table().'<br />';
  220:                         } else {
  221:                             $message .= "\n";
  222:                         }
  223:                     }
  224:                     if (@modified > 0) {
  225:                         my $text =
  226:                             &Apache::lonlocal::mt('The following [quant,_1,file appears,files appear]'.
  227:                                                   ' to have been modified locally:',scalar(@modified));
  228:                         if ($target eq 'web') {
  229:                             $message .= '<p>'.$text.'</p>'.
  230:                                         &Apache::loncommon::start_data_table().
  231:                                         &Apache::loncommon::start_data_table_header_row()."\n".
  232:                                         '<th>'.&Apache::lonlocal::mt('File').'</th>'."\n".
  233:                                         '<th>'.&Apache::lonlocal::mt('Version').'</th>'."\n".
  234:                                         &Apache::loncommon::end_data_table_header_row()."\n";
  235:                         } else {
  236:                             $message .= "$text\n";
  237:                         }
  238:                         foreach my $file (sort(@modified)) {
  239:                             if ($target eq 'web') {
  240:                                 $message .= &Apache::loncommon::start_data_table_row()."\n".
  241:                                             '<td>'.$file.'</td>'."\n".
  242:                                             '<td>'.$serverversions->{$file}.'</td>'."\n".
  243:                                             &Apache::loncommon::end_data_table_row()."\n";
  244:                             } else {
  245:                                 $message .= $file.' '.
  246:                                             &Apache::lonlocal::mt('(local rev: [_1])',
  247:                                                                   $serverversions->{$file}).
  248:                                             "\n";
  249:                             }
  250:                         }
  251:                         if ($target eq 'web') {
  252:                             $message .= &Apache::loncommon::end_data_table().'<br />';
  253:                         } else {
  254:                             $message .= "\n";
  255:                         }
  256:                     }
  257:                 }
  258:                 if (keys(%missing) > 0) {
  259:                     my $text = 
  260:                         &Apache::lonlocal::mt('The following [quant,_1,local file appears,local files appear]'.
  261:                                               ' to be missing:',scalar(keys(%missing))); 
  262:                     if ($target eq 'web') {
  263:                         $message .= '<p>'.$text.'</p>'.
  264:                                     &Apache::loncommon::start_data_table().
  265:                                     &Apache::loncommon::start_data_table_header_row()."\n".
  266:                                     '<th>'.&Apache::lonlocal::mt('File').'</th>'."\n".
  267:                                     '<th>'.&Apache::lonlocal::mt('Expected version').'</th>'."\n".
  268:                                     &Apache::loncommon::end_data_table_header_row()."\n";
  269:                     } else {
  270:                         $message .= "$text\n";
  271:                     }
  272:                     foreach my $file (sort(keys(%missing))) {
  273:                         my $revnum = $stdversions{$file};
  274:                         if ($target eq 'web') {
  275:                             $message .= '<td>'.$file.'</td>'."\n".
  276:                                         '<td>'.$revnum.'</td>'."\n".
  277:                                         &Apache::loncommon::end_data_table_row()."\n";
  278:                         } else {
  279:                             $message .= $file;
  280:                             if ($revnum) {
  281:                                 $message .= ' '.
  282:                                             &Apache::lonlocal::mt('(expected rev: [_1])',
  283:                                                                   $revnum);
  284:                             }
  285:                             $message .= "\n";
  286:                         }
  287:                     }
  288:                     if ($target eq 'web') {
  289:                         $message .= &Apache::loncommon::end_data_table();
  290:                     } else {
  291:                         $message .= "\n";
  292:                     }
  293:                 }
  294:                 if (keys(%extra) > 0) {
  295:                     my $text = 
  296:                         &Apache::lonlocal::mt('The following [quant,_1,file is,files are]'.
  297:                                               ' not required by the release you have installed:',
  298:                                               scalar(keys(%extra)));
  299:                     if ($target eq 'web') {
  300:                         $message .= '<p>'.$text.'</p>'.
  301:                                     &Apache::loncommon::start_data_table().
  302:                                     &Apache::loncommon::start_data_table_header_row()."\n".
  303:                                     '<th>'.&Apache::lonlocal::mt('File').'</th>'."\n".
  304:                                     '<th>'.&Apache::lonlocal::mt('Version').'</th>'."\n".
  305:                                     &Apache::loncommon::end_data_table_header_row()."\n";
  306:                     } else {
  307:                         $message .= "$text\n";
  308:                     }
  309:                     foreach my $file (sort(keys(%extra))) {
  310:                         if ($target eq 'web') {
  311:                             $message .= '<td>'.$file.'</td>'."\n".
  312:                                         '<td>'.$serverversions->{$file}.'</td>'."\n".
  313:                                         &Apache::loncommon::end_data_table_row()."\n";
  314:                         } else {
  315:                             $message .= $file.' '.
  316:                                         &Apache::lonlocal::mt('(local rev: [_1])',
  317:                                                               $serverversions->{$file}).
  318:                                         "\n";
  319:                         }
  320:                     }
  321:                     if ($target eq 'web') {
  322:                         $message .= &Apache::loncommon::end_data_table().'<br />';
  323:                     } else {
  324:                         $message .= "\n";
  325:                     }
  326:                 }
  327:             } else {
  328:                 $message = &Apache::lonlocal::mt('No differences detected between installed files and files expected for LON-CAPA [_1]',$version);
  329:             }
  330:         } else {
  331:             $message = &Apache::lonlocal::mt('No comparison attempted - failed to retrieve checksums for installed files.');
  332:         }
  333:     } else {
  334:         $message = &Apache::lonlocal::mt('No comparison attempted - failed to retrieve checksums for installed files.');
  335:     }
  336:     unless ($message) {
  337:         $message = &Apache::lonlocal::mt('LON-CAPA module check found no file changes.')."\n";
  338:     }
  339:     return ($message,$numchg);
  340: }
  341: 
  342: 1;
  343: 

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