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