redhat-packaging-updates-20090203
[openafs.git] / src / packaging / RedHat / mockbuild.pl
1 #!/usr/bin/perl
2 # mockbuild - mass RPM build driver for use with mock and many kernel-devel
3 #             RPMs
4 # from Simon Wilkinson for OpenAFS
5 # for "modern" yum, use "showdupesfromrepos=1" in all cfg files in /etc/mock
6
7 use strict;
8 use warnings;
9
10 use Getopt::Long;
11 use File::Path;
12 use IO::Dir;
13
14 my $suser="nsu";
15 my $rootbase="/var/lib/mock/";
16 my $resultbase="/tmp/result/";
17 my $stashbase="/disk/scratch/repository/";
18 my $mockcommand = "/usr/bin/mock";
19 my $resultfile;
20 my $buildall = 0;
21 my $ignorerelease = 1;
22 my @newrpms;
23
24 # Words cannot describe how gross this is. Yum no longer provides usable
25 # output, so we need to call the python interface. At some point this
26 # probably means this script should be entirely rewritten in python,
27 # but this is not that point.
28
29 sub findKernelModules {
30   my ($root, $uname, @modules) = @_;
31
32   my $modlist = join(",",map { "'".$_."'" } @modules);
33   my $python = <<EOS;
34 import yum;
35 import sys;
36 base = yum.YumBase();
37 base.doConfigSetup('$root/etc/yum.conf', '$root');
38 base.doRepoSetup();
39 base.doSackSetup();
40 EOS
41
42   if ($uname) {
43     $python.= <<EOS;
44
45 for pkg, values in base.searchPackageProvides(['kernel-devel-uname-r']).items():
46   if values[0].find('kernel-devel-uname-r = ') != -1:
47     print '%s.%s %s' % (pkg.name, pkg.arch, values[0].replace('kernel-devel-uname-r = ',''));
48
49 EOS
50   } else {
51     $python.= <<EOS;
52
53 print '\\n'.join(['%s.%s %s' % (x.name, x.arch, x.printVer()) for x in base.searchPackageProvides([$modlist]).keys()]);
54
55 EOS
56   }
57
58 #  my $output = `$suser -c "python -c \\\"$python\\\"" `;
59   my $output = `python -c "$python"`;
60
61   die "Python script to figure out available kernels failed : $output" 
62     if $?;
63
64   return $output;
65 }
66
67
68 my %platconf = ( "fedora-5-i386" => { osver => "fc5",
69                                       kmod => '1',
70                                       basearch => 'i386',
71                                       updaterepo => 'updates-released',
72                                       results => 'fedora-5/i386' },
73                  "fedora-5-x86_64" => { osver => "fc5",
74                                        kmod => '1',
75                                        basearch => 'x86_64',
76                                        updaterepo => 'updates-released',
77                                        results => 'fedora-5/x86_64' },
78                  "fedora-6-i386" => { osver => "fc6", 
79                                       kmod => '1', 
80                                       basearch => 'i386',
81                                       updaterepo => 'updates-released',
82                                       results => "fedora-6/i386" },
83                  "fedora-6-x86_64" => { osver => "fc6",
84                                         kmod => '1',
85                                         basearch => 'x86_64',
86                                         updaterepo => 'updates-released',
87                                         results => "fedora-6/x86_64" },
88                  "fedora-7-i386" => { osver => "fc7", 
89                                       kmod => '1', 
90                                       basearch => 'i386',
91                                       updaterepo => 'updates-released',
92                                       results => "fedora-7/i386" },
93                  "fedora-7-x86_64" => { osver => "fc7",
94                                         kmod => '1',
95                                         basearch => 'x86_64',
96                                         updaterepo => 'updates-released',
97                                         results => "fedora-7/x86_64" },
98                  "fedora-8-i386" => { osver => "fc8", 
99                                       kmod => '1', 
100                                       basearch => 'i386',
101                                       updaterepo => 'updates-released',
102                                       results => "fedora-8/i386" },
103                  "fedora-8-x86_64" => { osver => "fc8",
104                                         kmod => '1',
105                                         basearch => 'x86_64',
106                                         updaterepo => 'updates-released',
107                                         results => "fedora-8/x86_64" },
108                  "centos-4-i386" => { osver => "el4",
109                                      kmod => '1',
110                                      basearch => 'i386',
111                                      updaterepo => 'update',
112                                      results => 'rhel4/i386' },
113                  "centos-4-x86_64" => { osver => "el4",
114                                        kmod => '1',
115                                        basearch => 'x86_64',
116                                        updaterepo => 'update',
117                                        results => "rhel4/x86_64" },
118                  "centos-5-i386" => { osver => "el5", 
119                                       kmod => '1', 
120                                       basearch => 'i386',
121                                       updaterepo => 'update',
122                                       results => "rhel5/i386" },
123                  "centos-5-x86_64" => { osver => "el5",
124                                         kmod => '1',
125                                         basearch => 'x86_64',
126                                         updaterepo => 'update',
127                                         results => "rhel5/x86_64" },
128                  "fedora-9-i386" => { osver => "fc9",
129                                       kmod => '1',
130                                       basearch => 'i386',
131                                       updaterepo => "updates-released",
132                                       results => 'fedora-9/i386' },
133                  "fedora-9-x86_64" => { osver => "fc9",
134                                         kmod => "1",
135                                         basearch => "x86_64",
136                                         updaterepo => "updates-released",
137                                         results => "fedora-9/x86_64" },
138                  "fedora-10-i386" => { osver => "fc10",
139                                       kmod => '1',
140                                       basearch => 'i386',
141                                       updaterepo => "updates-released",
142                                       results => 'fedora-10/i386' },
143                  "fedora-10-x86_64" => { osver => "fc10",
144                                         kmod => "1",
145                                         basearch => "x86_64",
146                                         updaterepo => "updates-released",
147                                         results => "fedora-10/x86_64" },
148                  "fedora-11-i386" => { osver => "fc11",
149                                       kmod => '1',
150                                       basearch => 'i386',
151 #                                     updaterepo => "updates-released",
152                                       results => 'fedora-11/i386' },
153                  "fedora-11-x86_64" => { osver => "fc11",
154                                         kmod => "1",
155                                         basearch => "x86_64",
156 #                                       updaterepo => "updates-released",
157                                         results => "fedora-11/x86_64" },
158                  "fedora-development-i386" => { osver => "fcd",
159                                           kmod => '1',
160                                           basearch => 'i386',
161                                           results => 'fedora-devel/i386'},
162                  "fedora-development-x86_64" => { osver => "fcd",
163                                             kmod => '1',
164                                             basearch => 'x86_64',
165                                             results => 'fedora-devel/x86_64'} 
166 );
167
168 # The following are kernels that we can't successfully build modules against
169 # due to issues in the packaged kernel-devel RPM.
170
171 my %badkernels = (
172         "2.6.21-2950.fc8" => { "xen" => 1}, # Missing build ID
173 );
174
175 my $help;
176 my $ok = GetOptions("resultdir=s" => \$resultbase,
177                     "resultlist=s" => \$resultfile,
178                     "help" => \$help);
179
180 my @platforms = @ARGV;
181 my $srpm = pop @platforms;
182
183 if (!$ok || $help || !$srpm || $#platforms==-1) {
184   print "Usage: $0 [options] <platform> [<platform> [...]]  <srpm>\n";
185   print "    Options are : \n";
186   print "         --resultdir <dir>    Location to place output RPMS\n";
187   print "\n";
188   print "    Platform may be:\n";
189   foreach ("all", sort(keys(%platconf))) { print "        ".$_."\n"; };
190   exit(1);
191 }
192
193 my $oafsversion = `rpm -q --queryformat=%{VERSION} -p $srpm` or die $!;
194 chomp $oafsversion;
195 my $oafsrelease = `rpm -q --queryformat=%{RELEASE} -p $srpm` or die $!;
196 chomp $oafsrelease;
197 $oafsrelease=~s/^[^\.]*\.(.*)$/$1/;
198
199 print "Release is $oafsrelease\n";
200
201 if ($platforms[0] eq "all" and $#platforms == 0) {
202   @platforms = keys(%platconf);
203 }
204
205 foreach my $platform (@platforms) {
206   print "*******************************************************************\n";
207   print "Building for $platform\n";
208   die "Unknown platform $platform" if !$platconf{$platform};
209   my $osver = $platconf{$platform}{'osver'};
210   my $root = $rootbase.$platform."/root";
211   my $mockresults = $rootbase.$platform."/result";
212   my $resultdir = $resultbase."/".$platconf{$platform}{'results'};
213   my $basearch = $platconf{$platform}{'basearch'};
214   my $rpmstashdir = $stashbase.$platconf{$platform}{'results'}."/";
215
216   my $yumcachedir;
217   if (exists($platconf{$platform}{'updaterepo'})) {
218     $yumcachedir = $rootbase."cache/".$platform."/yum_cache/".
219                    $platconf{$platform}{'updaterepo'}."/packages/";
220   }
221
222   File::Path::mkpath($resultdir, 0, 0755);
223
224   if (system($mockcommand, "-r", $platform, "init") != 0) {
225     die "Initialising the root failed: $?\n";
226   }
227
228   my %modulelist;
229
230   print "-------------------------------------------------------------------\n";
231   print "Finding available kernel modules\n";
232
233   my $arbitraryversion = "";
234
235   my $modules;
236   if ($platform=~/fedora-development/) {
237     $modules = findKernelModules($root, 0, "kernel-devel");
238   } elsif ($platform=~/centos-4/) {
239     $modules = findKernelModules($root, 0, "kernel-devel", "kernel-smp-devel", 
240                                  "kernel-hugemem-devel", "kernel-xenU-devel");
241   } else {
242     $modules = findKernelModules($root, 0, 'kernel-devel');
243   }
244
245   foreach my $module (split(/\n/, $modules)) {
246       chomp $module;
247       my ($package, $version, $repo)=split(/\s+/, $module);
248       my ($arch) = ($package=~/\.(.*)$/);
249       my ($variant) = ($package=~/kernel-(.*)-devel/);
250       $variant = "" if !defined($variant);
251       next if ($package !~ /^kernel/);
252       next if ($arch eq "noarch");
253       next 
254           if (exists($badkernels{$version}) && ($badkernels{$version}{$variant}));
255       if ($platform=~/fedora-5/) {
256           next if ($variant eq "xen0"); # Fedora 5 has some bad xen0 kernel-devels
257           next if ($variant eq "smp");
258       }
259       if ($platform=~/fedora-8/ || $platform=~/fedora-9/ || $platform=~/fedora-10/ || $platform=~/fedora-development/) {
260           next if ($variant =~/debug$/); # Fedora 8 debug kernels are bad
261       }
262       print "$arch : $variant : $version\n";
263       $modulelist{$arch} ={} if !$modulelist{$arch};
264       $modulelist{$arch}{$version} = {} if !$modulelist{$arch}{$version};
265       $modulelist{$arch}{$version}{$variant} = 1;
266       $arbitraryversion = $version;
267   }
268
269   if (!$arbitraryversion) {
270     die "Unable to find a valid kernel-devel package for $platform\n";
271   }
272
273   print "-------------------------------------------------------------------\n";
274   print "Building the userland RPMs\n";
275   my @rpms = ('openafs', 'openafs-authlibs', 'openafs-authlibs-devel', 
276               'openafs-client', 'openafs-compat', 'openafs-debuginfo', 
277               'openafs-devel', 'openafs-docs', 'openafs-kernel-source', 
278               'openafs-kpasswd', 'openafs-krb5', 'openafs-server',
279               'dkms-openafs');
280   my @missingrpms=();
281
282   my $missing = 0;
283   foreach my $rpm (@rpms) {
284     if (! -f $resultdir."/".$rpm."-".$oafsversion."-".$osver.".".
285              $oafsrelease.".".$basearch.".rpm") {
286       $missing++;
287       print $resultdir."/".$rpm."-".$oafsversion."-".$osver.".".
288             $oafsrelease.".".$basearch.".rpm is missing!\n";
289       push @missingrpms, $rpm;
290     }
291   }
292   if ($missing) {
293     system($mockcommand." -r ".$platform." rebuild ".
294                         ' --define "fedorakmod 1" '.
295                         ' --define "kernvers '.$arbitraryversion.'" '.
296                         ' --define "osvers '.$osver.'" '.
297                         ' --define "build_modules 0" '.
298                         ' --define "build_userspace 1" '.
299                         ' --define "build_authlibs 1" '.
300                         $srpm) == 0
301       or die "build failed with : $!\n";
302     foreach my $rpm (@missingrpms) {
303       system("cp ".$mockresults."/".$rpm."-".$oafsversion."-".
304                    $osver.".".$oafsrelease.".".$basearch.".rpm ".
305                    $resultdir) == 0
306           or die "Copy failed with : $!\n";
307       push @newrpms, $resultdir."/".$rpm."-".$oafsversion."-".
308                      $osver.".".$oafsrelease.".".$basearch.".rpm";
309     }
310   } else {
311     print "All userland RPMs present for $platform. Skipping build\n";
312   }
313
314    print "-------------------------------------------------------------------\n";
315   print "Building kernel modules\n";
316
317  foreach my $arch (keys(%modulelist)) {
318     foreach my $version (keys(%{$modulelist{$arch}})) {
319       my $kversion = $version;
320       $kversion=~s/-/_/g;
321       my @tobuild;
322
323       if ($buildall) {
324         @tobuild = keys(%{$modulelist{$arch}{$version}});
325       } else {
326         foreach my $variant (keys(%{$modulelist{$arch}{$version}})) {
327           my $dvariant=$variant;
328           $dvariant.="-" if ($dvariant);
329           if (!-f $resultdir."/kmod-openafs-".$dvariant.
330                   $oafsversion."-".$oafsrelease.".".$kversion.".".
331                   $arch.".rpm") {
332             my @done = glob ($resultdir."/kmod-openafs-".$dvariant.
333                              $oafsversion."-*.".$kversion.".".$arch.".rpm");
334
335             if ($ignorerelease && $#done>=0) {
336               print "Kernel module for $kversion already exists for an".
337                     "older release. Skipping building it this time.\n";
338             } else {
339               push @tobuild, $variant;
340               print $resultdir."/kmod-openafs-".$dvariant.
341                     $oafsversion."-".$oafsrelease.".".$kversion.".".
342                     $arch.".rpm is missing\n";
343             }
344           }
345         }
346       }
347
348       if ($#tobuild>=0) {
349         my $variants = join(" ", map {$_ or '\\\\\"\\\\\"'} @tobuild);
350         print "Building $arch module for kernel $version with variants $variants\n";
351         system ("setarch $arch $mockcommand -r $platform rebuild ".
352                              " --arch ".$arch.
353                              ' --define "fedorakmod 1" '.
354                              ' --define "osvers '.$osver.'" '.
355                              ' --define "kernvers '.$version.'" '.
356                              ' --define "kvariants '.$variants.'" '.
357                              ' --define "build_modules 1" '.
358                              ' --define "build_userspace 0" '.
359                              $srpm) == 0
360           or die "Build failed with : $!\n";
361         foreach my $variant (@tobuild) {
362           if ($variant) {
363             $variant.="-";
364           }
365           system("cp ".$mockresults."/kmod-openafs-".$variant.$oafsversion."-".$oafsrelease.".".$kversion.".".$arch.".rpm $resultdir") == 0
366             or die "Copy failed with : $!\n";
367           push @newrpms, $resultdir."/kmod-openafs-".$variant.$oafsversion."-".$oafsrelease.".".$kversion.".".$arch.".rpm";
368         }
369       } else {
370          print "All kernel modules already built for $version on $arch\n";
371       }
372     }
373   }
374   print "-------------------------------------------------------------------\n";
375   print "Creating repository data\n";
376   system ("cd $resultdir; createrepo .") == 0
377     or die "Unable to build repository data\n";
378
379   if ($yumcachedir) {
380     print "-------------------------------------------------------------------\n";
381     print "Stashing kernel-devel RPMs\n";
382   
383     my $changed;
384     my $dirh = IO::Dir->new($yumcachedir);
385     if (defined($dirh)) {
386       my $file;
387       while (defined($file = $dirh->read)) {
388         if ( $file=~/^kernel.*devel/ &&
389               -f $yumcachedir.$file && ! -f $rpmstashdir.$file) {
390           print "Stashing $file for later use\n";
391           system("cp ".$yumcachedir.$file." ".$rpmstashdir.$file) == 0
392             or die "Copy failed with : $!\n";
393           $changed++;
394         }
395       }
396     }
397  
398     if ($changed) {
399       print "Updating stash repodata\n";
400       system ("cd $rpmstashdir; createrepo .") == 0
401         or die "Unable to update RPM stash repository data\n";
402     }
403   }
404 }
405
406 print "=====================================================================\n";
407 print "All builds complete\nBuilt:\n";
408 print join("\n",@newrpms);
409 if (defined($resultfile)) {
410   my $resultfh=new IO::File $resultfile, 'w';
411   print $resultfh join("\n",@newrpms);
412 }
413