--- loncom/debugging_tools/modify_config_files.pl 2004/08/09 21:05:40 1.1 +++ loncom/debugging_tools/modify_config_files.pl 2007/04/24 13:09:40 1.7 @@ -2,7 +2,7 @@ # # The LearningOnline Network # -# $Id: modify_config_files.pl,v 1.1 2004/08/09 21:05:40 matthew Exp $ +# $Id: modify_config_files.pl,v 1.7 2007/04/24 13:09:40 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -49,15 +49,29 @@ copies of each file are made in /etc. use strict; use File::Copy; - -&update_file('/etc/yum.conf', - [{section => 'loncapa-updates-i386', +use lib '/home/httpd/lib/perl/'; +use LONCAPA::Configuration; +my $loncapa_config=LONCAPA::Configuration::read_conf('loncapa.conf'); + +open(DSH,"$$loncapa_config{'lonDaemons'}/distprobe |"); +my $dist = ; +chomp($dist); +close(DSH); + +my $yum_status; +if ($dist =~ /^fedora\d+$/) { + $yum_status = + &update_file('/etc/yum.conf', + [{section => 'loncapa-updates-$basearch', key => 'name=', - value => 'Fedora Core $releasever LON-CAPA i386 Updates', - }, {section => 'loncapa-updates-i386', + value => 'Fedora Core $releasever LON-CAPA $basearch Updates', + }, {section => 'loncapa-updates-$basearch', key => 'baseurl=', value => 'http://install.loncapa.org/fedora/linux/loncapa/'. - '$releasever/i386', + '$releasever/$basearch', + }, {section => 'loncapa-updates-$basearch', + key => 'gpgcheck=', + value => '0', }, {section => 'loncapa-updates-noarch', key => 'name=', value => 'Fedora Core $releasever LON-CAPA noarch Updates', @@ -65,14 +79,71 @@ use File::Copy; key => 'baseurl=', value => 'http://install.loncapa.org/fedora/linux/loncapa/'. '$releasever/noarch', + }, {section => 'loncapa-updates-noarch', + key => 'gpgcheck=', + value => '0', }]); +} elsif ($dist =~ /^rhes(\d+)$/) { + if ($1 > 4) { + $yum_status = + &update_file('/etc/yum.conf', + [{section => 'loncapa-updates-$basearch', + key => 'name=', + value => 'RHEL $releasever LON-CAPA $basearch Updates', + }, {section => 'loncapa-updates-$basearch', + key => 'baseurl=', + value => 'http://install.loncapa.org/redhat/linux/loncapa/'. + '$releasever/$basearch', + }, {section => 'loncapa-updates-$basearch', + key => 'gpgcheck=', + value => '1', + }, {section => 'loncapa-updates-$basearch', + key => 'gpgkey=', + value => 'http://install.loncapa.org/versions/redhat/'. + 'RPM-GPG-KEY-loncapa', + }, {section => 'loncapa-updates-noarch', + key => 'name=', + value => 'RHEL $releasever LON-CAPA noarch Updates', + }, {section => 'loncapa-updates-noarch', + key => 'baseurl=', + value => 'http://install.loncapa.org/redhat/linux/loncapa/'. + '$releasever/noarch', + }, {section => 'loncapa-updates-noarch', + key => 'gpgcheck=', + value => '1', + }, {section => 'loncapa-updates-noarch', + key => 'gpgkey=', + value => 'http://install.loncapa.org/versions/redhat/'. + 'RPM-GPG-KEY-loncapa', + }]); + } +} -&update_file('/etc/my.cnf', +my $mysql_global_status = + &update_file('/etc/my.cnf', [{section =>'mysqld', key =>'set-variable=wait_timeout=', value =>'31536000', }]); -exit; +my $local_my_cnf = '/home/www/.my.cnf'; +if (! -e $local_my_cnf) { + # Create a file so we can do something with it... + system("touch $local_my_cnf"); +} +my $mysql_www_status = + &update_file($local_my_cnf, + [{section =>'client', + key =>'user=', + value =>'www',}, + {section =>'client', + key =>'password=', + value =>$loncapa_config->{'lonSqlAccess'}},]); + +my $exitvalue = 0; + +if ($mysql_global_status) { $exitvalue = 1; } + +exit $exitvalue; @@ -92,20 +163,24 @@ sub update_file { return 1 if (! -e $file); my $backup = $file.'.backup'; if (! copy($file,$backup)) { - warn "Error: Unable to make backup of $file"; + warn "**** Error: Unable to make backup of $file"; return 0; } my ($filedata) = &parse_config_file($file); - if (! ref($filedata)) { warn "Error: $filedata"; return 0;} + if (! ref($filedata)) { warn "**** Error: $filedata"; return 0;} + my $modified = 0; foreach my $data (@$newdata) { my $section = $data->{'section'}; my $key = $data->{'key'}; my $value = $data->{'value'}; - &modify_config_file($filedata,$section,$key,$value) + my $result = &modify_config_file($filedata,$section,$key,$value); + if ($result) { $modified = 1; } + } + if ($modified) { + my $result = &write_config_file($file,$filedata); + if (defined($result)) { warn 'Error:'.$result; return 0; } } - my $result = &write_config_file($file,$filedata); - if (defined($result)) { warn 'Error:'.$result; return 0; } - return 1; + return $modified; } ################################################################# @@ -138,6 +213,7 @@ sub parse_config_file { my $section_id = $1; push(@Structure,'__section__'.$section_id); while ($line = shift(@Input)) { + chomp($line); if ($line =~ /^\[([^\]]*)\]/) { unshift(@Input,$line); last; @@ -220,12 +296,16 @@ value prepended). $newkey: A line which matches this will be replaced with $newkey.$newvalue $newvalue: The new value to be placed with the new key. +Returns: 0 or 1, indicating if the file was modified(1) or not(0). + + =cut ################################################################# ################################################################# sub modify_config_file { my ($filedata,$section,$newkey,$newvalue)=@_; + my $modified = 0; # returned value - set to true if the file is modified my ($structure,$sections) = @$filedata; if (! defined($newvalue)) { $newvalue = ''; @@ -247,9 +327,11 @@ sub modify_config_file { # Put the item in or update it. my $key_is_new = 1; for (my $i=0;$i[$i] =~/^$newkey/) { - $target->[$i]=$newline; + if ($target->[$i] ne $newline) { + $target->[$i]=$newline; + $modified = 1; + } $key_is_new = 0; last; } @@ -259,14 +341,16 @@ sub modify_config_file { unshift(@$target,$newline); } else { # No need to put things after a blank line. - if ($target->[-1] =~ /^\s*$/) { + if (defined($target->[-1]) && $target->[-1] =~ /^\s*$/) { $target->[-1] = $newline; + $modified = 1; } else { push(@$target,$newline); + $modified = 1; } } } - return ($filedata); + return $modified; }