Annotation of loncom/debugging_tools/modify_config_files.pl, revision 1.3

1.1       matthew     1: #!/usr/bin/perl -w
                      2: #
                      3: # The LearningOnline Network
                      4: #
1.3     ! matthew     5: # $Id: modify_config_files.pl,v 1.2 2004/08/10 20:26:02 matthew Exp $
1.1       matthew     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;
1.3     ! matthew    52: use lib '/home/httpd/lib/perl/';
        !            53: use LONCAPA::Configuration;
        !            54: my $loncapa_config=LONCAPA::Configuration::read_conf('loncapa.conf');
1.1       matthew    55: 
                     56: &update_file('/etc/yum.conf',
                     57:              [{section => 'loncapa-updates-i386',
                     58:                key     => 'name=',
                     59:                value   => 'Fedora Core $releasever LON-CAPA i386 Updates',
                     60:            }, {section => 'loncapa-updates-i386',
                     61:                key     => 'baseurl=',
                     62:                value   => 'http://install.loncapa.org/fedora/linux/loncapa/'.
                     63:                    '$releasever/i386',
                     64:            }, {section => 'loncapa-updates-noarch',
                     65:                key     => 'name=',
                     66:                value   => 'Fedora Core $releasever LON-CAPA noarch Updates',
                     67:            }, {section => 'loncapa-updates-noarch',
                     68:                key     => 'baseurl=',
                     69:                value   => 'http://install.loncapa.org/fedora/linux/loncapa/'.
                     70:                    '$releasever/noarch',
                     71:            }]);
                     72: 
                     73: &update_file('/etc/my.cnf',
                     74:              [{section =>'mysqld',
                     75:                key     =>'set-variable=wait_timeout=',
                     76:                value   =>'31536000', }]);
                     77: 
1.3     ! matthew    78: 
        !            79: my $local_my_cnf = '/home/www/.my.cnf';
        !            80: if (! -e $local_my_cnf) {
        !            81: #    # Create a file so we can do something with it...
        !            82:     system("touch $local_my_cnf");
        !            83: }
        !            84: &update_file($local_my_cnf,
        !            85:              [{section =>'client',
        !            86:                key     =>'user=',
        !            87:                value   =>'www',},
        !            88:               {section =>'client',
        !            89:                key     =>'password=',
        !            90:                value   =>$loncapa_config->{'lonSqlAccess'}},]);
        !            91: 
1.1       matthew    92: exit;
                     93: 
                     94: 
                     95: 
                     96: #################################################################
                     97: #################################################################
                     98: 
                     99: =pod
                    100: 
                    101: =over 4
                    102: 
                    103: =cut
                    104: 
                    105: #################################################################
                    106: #################################################################
                    107: sub update_file {
                    108:     my ($file,$newdata) = @_;
                    109:     return 1 if (! -e $file);
                    110:     my $backup = $file.'.backup';
                    111:     if (! copy($file,$backup)) {
                    112:         warn "Error: Unable to make backup of $file";
                    113:         return 0;
                    114:     }
                    115:     my ($filedata) = &parse_config_file($file);
                    116:     if (! ref($filedata)) { warn "Error: $filedata"; return 0;}
                    117:     foreach my $data (@$newdata) {
                    118:         my $section = $data->{'section'};
                    119:         my $key = $data->{'key'};
                    120:         my $value = $data->{'value'};
                    121:         &modify_config_file($filedata,$section,$key,$value)
                    122:     }
                    123:     my $result = &write_config_file($file,$filedata);
                    124:     if (defined($result)) { warn 'Error:'.$result; return 0; }
                    125:     return 1;
                    126: }
                    127: 
                    128: #################################################################
                    129: #################################################################
                    130: 
                    131: =pod
                    132: 
                    133: =item &parse_config_file
                    134: 
                    135: Read a configuration file in and parse it into an internal data structure.
                    136: 
                    137: Input: filename
                    138: 
                    139: Output: array ref $filedata  OR  scalar error message
                    140: 
                    141: =cut
                    142: 
                    143: #################################################################
                    144: #################################################################
                    145: sub parse_config_file {
                    146:     my ($file) = @_;
                    147:     open(INFILE,$file) || return ('Unable to open '.$file.' for reading');
                    148:     my @Input = <INFILE>;
                    149:     close(INFILE);
                    150:     my @Structure;
                    151:     my %Sections;
                    152:     while (my $line = shift(@Input)) {
                    153:         chomp($line);
                    154:         if ($line =~ /^\[([^\]]*)\]/) {
                    155:             my $section_id = $1;
                    156:             push(@Structure,'__section__'.$section_id);
                    157:             while ($line = shift(@Input)) {
                    158:                 if ($line =~ /^\[([^\]]*)\]/) {
                    159:                     unshift(@Input,$line);
                    160:                     last;
                    161:                 } else {
                    162:                     push(@{$Sections{$section_id}},$line);
                    163:                 }
                    164:             }
                    165:         } else {
                    166:             push(@Structure,$line);
                    167:         }
                    168:     }
                    169:     my $filedata = [\@Structure,\%Sections];
                    170:     return $filedata;
                    171: }
                    172: 
                    173: #################################################################
                    174: #################################################################
                    175: 
                    176: =pod
                    177: 
                    178: =item
                    179: 
                    180: Write a configuration file out based on the internal data structure returned
                    181: by &parse_config_file
                    182: 
                    183: Inputs: filename, $filedata (the return value of &parse_config_file
                    184: 
                    185: Returns: undef on success, scalar error message on failure.
                    186: 
                    187: =cut
                    188: 
                    189: #################################################################
                    190: #################################################################
                    191: sub write_config_file {
                    192:     my ($file,$filedata) = @_;
                    193:     my ($structure,$sections) = @$filedata;
                    194:     if (! defined($structure) || ! ref($structure)) {
                    195:         return 'Bad subroutine inputs';
                    196:     }
                    197:     open(OUTPUT,'>'.$file) || return('Unable to open '.$file.' for writing');
                    198:     for (my $i=0;$i<scalar(@$structure);$i++) {
                    199:         my $line = $structure->[$i];
                    200:         chomp($line);
                    201:         if ($line =~ /^__section__(.*)$/) {
                    202:             my $section_id = $1;
                    203:             print OUTPUT ('['.$section_id.']'.$/);
                    204:             foreach my $section_line (@{$sections->{$section_id}}) {
                    205:                 chomp($section_line);
                    206:                 print OUTPUT $section_line.$/;
                    207:             }
                    208:             # Deal with blank lines
                    209:             if ($sections->{$section_id}->[-1] =~ /^\s*$/) {
                    210:                 # No need to output a blank line at the end if there is one 
                    211:                 # already
                    212:             } else {
                    213:                 print OUTPUT $/;
                    214:             }
                    215:         } else {
                    216:             print OUTPUT $line.$/;
                    217:         }
                    218:     }
                    219:     close OUTPUT;
                    220:     return undef;
                    221: }
                    222: 
                    223: #################################################################
                    224: #################################################################
                    225: 
                    226: =pod
                    227: 
                    228: =item &modify_config_file
                    229: 
                    230: Modifies the internal data structure of a configuration file to include new
                    231: sections and/or new configuration directives.
                    232: 
                    233: Inputs: $filedata (see &parse_config_file
                    234: $section, the [section] the new entry is to reside in.  A value of undef will
                    235: cause the "outer" section (as in yum.conf) to be updated (or have the new
                    236: value prepended).
                    237: $newkey: A line which matches this will be replaced with $newkey.$newvalue
                    238: $newvalue: The new value to be placed with the new key.
                    239: 
                    240: =cut
                    241: 
                    242: #################################################################
                    243: #################################################################
                    244: sub modify_config_file {
                    245:     my ($filedata,$section,$newkey,$newvalue)=@_;
                    246:     my ($structure,$sections) = @$filedata;
                    247:     if (! defined($newvalue)) {
                    248:         $newvalue = '';
                    249:     }
                    250:     my $newline = $newkey.$newvalue;
                    251:     #
                    252:     # Determine which array ref gets the item
                    253:     my $target;
                    254:     if (defined($section)) {
                    255:         if (! exists($sections->{$section})) {
                    256:             push(@$structure,'__section__'.$section);
                    257:             $sections->{$section}=[];
                    258:         }
                    259:         $target = $sections->{$section};
                    260:     } else {
                    261:         $target = $structure;
                    262:     }
                    263:     #
                    264:     # Put the item in or update it.
                    265:     my $key_is_new = 1;
                    266:     for (my $i=0;$i<scalar(@$target);$i++) {
                    267:         
                    268:         if ($target->[$i] =~/^$newkey/) {
                    269:             $target->[$i]=$newline;
                    270:             $key_is_new = 0;
                    271:             last;
                    272:         }
                    273:     }
                    274:     if ($key_is_new) {
                    275:         if (! defined($section)) {
                    276:             unshift(@$target,$newline);
                    277:         } else {
                    278:             # No need to put things after a blank line.
1.2       matthew   279:             if (defined($target->[-1]) && $target->[-1] =~ /^\s*$/) {
1.1       matthew   280:                 $target->[-1] = $newline;
                    281:             } else {
                    282:                 push(@$target,$newline);
                    283:             }
                    284:         }
                    285:     }
                    286:     return ($filedata);
                    287: }
                    288: 
                    289: 
                    290: #################################################################
                    291: #################################################################
                    292: 
                    293: =pod
                    294: 
                    295: =back
                    296: 
                    297: =cut
                    298: 
                    299: #################################################################
                    300: #################################################################

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