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

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

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