Annotation of loncom/debugging_tools/modify_config_files.pl, revision 1.13
1.1 matthew 1: #!/usr/bin/perl -w
2: #
3: # The LearningOnline Network
4: #
1.13 ! raeburn 5: # $Id: modify_config_files.pl,v 1.12 2011/05/31 02:55:15 raeburn 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:
1.9 raeburn 39: This script modifies /etc/my.cnf and one of: /etc/yum.conf
1.13 ! raeburn 40: (for CentOS/Scientific Linux/RHEL >=5), /etc/apt/sources.list
! 41: (for Debian/Ubuntu), /etc/sysconfig/rhn/sources (for RHEL4),
! 42: and /etc/yum.repos.d/loncapa.repo (Fedora >= 21).
1.1 matthew 43:
44: =head1 DESCRIPTION
45:
1.13 ! raeburn 46: This script modifies /etc/my.cnf, /etc/yum.conf, /etc/yum.repos.d/loncapa.repo,
! 47: /etc/apt/sources, or /etc/sysconfig/rhn/sources to ensure certain
! 48: parameters are set properly.
! 49:
! 50: The LON-CAPA yum repositories are added to /etc/yum.conf,
! 51: /etc/yum.repos.d/loncapa.repo, /etc/sysconfig/rhn/sources
! 52: and the LON-CAPA apt repositories are added to
1.9 raeburn 53: /etc/apt/sources.list.
54:
1.1 matthew 55: The /etc/my.cnf file is modified to set the wait_timeout to 1 year. Backup
1.9 raeburn 56: copies of each file are made in /etc, /etc/apt, and /etc/sysconfig/rhn, as
57: appropriate.
1.1 matthew 58:
59: =cut
60:
61: use strict;
62: use File::Copy;
1.3 matthew 63: use lib '/home/httpd/lib/perl/';
64: use LONCAPA::Configuration;
65: my $loncapa_config=LONCAPA::Configuration::read_conf('loncapa.conf');
1.1 matthew 66:
1.6 raeburn 67: open(DSH,"$$loncapa_config{'lonDaemons'}/distprobe |");
68: my $dist = <DSH>;
69: chomp($dist);
70: close(DSH);
71:
72: my $yum_status;
1.9 raeburn 73: my $loninst = 'http://install.loncapa.org';
74: my $loninst_re = 'http://install\.loncapa\.org';
75: if ($dist =~ /^fedora(\d+)$/) {
1.13 ! raeburn 76: my $file = '/etc/yum.conf';
1.9 raeburn 77: my $ver = $1;
1.10 raeburn 78: my $gpgchk = '0';
1.9 raeburn 79: my $gpg = "$loninst/versions/fedora/RPM-GPG-KEY-loncapa";
1.13 ! raeburn 80: my $nobackup;
1.9 raeburn 81: if ($ver > 6) {
1.10 raeburn 82: $gpgchk = '1';
1.9 raeburn 83: }
1.13 ! raeburn 84: if ($ver >= 21) {
! 85: $file = '/etc/yum.repos.d/loncapa.repo';
! 86: $nobackup = 1;
! 87: }
1.6 raeburn 88: $yum_status =
1.13 ! raeburn 89: &update_file($file,
1.10 raeburn 90: [{section => 'loncapa-updates-basearch',
1.1 matthew 91: key => 'name=',
1.6 raeburn 92: value => 'Fedora Core $releasever LON-CAPA $basearch Updates',
1.10 raeburn 93: }, {section => 'loncapa-updates-basearch',
1.1 matthew 94: key => 'baseurl=',
1.9 raeburn 95: value => $loninst.'/fedora/linux/loncapa/$releasever/$basearch',
1.10 raeburn 96: }, {section => 'loncapa-updates-basearch',
1.5 matthew 97: key => 'gpgcheck=',
1.9 raeburn 98: value => $gpgchk,
1.10 raeburn 99: }, {section => 'loncapa-updates-basearch',
1.13 ! raeburn 100: key => 'gpgkey=',
1.9 raeburn 101: value => $gpg,
1.1 matthew 102: }, {section => 'loncapa-updates-noarch',
103: key => 'name=',
104: value => 'Fedora Core $releasever LON-CAPA noarch Updates',
105: }, {section => 'loncapa-updates-noarch',
106: key => 'baseurl=',
1.9 raeburn 107: value => $loninst.'/fedora/linux/loncapa/$releasever/noarch',
1.5 matthew 108: }, {section => 'loncapa-updates-noarch',
109: key => 'gpgcheck=',
1.9 raeburn 110: value => $gpgchk,
111: }, {section => 'loncapa-updates-noarch',
1.13 ! raeburn 112: key => 'gpgkey=',
1.9 raeburn 113: value => $gpg,
1.13 ! raeburn 114: }],$nobackup);
1.9 raeburn 115: } elsif ($dist =~ /^(rhes|centos|scientific)(\d+)$/) {
116: my $type = $1;
117: my $ver = $2;
118: my $longver = $ver;
119: if ($type eq 'rhes') {
120: if ($ver == 4) {
121: $longver = '4ES';
122: } elsif ($ver == 5) {
123: $longver = '5Server';
124: }
125: }
126: my %info = (
127: rhes => {
128: title => 'RHEL',
129: path => 'redhat/linux/enterprise/loncapa',
130: gpg => 'versions/redhat/RPM-GPG-KEY-loncapa',
131: gpgchk => 1,
132: },
133: centos => {
134: title => 'CentOS',
135: path => 'centos/loncapa',
136: gpg => 'versions/centos/RPM-GPG-KEY-loncapa',
137: gpgchk => 1,
138: },
139: scientific => {
140: title => 'Scientific Linux',
141: path => 'scientific/loncapa',
142: gpg => 'versions/scientific/RPM-GPG-KEY-loncapa',
143: gpgchk => 1,
144: },
145: );
146: if (ref($info{$type}) eq 'HASH') {
147: if ($ver > 4) {
148: $yum_status =
149: &update_file('/etc/yum.conf',
150: [{section => 'loncapa-updates-basearch',
151: key => 'name=',
152: value => $info{$type}{title}.' $releasever LON-CAPA $basearch Updates',
153: }, {section => "loncapa-updates-basearch",
154: key => 'baseurl=',
155: value => "$loninst/$info{$type}{path}/".'$releasever/$basearch',
156: }, {section => 'loncapa-updates-basearch',
157: key => 'gpgcheck=',
1.11 raeburn 158: value => $info{$type}{gpgchk},
1.10 raeburn 159: }, {section => 'loncapa-updates-basearch',
1.9 raeburn 160: key => 'gpgkey=',
161: value => "$loninst/$info{$type}{gpg}",
162: }, {section => 'loncapa-updates-noarch',
163: key => 'name=',
164: value => $info{$type}{title}.' $releasever LON-CAPA noarch Updates',
165: }, {section => 'loncapa-updates-noarch',
166: key => 'baseurl=',
167: value => "$loninst/$info{$type}{path}/".'$releasever/noarch',
168: }, {section => 'loncapa-updates-noarch',
169: key => 'gpgcheck=',
1.11 raeburn 170: value => $info{$type}{gpgchk},
1.9 raeburn 171: }, {section => 'loncapa-updates-noarch',
172: key => 'gpgkey=',
173: value => "$loninst/$info{$type}{gpg}",
174: }]);
175: } elsif (($type eq 'rhes') && ($ver == 4)) {
176: my %rhn = (
177: basearch => {
178: regexp => '\s*yum\s+loncapa\-updates\-basearch\s+'.$loninst_re.'/'.$info{$type}{path}.'/'.$longver.'/\$ARCH',
179: text => "yum loncapa-updates-basearch $loninst/$info{$type}{path}/$longver/".'$ARCH',
180: },
181: noarch => {
182: regexp => '\s*yum\s+loncapa\-updates\-noarch\s+'.$loninst_re.'/'.$info{$type}{path}.'/'.$longver.'/noarch',
183: text => "yum loncapa-updates-noarch $loninst/$info{$type}{path}/$longver/noarch",
184: },
185: );
186: $yum_status = &update_rhn_source(\%rhn);
187: }
188: }
189: } elsif ($dist =~ /^(debian|ubuntu)\d+$/) {
190: my %apt_get_source = (
191: debian5 => {
192: regexp => '\s*deb\s+'.$loninst_re.'/debian/\s+lenny\s+main',
193: text => "deb $loninst/debian/ lenny main",
194: },
195: ubuntu6 => {
196: regexp => '\s*deb\s+'.$loninst_re.'/ubuntu/\s+dapper\s+main',
197: text => "deb $loninst/ubuntu/ dapper main",
198: },
199: ubuntu8 => {
200: regexp => '\s*deb\s+'.$loninst_re.'/ubuntu/\s+hardy\s+main',
201: text => "deb $loninst/ubuntu/ hardy main",
202: },
203: );
204: my $apt_status;
205: if (defined($apt_get_source{$dist})) {
206: $apt_status = &update_apt_source($apt_get_source{$dist},);
1.6 raeburn 207: }
208: }
1.1 matthew 209:
1.4 matthew 210: my $mysql_global_status =
211: &update_file('/etc/my.cnf',
1.1 matthew 212: [{section =>'mysqld',
1.12 raeburn 213: key =>'wait_timeout=',
1.1 matthew 214: value =>'31536000', }]);
215:
1.3 matthew 216: my $local_my_cnf = '/home/www/.my.cnf';
217: if (! -e $local_my_cnf) {
1.4 matthew 218: # Create a file so we can do something with it...
1.3 matthew 219: system("touch $local_my_cnf");
220: }
1.4 matthew 221: my $mysql_www_status =
222: &update_file($local_my_cnf,
1.3 matthew 223: [{section =>'client',
224: key =>'user=',
225: value =>'www',},
226: {section =>'client',
227: key =>'password=',
228: value =>$loncapa_config->{'lonSqlAccess'}},]);
229:
1.4 matthew 230: my $exitvalue = 0;
231:
232: if ($mysql_global_status) { $exitvalue = 1; }
233:
234: exit $exitvalue;
1.1 matthew 235:
236:
237: sub update_file {
1.13 ! raeburn 238: my ($file,$newdata,$nobackup) = @_;
1.1 matthew 239: return 1 if (! -e $file);
1.13 ! raeburn 240: unless ($nobackup) {
! 241: my $backup = $file.'.backup';
! 242: if (! copy($file,$backup)) {
! 243: warn "**** Error: Unable to make backup of $file";
! 244: return 0;
! 245: }
1.1 matthew 246: }
247: my ($filedata) = &parse_config_file($file);
1.4 matthew 248: if (! ref($filedata)) { warn "**** Error: $filedata"; return 0;}
249: my $modified = 0;
1.1 matthew 250: foreach my $data (@$newdata) {
251: my $section = $data->{'section'};
252: my $key = $data->{'key'};
253: my $value = $data->{'value'};
1.4 matthew 254: my $result = &modify_config_file($filedata,$section,$key,$value);
255: if ($result) { $modified = 1; }
256: }
257: if ($modified) {
258: my $result = &write_config_file($file,$filedata);
259: if (defined($result)) { warn 'Error:'.$result; return 0; }
1.1 matthew 260: }
1.4 matthew 261: return $modified;
1.1 matthew 262: }
263:
264: #################################################################
265: #################################################################
266:
267: =pod
268:
1.9 raeburn 269: =over 4
270:
271: =item &parse_config_file()
1.1 matthew 272:
273: Read a configuration file in and parse it into an internal data structure.
274:
275: Input: filename
276:
277: Output: array ref $filedata OR scalar error message
278:
1.9 raeburn 279: =back
280:
1.1 matthew 281: =cut
282:
283: #################################################################
284: #################################################################
285: sub parse_config_file {
286: my ($file) = @_;
287: open(INFILE,$file) || return ('Unable to open '.$file.' for reading');
288: my @Input = <INFILE>;
289: close(INFILE);
290: my @Structure;
291: my %Sections;
292: while (my $line = shift(@Input)) {
293: chomp($line);
294: if ($line =~ /^\[([^\]]*)\]/) {
295: my $section_id = $1;
296: push(@Structure,'__section__'.$section_id);
297: while ($line = shift(@Input)) {
1.4 matthew 298: chomp($line);
1.1 matthew 299: if ($line =~ /^\[([^\]]*)\]/) {
300: unshift(@Input,$line);
301: last;
302: } else {
303: push(@{$Sections{$section_id}},$line);
304: }
305: }
306: } else {
307: push(@Structure,$line);
308: }
309: }
310: my $filedata = [\@Structure,\%Sections];
311: return $filedata;
312: }
313:
314: #################################################################
315: #################################################################
316:
317: =pod
318:
1.9 raeburn 319: =over 4
320:
1.1 matthew 321: =item
322:
323: Write a configuration file out based on the internal data structure returned
324: by &parse_config_file
325:
326: Inputs: filename, $filedata (the return value of &parse_config_file
327:
328: Returns: undef on success, scalar error message on failure.
329:
1.9 raeburn 330: =back
331:
1.1 matthew 332: =cut
333:
334: #################################################################
335: #################################################################
336: sub write_config_file {
337: my ($file,$filedata) = @_;
338: my ($structure,$sections) = @$filedata;
339: if (! defined($structure) || ! ref($structure)) {
340: return 'Bad subroutine inputs';
341: }
342: open(OUTPUT,'>'.$file) || return('Unable to open '.$file.' for writing');
343: for (my $i=0;$i<scalar(@$structure);$i++) {
344: my $line = $structure->[$i];
345: chomp($line);
346: if ($line =~ /^__section__(.*)$/) {
347: my $section_id = $1;
348: print OUTPUT ('['.$section_id.']'.$/);
349: foreach my $section_line (@{$sections->{$section_id}}) {
350: chomp($section_line);
351: print OUTPUT $section_line.$/;
352: }
353: # Deal with blank lines
354: if ($sections->{$section_id}->[-1] =~ /^\s*$/) {
355: # No need to output a blank line at the end if there is one
356: # already
357: } else {
358: print OUTPUT $/;
359: }
360: } else {
361: print OUTPUT $line.$/;
362: }
363: }
364: close OUTPUT;
365: return undef;
366: }
367:
368: #################################################################
369: #################################################################
370:
371: =pod
372:
1.9 raeburn 373: =over 4
374:
375: =item &modify_config_file()
1.1 matthew 376:
377: Modifies the internal data structure of a configuration file to include new
378: sections and/or new configuration directives.
379:
380: Inputs: $filedata (see &parse_config_file
381: $section, the [section] the new entry is to reside in. A value of undef will
382: cause the "outer" section (as in yum.conf) to be updated (or have the new
383: value prepended).
384: $newkey: A line which matches this will be replaced with $newkey.$newvalue
385: $newvalue: The new value to be placed with the new key.
386:
1.4 matthew 387: Returns: 0 or 1, indicating if the file was modified(1) or not(0).
388:
1.9 raeburn 389: =back
1.4 matthew 390:
1.1 matthew 391: =cut
392:
393: #################################################################
394: #################################################################
395: sub modify_config_file {
396: my ($filedata,$section,$newkey,$newvalue)=@_;
1.4 matthew 397: my $modified = 0; # returned value - set to true if the file is modified
1.1 matthew 398: my ($structure,$sections) = @$filedata;
399: if (! defined($newvalue)) {
400: $newvalue = '';
401: }
402: my $newline = $newkey.$newvalue;
403: #
404: # Determine which array ref gets the item
405: my $target;
406: if (defined($section)) {
407: if (! exists($sections->{$section})) {
408: push(@$structure,'__section__'.$section);
409: $sections->{$section}=[];
410: }
411: $target = $sections->{$section};
412: } else {
413: $target = $structure;
414: }
415: #
416: # Put the item in or update it.
417: my $key_is_new = 1;
418: for (my $i=0;$i<scalar(@$target);$i++) {
419: if ($target->[$i] =~/^$newkey/) {
1.4 matthew 420: if ($target->[$i] ne $newline) {
421: $target->[$i]=$newline;
422: $modified = 1;
423: }
1.1 matthew 424: $key_is_new = 0;
425: last;
426: }
427: }
428: if ($key_is_new) {
429: if (! defined($section)) {
430: unshift(@$target,$newline);
431: } else {
432: # No need to put things after a blank line.
1.2 matthew 433: if (defined($target->[-1]) && $target->[-1] =~ /^\s*$/) {
1.1 matthew 434: $target->[-1] = $newline;
1.4 matthew 435: $modified = 1;
1.1 matthew 436: } else {
437: push(@$target,$newline);
1.4 matthew 438: $modified = 1;
1.1 matthew 439: }
440: }
441: }
1.4 matthew 442: return $modified;
1.1 matthew 443: }
444:
1.9 raeburn 445: #################################################################
446: #################################################################
447:
448: =pod
449:
450: =over 4
451:
452: =item &update_rhn_source()
453:
454: Modifies the Red Hat 4 sources file which includes repositories used by up2date
455:
456: Inputs:
457: $rhn_items - a reference to hash of a hash containing the regular expression
458: to test for, and the text string to append to the file, if an entry for the
459: LON-CAPA RHEL repository is missing for two cases:
460:
461: (a) basearch
462: (b) noarch
463:
464: Returns: 0 or 1, indicating if the file was modified(1) or not(0).
465:
466: =back
467:
468: =cut
469:
470: #################################################################
471: #################################################################
472: sub update_rhn_source {
473: my ($rhn_items) = @_;
474: return 0 if (ref($rhn_items) ne 'HASH');
475: return 0 if ((ref($rhn_items->{basearch}) ne 'HASH') || (ref($rhn_items->{noarch}) ne 'HASH'));
476: my $file = '/etc/sysconfig/rhn/sources';
477: return 0 if (! -e $file);
478: my $backup = $file.'.backup';
479: if (! copy($file,$backup)) {
480: warn "**** Error: Unable to make backup of $file";
481: return 0;
482: }
483: my $result = 0;
484: my $fh;
485: if (open($fh,"<$file")) {
486: my $total = 0;
487: my %found;
488: foreach my $item (keys(%{$rhn_items})) {
489: $found{$item} = 0;
490: }
491: while(<$fh>) {
492: foreach my $item (keys(%{$rhn_items})) {
493: if (ref($rhn_items->{$item}) eq 'HASH') {
494: my $pattern = $rhn_items->{$item}->{regexp};
495: if ($pattern ne '') {
496: if (m{^$pattern}) {
497: $found{$item} = 1;
498: $total ++;
499: }
500: }
501: }
502: }
503: last if $total == 2;
504: }
505: close($fh);
506: if ($total < 2) {
507: if (open($fh,">>$file")) {
508: foreach my $item (keys(%{$rhn_items})) {
509: unless ($found{$item}) {
510: if (ref($rhn_items->{$item}) eq 'HASH') {
511: if ($rhn_items->{$item}->{text} ne '') {
512: print $fh "\n".$rhn_items->{$item}->{text}."\n";
513: $result = 1;
514: }
515: }
516: }
517: }
518: close($fh);
519: }
520: }
521: }
522: return $result;
523: }
1.1 matthew 524:
525: #################################################################
526: #################################################################
527:
528: =pod
529:
1.9 raeburn 530: =over 4
531:
532: =item &update_apt_source()
533:
534: Modifies the source.list file which includes repositories used by apt-get
535:
536: Inputs:
537: $deb_row - a reference to containing the regular expression
538: to test for, and the text string to append to the file, if an entry for the
539: LON-CAPA Debian/ or Ubuntu repository is missing.
540:
541: Returns: 0 or 1, indicating if the file was modified(1) or not(0).
542:
1.1 matthew 543: =back
544:
545: =cut
546:
547: #################################################################
548: #################################################################
1.9 raeburn 549: sub update_apt_source {
550: my ($deb_row) = @_;
551: return 0 if (ref($deb_row) ne 'HASH');
552: return 0 if (($deb_row->{regexp} eq '') || ($deb_row->{text} eq ''));
553: my $file = '/etc/apt/sources.list';
554: return 0 if (! -e $file);
555: my $backup = $file.'.backup';
556: if (! copy($file,$backup)) {
557: warn "**** Error: Unable to make backup of $file";
558: return 0;
559: }
560: my $result = 0;
561: my $fh;
562: if (open($fh,"<$file")) {
563: my $found = 0;
564: my $pattern = $deb_row->{regexp};
565: while(<$fh>) {
566: if (m{^$pattern}) {
567: $found = 1;
568: last;
569: }
570: }
571: close($fh);
572: if (!$found) {
573: if (open($fh,">>$file")) {
574: print $fh "\n".$deb_row->{text}."\n";
575: close($fh);
576: $result = 1;
577: }
578: }
579: }
580: return $result;
581: }
582:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>