source: ProjectBuilder/devel/pb-modules/lib/ProjectBuilder/Distribution.pm@ 2334

Last change on this file since 2334 was 2334, checked in by Bruno Cornec, 6 years ago

Greatly improve apk build (still not finished however)

File size: 28.6 KB
Line 
1#!/usr/bin/perl -w
2#
3# Creates common environment for distributions
4#
5# Copyright B. Cornec 2007-2016
6# Eric Anderson's changes are (c) Copyright 2012 Hewlett Packard
7# Provided under the GPL v2
8#
9# $Id$
10#
11
12package ProjectBuilder::Distribution;
13
14use strict;
15use Data::Dumper;
16use Carp 'confess';
17use ProjectBuilder::Version;
18use ProjectBuilder::Base;
19use ProjectBuilder::Conf;
20use File::Basename;
21use File::Copy;
22# requires perl 5.004 minimum in VM/VE
23use File::Compare;
24
25# Global vars
26# Inherit from the "Exporter" module which handles exporting functions.
27
28use vars qw($VERSION $REVISION $PBCONFVER @ISA @EXPORT);
29use Exporter;
30
31# Export, by default, all the functions into the namespace of
32# any code which uses this module.
33
34our @ISA = qw(Exporter);
35our @EXPORT = qw(pb_distro_init pb_distro_conffile pb_distro_sysconffile pb_distro_get pb_distro_getlsb pb_distro_installdeps pb_distro_getdeps pb_distro_only_deps_needed pb_distro_setuprepo pb_distro_setuposrepo pb_distro_setuprepo_gen pb_distro_get_param pb_distro_get_context pb_distro_to_keylist pb_distro_conf_print pb_apply_conf_proxy);
36($VERSION,$REVISION,$PBCONFVER) = pb_version_init();
37
38=pod
39
40=head1 NAME
41
42ProjectBuilder::Distribution, part of the project-builder.org - module dealing with distribution detection
43
44=head1 DESCRIPTION
45
46This modules provides functions to allow detection of Linux distributions, and giving back some attributes concerning them.
47
48=head1 SYNOPSIS
49
50 use ProjectBuilder::Distribution;
51
52 #
53 # Return information on the running distro
54 #
55 my $pbos = pb_distro_get_context();
56 print "distro tuple: ".Dumper($pbos->name, $pbos->ver, $pbos->fam, $pbos->type, $pbos->pbsuf, $pbos->pbupd, $pbos->pbins, $pbos->arch)."\n";
57 #
58 # Return information on the requested distro
59 #
60 my $pbos = pb_distro_get_context("ubuntu-7.10-x86_64");
61 print "distro tuple: ".Dumper($pbos->name, $pbos->ver, $pbos->fam, $pbos->type, $pbos->pbsuf, $pbos->pbupd, $pbos->pbins, $pbos->arch)."\n";
62 #
63 # Return information on the running distro
64 #
65 my ($ddir,$dver) = pb_distro_get();
66
67=head1 USAGE
68
69=over 4
70
71=item B<pb_distro_conffile>
72
73This function returns the mandatory configuration file used for distribution/OS detection
74
75=cut
76
77sub pb_distro_conffile {
78
79if ($PBCONFVER < 1) {
80 return("CCCC/pb.conf");
81} else {
82 return("CCCC/pb.yml");
83}
84}
85
86=item B<pb_distro_sysconffile>
87
88This function returns the optional configuration file used for local customization
89
90=cut
91
92sub pb_distro_sysconffile {
93
94if ($PBCONFVER < 1) {
95 return("SSSS/pb.conf");
96} else {
97 return("SSSS/pb.yml");
98}
99}
100
101
102=item B<pb_distro_init>
103
104This function returns a hash of parameters indicating the distribution name, version, family, type of build system, suffix of packages, update command line, installation command line and architecture of the underlying Linux distribution. The value of the fields may be "unknown" in case the function was unable to recognize on which distribution it is running.
105
106As an example, Ubuntu and Debian are in the same "du" family. As well as RedHat, RHEL, CentOS, fedora are on the same "rh" family.
107Mandriva, Open SuSE and Fedora have all the same "rpm" type of build system. Ubuntu and Debian have the same "deb" type of build system.
108And "fc" is the extension generated for all Fedora packages (Version will be added by pb).
109All this information is stored in an external configuration file typically at /etc/pb/pb.yml
110
111When passing the distribution name and version as parameters, the B<pb_distro_init> function returns the parameter of that distribution instead of the underlying one.
112
113Cf: http://linuxmafia.com/faq/Admin/release-files.html
114Ideas taken from http://search.cpan.org/~kerberus/Linux-Distribution-0.14/lib/Linux/Distribution.pm
115
116=cut
117
118
119sub pb_distro_init {
120
121my $pbos = {
122 'name' => undef,
123 'version' => undef,
124 'arch' => undef,
125 'family' => "unknown",
126 'suffix' => "unknown",
127 'update' => "unknown",
128 'install' => "unknown",
129 'type' => "unknown",
130 'os' => "unknown",
131 'nover' => "false",
132 'rmdot' => "false",
133 'useminor' => "false",
134 };
135$pbos->{'name'} = shift;
136$pbos->{'version'} = shift;
137$pbos->{'arch'} = shift;
138
139# Adds conf file for distribution description
140# the location of the conf file is finalyzed at install time
141# depending whether we deal with package install or tar file install
142
143pb_conf_add(pb_distro_sysconffile());
144
145# Similarly for the local file available for sysadmin. After the previous one to allow overwrite to work
146pb_conf_add(pb_distro_conffile());
147
148# If we don't know which distribution we're on, then guess it
149($pbos->{'name'},$pbos->{'version'}) = pb_distro_get() if ((not defined $pbos->{'name'}) || (not defined $pbos->{'version'}));
150
151# For some rare cases, typically nover ones
152$pbos->{'name'} = "unknown" if (not defined $pbos->{'name'});
153$pbos->{'version'} = "unknown" if (not defined $pbos->{'version'});
154
155# Initialize arch
156$pbos->{'arch'} = pb_get_arch() if (not defined $pbos->{'arch'});
157# Solves a bug on Red Hat 6.x where real arch is not detected when using setarch and a chroot
158# As it was only i386 forcing it here.
159$pbos->{'arch'} = "i386" if (($pbos->{'name'} eq "redhat") && ($pbos->{'version'} =~ /^6\./));
160
161# Dig into the tuple to find the best answer
162# Do NOT factorize here, as it won't work as of now for hash creation
163# Do NOT change order without caution
164$pbos->{'useminor'} = pb_distro_get_param($pbos,pb_conf_get("osuseminorrel"));
165$pbos->{'family'} = pb_distro_get_param($pbos,pb_conf_get("osfamily"));
166$pbos->{'type'} = pb_distro_get_param($pbos,pb_conf_get("ostype"));
167($pbos->{'os'},$pbos->{'install'},$pbos->{'suffix'},$pbos->{'nover'},$pbos->{'rmdot'},$pbos->{'update'}) = pb_distro_get_param($pbos,pb_conf_get("os","osins","ossuffix","osnover","osremovedotinver","osupd"));
168($pbos->{'localinstall'}) = pb_distro_get_param($pbos,pb_conf_get_if("oslocalins"));
169#($pbos->{'family'},$pbos->{'type'},$pbos->{'os'},$pbos->{'install'},$pbos->{'suffix'},$pbos->{'nover'},$pbos->{'rmdot'},$pbos->{'update'}) = pb_distro_get_param($pbos,pb_conf_get("osfamily","ostype","os","osins","ossuffix","osnover","osremovedotinver","osupd"));
170
171# Some OS have no interesting version
172$pbos->{'version'} = "nover" if ((defined $pbos->{'nover'}) && ($pbos->{'nover'} eq "true"));
173
174# For some OS remove the . in version name for extension
175my $dver2 = $pbos->{'version'};
176$dver2 =~ s/\.//g if ((defined $pbos->{'rmdot'}) && ($pbos->{'rmdot'} eq "true"));
177
178if ((not defined $pbos->{'suffix'}) || ($pbos->{'suffix'} eq "")) {
179 # By default suffix is a concatenation of name and version
180 $pbos->{'suffix'} = ".$pbos->{'name'}$dver2"
181} else {
182 # concat just the version to what has been found
183 $pbos->{'suffix'} = ".$pbos->{'suffix'}$dver2";
184}
185
186# if ($arch eq "x86_64") {
187# $opt="--exclude=*.i?86";
188# }
189pb_log(2,"DEBUG: pb_distro_init: ".Dumper($pbos)."\n");
190
191return($pbos);
192}
193
194=item B<pb_distro_get>
195
196This function returns a list of 2 parameters indicating the distribution name and version of the underlying Linux distribution. The value of those 2 fields may be "unknown" in case the function was unable to recognize on which distribution it is running.
197
198On my home machine it would currently report ("mandriva","2010.2").
199
200=cut
201
202sub pb_distro_get {
203
204# 1: List of files that unambiguously indicates what distro we have
205# 2: List of files that ambiguously indicates what distro we have
206# 3: Should have the same keys as the previous one. If ambiguity, which other distributions should be checked
207# 4: Matching Rg. Expr to detect distribution and version
208my ($single_rel_files, $ambiguous_rel_files,$distro_similar,$distro_match,$nover) = pb_conf_get("osrelfile","osrelambfile","osambiguous","osrelexpr","osnover");
209
210my $release;
211my $distro;
212
213# Begin to test presence of non-ambiguous files
214# that way we reduce the choice
215my ($d,$r);
216while (($d,$r) = each %$single_rel_files) {
217 if (defined $ambiguous_rel_files->{$d}) {
218 print STDERR "The key $d is considered as both unambiguous and ambigous.\n";
219 print STDERR "Please fix your configuration file.\n"
220 }
221 if (-f "$r" && ! -l "$r") {
222 my $tmp=pb_get_content("$r");
223 # Found the only possibility.
224 # Try to get version and return
225 if (defined ($distro_match->{$d})) {
226 ($release) = $tmp =~ m/$distro_match->{$d}/m;
227 } else {
228 if (not defined ($nover->{$d})) {
229 print STDERR "Unable to find $d version in $r (non-ambiguous)\n";
230 print STDERR "Please report to the maintainer bruno_at_project-builder.org\n";
231 }
232 $release = "unknown";
233 }
234 return($d,$release);
235 }
236}
237
238# Now look at ambiguous files
239# Ubuntu before 10.04 includes a /etc/debian_version file that creates an ambiguity with debian
240# So we need to look at distros in reverse alphabetic order to treat ubuntu always first via lsb
241my $found = 0;
242foreach $d (reverse keys %$ambiguous_rel_files) {
243 $r = $ambiguous_rel_files->{$d};
244 if (-f "$r" && !-l "$r") {
245 # Found one possibility.
246 # Get all distros concerned by that file
247 my $tmp=pb_get_content("$r");
248 my $ptr = $distro_similar->{$d};
249 pb_log(2,"amb: ".Dumper($ptr)."\n");
250 $release = "unknown";
251 foreach my $dd (split(/,/,$ptr)) {
252 pb_log(2,"check $dd\n");
253 # Try to check pattern
254 if (defined $distro_match->{$dd}) {
255 pb_log(2,"cmp: $distro_match->{$dd} - vs - $tmp\n");
256 ($release) = $tmp =~ m/$distro_match->{$dd}/m;
257 if ((defined $release) && ($release ne "unknown")) {
258 $distro = $dd;
259 $found = 1;
260 last;
261 }
262 }
263 }
264 last if ($found == 1);
265 }
266}
267if ($found == 0) {
268 print STDERR "Unable to find a version in ".join(' ',keys %$ambiguous_rel_files)." (ambiguous)\n";
269 print STDERR "Please report to the maintainer bruno_at_project-builder.org\n";
270 return("unknown","unknown");
271} else {
272 return($distro,$release);
273}
274}
275
276=item B<pb_distro_getlsb>
277
278This function returns the 5 lsb values LSB version, distribution ID, Description, release and codename.
279As entry it takes an optional parameter to specify whether the output is short or not.
280
281=cut
282
283sub pb_distro_getlsb {
284
285my $s = shift;
286pb_log(3,"Entering pb_distro_getlsb\n");
287
288my ($ambiguous_rel_files) = pb_conf_get("osrelambfile");
289my $lsbf = $ambiguous_rel_files->{"lsb"};
290
291# LSB has not been configured.
292if (not defined $lsbf) {
293 print STDERR "no lsb entry defined for osrelambfile\n";
294 confess "You modified upstream delivery and lost !\n";
295}
296
297if (-r $lsbf) {
298 my $rep = pb_get_content($lsbf);
299 # Create elementary fields
300 my ($c, $r, $d, $i, $l) = ("", "", "", "", "");
301 for my $f (split(/\n/,$rep)) {
302 pb_log(3,"Reading file part ***$f***\n");
303 $c = $f if ($f =~ /^DISTRIB_CODENAME/);
304 $c =~ s/DISTRIB_CODENAME=/Codename:\t/;
305 $r = $f if ($f =~ /^DISTRIB_RELEASE/);
306 $r =~ s/DISTRIB_RELEASE=/Release:\t/;
307 $d = $f if ($f =~ /^DISTRIB_DESCRIPTION/);
308 $d =~ s/DISTRIB_DESCRIPTION=/Description:\t/;
309 $d =~ s/"//g;
310 $i = $f if ($f =~ /^DISTRIB_ID/);
311 $i =~ s/DISTRIB_ID=/Distributor ID:\t/;
312 $l = $f if ($f =~ /^LSB_VERSION/);
313 $l =~ s/LSB_VERSION=/LSB Version:\t/;
314 }
315 my $regexp = "^[A-z ]*:[\t ]*";
316 $c =~ s/$regexp// if (defined $s);
317 $r =~ s/$regexp// if (defined $s);
318 $d =~ s/$regexp// if (defined $s);
319 $i =~ s/$regexp// if (defined $s);
320 $l =~ s/$regexp// if (defined $s);
321 return($l, $i, $d, $r, $c);
322} else {
323 print STDERR "Unable to read $lsbf file\n";
324 confess "Please report to the maintainer bruno_at_project-builder.org\n";
325}
326}
327
328# Internal function
329
330sub pb_apply_conf_proxy {
331my ($pbos) = @_;
332
333my $ftp_proxy = pb_distro_get_param($pbos,pb_conf_get_if("ftp_proxy"));
334my $http_proxy = pb_distro_get_param($pbos,pb_conf_get_if("http_proxy"));
335my $https_proxy = pb_distro_get_param($pbos,pb_conf_get_if("https_proxy"));
336
337# We do not overwrite shell settings
338$ENV{'ftp_proxy'} ||= $ftp_proxy if ((defined $ftp_proxy) && ($ftp_proxy ne ""));
339$ENV{'http_proxy'} ||= $http_proxy if ((defined $http_proxy) && ($http_proxy ne ""));
340$ENV{'https_proxy'} ||= $https_proxy if ((defined $https_proxy) && ($https_proxy ne ""));
341}
342
343=item B<pb_distro_installdeps>
344
345This function install the dependencies required to build the package on a distro.
346If $forcerepo is defined then do not assume packages are alredy installed, but reinstall them
347(useful if you add a repo which contains more up to date packages that you need)
348Dependencies can be passed as the 4th parameter in which case they are not computed
349
350=cut
351
352sub pb_distro_installdeps {
353
354# SPEC file
355my $f = shift;
356my $pbos = shift;
357my $forcerepo = shift;
358my $deps = shift; # optional list of deps to install
359my $local = shift; # optional should we install local packages or remote (for deb command is different)
360
361# Protection
362confess "Missing install command for $pbos->{name}-$pbos->{version}-$pbos->{arch}" unless (defined $pbos->{install} && $pbos->{install} =~ /\w/);
363pb_apply_conf_proxy($pbos);
364pb_log(1, "ftp_proxy=$ENV{'ftp_proxy'}\n") if (defined $ENV{'ftp_proxy'});
365pb_log(1, "http_proxy=$ENV{'http_proxy'}\n") if (defined $ENV{'http_proxy'});
366pb_log(1, "https_proxy=$ENV{'https_proxy'}\n") if (defined $ENV{'https_proxy'});
367
368# Get dependencies in the build file if not forced
369$deps = pb_distro_getdeps($f,$pbos, $forcerepo) if ((not defined $deps) || (defined $forcerepo));
370pb_log(2,"deps: $deps\n");
371return if ((not defined $deps) || ($deps =~ /^\s*$/));
372
373# This may not be // proof. We should test for availability of repo and sleep if not
374my $cmd = "$pbos->{'install'} $deps";
375$cmd = "$pbos->{'localinstall'} $deps" if ((defined $local) && (defined $pbos->{'localinstall'}) && ($pbos->{'localinstall'} !~ /[ ]*/));
376my $ret = pb_system($cmd, "Installing dependencies ($cmd)","mayfail");
377# Try to accomodate deficient proxies
378if ($ret != 0) {
379 pb_system($cmd, "Re-trying installing dependencies ($cmd)");
380}
381# Check that all deps have been installed correctly
382# This time we don't forcerepo to avoid getting a list as a return as we have
383# already forced it previously, and this time we just want to check
384$deps = pb_distro_getdeps($f, $pbos, undef);
385confess "Some dependencies did not install ($deps)" if ((defined $deps) && ($deps =~ /\S/) && ($Global::pb_stop_on_error));
386}
387
388=item B<pb_distro_getdeps>
389
390This function computes the dependencies indicated in the build file and return them as a string of packages to install
391
392=cut
393
394sub pb_distro_getdeps {
395
396my $f = shift;
397my $pbos = shift;
398my $forcerepo = shift;
399
400my $regexp = "";
401my $deps = "";
402my $sep = $/;
403
404# Protection
405return("") if (not defined $pbos->{'type'});
406return("") if (not defined $f);
407
408pb_log(3,"entering pb_distro_getdeps: $pbos->{'type'} - $f\n");
409if ($pbos->{'type'} eq "rpm") {
410 # In RPM this could include files, but we do not handle them atm.
411 $regexp = '^BuildRequires:(.*)$';
412} elsif ($pbos->{'type'} eq "deb") {
413 $regexp = '^Build-Depends:(.*)$';
414} elsif ($pbos->{'type'} eq "apk") {
415 $regexp = '^makedepends=(.*)$';
416} elsif ($pbos->{'type'} eq "ebuild") {
417 $sep = '"'.$/;
418 $regexp = '^DEPEND="(.*)"\n';
419} else {
420 # No idea
421 return("");
422}
423pb_log(2,"regexp: $regexp\n");
424
425# Preserve separator before using the one we need
426my $oldsep = $/;
427$/ = $sep;
428open(DESC,"$f") || confess "Unable to open $f";
429while (<DESC>) {
430 pb_log(4,"read: $_\n");
431 next if (! /$regexp/);
432 chomp();
433
434 my $nextline;
435 # Support multi-lines deps for .deb
436 if ($pbos->{type} eq 'deb') {
437 while ($nextline = <DESC>) {
438 last unless $nextline =~ /^\s+(.+)$/o;
439 $_ .= $1;
440 }
441 }
442
443 # What we found with the regexp is the list of deps.
444 pb_log(2,"found deps: $_\n");
445 s/$regexp/$1/i;
446 pb_log(4,"found deps 1: $_\n");
447 # Remove conditions in the middle and at the end for deb
448 s/\([><=]+[^,]*,/,/g;
449 pb_log(4,"found deps 2: $_\n");
450 s/\([><=]+[^,]*$//g;
451 pb_log(4,"found deps 3: $_\n");
452 # Same for rpm
453 s/[><=]+[^,]*,/,/g;
454 pb_log(4,"found deps 4: $_\n");
455 s/[><=]+.*$//g;
456 pb_log(4,"found deps 5: $_\n");
457 # Improve string format (remove , and spaces at start, end and in double
458 s/,/ /g;
459 pb_log(4,"found deps 6: $_\n");
460 s/^\s*//;
461 pb_log(4,"found deps 7: $_\n");
462 # $ here removes the \n
463 s/\s*$//;
464 pb_log(4,"found deps 8: $_\n");
465 s/\s+/ /g;
466 pb_log(4,"found deps 9: $_\n");
467 $deps .= " ".$_;
468 pb_log(4,"found deps end: $deps\n");
469
470 # Support multi-lines deps for .deb (fwup)
471 if (defined $nextline) {
472 $_ = $nextline;
473 redo;
474 }
475}
476close(DESC);
477$/ = $oldsep;
478pb_log(2,"now deps: $deps\n");
479if (defined $forcerepo) {
480 # We want to force installation of all pkgs
481 # because a repo was setup in between, which may contains updated versions
482 pb_log(0,"Forcing installation of all packages due to previous repo setup\n");
483 return($deps);
484} else {
485 pb_log(0,"Installation of only necessary packages\n");
486 my $deps2 = pb_distro_only_deps_needed($pbos,$deps);
487 return($deps2);
488}
489}
490
491
492=item B<pb_distro_only_deps_needed>
493
494This function returns only the dependencies not yet installed
495
496=cut
497
498sub pb_distro_only_deps_needed {
499
500my $pbos = shift;
501my $deps = shift;
502
503return("") if ((not defined $deps) || ($deps =~ /^\s*$/));
504my $deps2 = "";
505# Avoid to install what is already there
506delete $ENV{'COLUMNS'};
507foreach my $p (split(/\s+/,$deps)) {
508 next if $p =~ /^\s*$/o;
509 if ($pbos->{'type'} eq "rpm") {
510 my $rpmcmd = "rpm -q --whatprovides --quiet";
511 # whatprovides doesn't work for RH6.2
512 $rpmcmd = "rpm -q --quiet" if (($pbos->{'name'} eq "redhat") && ($pbos->{'version'} =~ /6/));
513 my $res = pb_system("$rpmcmd $p","Looking for $p","mayfail");
514 next if ($res eq 0);
515 pb_log(1, "INFO: missing dependency $p\n");
516 } elsif ($pbos->{'type'} eq "deb") {
517 my $res = pb_system("dpkg -L $p","Looking for $p","mayfail");
518 next if ($res eq 0);
519 open(CMD,"dpkg -l $p |") or confess "Unable to run dpkg -l $p: $!";
520 my $ok = 0;
521 while (<CMD>) {
522 $ok = 1 if /^ii\s+$p/;
523 }
524 close(CMD);
525 next if $ok;
526 pb_log(1, "INFO: missing dependency $p\n");
527 } elsif ($pbos->{'type'} eq "ebuild") {
528 } elsif ($pbos->{'type'} eq "apk") {
529 my $res = pb_system("apk info $p","Looking for $p","mayfail");
530 next if ($res eq 0);
531 pb_log(1, "INFO: missing dependency $p\n");
532 } else {
533 # Not reached
534 }
535 pb_log(2,"found deps2: $p\n");
536 $deps2 .= " $p";
537}
538
539$deps2 =~ s/^\s*//;
540pb_log(2,"now deps2: $deps2\n");
541return($deps2);
542}
543
544=item B<pb_distro_setuposrepo>
545
546This function sets up potential additional repository for the setup phase
547
548=cut
549
550sub pb_distro_setuposrepo {
551
552my $pbos = shift;
553
554return(pb_distro_setuprepo_gen_conf($pbos,pb_distro_conffile(),"osrepo"));
555}
556
557=item B<pb_distro_setuprepo>
558
559This function sets up potential additional repository to the build environment
560
561=cut
562
563sub pb_distro_setuprepo {
564
565my $pbos = shift;
566
567return(pb_distro_setuprepo_gen_conf($pbos,"$ENV{'PBDESTDIR'}/pbrc.yml","addrepo"));
568}
569
570# Internal
571sub pb_distro_compare_repo {
572
573my $src = shift;
574my $dest = shift;
575
576if (not -f $dest) {
577 pb_log(1, "INFO: Creating new file $dest\n");
578} elsif (-f $dest && -s $dest == 0) {
579 pb_log(1, "INFO: Overwriting empty file $dest\n");
580} elsif (-f $dest && compare("$src", $dest) == 0) {
581 pb_log(1, "INFO: Overwriting identical file $dest\n");
582} else {
583 pb_log(0, "ERROR: destination file $dest exists and is different than source $src\n");
584 pb_system("cat $dest","INFO: Dest...\n","verbose");
585 pb_system("cat $src","INFO: New...\n","verbose");
586 pb_log(1, "INFO: Returning...\n");
587 return(1);
588}
589return(0);
590}
591
592=item B<pb_distro_setuprepo_gen_conf>
593
594This function sets up in a generic way potential additional repository using conf files
595
596=cut
597
598sub pb_distro_setuprepo_gen_conf {
599
600my $pbos = shift;
601my $pbconf = shift;
602my $pbkey = shift;
603
604return undef if (not defined $pbconf);
605return undef if (not defined $pbkey);
606my ($addrepo) = pb_conf_read($pbconf,$pbkey);
607return undef if (not defined $addrepo);
608
609my $param = pb_distro_get_param($pbos,$addrepo);
610return undef if ($param eq "");
611
612return(pb_distro_setuprepo_gen($pbos,$param));
613}
614
615
616=item B<pb_distro_setuprepo_gen>
617
618This function sets up in a generic way potential additional repository passed as a param
619
620=cut
621
622sub pb_distro_setuprepo_gen {
623
624my $pbos = shift;
625my $param = shift;
626
627return if (not defined $param);
628
629pb_apply_conf_proxy($pbos);
630
631# Loop on the list of additional repo
632foreach my $i (split(/,/,$param)) {
633
634 pb_log(1,"Adding repository defined by $i\n");
635 my ($scheme, $account, $host, $port, $path) = pb_get_uri($i);
636 my $bn = basename($i);
637
638 # The repo file can be local or remote. download or copy at the right place
639 if (($scheme eq "ftp") || ($scheme =~ /http/)) {
640 pb_system("wget -O $ENV{'PBTMP'}/$bn $i","Downloading additional repository file $i");
641 } else {
642 copy($i,"$ENV{'PBTMP'}/$bn");
643 }
644
645 # The repo file can be a real file or a package
646 if ($pbos->{'type'} eq "rpm") {
647 if ($bn =~ /\.rpm$/) {
648 my $pn = $bn;
649 $pn =~ s/\.rpm//;
650 if (pb_system("rpm -q --quiet $pn","","mayfail") != 0) {
651 pb_system("sudo rpm -Uvh $ENV{'PBTMP'}/$bn","Adding package $bn to setup repository");
652 }
653 } elsif ($bn =~ /\.repo$/) {
654 my $dirdest = "";
655 my $reponame = "";
656 # TODO: could go in pb.yml in fact
657 if ($pbos->{install} =~ /\byum\b/) {
658 $reponame="yum";
659 $dirdest = "/etc/yum.repos.d";
660 } elsif ($pbos->{install} =~ /\bdnf\b/) {
661 $reponame="dnf";
662 $dirdest = "/etc/yum.repos.d";
663 } elsif ($pbos->{install} =~ /\bzypper\b/) {
664 $reponame="zypper";
665 $dirdest = "/etc/zypp/repos.d";
666 } else {
667 confess "Unknown location for repository file for '$pbos->{install}' command";
668 }
669 my $dest = "$dirdest/$bn";
670 return undef if (pb_distro_compare_repo("$ENV{'PBTMP'}/$bn",$dest) == 1);
671 confess "Missing directory $dirdest ($reponame)" unless (-d $dirdest);
672 pb_system("sudo mv $ENV{'PBTMP'}/$bn $dest","Adding $reponame repository") if (not -f "$dest");
673 # OpenSUSE does't seem to import keys automatically
674 # :-(
675 if ($pbos->{install} =~ /\bzypper\b/) {
676 my $keyfile = undef;
677 open(REPO,"$dest") || confess "Unable to open $dest";
678 while (<REPO>) {
679 $keyfile = $_;
680 if ($keyfile =~ /^gpgkey=/) {
681 $keyfile =~ s/gpgkey=//;
682 last;
683 }
684 }
685 close(REPO);
686 if (defined $keyfile) {
687 pb_system("wget -O $ENV{'PBTMP'}/$bn $keyfile","Downloading GPG key file $keyfile");
688 pb_system("sudo rpm --import $ENV{'PBTMP'}/$bn","Importing GPG key file $keyfile");
689 unlink("$ENV{'PBTMP'}/$bn");
690 }
691 }
692 } elsif ($bn =~ /\.addmedia/) {
693 # URPMI repo
694 # We should test that it's not already a urpmi repo
695 pb_system("chmod 755 $ENV{'PBTMP'}/$bn ; sudo $ENV{'PBTMP'}/$bn 2>&1 > /dev/null","Adding urpmi repository");
696 } else {
697 pb_log(0,"ERROR: Unable to deal with repository file $i on rpm distro ! Please report to dev team\n");
698 }
699 } elsif ($pbos->{'type'} eq "deb") {
700 if ($bn =~ /\.sources.list$/) {
701 my $dest = "/etc/apt/sources.list.d/$bn";
702 return if (pb_distro_compare_repo("$ENV{'PBTMP'}/$bn",$dest) == 1);
703 pb_system("sudo mv $ENV{'PBTMP'}/$bn $dest","Adding apt repository $dest");
704 # Check whether GPG keys for this repo are already known and if
705 # not add them
706 open(REPO,"$dest") || confess "Unable to open $dest";
707 my $debrepo;
708 while (<REPO>) {
709 if (/^deb\s/) {
710 $debrepo = $_;
711 chomp($debrepo);
712 $debrepo =~ s|^deb ([^\s]+)\s([^\s]+)\s([^\s]+)|$1/dists/$2|;
713 last;
714 }
715 }
716 close(REPO);
717 return if (not defined $debrepo);
718
719 pb_system("wget -O $ENV{'PBTMP'}/Release $debrepo/Release","Downloading $debrepo/Release");
720 pb_system("wget -O $ENV{'PBTMP'}/Release.gpg $debrepo/Release.gpg","Downloading $debrepo/Release.gpg");
721 my $signature;
722 open(SIGN,"LANGUAGE=C LANG=C gpg --verify $ENV{'PBTMP'}/Release.gpg $ENV{'PBTMP'}/Release 2>&1 |") || confess "Unable to verify GPG signature from Release.gpg\n";
723 while(<SIGN>) {
724 chomp();
725 if (/^gpg: .*key ID/) {
726 $signature = $_;
727 $signature =~ s/^gpg: .*key ID ([A-Z0-9]+)/$1/;
728 #TODO: create a pbkeyserver conf var for that
729 pb_system("gpg --recv-keys --keyserver hkp://pgp.mit.edu $signature","Importing GPG signature for $signature");
730 $signature = undef;
731 last;
732 }
733 }
734 close(SIGN);
735 open(SIGN,"LANGUAGE=C LANG=C gpg --verify $ENV{'PBTMP'}/Release.gpg $ENV{'PBTMP'}/Release 2>&1 |") || confess "Unable to verify GPG signature from $debrepo/Release.gpg\n";
736 while(<SIGN>) {
737 chomp();
738 if (/^gpg: Good signature/) {
739 $signature = $_;
740 $signature =~ s/^gpg: Good signature from "([^\"]+)"/$1/;
741 }
742 }
743 close(SIGN);
744
745 return if (not defined $signature);
746 pb_log(3, "GnuPG repo verify returned: $signature\n");
747 unlink("$ENV{'PBTMP'}/apt.sig");
748 pb_system("gpg -a --export -o $ENV{'PBTMP'}/apt.sig \'$signature\'","Exporting GnuPG signature of $signature");
749 pb_system("sudo apt-key add $ENV{'PBTMP'}/apt.sig","Adding GnuPG signature of $signature to APT key ring");
750 pb_system("sudo apt-get update","Updating apt repository");
751 } else {
752 pb_log(0,"ERROR: Unable to deal with repository file $i on deb distro ! Please report to dev team\n");
753 }
754 } else {
755 pb_log(0,"ERROR: Unable to deal with repository file $i on that distro ! Please report to dev team\n");
756 }
757}
758return("forcerepo");
759}
760
761=item B<pb_distro_to_keylist>
762
763Given a pbos object (first param) and the generic key (second param), get the list of possible keys for looking up variable for
764filter names. The list will be sorted most-specific to least specific.
765
766=cut
767
768sub pb_distro_to_keylist ($$) {
769
770my ($pbos, $generic) = @_;
771
772foreach my $key (qw/name version arch family type os/) {
773 confess "missing pbos key $key" unless (defined $pbos->{$key});
774}
775
776my @keylist = ("$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}", "$pbos->{'name'}-$pbos->{'version'}");
777
778# Loop to include also previous minor versions
779# if configured so
780if ((defined $pbos->{'useminor'}) && ($pbos->{'useminor'} eq "true") && ($pbos->{'version'} =~ /^(\d+)\.(\d+)$/o)) {
781 my ($major, $minor) = ($1, $2);
782 while ($minor > 0) {
783 $minor--;
784 push (@keylist, "$pbos->{'name'}-${major}.$minor");
785 }
786 push (@keylist, "$pbos->{'name'}-$major");
787}
788
789push (@keylist, $pbos->{'name'}, $pbos->{'family'}, $pbos->{'type'}, $pbos->{'os'}, $generic);
790return @keylist;
791}
792
793=item B<pb_distro_get_param>
794
795This function gets the parameter in the conf file from the most precise tuple up to default
796
797=cut
798
799sub pb_distro_get_param {
800
801my @param = ();
802my $pbos = shift;
803
804my @keylist = pb_distro_to_keylist($pbos,"default");
805pb_log(2,"DEBUG: pb_distro_get_param on $pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'} for ".Dumper(@_)."\n");
806foreach my $opt (@_) {
807 my $param = "";
808 foreach my $key (@keylist) {
809 if (defined $opt->{$key}) {
810 $param = $opt->{$key};
811 last;
812 }
813 }
814 # Allow replacement of variables inside the parameter such as name, version, arch for rpmbootstrap
815 # but not shell variable which are backslashed
816 if ($param =~ /[^\\]\$/) {
817 pb_log(3,"Expanding variable on $param\n");
818 eval { $param =~ s/(\$\w+->\{\'\w+\'\})/$1/eeg };
819 }
820 push @param,$param;
821}
822
823pb_log(2,"DEBUG: pb_distro_get_param on $pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'} returns ==".Dumper(@param)."==\n");
824
825# Return one param if user only asked for one lookup, an array if not.
826my $nb = @param;
827if ($nb eq 1) {
828 return($param[0]);
829} else {
830 return(@param);
831}
832}
833
834=item B<pb_distro_get_context>
835
836This function gets the OS context passed as parameter and return the corresponding distribution hash
837If passed undef or "" then auto-detects
838
839=cut
840
841
842sub pb_distro_get_context {
843
844my $os = shift;
845my $pbos;
846
847if ((defined $os) && ($os ne "")) {
848 my ($name,$ver,$darch) = split(/-/,$os);
849 pb_log(0,"Bad format for $os") if ((not defined $name) || (not defined $ver) || (not defined $darch)) ;
850 chomp($darch);
851 $pbos = pb_distro_init($name,$ver,$darch);
852} else {
853 $pbos = pb_distro_init();
854}
855return($pbos);
856}
857
858=item B<pb_distro_conf_print>
859
860This function prints every configuration parameter in order to help debug stacking issues with conf files. If a VM/VE/RM is given, restrict display to this distribution. If parameters are passed, restrict again the display to these values only.
861
862=cut
863
864sub pb_distro_conf_print {
865
866my $pbos = shift;
867my @keys = @_;
868
869if ($#keys == -1) {
870 pb_log(0,"Full pb configuration for project $ENV{'PBPROJ'}\n");
871 pb_log(0,"================================================\n");
872 @keys = pb_conf_get_all();
873}
874if (defined $ENV{'PBV'}) {
875 pb_log(1,"Distribution $ENV{'PBV'}\n");
876 pb_log(1,"========================\n");
877} else {
878 pb_log(1,"Local Distribution\n");
879 pb_log(1,"==================\n");
880}
881
882my %rep;
883my $i = 0;
884# Index on distro
885foreach my $r (pb_distro_get_param($pbos,pb_conf_get(@keys))) {
886 $rep{$keys[$i]} = $r if (defined $keys[$i]);
887 $i++;
888}
889$i = 0;
890# Then Index on prj to overwrite previous value if needed
891foreach my $r (pb_conf_get(@keys)) {
892 $rep{$keys[$i]} = $r->{'default'} if (defined $r->{'default'});
893 $rep{$keys[$i]} = $r->{$ENV{'PBPROJ'}} if (defined $r->{$ENV{'PBPROJ'}});
894 $i++;
895}
896foreach my $r (keys %rep) {
897 pb_log(1, "$r => ");
898 pb_log(0, "$rep{$r}\n");
899}
900}
901
902
903=back
904
905=head1 WEB SITES
906
907The main Web site of the project is available at L<http://www.project-builder.org/>. Bug reports should be filled using the trac instance of the project at L<http://trac.project-builder.org/>.
908
909=head1 USER MAILING LIST
910
911None exists for the moment.
912
913=head1 AUTHORS
914
915The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
916
917=head1 COPYRIGHT
918
919Project-Builder.org is distributed under the GPL v2.0 license
920described in the file C<COPYING> included with the distribution.
921
922=cut
923
924
9251;
Note: See TracBrowser for help on using the repository browser.