afsd: Make mountdir check kernel-specific
[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-12-i386" => { osver => "fc12",
159                                       kmod => '1',
160                                       basearch => 'i386',
161 #                                     updaterepo => "updates-released",
162                                       results => 'fedora-12/i386' },
163                  "fedora-12-x86_64" => { osver => "fc12",
164                                         kmod => "1",
165                                         basearch => "x86_64",
166 #                                       updaterepo => "updates-released",
167                                         results => "fedora-12/x86_64" },
168                  "fedora-13-i386" => { osver => "fc13",
169                                         kmod => "1",
170                                         basearch => "i386",
171 #                                       updaterepo => "updates-released",
172                                         results => "fedora-13/i386" },
173                  "fedora-13-x86_64" => { osver => "fc13",
174                                         kmod => "1",
175                                         basearch => "x86_64",
176 #                                       updaterepo => "updates-released",
177                                         results => "fedora-13/x86_64" },
178
179                  "fedora-development-i386" => { osver => "fcd",
180                                           kmod => '1',
181                                           basearch => 'i386',
182                                           results => 'fedora-devel/i386'},
183                  "fedora-development-x86_64" => { osver => "fcd",
184                                             kmod => '1',
185                                             basearch => 'x86_64',
186                                             results => 'fedora-devel/x86_64'} 
187 );
188
189 # The following are kernels that we can't successfully build modules against
190 # due to issues in the packaged kernel-devel RPM.
191
192 my %badkernels = (
193         "2.6.21-2950.fc8" => { "xen" => 1}, # Missing build ID
194 );
195
196 my $help;
197 my $ok = GetOptions("resultdir=s" => \$resultbase,
198                     "resultlist=s" => \$resultfile,
199                     "help" => \$help);
200
201 my @platforms = @ARGV;
202 my $srpm = pop @platforms;
203
204 if (!$ok || $help || !$srpm || $#platforms==-1) {
205   print "Usage: $0 [options] <platform> [<platform> [...]]  <srpm>\n";
206   print "    Options are : \n";
207   print "         --resultdir <dir>    Location to place output RPMS\n";
208   print "\n";
209   print "    Platform may be:\n";
210   foreach ("all", sort(keys(%platconf))) { print "        ".$_."\n"; };
211   exit(1);
212 }
213
214 my $oafsversion = `rpm -q --queryformat=%{VERSION} -p $srpm` or die $!;
215 chomp $oafsversion;
216 my $oafsrelease = `rpm -q --queryformat=%{RELEASE} -p $srpm` or die $!;
217 chomp $oafsrelease;
218 $oafsrelease=~s/^[^\.]*\.(.*)$/$1/;
219
220 print "Release is $oafsrelease\n";
221
222 if ($platforms[0] eq "all" and $#platforms == 0) {
223   @platforms = keys(%platconf);
224 }
225
226 foreach my $platform (@platforms) {
227   print "*******************************************************************\n";
228   print "Building for $platform\n";
229   die "Unknown platform $platform" if !$platconf{$platform};
230   my $osver = $platconf{$platform}{'osver'};
231   my $root = $rootbase.$platform."/root";
232   my $mockresults = $rootbase.$platform."/result";
233   my $resultdir = $resultbase."/".$platconf{$platform}{'results'};
234   my $basearch = $platconf{$platform}{'basearch'};
235   my $rpmstashdir = $stashbase.$platconf{$platform}{'results'}."/";
236
237   my $yumcachedir;
238   if (exists($platconf{$platform}{'updaterepo'})) {
239     $yumcachedir = $rootbase."cache/".$platform."/yum_cache/".
240                    $platconf{$platform}{'updaterepo'}."/packages/";
241   }
242
243   File::Path::mkpath($resultdir, 0, 0755);
244
245   if (system($mockcommand, "-r", $platform, "init") != 0) {
246     die "Initialising the root failed: $?\n";
247   }
248
249   my %modulelist;
250
251   print "-------------------------------------------------------------------\n";
252   print "Finding available kernel modules\n";
253
254   my $arbitraryversion = "";
255
256   my $modules;
257   if ($platform=~/fedora-development/) {
258     $modules = findKernelModules($root, 0, "kernel-devel");
259   } elsif ($platform=~/centos-4/) {
260     $modules = findKernelModules($root, 0, "kernel-devel", "kernel-smp-devel", 
261                                  "kernel-hugemem-devel", "kernel-xenU-devel");
262   } else {
263     $modules = findKernelModules($root, 0, 'kernel-devel');
264   }
265
266   foreach my $module (split(/\n/, $modules)) {
267       chomp $module;
268       my ($package, $version, $repo)=split(/\s+/, $module);
269       my ($arch) = ($package=~/\.(.*)$/);
270       my ($variant) = ($package=~/kernel-(.*)-devel/);
271       $variant = "" if !defined($variant);
272       next if ($package !~ /^kernel/);
273       next if ($arch eq "noarch");
274       next 
275           if (exists($badkernels{$version}) && ($badkernels{$version}{$variant}));
276       if ($platform=~/fedora-5/) {
277           next if ($variant eq "xen0"); # Fedora 5 has some bad xen0 kernel-devels
278           next if ($variant eq "smp");
279       }
280       next if ($variant =~/debug$/); # Fedora 8 debug kernels are bad
281
282       print "$arch : $variant : $version\n";
283       $modulelist{$arch} ={} if !$modulelist{$arch};
284       $modulelist{$arch}{$version} = {} if !$modulelist{$arch}{$version};
285       $modulelist{$arch}{$version}{$variant} = 1;
286       $arbitraryversion = $version;
287   }
288
289   if (!$arbitraryversion) {
290     die "Unable to find a valid kernel-devel package for $platform\n";
291   }
292
293   print "-------------------------------------------------------------------\n";
294   print "Building the userland RPMs\n";
295   my @rpms = ('openafs', 'openafs-authlibs', 'openafs-authlibs-devel', 
296               'openafs-client', 'openafs-compat', 'openafs-debuginfo', 
297               'openafs-devel', 'openafs-docs', 'openafs-kernel-source', 
298               'openafs-kpasswd', 'openafs-krb5', 'openafs-server',
299               'dkms-openafs');
300   my @missingrpms=();
301
302   my $missing = 0;
303   foreach my $rpm (@rpms) {
304     if (! -f $resultdir."/".$rpm."-".$oafsversion."-".$osver.".".
305              $oafsrelease.".".$basearch.".rpm") {
306       $missing++;
307       print $resultdir."/".$rpm."-".$oafsversion."-".$osver.".".
308             $oafsrelease.".".$basearch.".rpm is missing!\n";
309       push @missingrpms, $rpm;
310     }
311   }
312   if ($missing) {
313     system($mockcommand." -r ".$platform." rebuild ".
314                         ' --define "fedorakmod 1" '.
315                         ' --define "kernvers '.$arbitraryversion.'" '.
316                         ' --define "osvers '.$osver.'" '.
317                         ' --define "build_modules 0" '.
318                         ' --define "build_userspace 1" '.
319                         ' --define "build_authlibs 1" '.
320                         $srpm) == 0
321       or die "build failed with : $!\n";
322     foreach my $rpm (@missingrpms) {
323       system("cp ".$mockresults."/".$rpm."-".$oafsversion."-".
324                    $osver.".".$oafsrelease.".".$basearch.".rpm ".
325                    $resultdir) == 0
326           or die "Copy failed with : $!\n";
327       push @newrpms, $resultdir."/".$rpm."-".$oafsversion."-".
328                      $osver.".".$oafsrelease.".".$basearch.".rpm";
329     }
330   } else {
331     print "All userland RPMs present for $platform. Skipping build\n";
332   }
333
334    print "-------------------------------------------------------------------\n";
335   print "Building kernel modules\n";
336
337  foreach my $arch (keys(%modulelist)) {
338     foreach my $version (keys(%{$modulelist{$arch}})) {
339       my $kversion = $version;
340       $kversion=~s/-/_/g;
341       my @tobuild;
342
343       if ($buildall) {
344         @tobuild = keys(%{$modulelist{$arch}{$version}});
345       } else {
346         foreach my $variant (keys(%{$modulelist{$arch}{$version}})) {
347           my $dvariant=$variant;
348           $dvariant.="-" if ($dvariant);
349           if (!-f $resultdir."/kmod-openafs-".$dvariant.
350                   $oafsversion."-".$oafsrelease.".".$kversion.".".
351                   $arch.".rpm") {
352             my @done = glob ($resultdir."/kmod-openafs-".$dvariant.
353                              $oafsversion."-*.".$kversion.".".$arch.".rpm");
354
355             if ($ignorerelease && $#done>=0) {
356               print "Kernel module for $kversion already exists for an".
357                     "older release. Skipping building it this time.\n";
358             } else {
359               push @tobuild, $variant;
360               print $resultdir."/kmod-openafs-".$dvariant.
361                     $oafsversion."-".$oafsrelease.".".$kversion.".".
362                     $arch.".rpm is missing\n";
363             }
364           }
365         }
366       }
367
368       if ($#tobuild>=0) {
369         my $variants = join(" ", map {$_ or '\\\\\"\\\\\"'} @tobuild);
370         print "Building $arch module for kernel $version with variants $variants\n";
371         system ("setarch $arch $mockcommand -r $platform rebuild ".
372                              " --arch ".$arch.
373                              ' --define "fedorakmod 1" '.
374                              ' --define "osvers '.$osver.'" '.
375                              ' --define "kernvers '.$version.'" '.
376                              ' --define "kvariants '.$variants.'" '.
377                              ' --define "build_modules 1" '.
378                              ' --define "build_userspace 0" '.
379                              $srpm) == 0
380           or die "Build failed with : $!\n";
381         foreach my $variant (@tobuild) {
382           if ($variant) {
383             $variant.="-";
384           }
385           system("cp ".$mockresults."/kmod-openafs-".$variant.$oafsversion."-".$oafsrelease.".".$kversion.".".$arch.".rpm $resultdir") == 0
386             or die "Copy failed with : $!\n";
387           push @newrpms, $resultdir."/kmod-openafs-".$variant.$oafsversion."-".$oafsrelease.".".$kversion.".".$arch.".rpm";
388         }
389       } else {
390          print "All kernel modules already built for $version on $arch\n";
391       }
392     }
393   }
394   print "-------------------------------------------------------------------\n";
395   print "Creating repository data\n";
396   system ("cd $resultdir; createrepo .") == 0
397     or die "Unable to build repository data\n";
398
399   if ($yumcachedir) {
400     print "-------------------------------------------------------------------\n";
401     print "Stashing kernel-devel RPMs\n";
402   
403     my $changed;
404     my $dirh = IO::Dir->new($yumcachedir);
405     if (defined($dirh)) {
406       my $file;
407       while (defined($file = $dirh->read)) {
408         if ( $file=~/^kernel.*devel/ &&
409               -f $yumcachedir.$file && ! -f $rpmstashdir.$file) {
410           print "Stashing $file for later use\n";
411           system("cp ".$yumcachedir.$file." ".$rpmstashdir.$file) == 0
412             or die "Copy failed with : $!\n";
413           $changed++;
414         }
415       }
416     }
417  
418     if ($changed) {
419       print "Updating stash repodata\n";
420       system ("cd $rpmstashdir; createrepo .") == 0
421         or die "Unable to update RPM stash repository data\n";
422     }
423   }
424 }
425
426 print "=====================================================================\n";
427 print "All builds complete\nBuilt:\n";
428 print join("\n",@newrpms);
429 if (defined($resultfile)) {
430   my $resultfh=new IO::File $resultfile, 'w';
431   print $resultfh join("\n",@newrpms);
432 }
433