File:  [LON-CAPA] / loncom / debugging_tools / modify_config_files.pl
Revision 1.13: download - view: text, annotated - select for diffs
Mon Jun 5 21:06:25 2017 UTC (6 years, 11 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_11_2_uiuc, version_2_11_2_msu, version_2_11_2_educog, version_2_11_2, HEAD
- Update documentation
- Fedora 21 and newer use /etc/yum.repos.d/loncapa.repo instead of yum.conf

    1: #!/usr/bin/perl -w
    2: #
    3: # The LearningOnline Network
    4: #
    5: # $Id: modify_config_files.pl,v 1.13 2017/06/05 21:06:25 raeburn Exp $
    6: #
    7: # Copyright Michigan State University Board of Trustees
    8: #
    9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   10: #
   11: # LON-CAPA is free software; you can redistribute it and/or modify
   12: # it under the terms of the GNU General Public License as published by
   13: # the Free Software Foundation; either version 2 of the License, or
   14: # (at your option) any later version.
   15: #
   16: # LON-CAPA is distributed in the hope that it will be useful,
   17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   19: # GNU General Public License for more details.
   20: #
   21: # You should have received a copy of the GNU General Public License
   22: # along with LON-CAPA; if not, write to the Free Software
   23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   24: #
   25: # /home/httpd/html/adm/gpl.txt
   26: #
   27: # http://www.lon-capa.org/
   28: #
   29: ###
   30: 
   31: =pod
   32: 
   33: =head1 NAME
   34: 
   35: B<modify_config_files.pl>
   36: 
   37: =head1 SYNOPSIS
   38: 
   39: This script modifies /etc/my.cnf and one of: /etc/yum.conf 
   40: (for CentOS/Scientific Linux/RHEL >=5), /etc/apt/sources.list
   41: (for Debian/Ubuntu), /etc/sysconfig/rhn/sources (for RHEL4),
   42: and /etc/yum.repos.d/loncapa.repo (Fedora >= 21).
   43: 
   44: =head1 DESCRIPTION
   45: 
   46: This script modifies /etc/my.cnf, /etc/yum.conf, /etc/yum.repos.d/loncapa.repo,
   47: /etc/apt/sources, or /etc/sysconfig/rhn/sources to ensure certain
   48: parameters are set properly.
   49: 
   50: The LON-CAPA yum repositories are added to /etc/yum.conf, 
   51: /etc/yum.repos.d/loncapa.repo, /etc/sysconfig/rhn/sources
   52: and the LON-CAPA apt repositories are added to 
   53: /etc/apt/sources.list.
   54: 
   55: The /etc/my.cnf file is modified to set the wait_timeout to 1 year.  Backup
   56: copies of each file are made in /etc, /etc/apt, and /etc/sysconfig/rhn, as 
   57: appropriate.
   58: 
   59: =cut
   60: 
   61: use strict;
   62: use File::Copy;
   63: use lib '/home/httpd/lib/perl/';
   64: use LONCAPA::Configuration;
   65: my $loncapa_config=LONCAPA::Configuration::read_conf('loncapa.conf');
   66: 
   67: open(DSH,"$$loncapa_config{'lonDaemons'}/distprobe |");
   68: my $dist = <DSH>;
   69: chomp($dist);
   70: close(DSH);
   71: 
   72: my $yum_status;
   73: my $loninst = 'http://install.loncapa.org';
   74: my $loninst_re = 'http://install\.loncapa\.org';
   75: if ($dist =~ /^fedora(\d+)$/) {
   76:     my $file = '/etc/yum.conf';
   77:     my $ver = $1;
   78:     my $gpgchk = '0';
   79:     my $gpg = "$loninst/versions/fedora/RPM-GPG-KEY-loncapa"; 
   80:     my $nobackup;
   81:     if ($ver > 6) {
   82:         $gpgchk = '1';
   83:     }
   84:     if ($ver >= 21) {
   85:         $file = '/etc/yum.repos.d/loncapa.repo';
   86:         $nobackup = 1;
   87:     }
   88:     $yum_status =  
   89:         &update_file($file,
   90:              [{section => 'loncapa-updates-basearch',
   91:                key     => 'name=',
   92:                value   => 'Fedora Core $releasever LON-CAPA $basearch Updates',
   93:            }, {section => 'loncapa-updates-basearch',
   94:                key     => 'baseurl=',
   95:                value   => $loninst.'/fedora/linux/loncapa/$releasever/$basearch',
   96:            }, {section => 'loncapa-updates-basearch',
   97:                key     => 'gpgcheck=',
   98:                value   =>  $gpgchk,
   99:            }, {section => 'loncapa-updates-basearch',
  100:                key     => 'gpgkey=',
  101:                value   => $gpg,
  102:            }, {section => 'loncapa-updates-noarch',
  103:                key     => 'name=',
  104:                value   => 'Fedora Core $releasever LON-CAPA noarch Updates',
  105:            }, {section => 'loncapa-updates-noarch',
  106:                key     => 'baseurl=',
  107:                value   => $loninst.'/fedora/linux/loncapa/$releasever/noarch',
  108:            }, {section => 'loncapa-updates-noarch',
  109:                key     => 'gpgcheck=',
  110:                value   => $gpgchk,
  111:            }, {section => 'loncapa-updates-noarch',
  112:                key     => 'gpgkey=',
  113:                value   => $gpg,
  114:            }],$nobackup);
  115: } elsif ($dist =~ /^(rhes|centos|scientific)(\d+)$/) {
  116:     my $type = $1;
  117:     my $ver = $2;
  118:     my $longver = $ver;
  119:     if ($type eq 'rhes') {
  120:         if ($ver == 4) {
  121:             $longver = '4ES';
  122:         } elsif ($ver == 5) {
  123:             $longver = '5Server';
  124:         }
  125:     }
  126:     my %info = (
  127:                  rhes => {
  128:                            title => 'RHEL',
  129:                            path => 'redhat/linux/enterprise/loncapa',
  130:                            gpg => 'versions/redhat/RPM-GPG-KEY-loncapa',
  131:                            gpgchk => 1,
  132:                          },
  133:                  centos => {
  134:                              title => 'CentOS',
  135:                              path => 'centos/loncapa',
  136:                              gpg => 'versions/centos/RPM-GPG-KEY-loncapa',
  137:                              gpgchk => 1,
  138:                            },
  139:                  scientific => {
  140:                                  title => 'Scientific Linux',
  141:                                  path => 'scientific/loncapa',
  142:                                  gpg => 'versions/scientific/RPM-GPG-KEY-loncapa',
  143:                                  gpgchk => 1,
  144:                                },
  145:                );
  146:     if (ref($info{$type}) eq 'HASH') {
  147:         if ($ver > 4) {
  148:             $yum_status =
  149:                 &update_file('/etc/yum.conf',
  150:                      [{section => 'loncapa-updates-basearch',
  151:                        key     => 'name=',
  152:                        value   => $info{$type}{title}.' $releasever LON-CAPA $basearch Updates',
  153:                       }, {section => "loncapa-updates-basearch",
  154:                           key     => 'baseurl=',
  155:                           value   => "$loninst/$info{$type}{path}/".'$releasever/$basearch',
  156:                       }, {section => 'loncapa-updates-basearch',
  157:                           key     => 'gpgcheck=',
  158:                           value   => $info{$type}{gpgchk},
  159:                       }, {section => 'loncapa-updates-basearch',
  160:                           key     => 'gpgkey=',
  161:                           value   => "$loninst/$info{$type}{gpg}",
  162:                       }, {section => 'loncapa-updates-noarch',
  163:                           key     => 'name=',
  164:                           value   => $info{$type}{title}.' $releasever LON-CAPA noarch Updates',
  165:                       }, {section => 'loncapa-updates-noarch',
  166:                           key     => 'baseurl=',
  167:                           value   => "$loninst/$info{$type}{path}/".'$releasever/noarch',
  168:                       }, {section => 'loncapa-updates-noarch',
  169:                           key     => 'gpgcheck=',
  170:                           value   => $info{$type}{gpgchk},
  171:                       }, {section => 'loncapa-updates-noarch',
  172:                           key     => 'gpgkey=',
  173:                           value   => "$loninst/$info{$type}{gpg}",
  174:                       }]);
  175:         } elsif (($type eq 'rhes') && ($ver == 4)) {
  176:             my %rhn = (
  177:                         basearch => { 
  178:                             regexp => '\s*yum\s+loncapa\-updates\-basearch\s+'.$loninst_re.'/'.$info{$type}{path}.'/'.$longver.'/\$ARCH',
  179:                             text => "yum loncapa-updates-basearch $loninst/$info{$type}{path}/$longver/".'$ARCH',
  180:                                     },
  181:                         noarch =>  {
  182:                             regexp => '\s*yum\s+loncapa\-updates\-noarch\s+'.$loninst_re.'/'.$info{$type}{path}.'/'.$longver.'/noarch',
  183:                             text => "yum loncapa-updates-noarch $loninst/$info{$type}{path}/$longver/noarch",
  184:                                    },
  185:                       );
  186:             $yum_status = &update_rhn_source(\%rhn); 
  187:         }
  188:     }
  189: } elsif ($dist =~ /^(debian|ubuntu)\d+$/) {
  190:     my %apt_get_source = (
  191:                            debian5 => {
  192:                                         regexp => '\s*deb\s+'.$loninst_re.'/debian/\s+lenny\s+main',
  193:                                         text   => "deb $loninst/debian/ lenny main",
  194:                                       },
  195:                            ubuntu6 => {
  196:                                         regexp => '\s*deb\s+'.$loninst_re.'/ubuntu/\s+dapper\s+main',
  197:                                         text   => "deb $loninst/ubuntu/ dapper main",
  198:                                       },
  199:                            ubuntu8 => {
  200:                                         regexp => '\s*deb\s+'.$loninst_re.'/ubuntu/\s+hardy\s+main',
  201:                                         text   => "deb $loninst/ubuntu/ hardy main",
  202:                                       },
  203:                          );
  204:     my $apt_status;
  205:     if (defined($apt_get_source{$dist})) {
  206:         $apt_status = &update_apt_source($apt_get_source{$dist},);
  207:     }
  208: }
  209: 
  210: my $mysql_global_status =
  211:     &update_file('/etc/my.cnf',
  212:              [{section =>'mysqld',
  213:                key     =>'wait_timeout=',
  214:                value   =>'31536000', }]);
  215: 
  216: my $local_my_cnf = '/home/www/.my.cnf';
  217: if (! -e $local_my_cnf) {
  218:     # Create a file so we can do something with it...
  219:     system("touch $local_my_cnf");
  220: }
  221: my $mysql_www_status =
  222:     &update_file($local_my_cnf,
  223:              [{section =>'client',
  224:                key     =>'user=',
  225:                value   =>'www',},
  226:               {section =>'client',
  227:                key     =>'password=',
  228:                value   =>$loncapa_config->{'lonSqlAccess'}},]);
  229: 
  230: my $exitvalue = 0;
  231: 
  232: if ($mysql_global_status) { $exitvalue = 1; }
  233: 
  234: exit $exitvalue;
  235: 
  236: 
  237: sub update_file {
  238:     my ($file,$newdata,$nobackup) = @_;
  239:     return 1 if (! -e $file);
  240:     unless ($nobackup) {
  241:         my $backup = $file.'.backup';
  242:         if (! copy($file,$backup)) {
  243:             warn "**** Error: Unable to make backup of $file";
  244:             return 0;
  245:         }
  246:     }
  247:     my ($filedata) = &parse_config_file($file);
  248:     if (! ref($filedata)) { warn "**** Error: $filedata"; return 0;}
  249:     my $modified = 0;
  250:     foreach my $data (@$newdata) {
  251:         my $section = $data->{'section'};
  252:         my $key = $data->{'key'};
  253:         my $value = $data->{'value'};
  254:         my $result = &modify_config_file($filedata,$section,$key,$value);
  255:         if ($result) { $modified = 1; }
  256:     }
  257:     if ($modified) {
  258:         my $result = &write_config_file($file,$filedata);
  259:         if (defined($result)) { warn 'Error:'.$result; return 0; }
  260:     }
  261:     return $modified;
  262: }
  263: 
  264: #################################################################
  265: #################################################################
  266: 
  267: =pod
  268: 
  269: =over 4
  270: 
  271: =item &parse_config_file()
  272: 
  273: Read a configuration file in and parse it into an internal data structure.
  274: 
  275: Input: filename
  276: 
  277: Output: array ref $filedata  OR  scalar error message
  278: 
  279: =back 
  280: 
  281: =cut
  282: 
  283: #################################################################
  284: #################################################################
  285: sub parse_config_file {
  286:     my ($file) = @_;
  287:     open(INFILE,$file) || return ('Unable to open '.$file.' for reading');
  288:     my @Input = <INFILE>;
  289:     close(INFILE);
  290:     my @Structure;
  291:     my %Sections;
  292:     while (my $line = shift(@Input)) {
  293:         chomp($line);
  294:         if ($line =~ /^\[([^\]]*)\]/) {
  295:             my $section_id = $1;
  296:             push(@Structure,'__section__'.$section_id);
  297:             while ($line = shift(@Input)) {
  298:                 chomp($line);
  299:                 if ($line =~ /^\[([^\]]*)\]/) {
  300:                     unshift(@Input,$line);
  301:                     last;
  302:                 } else {
  303:                     push(@{$Sections{$section_id}},$line);
  304:                 }
  305:             }
  306:         } else {
  307:             push(@Structure,$line);
  308:         }
  309:     }
  310:     my $filedata = [\@Structure,\%Sections];
  311:     return $filedata;
  312: }
  313: 
  314: #################################################################
  315: #################################################################
  316: 
  317: =pod
  318: 
  319: =over 4
  320: 
  321: =item
  322: 
  323: Write a configuration file out based on the internal data structure returned
  324: by &parse_config_file
  325: 
  326: Inputs: filename, $filedata (the return value of &parse_config_file
  327: 
  328: Returns: undef on success, scalar error message on failure.
  329: 
  330: =back
  331: 
  332: =cut
  333: 
  334: #################################################################
  335: #################################################################
  336: sub write_config_file {
  337:     my ($file,$filedata) = @_;
  338:     my ($structure,$sections) = @$filedata;
  339:     if (! defined($structure) || ! ref($structure)) {
  340:         return 'Bad subroutine inputs';
  341:     }
  342:     open(OUTPUT,'>'.$file) || return('Unable to open '.$file.' for writing');
  343:     for (my $i=0;$i<scalar(@$structure);$i++) {
  344:         my $line = $structure->[$i];
  345:         chomp($line);
  346:         if ($line =~ /^__section__(.*)$/) {
  347:             my $section_id = $1;
  348:             print OUTPUT ('['.$section_id.']'.$/);
  349:             foreach my $section_line (@{$sections->{$section_id}}) {
  350:                 chomp($section_line);
  351:                 print OUTPUT $section_line.$/;
  352:             }
  353:             # Deal with blank lines
  354:             if ($sections->{$section_id}->[-1] =~ /^\s*$/) {
  355:                 # No need to output a blank line at the end if there is one 
  356:                 # already
  357:             } else {
  358:                 print OUTPUT $/;
  359:             }
  360:         } else {
  361:             print OUTPUT $line.$/;
  362:         }
  363:     }
  364:     close OUTPUT;
  365:     return undef;
  366: }
  367: 
  368: #################################################################
  369: #################################################################
  370: 
  371: =pod
  372: 
  373: =over 4
  374: 
  375: =item &modify_config_file()
  376: 
  377: Modifies the internal data structure of a configuration file to include new
  378: sections and/or new configuration directives.
  379: 
  380: Inputs: $filedata (see &parse_config_file
  381: $section, the [section] the new entry is to reside in.  A value of undef will
  382: cause the "outer" section (as in yum.conf) to be updated (or have the new
  383: value prepended).
  384: $newkey: A line which matches this will be replaced with $newkey.$newvalue
  385: $newvalue: The new value to be placed with the new key.
  386: 
  387: Returns: 0 or 1, indicating if the file was modified(1) or not(0).
  388: 
  389: =back 
  390: 
  391: =cut
  392: 
  393: #################################################################
  394: #################################################################
  395: sub modify_config_file {
  396:     my ($filedata,$section,$newkey,$newvalue)=@_;
  397:     my $modified = 0;    # returned value - set to true if the file is modified
  398:     my ($structure,$sections) = @$filedata;
  399:     if (! defined($newvalue)) {
  400:         $newvalue = '';
  401:     }
  402:     my $newline = $newkey.$newvalue;
  403:     #
  404:     # Determine which array ref gets the item
  405:     my $target;
  406:     if (defined($section)) {
  407:         if (! exists($sections->{$section})) {
  408:             push(@$structure,'__section__'.$section);
  409:             $sections->{$section}=[];
  410:         }
  411:         $target = $sections->{$section};
  412:     } else {
  413:         $target = $structure;
  414:     }
  415:     #
  416:     # Put the item in or update it.
  417:     my $key_is_new = 1;
  418:     for (my $i=0;$i<scalar(@$target);$i++) {
  419:         if ($target->[$i] =~/^$newkey/) {
  420:             if ($target->[$i] ne $newline) {
  421:                 $target->[$i]=$newline;
  422:                 $modified = 1;
  423:             }
  424:             $key_is_new = 0;
  425:             last;
  426:         }
  427:     }
  428:     if ($key_is_new) {
  429:         if (! defined($section)) {
  430:             unshift(@$target,$newline);
  431:         } else {
  432:             # No need to put things after a blank line.
  433:             if (defined($target->[-1]) && $target->[-1] =~ /^\s*$/) {
  434:                 $target->[-1] = $newline;
  435:                 $modified = 1;
  436:             } else {
  437:                 push(@$target,$newline);
  438:                 $modified = 1;
  439:             }
  440:         }
  441:     }
  442:     return $modified;
  443: }
  444: 
  445: #################################################################
  446: #################################################################
  447: 
  448: =pod
  449: 
  450: =over 4
  451: 
  452: =item &update_rhn_source()
  453: 
  454: Modifies the Red Hat 4 sources file which includes repositories used by up2date 
  455: 
  456: Inputs: 
  457: $rhn_items - a reference to hash of a hash containing the regular expression
  458: to test for, and the text string to append to the file, if an entry for the 
  459: LON-CAPA RHEL repository is missing for two cases:
  460: 
  461: (a) basearch
  462: (b) noarch 
  463: 
  464: Returns: 0 or 1, indicating if the file was modified(1) or not(0).
  465: 
  466: =back
  467: 
  468: =cut
  469: 
  470: #################################################################
  471: #################################################################
  472: sub update_rhn_source {
  473:     my ($rhn_items) = @_;
  474:     return 0 if (ref($rhn_items) ne 'HASH');
  475:     return 0 if ((ref($rhn_items->{basearch}) ne 'HASH') || (ref($rhn_items->{noarch}) ne 'HASH'));
  476:     my $file = '/etc/sysconfig/rhn/sources';
  477:     return 0 if (! -e $file);
  478:     my $backup = $file.'.backup';
  479:     if (! copy($file,$backup)) {
  480:         warn "**** Error: Unable to make backup of $file";
  481:         return 0;
  482:     }
  483:     my $result = 0;
  484:     my $fh;
  485:     if (open($fh,"<$file")) {
  486:         my $total = 0;
  487:         my %found;
  488:         foreach my $item (keys(%{$rhn_items})) {
  489:             $found{$item} = 0;
  490:         }
  491:         while(<$fh>) {
  492:             foreach my $item (keys(%{$rhn_items})) {
  493:                 if (ref($rhn_items->{$item}) eq 'HASH') {
  494:                     my $pattern = $rhn_items->{$item}->{regexp};
  495:                     if ($pattern ne '') { 
  496:                         if (m{^$pattern}) {
  497:                             $found{$item} = 1;
  498:                             $total ++;
  499:                         }
  500:                     }
  501:                 }
  502:             }
  503:             last if $total == 2;
  504:         }
  505:         close($fh);
  506:         if ($total < 2) {
  507:             if (open($fh,">>$file")) {
  508:                 foreach my $item (keys(%{$rhn_items})) {
  509:                     unless ($found{$item}) {
  510:                         if (ref($rhn_items->{$item}) eq 'HASH') {
  511:                             if ($rhn_items->{$item}->{text} ne '') {
  512:                                 print $fh "\n".$rhn_items->{$item}->{text}."\n";
  513:                                 $result = 1;
  514:                             }
  515:                         }
  516:                     }
  517:                 }
  518:                 close($fh);
  519:             }
  520:         }
  521:     }
  522:     return $result;
  523: }
  524: 
  525: #################################################################
  526: #################################################################
  527: 
  528: =pod
  529: 
  530: =over 4
  531: 
  532: =item &update_apt_source()
  533: 
  534: Modifies the source.list file which includes repositories used by apt-get
  535: 
  536: Inputs:
  537: $deb_row - a reference to containing the regular expression
  538: to test for, and the text string to append to the file, if an entry for the
  539: LON-CAPA Debian/ or Ubuntu repository is missing.
  540: 
  541: Returns: 0 or 1, indicating if the file was modified(1) or not(0).
  542: 
  543: =back
  544: 
  545: =cut
  546: 
  547: #################################################################
  548: #################################################################
  549: sub update_apt_source {
  550:     my ($deb_row) = @_;
  551:     return 0 if (ref($deb_row) ne 'HASH');
  552:     return 0 if (($deb_row->{regexp} eq '') || ($deb_row->{text} eq ''));
  553:     my $file = '/etc/apt/sources.list';
  554:     return 0 if (! -e $file);
  555:     my $backup = $file.'.backup';
  556:     if (! copy($file,$backup)) {
  557:         warn "**** Error: Unable to make backup of $file";
  558:         return 0;
  559:     }
  560:     my $result = 0;
  561:     my $fh;
  562:     if (open($fh,"<$file")) {
  563:         my $found = 0;
  564:         my $pattern = $deb_row->{regexp};
  565:         while(<$fh>) {
  566:             if (m{^$pattern}) {
  567:                 $found = 1;
  568:                 last;
  569:             }
  570:         }
  571:         close($fh);
  572:         if (!$found) {
  573:             if (open($fh,">>$file")) {
  574:                 print $fh "\n".$deb_row->{text}."\n";
  575:                 close($fh);
  576:                 $result = 1;
  577:             }
  578:         }
  579:     }
  580:     return $result;
  581: }
  582: 

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