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

Last change on this file since 2361 was 2361, checked in by Bruno Cornec, 5 years ago

exits when conf errors are detected instead of going on

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