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

Last change on this file since 2362 was 2362, checked in by bruno, 5 months ago

better usae of cluck instead of confess to avoid exiting abrutely when not needed

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