File:  [LON-CAPA] / loncom / debugging_tools / modify_config_files.pl
Revision 1.16: download - view: text, annotated - select for diffs
Wed Oct 2 22:28:24 2019 UTC (4 years, 7 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Oracle Linux and RHEL/CentOS 8 and newer use /etc/yum.repos.d/loncapa.repo
  instead of /etc/yum.conf

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

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