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

Last change on this file since 2336 was 2336, checked in by Bruno Cornec, 7 years ago

Improve again apk build up to prepve

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 pb_log(0,"Looking for $p");
530 my $res = system("apk info | grep -E '^$p\$'");
531 if ($res eq 0) {
532 pb_log(0, "... OK\n");
533 next;
534 }
535 pb_log(1, "INFO: missing dependency $p\n");
536 } else {
537 # Not reached
538 }
539 pb_log(2,"found deps2: $p\n");
540 $deps2 .= " $p";
541}
542
543$deps2 =~ s/^\s*//;
544pb_log(2,"now deps2: $deps2\n");
545return($deps2);
546}
547
548=item B<pb_distro_setuposrepo>
549
550This function sets up potential additional repository for the setup phase
551
552=cut
553
554sub pb_distro_setuposrepo {
555
556my $pbos = shift;
557
558return(pb_distro_setuprepo_gen_conf($pbos,pb_distro_conffile(),"osrepo"));
559}
560
561=item B<pb_distro_setuprepo>
562
563This function sets up potential additional repository to the build environment
564
565=cut
566
567sub pb_distro_setuprepo {
568
569my $pbos = shift;
570
571return(pb_distro_setuprepo_gen_conf($pbos,"$ENV{'PBDESTDIR'}/pbrc.yml","addrepo"));
572}
573
574# Internal
575sub pb_distro_compare_repo {
576
577my $src = shift;
578my $dest = shift;
579
580if (not -f $dest) {
581 pb_log(1, "INFO: Creating new file $dest\n");
582} elsif (-f $dest && -s $dest == 0) {
583 pb_log(1, "INFO: Overwriting empty file $dest\n");
584} elsif (-f $dest && compare("$src", $dest) == 0) {
585 pb_log(1, "INFO: Overwriting identical file $dest\n");
586} else {
587 pb_log(0, "ERROR: destination file $dest exists and is different than source $src\n");
588 pb_system("cat $dest","INFO: Dest...\n","verbose");
589 pb_system("cat $src","INFO: New...\n","verbose");
590 pb_log(1, "INFO: Returning...\n");
591 return(1);
592}
593return(0);
594}
595
596=item B<pb_distro_setuprepo_gen_conf>
597
598This function sets up in a generic way potential additional repository using conf files
599
600=cut
601
602sub pb_distro_setuprepo_gen_conf {
603
604my $pbos = shift;
605my $pbconf = shift;
606my $pbkey = shift;
607
608return undef if (not defined $pbconf);
609return undef if (not defined $pbkey);
610my ($addrepo) = pb_conf_read($pbconf,$pbkey);
611return undef if (not defined $addrepo);
612
613my $param = pb_distro_get_param($pbos,$addrepo);
614return undef if ($param eq "");
615
616return(pb_distro_setuprepo_gen($pbos,$param));
617}
618
619
620=item B<pb_distro_setuprepo_gen>
621
622This function sets up in a generic way potential additional repository passed as a param
623
624=cut
625
626sub pb_distro_setuprepo_gen {
627
628my $pbos = shift;
629my $param = shift;
630
631return if (not defined $param);
632
633pb_apply_conf_proxy($pbos);
634
635# Loop on the list of additional repo
636foreach my $i (split(/,/,$param)) {
637
638 pb_log(1,"Adding repository defined by $i\n");
639 my ($scheme, $account, $host, $port, $path) = pb_get_uri($i);
640 my $bn = basename($i);
641
642 # The repo file can be local or remote. download or copy at the right place
643 if (($scheme eq "ftp") || ($scheme =~ /http/)) {
644 pb_system("wget -O $ENV{'PBTMP'}/$bn $i","Downloading additional repository file $i");
645 } else {
646 copy($i,"$ENV{'PBTMP'}/$bn");
647 }
648
649 # The repo file can be a real file or a package
650 if ($pbos->{'type'} eq "rpm") {
651 if ($bn =~ /\.rpm$/) {
652 my $pn = $bn;
653 $pn =~ s/\.rpm//;
654 if (pb_system("rpm -q --quiet $pn","","mayfail") != 0) {
655 pb_system("sudo rpm -Uvh $ENV{'PBTMP'}/$bn","Adding package $bn to setup repository");
656 }
657 } elsif ($bn =~ /\.repo$/) {
658 my $dirdest = "";
659 my $reponame = "";
660 # TODO: could go in pb.yml in fact
661 if ($pbos->{install} =~ /\byum\b/) {
662 $reponame="yum";
663 $dirdest = "/etc/yum.repos.d";
664 } elsif ($pbos->{install} =~ /\bdnf\b/) {
665 $reponame="dnf";
666 $dirdest = "/etc/yum.repos.d";
667 } elsif ($pbos->{install} =~ /\bzypper\b/) {
668 $reponame="zypper";
669 $dirdest = "/etc/zypp/repos.d";
670 } else {
671 confess "Unknown location for repository file for '$pbos->{install}' command";
672 }
673 my $dest = "$dirdest/$bn";
674 return undef if (pb_distro_compare_repo("$ENV{'PBTMP'}/$bn",$dest) == 1);
675 confess "Missing directory $dirdest ($reponame)" unless (-d $dirdest);
676 pb_system("sudo mv $ENV{'PBTMP'}/$bn $dest","Adding $reponame repository") if (not -f "$dest");
677 # OpenSUSE does't seem to import keys automatically
678 # :-(
679 if ($pbos->{install} =~ /\bzypper\b/) {
680 my $keyfile = undef;
681 open(REPO,"$dest") || confess "Unable to open $dest";
682 while (<REPO>) {
683 $keyfile = $_;
684 if ($keyfile =~ /^gpgkey=/) {
685 $keyfile =~ s/gpgkey=//;
686 last;
687 }
688 }
689 close(REPO);
690 if (defined $keyfile) {
691 pb_system("wget -O $ENV{'PBTMP'}/$bn $keyfile","Downloading GPG key file $keyfile");
692 pb_system("sudo rpm --import $ENV{'PBTMP'}/$bn","Importing GPG key file $keyfile");
693 unlink("$ENV{'PBTMP'}/$bn");
694 }
695 }
696 } elsif ($bn =~ /\.addmedia/) {
697 # URPMI repo
698 # We should test that it's not already a urpmi repo
699 pb_system("chmod 755 $ENV{'PBTMP'}/$bn ; sudo $ENV{'PBTMP'}/$bn 2>&1 > /dev/null","Adding urpmi repository");
700 } else {
701 pb_log(0,"ERROR: Unable to deal with repository file $i on rpm distro ! Please report to dev team\n");
702 }
703 } elsif ($pbos->{'type'} eq "deb") {
704 if ($bn =~ /\.sources.list$/) {
705 my $dest = "/etc/apt/sources.list.d/$bn";
706 return if (pb_distro_compare_repo("$ENV{'PBTMP'}/$bn",$dest) == 1);
707 pb_system("sudo mv $ENV{'PBTMP'}/$bn $dest","Adding apt repository $dest");
708 # Check whether GPG keys for this repo are already known and if
709 # not add them
710 open(REPO,"$dest") || confess "Unable to open $dest";
711 my $debrepo;
712 while (<REPO>) {
713 if (/^deb\s/) {
714 $debrepo = $_;
715 chomp($debrepo);
716 $debrepo =~ s|^deb ([^\s]+)\s([^\s]+)\s([^\s]+)|$1/dists/$2|;
717 last;
718 }
719 }
720 close(REPO);
721 return if (not defined $debrepo);
722
723 pb_system("wget -O $ENV{'PBTMP'}/Release $debrepo/Release","Downloading $debrepo/Release");
724 pb_system("wget -O $ENV{'PBTMP'}/Release.gpg $debrepo/Release.gpg","Downloading $debrepo/Release.gpg");
725 my $signature;
726 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";
727 while(<SIGN>) {
728 chomp();
729 if (/^gpg: .*key ID/) {
730 $signature = $_;
731 $signature =~ s/^gpg: .*key ID ([A-Z0-9]+)/$1/;
732 #TODO: create a pbkeyserver conf var for that
733 pb_system("gpg --recv-keys --keyserver hkp://pgp.mit.edu $signature","Importing GPG signature for $signature");
734 $signature = undef;
735 last;
736 }
737 }
738 close(SIGN);
739 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";
740 while(<SIGN>) {
741 chomp();
742 if (/^gpg: Good signature/) {
743 $signature = $_;
744 $signature =~ s/^gpg: Good signature from "([^\"]+)"/$1/;
745 }
746 }
747 close(SIGN);
748
749 return if (not defined $signature);
750 pb_log(3, "GnuPG repo verify returned: $signature\n");
751 unlink("$ENV{'PBTMP'}/apt.sig");
752 pb_system("gpg -a --export -o $ENV{'PBTMP'}/apt.sig \'$signature\'","Exporting GnuPG signature of $signature");
753 pb_system("sudo apt-key add $ENV{'PBTMP'}/apt.sig","Adding GnuPG signature of $signature to APT key ring");
754 pb_system("sudo apt-get update","Updating apt repository");
755 } else {
756 pb_log(0,"ERROR: Unable to deal with repository file $i on deb distro ! Please report to dev team\n");
757 }
758 } else {
759 pb_log(0,"ERROR: Unable to deal with repository file $i on that distro ! Please report to dev team\n");
760 }
761}
762return("forcerepo");
763}
764
765=item B<pb_distro_to_keylist>
766
767Given a pbos object (first param) and the generic key (second param), get the list of possible keys for looking up variable for
768filter names. The list will be sorted most-specific to least specific.
769
770=cut
771
772sub pb_distro_to_keylist ($$) {
773
774my ($pbos, $generic) = @_;
775
776foreach my $key (qw/name version arch family type os/) {
777 confess "missing pbos key $key" unless (defined $pbos->{$key});
778}
779
780my @keylist = ("$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}", "$pbos->{'name'}-$pbos->{'version'}");
781
782# Loop to include also previous minor versions
783# if configured so
784if ((defined $pbos->{'useminor'}) && ($pbos->{'useminor'} eq "true") && ($pbos->{'version'} =~ /^(\d+)\.(\d+)$/o)) {
785 my ($major, $minor) = ($1, $2);
786 while ($minor > 0) {
787 $minor--;
788 push (@keylist, "$pbos->{'name'}-${major}.$minor");
789 }
790 push (@keylist, "$pbos->{'name'}-$major");
791}
792
793push (@keylist, $pbos->{'name'}, $pbos->{'family'}, $pbos->{'type'}, $pbos->{'os'}, $generic);
794return @keylist;
795}
796
797=item B<pb_distro_get_param>
798
799This function gets the parameter in the conf file from the most precise tuple up to default
800
801=cut
802
803sub pb_distro_get_param {
804
805my @param = ();
806my $pbos = shift;
807
808my @keylist = pb_distro_to_keylist($pbos,"default");
809pb_log(2,"DEBUG: pb_distro_get_param on $pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'} for ".Dumper(@_)."\n");
810foreach my $opt (@_) {
811 my $param = "";
812 foreach my $key (@keylist) {
813 if (defined $opt->{$key}) {
814 $param = $opt->{$key};
815 last;
816 }
817 }
818 # Allow replacement of variables inside the parameter such as name, version, arch for rpmbootstrap
819 # but not shell variable which are backslashed
820 if ($param =~ /[^\\]\$/) {
821 pb_log(3,"Expanding variable on $param\n");
822 eval { $param =~ s/(\$\w+->\{\'\w+\'\})/$1/eeg };
823 }
824 push @param,$param;
825}
826
827pb_log(2,"DEBUG: pb_distro_get_param on $pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'} returns ==".Dumper(@param)."==\n");
828
829# Return one param if user only asked for one lookup, an array if not.
830my $nb = @param;
831if ($nb eq 1) {
832 return($param[0]);
833} else {
834 return(@param);
835}
836}
837
838=item B<pb_distro_get_context>
839
840This function gets the OS context passed as parameter and return the corresponding distribution hash
841If passed undef or "" then auto-detects
842
843=cut
844
845
846sub pb_distro_get_context {
847
848my $os = shift;
849my $pbos;
850
851if ((defined $os) && ($os ne "")) {
852 my ($name,$ver,$darch) = split(/-/,$os);
853 pb_log(0,"Bad format for $os") if ((not defined $name) || (not defined $ver) || (not defined $darch)) ;
854 chomp($darch);
855 $pbos = pb_distro_init($name,$ver,$darch);
856} else {
857 $pbos = pb_distro_init();
858}
859return($pbos);
860}
861
862=item B<pb_distro_conf_print>
863
864This 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.
865
866=cut
867
868sub pb_distro_conf_print {
869
870my $pbos = shift;
871my @keys = @_;
872
873if ($#keys == -1) {
874 pb_log(0,"Full pb configuration for project $ENV{'PBPROJ'}\n");
875 pb_log(0,"================================================\n");
876 @keys = pb_conf_get_all();
877}
878if (defined $ENV{'PBV'}) {
879 pb_log(1,"Distribution $ENV{'PBV'}\n");
880 pb_log(1,"========================\n");
881} else {
882 pb_log(1,"Local Distribution\n");
883 pb_log(1,"==================\n");
884}
885
886my %rep;
887my $i = 0;
888# Index on distro
889foreach my $r (pb_distro_get_param($pbos,pb_conf_get(@keys))) {
890 $rep{$keys[$i]} = $r if (defined $keys[$i]);
891 $i++;
892}
893$i = 0;
894# Then Index on prj to overwrite previous value if needed
895foreach my $r (pb_conf_get(@keys)) {
896 $rep{$keys[$i]} = $r->{'default'} if (defined $r->{'default'});
897 $rep{$keys[$i]} = $r->{$ENV{'PBPROJ'}} if (defined $r->{$ENV{'PBPROJ'}});
898 $i++;
899}
900foreach my $r (keys %rep) {
901 pb_log(1, "$r => ");
902 pb_log(0, "$rep{$r}\n");
903}
904}
905
906
907=back
908
909=head1 WEB SITES
910
911The 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/>.
912
913=head1 USER MAILING LIST
914
915None exists for the moment.
916
917=head1 AUTHORS
918
919The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
920
921=head1 COPYRIGHT
922
923Project-Builder.org is distributed under the GPL v2.0 license
924described in the file C<COPYING> included with the distribution.
925
926=cut
927
928
9291;
Note: See TracBrowser for help on using the repository browser.