77c19ddde5b3edb5e2c423d586079a96dc2fe93e
[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 $buildall = 0;
20
21 my @newrpms;
22
23 my %platconf = ( "fedora-5-i386" => { osver => "fc5",
24                                       kmod => '1',
25                                       basearch => 'i386',
26                                       updaterepo => 'updates-released',
27                                       results => 'fc5/i386' },
28                  "fedora-5-x86_64" => { osver => "fc5",
29                                        kmod => '1',
30                                        basearch => 'x86_64',
31                                        updaterepo => 'updates-released',
32                                        results => 'fc5/x86_64' },
33                  "fedora-6-i386" => { osver => "fc6", 
34                                       kmod => '1', 
35                                       basearch => 'i386',
36                                       updaterepo => 'updates-released',
37                                       results => "fc6/i386" },
38                  "fedora-6-x86_64" => { osver => "fc6",
39                                         kmod => '1',
40                                         basearch => 'x86_64',
41                                         updaterepo => 'updates-released',
42                                         results => "fc6/x86_64" },
43                  "fedora-7-i386" => { osver => "fc7", 
44                                       kmod => '1', 
45                                       basearch => 'i386',
46                                       updaterepo => 'updates-released',
47                                       results => "fc7/i386" },
48                  "fedora-7-x86_64" => { osver => "fc7",
49                                         kmod => '1',
50                                         basearch => 'x86_64',
51                                         updaterepo => 'updates-released',
52                                         results => "fc7/x86_64" },
53                  "fedora-8-i386" => { osver => "fc8", 
54                                       kmod => '1', 
55                                       basearch => 'i386',
56                                       updaterepo => 'updates-released',
57                                       results => "fc8/i386" },
58                  "fedora-8-x86_64" => { osver => "fc8",
59                                         kmod => '1',
60                                         basearch => 'x86_64',
61                                         updaterepo => 'updates-released',
62                                         results => "fc8/x86_64" },
63                  "centos-4-i386" => { osver => "el4",
64                                      kmod => '1',
65                                      basearch => 'i386',
66                                      updaterepo => 'update',
67                                      results => 'el4/i386' },
68                  "centos-4-x86_64" => { osver => "el4",
69                                        kmod => '1',
70                                        basearch => 'x86_64',
71                                        updaterepo => 'update',
72                                        results => "el4/x86_64" },
73                  "centos-5-i386" => { osver => "el5", 
74                                       kmod => '1', 
75                                       basearch => 'i386',
76                                       updaterepo => 'update',
77                                       results => "el5/i386" },
78                  "centos-5-x86_64" => { osver => "el5",
79                                         kmod => '1',
80                                         basearch => 'x86_64',
81                                         updaterepo => 'update',
82                                         results => "el5/x86_64" },
83 #                "fedora-development-i386" => { osver => "fcd",
84 #                                         kmod => '1',
85 #                                         basearch => 'i386',
86 #                                         results => 'fedora-devel/i386'},
87 #                "fedora-development-x86_64" => { osver => "fcd",
88 #                                           kmod => '1',
89 #                                           basearch => 'x86_64',
90 #                                           results => 'fedora-devel/x86_64'} 
91 );
92
93 # The following are kernels that we can't successfully build modules against
94 # due to issues in the packaged kernel-devel RPM.
95
96 my %badkernels = (
97         "2.6.21-2950.fc8" => { "xen" => 1} # Missing build ID
98 );
99
100 my @platforms = @ARGV;
101 my $srpm = pop @platforms;
102
103 if (!$srpm || $#platforms==-1) {
104   print "Usage: $0 <platform> [<platform> [<platform> ...] ]  <srpm>\n";
105   print "    Platform may be:\n";
106   foreach ("all", sort(keys(%platconf))) { print "        ".$_."\n"; };
107   exit(1);
108 }
109
110 my $oafsversion = `rpm -q --queryformat=%{VERSION} -p $srpm` or die $!;
111 chomp $oafsversion;
112 my $oafsrelease = `rpm -q --queryformat=%{RELEASE} -p $srpm` or die $!;
113 chomp $oafsrelease;
114 $oafsrelease=~s/^[^\.]*\.(.*)$/$1/;
115
116 print "Release is $oafsrelease\n";
117
118 if ($platforms[0] eq "all" and $#platforms == 0) {
119   @platforms = keys(%platconf);
120 }
121
122 foreach my $platform (@platforms) {
123   print "*******************************************************************\n";
124   print "Building for $platform\n";
125   die "Unknown platform $platform" if !$platconf{$platform};
126   my $osver = $platconf{$platform}{'osver'};
127   my $root = $rootbase.$platform."/root";
128   my $mockresults = $rootbase.$platform."/result";
129   my $resultdir = $resultbase.$platconf{$platform}{'results'};
130   my $basearch = $platconf{$platform}{'basearch'};
131   my $rpmstashdir = $stashbase.$platconf{$platform}{'results'}."/";
132
133   my $yumcachedir;
134   if (exists($platconf{$platform}{'updaterepo'})) {
135     $yumcachedir = $rootbase."cache/".$platform."/yum_cache/".
136                    $platconf{$platform}{'updaterepo'}."/packages/";
137   }
138
139   File::Path::mkpath($resultdir, 0, 0755);
140
141   if (system($mockcommand, "-r", $platform, "init") != 0) {
142     die "Initialising the root failed: $?\n";
143   }
144
145   my %modulelist;
146
147   print "-------------------------------------------------------------------\n";
148   print "Finding available kernel modules\n";
149
150   my $arbitraryversion = "";
151   my $modules=`$suser -c 'yum --installroot $root provides kernel-devel'`;
152   if ($modules eq "") {
153       $modules=`$suser -c 'yum -d 2 --installroot $root provides kernel-devel'`;
154       my $modulen;
155       my %modulel;
156       foreach $modulen (split(/\n/, $modules)) {
157           my ($pk, $colon, $comment)=split(/\s+/, $modulen);
158           if ($pk =~ /^kernel/) {
159               $modulel{$pk} = "$pk";
160           } 
161       }
162       $modulen=join(" ", keys(%modulel));
163       $modules=`$suser -c 'yum --installroot $root list $modulen'`;
164   }
165   foreach my $module (split(/\n/, $modules)) {
166       my ($package, $version, $repo)=split(/\s+/, $module);
167       my ($arch) = ($package=~/\.(.*)$/);
168       my ($variant) = ($package=~/kernel-(.*)-devel/);
169       $variant = "" if !defined($variant);
170       next if ($package !~ /^kernel/);
171       next if ($arch eq "noarch");
172       next 
173           if (exists($badkernels{$version}) && ($badkernels{$version}{$variant}));
174       if ($platform=~/fedora-5/) {
175           next if ($variant eq "xen0"); # Fedora 5 has some bad xen0 kernel-devels
176           next if ($variant eq "smp");
177       }
178       print "$arch : $variant : $version\n";
179       $modulelist{$arch} ={} if !$modulelist{$arch};
180       $modulelist{$arch}{$version} = {} if !$modulelist{$arch}{$version};
181       $modulelist{$arch}{$version}{$variant} = 1;
182       $arbitraryversion = $version;
183   }
184
185   if (!$arbitraryversion) {
186     die "Unable to find a valid kernel-devel package for $platform\n";
187   }
188
189   print "-------------------------------------------------------------------\n";
190   print "Building the userland RPMs\n";
191   my @rpms = ('', '-authlibs', '-authlibs-devel', '-client', '-compat',
192               '-debuginfo', '-devel', '-docs', '-kernel-source', '-kpasswd',
193               '-krb5', '-server');
194
195   my $missing = 0;
196   foreach my $rpm (@rpms) {
197     if (! -f $resultdir."/openafs".$rpm."-".$oafsversion."-".$osver.".".
198              $oafsrelease.".".$basearch.".rpm") {
199       $missing++;
200       print $resultdir."/openafs".$rpm."-".$oafsversion."-".$osver.".".
201             $oafsrelease.".".$basearch.".rpm is missing!\n"
202     }
203   }
204   if ($missing) {
205     system($mockcommand." -r ".$platform." rebuild ".
206                         ' --define "fedorakmod 1" '.
207                         ' --define "kernvers '.$arbitraryversion.'" '.
208                         ' --define "osvers '.$osver.'" '.
209                         ' --define "build_modules 0" '.
210                         ' --define "build_userspace 1" '.
211                         ' --define "build_authlibs 1" '.
212                         $srpm) == 0
213       or die "build failed with : $!\n";
214     foreach my $rpm (@rpms) {
215       system("cp ".$mockresults."/openafs".$rpm."-".$oafsversion."-".
216                    $osver.".".$oafsrelease.".".$basearch.".rpm ".
217                    $resultdir) == 0
218           or die "Copy failed with : $!\n";
219       push @newrpms, $mockresults."/openafs".$rpm."-".$oafsversion."-".
220                      $osver.".".$oafsrelease.".".$basearch.".rpm";
221     }
222   } else {
223     print "All userland RPMs present for $platform. Skipping build\n";
224   }
225
226    print "-------------------------------------------------------------------\n";
227   print "Building kernel modules\n";
228
229  foreach my $arch (keys(%modulelist)) {
230     foreach my $version (keys(%{$modulelist{$arch}})) {
231       my $kversion = $version;
232       $kversion=~s/-/_/g;
233       my @tobuild;
234
235       if ($buildall) {
236         @tobuild = keys(%{$modulelist{$arch}{$version}});
237       } else {
238         foreach my $variant (keys(%{$modulelist{$arch}{$version}})) {
239           my $dvariant=$variant;
240           $dvariant.="-" if ($dvariant);
241           if (!-f $resultdir."/kmod-openafs-".$dvariant.
242                   $oafsversion."-".$oafsrelease.".".$kversion.".".
243                   $arch.".rpm") {
244             push @tobuild, $variant;
245             print $resultdir."/kmod-openafs-".$dvariant.
246                   $oafsversion."-".$oafsrelease.".".$kversion.".".
247                   $arch.".rpm is missing\n";
248           }
249         }
250       }
251
252       if ($#tobuild>=0) {
253         my $variants = join(" ", map {$_ or '\\\\\"\\\\\"'} @tobuild);
254         print "Building $arch module for kernel $version with variants $variants\n";
255         system ("setarch $arch $mockcommand -r $platform rebuild ".
256                              " --arch ".$arch.
257                              ' --define "fedorakmod 1" '.
258                              ' --define "osvers '.$osver.'" '.
259                              ' --define "kernvers '.$version.'" '.
260                              ' --define "kvariants '.$variants.'" '.
261                              ' --define "build_modules 1" '.
262                              ' --define "build_userspace 0" '.
263                              $srpm) == 0
264           or die "Build failed with : $!\n";
265         foreach my $variant (@tobuild) {
266           if ($variant) {
267             $variant.="-";
268           }
269           system("cp ".$mockresults."/kmod-openafs-".$variant.$oafsversion."-".$oafsrelease.".".$kversion.".".$arch.".rpm $resultdir") == 0
270             or die "Copy failed with : $!\n";
271           push @newrpms, $mockresults."/kmod-openafs-".$variant.$oafsversion."-".$oafsrelease.".".$kversion.".".$arch.".rpm";
272         }
273       } else {
274          print "All kernel modules already built for $version on $arch\n";
275       }
276     }
277   }
278   print "-------------------------------------------------------------------\n";
279   print "Creating repository data\n";
280   system ("cd $resultdir; createrepo .") == 0
281     or die "Unable to build repository data\n";
282
283   if ($yumcachedir) {
284     print "-------------------------------------------------------------------\n";
285     print "Stashing kernel-devel RPMs\n";
286   
287     my $changed;
288     my $dirh = IO::Dir->new($yumcachedir);
289     if (defined($dirh)) {
290       my $file;
291       while (defined($file = $dirh->read)) {
292         if ( $file=~/^kernel-devel/ &&
293               -f $yumcachedir.$file && ! -f $rpmstashdir.$file) {
294           print "Stashing $file for later use\n";
295           system("cp ".$yumcachedir.$file." ".$rpmstashdir.$file) == 0
296             or die "Copy failed with : $!\n";
297           $changed++;
298         }
299       }
300     }
301  
302     if ($changed) {
303       print "Updating stash repodata\n";
304       system ("cd $rpmstashdir; createrepo .") == 0
305         or die "Unable to update RPM stash repository data\n";
306     }
307   }
308 }
309
310 print "=====================================================================\n";
311 print "All builds complete\nBuilt:\n";
312 print join("\n",@newrpms);
313