[LON-CAPA-cvs] cvs: loncom /debugging_tools modify_config_files.pl

raeburn raeburn@source.lon-capa.org
Tue, 14 Jul 2009 03:05:59 -0000


This is a MIME encoded message

--raeburn1247540759
Content-Type: text/plain

raeburn		Tue Jul 14 03:05:59 2009 EDT

  Modified files:              
    /loncom/debugging_tools	modify_config_files.pl 
  Log:
  Add entries for LON-CAPA repositories for CentOS, Scientific Linux (to /etc/yum.conf),
  and for Red Hat 4 (to /etc/sysconfig/rhn/sources), and for Debian, Ubuntu (to 
  /etc/apt/sources.list).
  
  These files are updated if the LON-CAPA repo entry is missing for a particular distro.
  
  
--raeburn1247540759
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20090714030559.txt"

Index: loncom/debugging_tools/modify_config_files.pl
diff -u loncom/debugging_tools/modify_config_files.pl:1.8 loncom/debugging_tools/modify_config_files.pl:1.9
--- loncom/debugging_tools/modify_config_files.pl:1.8	Sat Jan 12 21:15:00 2008
+++ loncom/debugging_tools/modify_config_files.pl	Tue Jul 14 03:05:59 2009
@@ -2,7 +2,7 @@
 #
 # The LearningOnline Network
 #
-# $Id: modify_config_files.pl,v 1.8 2008/01/12 21:15:00 raeburn Exp $
+# $Id: modify_config_files.pl,v 1.9 2009/07/14 03:05:59 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -36,14 +36,21 @@
 
 =head1 SYNOPSIS
 
-This script modifies /etc/yum.conf and /etc/my.cnf.
+This script modifies /etc/my.cnf and one of: /etc/yum.conf 
+(for Fedora/CentOS/Scientific Linux/RHEL5), /etc/apt/sources.list
+(for Debian/Ubuntu) and /etc/sysconfig/rhn/sources (for RHEL4). 
 
 =head1 DESCRIPTION
 
-This script modifies /etc/yum.conf and /etc/my.cnf to ensure certain parameters
-are set properly.  The LON-CAPA yum repositories are added to /etc/yum.conf.
+This script modifies /etc/my.cnf and /etc/yum.conf or /etc/apt/sources
+or /etc/sysconfig/rhn/sources to ensure certain parameters are set
+properly.  The LON-CAPA yum repositories are added to /etc/yum.conf or
+/etc/sysconfig/rhn/sources and the LON-CAPA apt repositories are added to 
+/etc/apt/sources.list.
+
 The /etc/my.cnf file is modified to set the wait_timeout to 1 year.  Backup
-copies of each file are made in /etc.
+copies of each file are made in /etc, /etc/apt, and /etc/sysconfig/rhn, as 
+appropriate.
 
 =cut
 
@@ -59,7 +66,15 @@
 close(DSH);
 
 my $yum_status;
-if ($dist =~ /^fedora\d+$/) {
+my $loninst = 'http://install.loncapa.org';
+my $loninst_re = 'http://install\.loncapa\.org';
+if ($dist =~ /^fedora(\d+)$/) {
+    my $ver = $1;
+    my $gpgchk = 0;
+    my $gpg = "$loninst/versions/fedora/RPM-GPG-KEY-loncapa"; 
+    if ($ver > 6) {
+        $gpgchk = 1;
+    }
     $yum_status =  
         &update_file('/etc/yum.conf',
              [{section => 'loncapa-updates-$basearch',
@@ -67,55 +82,118 @@
                value   => 'Fedora Core $releasever LON-CAPA $basearch Updates',
            }, {section => 'loncapa-updates-$basearch',
                key     => 'baseurl=',
-               value   => 'http://install.loncapa.org/fedora/linux/loncapa/'.
-                   '$releasever/$basearch',
+               value   => $loninst.'/fedora/linux/loncapa/$releasever/$basearch',
            }, {section => 'loncapa-updates-$basearch',
                key     => 'gpgcheck=',
-               value   => '0',
+               value   =>  $gpgchk,
+           }, {section => 'loncapa-updates-$basearch',
+               key     => 'gpg=',
+               value   => $gpg,
            }, {section => 'loncapa-updates-noarch',
                key     => 'name=',
                value   => 'Fedora Core $releasever LON-CAPA noarch Updates',
            }, {section => 'loncapa-updates-noarch',
                key     => 'baseurl=',
-               value   => 'http://install.loncapa.org/fedora/linux/loncapa/'.
-                   '$releasever/noarch',
+               value   => $loninst.'/fedora/linux/loncapa/$releasever/noarch',
            }, {section => 'loncapa-updates-noarch',
                key     => 'gpgcheck=',
-               value   => '0',
+               value   => $gpgchk,
+           }, {section => 'loncapa-updates-noarch',
+               key     => 'gpg=',
+               value   => $gpg,
            }]);
-} elsif ($dist =~ /^rhes(\d+)$/) {
-    if ($1 > 4) {
-        $yum_status =
-            &update_file('/etc/yum.conf',
-                 [{section => 'loncapa-updates-$basearch',
-                   key     => 'name=',
-                   value   => 'RHEL $releasever LON-CAPA $basearch Updates',
-               }, {section => 'loncapa-updates-$basearch',
-                   key     => 'baseurl=',
-                   value   => 'http://install.loncapa.org/redhat/linux/enterprise/loncapa/'.
-                       '$releasever/$basearch',
-               }, {section => 'loncapa-updates-$basearch',
-                   key     => 'gpgcheck=',
-                   value   => '1',
-               }, {section => 'loncapa-updates-$basearch',
-                   key     => 'gpgkey=',
-                   value   => 'http://install.loncapa.org/versions/redhat/'.
-                              'RPM-GPG-KEY-loncapa',
-               }, {section => 'loncapa-updates-noarch',
-                   key     => 'name=',
-                   value   => 'RHEL $releasever LON-CAPA noarch Updates',
-               }, {section => 'loncapa-updates-noarch',
-                   key     => 'baseurl=',
-                   value   => 'http://install.loncapa.org/redhat/linux/enterprise/loncapa/'.
-                       '$releasever/noarch',
-               }, {section => 'loncapa-updates-noarch',
-                   key     => 'gpgcheck=',
-                   value   => '1',
-               }, {section => 'loncapa-updates-noarch',
-                   key     => 'gpgkey=',
-                   value   => 'http://install.loncapa.org/versions/redhat/'.
-                              'RPM-GPG-KEY-loncapa',
-               }]);
+} elsif ($dist =~ /^(rhes|centos|scientific)(\d+)$/) {
+    my $type = $1;
+    my $ver = $2;
+    my $longver = $ver;
+    if ($type eq 'rhes') {
+        if ($ver == 4) {
+            $longver = '4ES';
+        } elsif ($ver == 5) {
+            $longver = '5Server';
+        }
+    }
+    my %info = (
+                 rhes => {
+                           title => 'RHEL',
+                           path => 'redhat/linux/enterprise/loncapa',
+                           gpg => 'versions/redhat/RPM-GPG-KEY-loncapa',
+                           gpgchk => 1,
+                         },
+                 centos => {
+                             title => 'CentOS',
+                             path => 'centos/loncapa',
+                             gpg => 'versions/centos/RPM-GPG-KEY-loncapa',
+                             gpgchk => 1,
+                           },
+                 scientific => {
+                                 title => 'Scientific Linux',
+                                 path => 'scientific/loncapa',
+                                 gpg => 'versions/scientific/RPM-GPG-KEY-loncapa',
+                                 gpgchk => 1,
+                               },
+               );
+    if (ref($info{$type}) eq 'HASH') {
+        if ($ver > 4) {
+            $yum_status =
+                &update_file('/etc/yum.conf',
+                     [{section => 'loncapa-updates-basearch',
+                       key     => 'name=',
+                       value   => $info{$type}{title}.' $releasever LON-CAPA $basearch Updates',
+                      }, {section => "loncapa-updates-basearch",
+                          key     => 'baseurl=',
+                          value   => "$loninst/$info{$type}{path}/".'$releasever/$basearch',
+                      }, {section => 'loncapa-updates-basearch',
+                          key     => 'gpgcheck=',
+                          value   => $info{$type}{gpchk},
+                      }, {section => 'loncapa-updates-$basearch',
+                          key     => 'gpgkey=',
+                          value   => "$loninst/$info{$type}{gpg}",
+                      }, {section => 'loncapa-updates-noarch',
+                          key     => 'name=',
+                          value   => $info{$type}{title}.' $releasever LON-CAPA noarch Updates',
+                      }, {section => 'loncapa-updates-noarch',
+                          key     => 'baseurl=',
+                          value   => "$loninst/$info{$type}{path}/".'$releasever/noarch',
+                      }, {section => 'loncapa-updates-noarch',
+                          key     => 'gpgcheck=',
+                          value   => $info{$type}{gpchk},
+                      }, {section => 'loncapa-updates-noarch',
+                          key     => 'gpgkey=',
+                          value   => "$loninst/$info{$type}{gpg}",
+                      }]);
+        } elsif (($type eq 'rhes') && ($ver == 4)) {
+            my %rhn = (
+                        basearch => { 
+                            regexp => '\s*yum\s+loncapa\-updates\-basearch\s+'.$loninst_re.'/'.$info{$type}{path}.'/'.$longver.'/\$ARCH',
+                            text => "yum loncapa-updates-basearch $loninst/$info{$type}{path}/$longver/".'$ARCH',
+                                    },
+                        noarch =>  {
+                            regexp => '\s*yum\s+loncapa\-updates\-noarch\s+'.$loninst_re.'/'.$info{$type}{path}.'/'.$longver.'/noarch',
+                            text => "yum loncapa-updates-noarch $loninst/$info{$type}{path}/$longver/noarch",
+                                   },
+                      );
+            $yum_status = &update_rhn_source(\%rhn); 
+        }
+    }
+} elsif ($dist =~ /^(debian|ubuntu)\d+$/) {
+    my %apt_get_source = (
+                           debian5 => {
+                                        regexp => '\s*deb\s+'.$loninst_re.'/debian/\s+lenny\s+main',
+                                        text   => "deb $loninst/debian/ lenny main",
+                                      },
+                           ubuntu6 => {
+                                        regexp => '\s*deb\s+'.$loninst_re.'/ubuntu/\s+dapper\s+main',
+                                        text   => "deb $loninst/ubuntu/ dapper main",
+                                      },
+                           ubuntu8 => {
+                                        regexp => '\s*deb\s+'.$loninst_re.'/ubuntu/\s+hardy\s+main',
+                                        text   => "deb $loninst/ubuntu/ hardy main",
+                                      },
+                         );
+    my $apt_status;
+    if (defined($apt_get_source{$dist})) {
+        $apt_status = &update_apt_source($apt_get_source{$dist},);
     }
 }
 
@@ -146,18 +224,6 @@
 exit $exitvalue;
 
 
-
-#################################################################
-#################################################################
-
-=pod
-
-=over 4
-
-=cut
-
-#################################################################
-#################################################################
 sub update_file {
     my ($file,$newdata) = @_;
     return 1 if (! -e $file);
@@ -188,7 +254,9 @@
 
 =pod
 
-=item &parse_config_file
+=over 4
+
+=item &parse_config_file()
 
 Read a configuration file in and parse it into an internal data structure.
 
@@ -196,6 +264,8 @@
 
 Output: array ref $filedata  OR  scalar error message
 
+=back 
+
 =cut
 
 #################################################################
@@ -234,6 +304,8 @@
 
 =pod
 
+=over 4
+
 =item
 
 Write a configuration file out based on the internal data structure returned
@@ -243,6 +315,8 @@
 
 Returns: undef on success, scalar error message on failure.
 
+=back
+
 =cut
 
 #################################################################
@@ -284,7 +358,9 @@
 
 =pod
 
-=item &modify_config_file
+=over 4
+
+=item &modify_config_file()
 
 Modifies the internal data structure of a configuration file to include new
 sections and/or new configuration directives.
@@ -298,6 +374,7 @@
 
 Returns: 0 or 1, indicating if the file was modified(1) or not(0).
 
+=back 
 
 =cut
 
@@ -353,15 +430,141 @@
     return $modified;
 }
 
+#################################################################
+#################################################################
+
+=pod
+
+=over 4
+
+=item &update_rhn_source()
+
+Modifies the Red Hat 4 sources file which includes repositories used by up2date 
+
+Inputs: 
+$rhn_items - a reference to hash of a hash containing the regular expression
+to test for, and the text string to append to the file, if an entry for the 
+LON-CAPA RHEL repository is missing for two cases:
+
+(a) basearch
+(b) noarch 
+
+Returns: 0 or 1, indicating if the file was modified(1) or not(0).
+
+=back
+
+=cut
+
+#################################################################
+#################################################################
+sub update_rhn_source {
+    my ($rhn_items) = @_;
+    return 0 if (ref($rhn_items) ne 'HASH');
+    return 0 if ((ref($rhn_items->{basearch}) ne 'HASH') || (ref($rhn_items->{noarch}) ne 'HASH'));
+    my $file = '/etc/sysconfig/rhn/sources';
+    return 0 if (! -e $file);
+    my $backup = $file.'.backup';
+    if (! copy($file,$backup)) {
+        warn "**** Error: Unable to make backup of $file";
+        return 0;
+    }
+    my $result = 0;
+    my $fh;
+    if (open($fh,"<$file")) {
+        my $total = 0;
+        my %found;
+        foreach my $item (keys(%{$rhn_items})) {
+            $found{$item} = 0;
+        }
+        while(<$fh>) {
+            foreach my $item (keys(%{$rhn_items})) {
+                if (ref($rhn_items->{$item}) eq 'HASH') {
+                    my $pattern = $rhn_items->{$item}->{regexp};
+                    if ($pattern ne '') { 
+                        if (m{^$pattern}) {
+                            $found{$item} = 1;
+                            $total ++;
+                        }
+                    }
+                }
+            }
+            last if $total == 2;
+        }
+        close($fh);
+        if ($total < 2) {
+            if (open($fh,">>$file")) {
+                foreach my $item (keys(%{$rhn_items})) {
+                    unless ($found{$item}) {
+                        if (ref($rhn_items->{$item}) eq 'HASH') {
+                            if ($rhn_items->{$item}->{text} ne '') {
+                                print $fh "\n".$rhn_items->{$item}->{text}."\n";
+                                $result = 1;
+                            }
+                        }
+                    }
+                }
+                close($fh);
+            }
+        }
+    }
+    return $result;
+}
 
 #################################################################
 #################################################################
 
 =pod
 
+=over 4
+
+=item &update_apt_source()
+
+Modifies the source.list file which includes repositories used by apt-get
+
+Inputs:
+$deb_row - a reference to containing the regular expression
+to test for, and the text string to append to the file, if an entry for the
+LON-CAPA Debian/ or Ubuntu repository is missing.
+
+Returns: 0 or 1, indicating if the file was modified(1) or not(0).
+
 =back
 
 =cut
 
 #################################################################
 #################################################################
+sub update_apt_source {
+    my ($deb_row) = @_;
+    return 0 if (ref($deb_row) ne 'HASH');
+    return 0 if (($deb_row->{regexp} eq '') || ($deb_row->{text} eq ''));
+    my $file = '/etc/apt/sources.list';
+    return 0 if (! -e $file);
+    my $backup = $file.'.backup';
+    if (! copy($file,$backup)) {
+        warn "**** Error: Unable to make backup of $file";
+        return 0;
+    }
+    my $result = 0;
+    my $fh;
+    if (open($fh,"<$file")) {
+        my $found = 0;
+        my $pattern = $deb_row->{regexp};
+        while(<$fh>) {
+            if (m{^$pattern}) {
+                $found = 1;
+                last;
+            }
+        }
+        close($fh);
+        if (!$found) {
+            if (open($fh,">>$file")) {
+                print $fh "\n".$deb_row->{text}."\n";
+                close($fh);
+                $result = 1;
+            }
+        }
+    }
+    return $result;
+}
+

--raeburn1247540759--