File:  [LON-CAPA] / loncom / debugging_tools / modify_config_files.pl
Revision 1.8: download - view: text, annotated - select for diffs
Sat Jan 12 21:15:00 2008 UTC (16 years, 4 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_8_X, version_2_8_2, version_2_8_1, version_2_8_0, version_2_7_X, version_2_7_99_1, version_2_7_99_0, version_2_7_1, version_2_7_0, version_2_6_X, version_2_6_99_1, version_2_6_99_0, version_2_6_3, version_2_6_2, version_2_6_1, bz5969, HEAD, GCI_1, BZ5971-printing-apage, BZ5434-fox
Correct path to yum repositories for RHEL5.

    1: #!/usr/bin/perl -w
    2: #
    3: # The LearningOnline Network
    4: #
    5: # $Id: modify_config_files.pl,v 1.8 2008/01/12 21:15:00 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/yum.conf and /etc/my.cnf.
   40: 
   41: =head1 DESCRIPTION
   42: 
   43: This script modifies /etc/yum.conf and /etc/my.cnf to ensure certain parameters
   44: are set properly.  The LON-CAPA yum repositories are added to /etc/yum.conf.
   45: The /etc/my.cnf file is modified to set the wait_timeout to 1 year.  Backup
   46: copies of each file are made in /etc.
   47: 
   48: =cut
   49: 
   50: use strict;
   51: use File::Copy;
   52: use lib '/home/httpd/lib/perl/';
   53: use LONCAPA::Configuration;
   54: my $loncapa_config=LONCAPA::Configuration::read_conf('loncapa.conf');
   55: 
   56: open(DSH,"$$loncapa_config{'lonDaemons'}/distprobe |");
   57: my $dist = <DSH>;
   58: chomp($dist);
   59: close(DSH);
   60: 
   61: my $yum_status;
   62: if ($dist =~ /^fedora\d+$/) {
   63:     $yum_status =  
   64:         &update_file('/etc/yum.conf',
   65:              [{section => 'loncapa-updates-$basearch',
   66:                key     => 'name=',
   67:                value   => 'Fedora Core $releasever LON-CAPA $basearch Updates',
   68:            }, {section => 'loncapa-updates-$basearch',
   69:                key     => 'baseurl=',
   70:                value   => 'http://install.loncapa.org/fedora/linux/loncapa/'.
   71:                    '$releasever/$basearch',
   72:            }, {section => 'loncapa-updates-$basearch',
   73:                key     => 'gpgcheck=',
   74:                value   => '0',
   75:            }, {section => 'loncapa-updates-noarch',
   76:                key     => 'name=',
   77:                value   => 'Fedora Core $releasever LON-CAPA noarch Updates',
   78:            }, {section => 'loncapa-updates-noarch',
   79:                key     => 'baseurl=',
   80:                value   => 'http://install.loncapa.org/fedora/linux/loncapa/'.
   81:                    '$releasever/noarch',
   82:            }, {section => 'loncapa-updates-noarch',
   83:                key     => 'gpgcheck=',
   84:                value   => '0',
   85:            }]);
   86: } elsif ($dist =~ /^rhes(\d+)$/) {
   87:     if ($1 > 4) {
   88:         $yum_status =
   89:             &update_file('/etc/yum.conf',
   90:                  [{section => 'loncapa-updates-$basearch',
   91:                    key     => 'name=',
   92:                    value   => 'RHEL $releasever LON-CAPA $basearch Updates',
   93:                }, {section => 'loncapa-updates-$basearch',
   94:                    key     => 'baseurl=',
   95:                    value   => 'http://install.loncapa.org/redhat/linux/enterprise/loncapa/'.
   96:                        '$releasever/$basearch',
   97:                }, {section => 'loncapa-updates-$basearch',
   98:                    key     => 'gpgcheck=',
   99:                    value   => '1',
  100:                }, {section => 'loncapa-updates-$basearch',
  101:                    key     => 'gpgkey=',
  102:                    value   => 'http://install.loncapa.org/versions/redhat/'.
  103:                               'RPM-GPG-KEY-loncapa',
  104:                }, {section => 'loncapa-updates-noarch',
  105:                    key     => 'name=',
  106:                    value   => 'RHEL $releasever LON-CAPA noarch Updates',
  107:                }, {section => 'loncapa-updates-noarch',
  108:                    key     => 'baseurl=',
  109:                    value   => 'http://install.loncapa.org/redhat/linux/enterprise/loncapa/'.
  110:                        '$releasever/noarch',
  111:                }, {section => 'loncapa-updates-noarch',
  112:                    key     => 'gpgcheck=',
  113:                    value   => '1',
  114:                }, {section => 'loncapa-updates-noarch',
  115:                    key     => 'gpgkey=',
  116:                    value   => 'http://install.loncapa.org/versions/redhat/'.
  117:                               'RPM-GPG-KEY-loncapa',
  118:                }]);
  119:     }
  120: }
  121: 
  122: my $mysql_global_status =
  123:     &update_file('/etc/my.cnf',
  124:              [{section =>'mysqld',
  125:                key     =>'set-variable=wait_timeout=',
  126:                value   =>'31536000', }]);
  127: 
  128: my $local_my_cnf = '/home/www/.my.cnf';
  129: if (! -e $local_my_cnf) {
  130:     # Create a file so we can do something with it...
  131:     system("touch $local_my_cnf");
  132: }
  133: my $mysql_www_status =
  134:     &update_file($local_my_cnf,
  135:              [{section =>'client',
  136:                key     =>'user=',
  137:                value   =>'www',},
  138:               {section =>'client',
  139:                key     =>'password=',
  140:                value   =>$loncapa_config->{'lonSqlAccess'}},]);
  141: 
  142: my $exitvalue = 0;
  143: 
  144: if ($mysql_global_status) { $exitvalue = 1; }
  145: 
  146: exit $exitvalue;
  147: 
  148: 
  149: 
  150: #################################################################
  151: #################################################################
  152: 
  153: =pod
  154: 
  155: =over 4
  156: 
  157: =cut
  158: 
  159: #################################################################
  160: #################################################################
  161: sub update_file {
  162:     my ($file,$newdata) = @_;
  163:     return 1 if (! -e $file);
  164:     my $backup = $file.'.backup';
  165:     if (! copy($file,$backup)) {
  166:         warn "**** Error: Unable to make backup of $file";
  167:         return 0;
  168:     }
  169:     my ($filedata) = &parse_config_file($file);
  170:     if (! ref($filedata)) { warn "**** Error: $filedata"; return 0;}
  171:     my $modified = 0;
  172:     foreach my $data (@$newdata) {
  173:         my $section = $data->{'section'};
  174:         my $key = $data->{'key'};
  175:         my $value = $data->{'value'};
  176:         my $result = &modify_config_file($filedata,$section,$key,$value);
  177:         if ($result) { $modified = 1; }
  178:     }
  179:     if ($modified) {
  180:         my $result = &write_config_file($file,$filedata);
  181:         if (defined($result)) { warn 'Error:'.$result; return 0; }
  182:     }
  183:     return $modified;
  184: }
  185: 
  186: #################################################################
  187: #################################################################
  188: 
  189: =pod
  190: 
  191: =item &parse_config_file
  192: 
  193: Read a configuration file in and parse it into an internal data structure.
  194: 
  195: Input: filename
  196: 
  197: Output: array ref $filedata  OR  scalar error message
  198: 
  199: =cut
  200: 
  201: #################################################################
  202: #################################################################
  203: sub parse_config_file {
  204:     my ($file) = @_;
  205:     open(INFILE,$file) || return ('Unable to open '.$file.' for reading');
  206:     my @Input = <INFILE>;
  207:     close(INFILE);
  208:     my @Structure;
  209:     my %Sections;
  210:     while (my $line = shift(@Input)) {
  211:         chomp($line);
  212:         if ($line =~ /^\[([^\]]*)\]/) {
  213:             my $section_id = $1;
  214:             push(@Structure,'__section__'.$section_id);
  215:             while ($line = shift(@Input)) {
  216:                 chomp($line);
  217:                 if ($line =~ /^\[([^\]]*)\]/) {
  218:                     unshift(@Input,$line);
  219:                     last;
  220:                 } else {
  221:                     push(@{$Sections{$section_id}},$line);
  222:                 }
  223:             }
  224:         } else {
  225:             push(@Structure,$line);
  226:         }
  227:     }
  228:     my $filedata = [\@Structure,\%Sections];
  229:     return $filedata;
  230: }
  231: 
  232: #################################################################
  233: #################################################################
  234: 
  235: =pod
  236: 
  237: =item
  238: 
  239: Write a configuration file out based on the internal data structure returned
  240: by &parse_config_file
  241: 
  242: Inputs: filename, $filedata (the return value of &parse_config_file
  243: 
  244: Returns: undef on success, scalar error message on failure.
  245: 
  246: =cut
  247: 
  248: #################################################################
  249: #################################################################
  250: sub write_config_file {
  251:     my ($file,$filedata) = @_;
  252:     my ($structure,$sections) = @$filedata;
  253:     if (! defined($structure) || ! ref($structure)) {
  254:         return 'Bad subroutine inputs';
  255:     }
  256:     open(OUTPUT,'>'.$file) || return('Unable to open '.$file.' for writing');
  257:     for (my $i=0;$i<scalar(@$structure);$i++) {
  258:         my $line = $structure->[$i];
  259:         chomp($line);
  260:         if ($line =~ /^__section__(.*)$/) {
  261:             my $section_id = $1;
  262:             print OUTPUT ('['.$section_id.']'.$/);
  263:             foreach my $section_line (@{$sections->{$section_id}}) {
  264:                 chomp($section_line);
  265:                 print OUTPUT $section_line.$/;
  266:             }
  267:             # Deal with blank lines
  268:             if ($sections->{$section_id}->[-1] =~ /^\s*$/) {
  269:                 # No need to output a blank line at the end if there is one 
  270:                 # already
  271:             } else {
  272:                 print OUTPUT $/;
  273:             }
  274:         } else {
  275:             print OUTPUT $line.$/;
  276:         }
  277:     }
  278:     close OUTPUT;
  279:     return undef;
  280: }
  281: 
  282: #################################################################
  283: #################################################################
  284: 
  285: =pod
  286: 
  287: =item &modify_config_file
  288: 
  289: Modifies the internal data structure of a configuration file to include new
  290: sections and/or new configuration directives.
  291: 
  292: Inputs: $filedata (see &parse_config_file
  293: $section, the [section] the new entry is to reside in.  A value of undef will
  294: cause the "outer" section (as in yum.conf) to be updated (or have the new
  295: value prepended).
  296: $newkey: A line which matches this will be replaced with $newkey.$newvalue
  297: $newvalue: The new value to be placed with the new key.
  298: 
  299: Returns: 0 or 1, indicating if the file was modified(1) or not(0).
  300: 
  301: 
  302: =cut
  303: 
  304: #################################################################
  305: #################################################################
  306: sub modify_config_file {
  307:     my ($filedata,$section,$newkey,$newvalue)=@_;
  308:     my $modified = 0;    # returned value - set to true if the file is modified
  309:     my ($structure,$sections) = @$filedata;
  310:     if (! defined($newvalue)) {
  311:         $newvalue = '';
  312:     }
  313:     my $newline = $newkey.$newvalue;
  314:     #
  315:     # Determine which array ref gets the item
  316:     my $target;
  317:     if (defined($section)) {
  318:         if (! exists($sections->{$section})) {
  319:             push(@$structure,'__section__'.$section);
  320:             $sections->{$section}=[];
  321:         }
  322:         $target = $sections->{$section};
  323:     } else {
  324:         $target = $structure;
  325:     }
  326:     #
  327:     # Put the item in or update it.
  328:     my $key_is_new = 1;
  329:     for (my $i=0;$i<scalar(@$target);$i++) {
  330:         if ($target->[$i] =~/^$newkey/) {
  331:             if ($target->[$i] ne $newline) {
  332:                 $target->[$i]=$newline;
  333:                 $modified = 1;
  334:             }
  335:             $key_is_new = 0;
  336:             last;
  337:         }
  338:     }
  339:     if ($key_is_new) {
  340:         if (! defined($section)) {
  341:             unshift(@$target,$newline);
  342:         } else {
  343:             # No need to put things after a blank line.
  344:             if (defined($target->[-1]) && $target->[-1] =~ /^\s*$/) {
  345:                 $target->[-1] = $newline;
  346:                 $modified = 1;
  347:             } else {
  348:                 push(@$target,$newline);
  349:                 $modified = 1;
  350:             }
  351:         }
  352:     }
  353:     return $modified;
  354: }
  355: 
  356: 
  357: #################################################################
  358: #################################################################
  359: 
  360: =pod
  361: 
  362: =back
  363: 
  364: =cut
  365: 
  366: #################################################################
  367: #################################################################

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