Annotation of loncom/debugging_tools/modify_config_files.pl, revision 1.17
1.1 matthew 1: #!/usr/bin/perl -w
2: #
3: # The LearningOnline Network
4: #
1.17 ! raeburn 5: # $Id: modify_config_files.pl,v 1.16 2019/10/02 22:28:24 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 => {
205: regexp => '\s*deb\s+'.$loninst_re.'/debian/\s+lenny\s+main',
206: text => "deb $loninst/debian/ lenny main",
207: },
208: ubuntu6 => {
209: regexp => '\s*deb\s+'.$loninst_re.'/ubuntu/\s+dapper\s+main',
210: text => "deb $loninst/ubuntu/ dapper main",
211: },
212: ubuntu8 => {
213: regexp => '\s*deb\s+'.$loninst_re.'/ubuntu/\s+hardy\s+main',
214: text => "deb $loninst/ubuntu/ hardy main",
215: },
1.14 raeburn 216: ubuntu10 => {
217: regexp => '\s*deb\s+'.$loninst_re.'/ubuntu/\s+lucid\s+main',
218: text => "deb $loninst/ubuntu/ lucid main",
219: },
220: ubuntu12 => {
221: regexp => '\s*deb\s+'.$loninst_re.'/ubuntu/\s+precise\s+main',
222: text => "deb $loninst/ubuntu/ precise main",
223: },
224: ubuntu14 => {
225: regexp => '\s*deb\s+'.$loninst_re.'/ubuntu/\s+trusty\s+main',
226: text => "deb $loninst/ubuntu/ trusty main",
227: },
228: ubuntu16 => {
229: regexp => '\s*deb\s+'.$loninst_re.'/ubuntu/\s+xenial\s+main',
230: text => "deb $loninst/ubuntu/ xenial main",
231: },
232: ubuntu18 => {
233: regexp => '\s*deb\s+'.$loninst_re.'/ubuntu/\s+bionic\s+main',
234: text => "deb $loninst/ubuntu/ bionic main",
235: },
1.17 ! raeburn 236: ubuntu20 => {
! 237: regexp => '\s*deb\s+'.$loninst_re.'/ubuntu/\s+focal\s+main',
! 238: text => "deb $loninst/ubuntu/ focal main",
! 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.4 matthew 247: my $mysql_global_status =
248: &update_file('/etc/my.cnf',
1.1 matthew 249: [{section =>'mysqld',
1.12 raeburn 250: key =>'wait_timeout=',
1.1 matthew 251: value =>'31536000', }]);
252:
1.3 matthew 253: my $local_my_cnf = '/home/www/.my.cnf';
254: if (! -e $local_my_cnf) {
1.4 matthew 255: # Create a file so we can do something with it...
1.3 matthew 256: system("touch $local_my_cnf");
257: }
1.4 matthew 258: my $mysql_www_status =
259: &update_file($local_my_cnf,
1.3 matthew 260: [{section =>'client',
261: key =>'user=',
262: value =>'www',},
263: {section =>'client',
264: key =>'password=',
265: value =>$loncapa_config->{'lonSqlAccess'}},]);
266:
1.4 matthew 267: my $exitvalue = 0;
268:
269: if ($mysql_global_status) { $exitvalue = 1; }
270:
271: exit $exitvalue;
1.1 matthew 272:
273:
274: sub update_file {
1.13 raeburn 275: my ($file,$newdata,$nobackup) = @_;
1.1 matthew 276: return 1 if (! -e $file);
1.13 raeburn 277: unless ($nobackup) {
278: my $backup = $file.'.backup';
279: if (! copy($file,$backup)) {
280: warn "**** Error: Unable to make backup of $file";
281: return 0;
282: }
1.1 matthew 283: }
284: my ($filedata) = &parse_config_file($file);
1.4 matthew 285: if (! ref($filedata)) { warn "**** Error: $filedata"; return 0;}
286: my $modified = 0;
1.1 matthew 287: foreach my $data (@$newdata) {
288: my $section = $data->{'section'};
289: my $key = $data->{'key'};
290: my $value = $data->{'value'};
1.4 matthew 291: my $result = &modify_config_file($filedata,$section,$key,$value);
292: if ($result) { $modified = 1; }
293: }
294: if ($modified) {
295: my $result = &write_config_file($file,$filedata);
296: if (defined($result)) { warn 'Error:'.$result; return 0; }
1.1 matthew 297: }
1.4 matthew 298: return $modified;
1.1 matthew 299: }
300:
301: #################################################################
302: #################################################################
303:
304: =pod
305:
1.9 raeburn 306: =over 4
307:
308: =item &parse_config_file()
1.1 matthew 309:
310: Read a configuration file in and parse it into an internal data structure.
311:
312: Input: filename
313:
314: Output: array ref $filedata OR scalar error message
315:
1.9 raeburn 316: =back
317:
1.1 matthew 318: =cut
319:
320: #################################################################
321: #################################################################
322: sub parse_config_file {
323: my ($file) = @_;
324: open(INFILE,$file) || return ('Unable to open '.$file.' for reading');
325: my @Input = <INFILE>;
326: close(INFILE);
327: my @Structure;
328: my %Sections;
329: while (my $line = shift(@Input)) {
330: chomp($line);
331: if ($line =~ /^\[([^\]]*)\]/) {
332: my $section_id = $1;
333: push(@Structure,'__section__'.$section_id);
334: while ($line = shift(@Input)) {
1.4 matthew 335: chomp($line);
1.1 matthew 336: if ($line =~ /^\[([^\]]*)\]/) {
337: unshift(@Input,$line);
338: last;
339: } else {
340: push(@{$Sections{$section_id}},$line);
341: }
342: }
343: } else {
344: push(@Structure,$line);
345: }
346: }
347: my $filedata = [\@Structure,\%Sections];
348: return $filedata;
349: }
350:
351: #################################################################
352: #################################################################
353:
354: =pod
355:
1.9 raeburn 356: =over 4
357:
1.1 matthew 358: =item
359:
360: Write a configuration file out based on the internal data structure returned
361: by &parse_config_file
362:
363: Inputs: filename, $filedata (the return value of &parse_config_file
364:
365: Returns: undef on success, scalar error message on failure.
366:
1.9 raeburn 367: =back
368:
1.1 matthew 369: =cut
370:
371: #################################################################
372: #################################################################
373: sub write_config_file {
374: my ($file,$filedata) = @_;
375: my ($structure,$sections) = @$filedata;
376: if (! defined($structure) || ! ref($structure)) {
377: return 'Bad subroutine inputs';
378: }
379: open(OUTPUT,'>'.$file) || return('Unable to open '.$file.' for writing');
380: for (my $i=0;$i<scalar(@$structure);$i++) {
381: my $line = $structure->[$i];
382: chomp($line);
383: if ($line =~ /^__section__(.*)$/) {
384: my $section_id = $1;
385: print OUTPUT ('['.$section_id.']'.$/);
386: foreach my $section_line (@{$sections->{$section_id}}) {
387: chomp($section_line);
388: print OUTPUT $section_line.$/;
389: }
390: # Deal with blank lines
391: if ($sections->{$section_id}->[-1] =~ /^\s*$/) {
392: # No need to output a blank line at the end if there is one
393: # already
394: } else {
395: print OUTPUT $/;
396: }
397: } else {
398: print OUTPUT $line.$/;
399: }
400: }
401: close OUTPUT;
402: return undef;
403: }
404:
405: #################################################################
406: #################################################################
407:
408: =pod
409:
1.9 raeburn 410: =over 4
411:
412: =item &modify_config_file()
1.1 matthew 413:
414: Modifies the internal data structure of a configuration file to include new
415: sections and/or new configuration directives.
416:
417: Inputs: $filedata (see &parse_config_file
418: $section, the [section] the new entry is to reside in. A value of undef will
419: cause the "outer" section (as in yum.conf) to be updated (or have the new
420: value prepended).
421: $newkey: A line which matches this will be replaced with $newkey.$newvalue
422: $newvalue: The new value to be placed with the new key.
423:
1.4 matthew 424: Returns: 0 or 1, indicating if the file was modified(1) or not(0).
425:
1.9 raeburn 426: =back
1.4 matthew 427:
1.1 matthew 428: =cut
429:
430: #################################################################
431: #################################################################
432: sub modify_config_file {
433: my ($filedata,$section,$newkey,$newvalue)=@_;
1.4 matthew 434: my $modified = 0; # returned value - set to true if the file is modified
1.1 matthew 435: my ($structure,$sections) = @$filedata;
436: if (! defined($newvalue)) {
437: $newvalue = '';
438: }
439: my $newline = $newkey.$newvalue;
440: #
441: # Determine which array ref gets the item
442: my $target;
443: if (defined($section)) {
444: if (! exists($sections->{$section})) {
445: push(@$structure,'__section__'.$section);
446: $sections->{$section}=[];
447: }
448: $target = $sections->{$section};
449: } else {
450: $target = $structure;
451: }
452: #
453: # Put the item in or update it.
454: my $key_is_new = 1;
455: for (my $i=0;$i<scalar(@$target);$i++) {
456: if ($target->[$i] =~/^$newkey/) {
1.4 matthew 457: if ($target->[$i] ne $newline) {
458: $target->[$i]=$newline;
459: $modified = 1;
460: }
1.1 matthew 461: $key_is_new = 0;
462: last;
463: }
464: }
465: if ($key_is_new) {
466: if (! defined($section)) {
467: unshift(@$target,$newline);
468: } else {
469: # No need to put things after a blank line.
1.2 matthew 470: if (defined($target->[-1]) && $target->[-1] =~ /^\s*$/) {
1.1 matthew 471: $target->[-1] = $newline;
1.4 matthew 472: $modified = 1;
1.1 matthew 473: } else {
474: push(@$target,$newline);
1.4 matthew 475: $modified = 1;
1.1 matthew 476: }
477: }
478: }
1.4 matthew 479: return $modified;
1.1 matthew 480: }
481:
1.9 raeburn 482: #################################################################
483: #################################################################
484:
485: =pod
486:
487: =over 4
488:
489: =item &update_rhn_source()
490:
491: Modifies the Red Hat 4 sources file which includes repositories used by up2date
492:
493: Inputs:
494: $rhn_items - a reference to hash of a hash containing the regular expression
495: to test for, and the text string to append to the file, if an entry for the
496: LON-CAPA RHEL repository is missing for two cases:
497:
498: (a) basearch
499: (b) noarch
500:
501: Returns: 0 or 1, indicating if the file was modified(1) or not(0).
502:
503: =back
504:
505: =cut
506:
507: #################################################################
508: #################################################################
509: sub update_rhn_source {
510: my ($rhn_items) = @_;
511: return 0 if (ref($rhn_items) ne 'HASH');
512: return 0 if ((ref($rhn_items->{basearch}) ne 'HASH') || (ref($rhn_items->{noarch}) ne 'HASH'));
513: my $file = '/etc/sysconfig/rhn/sources';
514: return 0 if (! -e $file);
515: my $backup = $file.'.backup';
516: if (! copy($file,$backup)) {
517: warn "**** Error: Unable to make backup of $file";
518: return 0;
519: }
520: my $result = 0;
521: my $fh;
522: if (open($fh,"<$file")) {
523: my $total = 0;
524: my %found;
525: foreach my $item (keys(%{$rhn_items})) {
526: $found{$item} = 0;
527: }
528: while(<$fh>) {
529: foreach my $item (keys(%{$rhn_items})) {
530: if (ref($rhn_items->{$item}) eq 'HASH') {
531: my $pattern = $rhn_items->{$item}->{regexp};
532: if ($pattern ne '') {
533: if (m{^$pattern}) {
534: $found{$item} = 1;
535: $total ++;
536: }
537: }
538: }
539: }
540: last if $total == 2;
541: }
542: close($fh);
543: if ($total < 2) {
544: if (open($fh,">>$file")) {
545: foreach my $item (keys(%{$rhn_items})) {
546: unless ($found{$item}) {
547: if (ref($rhn_items->{$item}) eq 'HASH') {
548: if ($rhn_items->{$item}->{text} ne '') {
549: print $fh "\n".$rhn_items->{$item}->{text}."\n";
550: $result = 1;
551: }
552: }
553: }
554: }
555: close($fh);
556: }
557: }
558: }
559: return $result;
560: }
1.1 matthew 561:
562: #################################################################
563: #################################################################
564:
565: =pod
566:
1.9 raeburn 567: =over 4
568:
569: =item &update_apt_source()
570:
571: Modifies the source.list file which includes repositories used by apt-get
572:
573: Inputs:
574: $deb_row - a reference to containing the regular expression
575: to test for, and the text string to append to the file, if an entry for the
576: LON-CAPA Debian/ or Ubuntu repository is missing.
577:
578: Returns: 0 or 1, indicating if the file was modified(1) or not(0).
579:
1.1 matthew 580: =back
581:
582: =cut
583:
584: #################################################################
585: #################################################################
1.9 raeburn 586: sub update_apt_source {
587: my ($deb_row) = @_;
588: return 0 if (ref($deb_row) ne 'HASH');
589: return 0 if (($deb_row->{regexp} eq '') || ($deb_row->{text} eq ''));
590: my $file = '/etc/apt/sources.list';
591: return 0 if (! -e $file);
592: my $backup = $file.'.backup';
593: if (! copy($file,$backup)) {
594: warn "**** Error: Unable to make backup of $file";
595: return 0;
596: }
597: my $result = 0;
598: my $fh;
599: if (open($fh,"<$file")) {
600: my $found = 0;
601: my $pattern = $deb_row->{regexp};
602: while(<$fh>) {
603: if (m{^$pattern}) {
604: $found = 1;
605: last;
606: }
607: }
608: close($fh);
609: if (!$found) {
610: if (open($fh,">>$file")) {
611: print $fh "\n".$deb_row->{text}."\n";
612: close($fh);
613: $result = 1;
614: }
615: }
616: }
617: return $result;
618: }
619:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>