--- loncom/debugging_tools/modify_config_files.pl 2004/08/11 17:32:53 1.3 +++ loncom/debugging_tools/modify_config_files.pl 2004/08/23 19:47:39 1.4 @@ -2,7 +2,7 @@ # # The LearningOnline Network # -# $Id: modify_config_files.pl,v 1.3 2004/08/11 17:32:53 matthew Exp $ +# $Id: modify_config_files.pl,v 1.4 2004/08/23 19:47:39 matthew Exp $ # # Copyright Michigan State University Board of Trustees # @@ -53,7 +53,8 @@ use lib '/home/httpd/lib/perl/'; use LONCAPA::Configuration; my $loncapa_config=LONCAPA::Configuration::read_conf('loncapa.conf'); -&update_file('/etc/yum.conf', +my $yum_status = + &update_file('/etc/yum.conf', [{section => 'loncapa-updates-i386', key => 'name=', value => 'Fedora Core $releasever LON-CAPA i386 Updates', @@ -70,18 +71,19 @@ my $loncapa_config=LONCAPA::Configuratio '$releasever/noarch', }]); -&update_file('/etc/my.cnf', +my $mysql_global_status = + &update_file('/etc/my.cnf', [{section =>'mysqld', key =>'set-variable=wait_timeout=', value =>'31536000', }]); - my $local_my_cnf = '/home/www/.my.cnf'; if (! -e $local_my_cnf) { -# # Create a file so we can do something with it... + # Create a file so we can do something with it... system("touch $local_my_cnf"); } -&update_file($local_my_cnf, +my $mysql_www_status = + &update_file($local_my_cnf, [{section =>'client', key =>'user=', value =>'www',}, @@ -89,7 +91,11 @@ if (! -e $local_my_cnf) { key =>'password=', value =>$loncapa_config->{'lonSqlAccess'}},]); -exit; +my $exitvalue = 0; + +if ($mysql_global_status) { $exitvalue = 1; } + +exit $exitvalue; @@ -109,20 +115,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; } ################################################################# @@ -155,6 +165,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; @@ -237,12 +248,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 = ''; @@ -264,9 +279,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; } @@ -278,12 +295,14 @@ sub modify_config_file { # No need to put things after a blank line. if (defined($target->[-1]) && $target->[-1] =~ /^\s*$/) { $target->[-1] = $newline; + $modified = 1; } else { push(@$target,$newline); + $modified = 1; } } } - return ($filedata); + return $modified; }