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

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

YAML support working for most distros

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