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