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

Last change on this file since 2279 was 2279, checked in by bruno, 23 months 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
Line 
1#!/usr/bin/perl -w
2#
3# Creates common environment for distributions
4#
5# Copyright B. Cornec 2007-2016
6# Eric Anderson's changes are (c) Copyright 2012 Hewlett Packard
7# Provided under the GPL v2
8#
9# $Id$
10#
11
12package ProjectBuilder::Distribution;
13
14use strict;
15use Data::Dumper;
16use Carp 'confess';
17use ProjectBuilder::Version;
18use ProjectBuilder::Base;
19use ProjectBuilder::Conf;
20use File::Basename;
21use File::Copy;
22# requires perl 5.004 minimum in VM/VE
23use File::Compare;
24
25# Global vars
26# Inherit from the "Exporter" module which handles exporting functions.
27 
28use vars qw($VERSION $REVISION $PBCONFVER @ISA @EXPORT);
29use Exporter;
30 
31# Export, by default, all the functions into the namespace of
32# any code which uses this module.
33 
34our @ISA = qw(Exporter);
35our @EXPORT = qw(pb_distro_init pb_distro_conffile pb_distro_sysconffile pb_distro_get pb_distro_getlsb pb_distro_installdeps pb_distro_getdeps pb_distro_only_deps_needed pb_distro_setuprepo pb_distro_setuposrepo pb_distro_setuprepo_gen pb_distro_get_param pb_distro_get_context pb_distro_to_keylist pb_distro_conf_print pb_apply_conf_proxy);
36($VERSION,$REVISION,$PBCONFVER) = pb_version_init();
37
38=pod
39
40=head1 NAME
41
42ProjectBuilder::Distribution, part of the project-builder.org - module dealing with distribution detection
43
44=head1 DESCRIPTION
45
46This modules provides functions to allow detection of Linux distributions, and giving back some attributes concerning them.
47
48=head1 SYNOPSIS
49
50  use ProjectBuilder::Distribution;
51
52  #
53  # Return information on the running distro
54  #
55  my $pbos = pb_distro_get_context();
56  print "distro tuple: ".Dumper($pbos->name, $pbos->ver, $pbos->fam, $pbos->type, $pbos->pbsuf, $pbos->pbupd, $pbos->pbins, $pbos->arch)."\n";
57  #
58  # Return information on the requested distro
59  #
60  my $pbos = pb_distro_get_context("ubuntu-7.10-x86_64");
61  print "distro tuple: ".Dumper($pbos->name, $pbos->ver, $pbos->fam, $pbos->type, $pbos->pbsuf, $pbos->pbupd, $pbos->pbins, $pbos->arch)."\n";
62  #
63  # Return information on the running distro
64  #
65  my ($ddir,$dver) = pb_distro_get();
66
67=head1 USAGE
68
69=over 4
70
71=item B<pb_distro_conffile>
72
73This function returns the mandatory configuration file used for distribution/OS detection
74
75=cut
76
77sub pb_distro_conffile {
78
79if ($PBCONFVER < 1) {
80    return("CCCC/pb.conf");
81} else {
82    return("CCCC/pb.yml");
83}
84}
85
86=item B<pb_distro_sysconffile>
87
88This function returns the optional configuration file used for local customization
89
90=cut
91
92sub pb_distro_sysconffile {
93
94if ($PBCONFVER < 1) {
95    return("SSSS/pb.conf");
96} else {
97    return("SSSS/pb.yml");
98}
99}
100
101
102=item B<pb_distro_init>
103
104This function returns a hash of parameters indicating the distribution name, version, family, type of build system, suffix of packages, update command line, installation command line and architecture of the underlying Linux distribution. The value of the fields may be "unknown" in case the function was unable to recognize on which distribution it is running.
105
106As an example, Ubuntu and Debian are in the same "du" family. As well as RedHat, RHEL, CentOS, fedora are on the same "rh" family.
107Mandriva, Open SuSE and Fedora have all the same "rpm" type of build system. Ubuntu and Debian have the same "deb" type of build system.
108And "fc" is the extension generated for all Fedora packages (Version will be added by pb).
109All this information is stored in an external configuration file typically at /etc/pb/pb.conf
110
111When passing the distribution name and version as parameters, the B<pb_distro_init> function returns the parameter of that distribution instead of the underlying one.
112
113Cf: http://linuxmafia.com/faq/Admin/release-files.html
114Ideas taken from http://search.cpan.org/~kerberus/Linux-Distribution-0.14/lib/Linux/Distribution.pm
115
116=cut
117
118
119sub pb_distro_init {
120
121my $pbos = {
122    'name' => undef,
123    'version' => undef,
124    'arch' => undef,
125    'family' => "unknown",
126    'suffix' => "unknown",
127    'update' => "unknown",
128    'install' => "unknown",
129    'type' => "unknown",
130    'os' => "unknown",
131    'nover' => "false",
132    'rmdot' => "false",
133    'useminor' => "false",
134    };
135$pbos->{'name'} = shift;
136$pbos->{'version'} = shift;
137$pbos->{'arch'} = shift;
138
139# Adds conf file for distribution description
140# the location of the conf file is finalyzed at install time
141# depending whether we deal with package install or tar file install
142
143pb_conf_add(pb_distro_sysconffile());
144
145# Similarly for the local file available for sysadmin. After the previous one to allow overwrite to work
146pb_conf_add(pb_distro_conffile());
147
148# If we don't know which distribution we're on, then guess it
149($pbos->{'name'},$pbos->{'version'}) = pb_distro_get() if ((not defined $pbos->{'name'}) || (not defined $pbos->{'version'}));
150
151# For some rare cases, typically nover ones
152$pbos->{'name'} = "unknown" if (not defined $pbos->{'name'});
153$pbos->{'version'} = "unknown" if (not defined $pbos->{'version'});
154
155# Initialize arch
156$pbos->{'arch'} = pb_get_arch() if (not defined $pbos->{'arch'});
157# Solves a bug on Red Hat 6.x where real arch is not detected when using setarch and a chroot
158# As it was only i386 forcing it here.
159$pbos->{'arch'} = "i386" if (($pbos->{'name'} eq "redhat") && ($pbos->{'version'} =~ /^6\./));
160
161# Dig into the tuple to find the best answer
162# Do NOT factorize here, as it won't work as of now for hash creation
163# Do NOT change order without caution
164$pbos->{'useminor'} = pb_distro_get_param($pbos,pb_conf_get("osuseminorrel"));
165$pbos->{'family'} = pb_distro_get_param($pbos,pb_conf_get("osfamily"));
166$pbos->{'type'} = pb_distro_get_param($pbos,pb_conf_get("ostype"));
167($pbos->{'os'},$pbos->{'install'},$pbos->{'suffix'},$pbos->{'nover'},$pbos->{'rmdot'},$pbos->{'update'}) = pb_distro_get_param($pbos,pb_conf_get("os","osins","ossuffix","osnover","osremovedotinver","osupd"));
168#($pbos->{'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"));
169
170# Some OS have no interesting version
171$pbos->{'version'} = "nover" if ((defined $pbos->{'nover'}) && ($pbos->{'nover'} eq "true"));
172
173# For some OS remove the . in version name for extension
174my $dver2 = $pbos->{'version'};
175$dver2 =~ s/\.//g if ((defined $pbos->{'rmdot'}) && ($pbos->{'rmdot'} eq "true"));
176
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" 
180} else {
181    # concat just the version to what has been found
182    $pbos->{'suffix'} = ".$pbos->{'suffix'}$dver2";
183}
184
185#   if ($arch eq "x86_64") {
186#   $opt="--exclude=*.i?86";
187#   }
188pb_log(2,"DEBUG: pb_distro_init: ".Dumper($pbos)."\n");
189
190return($pbos);
191}
192
193=item B<pb_distro_get>
194
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.
196
197On my home machine it would currently report ("mandriva","2010.2").
198
199=cut
200
201sub pb_distro_get {
202
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");
208
209my $release;
210my $distro;
211
212# Begin to test presence of non-ambiguous files
213# that way we reduce the choice
214my ($d,$r);
215while (($d,$r) = each %$single_rel_files) {
216    if (defined $ambiguous_rel_files->{$d}) {
217        print STDERR "The key $d is considered as both unambiguous and ambigous.\n";
218        print STDERR "Please fix your configuration file.\n"
219    }
220    if (-f "$r" && ! -l "$r") {
221        my $tmp=pb_get_content("$r");
222        # Found the only possibility.
223        # Try to get version and return
224        if (defined ($distro_match->{$d})) {
225            ($release) = $tmp =~ m/$distro_match->{$d}/m;
226        } else {
227            print STDERR "Unable to find $d version in $r (non-ambiguous)\n";
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
235# Now look at ambiguous files
236# Ubuntu before 10.04 includes a /etc/debian_version file that creates an ambiguity with debian
237# So we need to look at distros in reverse alphabetic order to treat ubuntu always first via lsb
238my $found = 0;
239foreach $d (reverse keys %$ambiguous_rel_files) {
240    $r = $ambiguous_rel_files->{$d};
241    if (-f "$r" && !-l "$r") {
242        # Found one possibility.
243        # Get all distros concerned by that file
244        my $tmp=pb_get_content("$r");
245        my $ptr = $distro_similar->{$d};
246        pb_log(2,"amb: ".Dumper($ptr)."\n");
247        $release = "unknown";
248        foreach my $dd (split(/,/,$ptr)) {
249            pb_log(2,"check $dd\n");
250            # Try to check pattern
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;
254                if ((defined $release) && ($release ne "unknown")) {
255                    $distro = $dd;
256                    $found = 1;
257                    last;
258                }
259            }
260        }
261        last if ($found == 1);
262    }
263}
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);
270}
271}
272
273=item B<pb_distro_getlsb>
274
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";
291    confess "You modified upstream delivery and lost !\n";
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    }
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);
318    return($l, $i, $d, $r, $c);
319} else {
320    print STDERR "Unable to read $lsbf file\n";
321    confess "Please report to the maintainer bruno_at_project-builder.org\n";
322}
323}
324
325# Internal function
326
327sub pb_apply_conf_proxy {
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"));
332my $https_proxy = pb_distro_get_param($pbos,pb_conf_get_if("https_proxy"));
333
334# We do not overwrite shell settings
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 ""));
337$ENV{https_proxy} ||= $https_proxy if ((defined $https_proxy) && ($https_proxy ne ""));
338}
339
340=item B<pb_distro_installdeps>
341
342This function install the dependencies required to build the package on a distro.
343If $forcerepo is defined then do not assume packages are alredy installed, but reinstall them
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
346
347=cut
348
349sub pb_distro_installdeps {
350
351# SPEC file
352my $f = shift;
353my $pbos = shift;
354my $forcerepo = shift;
355my $deps = shift;   # optional list of deps to install
356
357# Protection
358confess "Missing install command for $pbos->{name}-$pbos->{version}-$pbos->{arch}" unless (defined $pbos->{install} && $pbos->{install} =~ /\w/);
359pb_apply_conf_proxy($pbos);
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});
362pb_log(1, "https_proxy=$ENV{https_proxy}\n")  if (defined $ENV{https_proxy});
363
364# Get dependencies in the build file if not forced
365$deps = pb_distro_getdeps($f,$pbos, $forcerepo) if ((not defined $deps) || (defined $forcerepo));
366pb_log(2,"deps: $deps\n");
367return if ((not defined $deps) || ($deps =~ /^\s*$/));
368
369# This may not be // proof. We should test for availability of repo and sleep if not
370my $cmd = "$pbos->{'install'} $deps";
371my $ret = pb_system($cmd, "Installing dependencies ($cmd)","mayfail");
372# Try to accomodate deficient proxies
373if ($ret != 0) {
374    pb_system($cmd, "Re-trying installing dependencies ($cmd)");
375}
376# Check that all deps have been installed correctly
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);
380confess "Some dependencies did not install ($deps)" if ((defined $deps) && ($deps =~ /\S/) && ($Global::pb_stop_on_error));
381}
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
391my $f = shift;
392my $pbos = shift;
393my $forcerepo = shift;
394
395my $regexp = "";
396my $deps = "";
397my $sep = $/;
398
399# Protection
400return("") if (not defined $pbos->{'type'});
401return("") if (not defined $f);
402
403pb_log(3,"entering pb_distro_getdeps: $pbos->{'type'} - $f\n");
404if ($pbos->{'type'} eq  "rpm") {
405    # In RPM this could include files, but we do not handle them atm.
406    $regexp = '^BuildRequires:(.*)$';
407} elsif ($pbos->{'type'} eq "deb") {
408    $regexp = '^Build-Depends:(.*)$';
409} elsif ($pbos->{'type'} eq "ebuild") {
410    $sep = '"'.$/;
411    $regexp = '^DEPEND="(.*)"\n'
412} else {
413    # No idea
414    return("");
415}
416pb_log(2,"regexp: $regexp\n");
417
418# Preserve separator before using the one we need
419my $oldsep = $/;
420$/ = $sep;
421open(DESC,"$f") || confess "Unable to open $f";
422while (<DESC>) {
423    pb_log(4,"read: $_\n");
424    next if (! /$regexp/);
425    chomp();
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
436    # What we found with the regexp is the list of deps.
437    pb_log(2,"found deps: $_\n");
438    s/$regexp/$1/i;
439    pb_log(4,"found deps 1: $_\n");
440    # Remove conditions in the middle and at the end for deb
441    s/\([><=]+[^,]*,/,/g;
442    pb_log(4,"found deps 2: $_\n");
443    s/\([><=]+[^,]*$//g;
444    pb_log(4,"found deps 3: $_\n");
445    # Same for rpm
446    s/[><=]+[^,]*,/,/g;
447    pb_log(4,"found deps 4: $_\n");
448    s/[><=]+.*$//g;
449    pb_log(4,"found deps 5: $_\n");
450    # Improve string format (remove , and spaces at start, end and in double
451    s/,/ /g;
452    pb_log(4,"found deps 6: $_\n");
453    s/^\s*//;
454    pb_log(4,"found deps 7: $_\n");
455    # $ here removes the \n
456    s/\s*$//;
457    pb_log(4,"found deps 8: $_\n");
458    s/\s+/ /g;
459    pb_log(4,"found deps 9: $_\n");
460    $deps .= " ".$_;
461    pb_log(4,"found deps end: $deps\n");
462
463    # Support multi-lines deps for .deb (fwup)
464    if (defined $nextline) {
465        $_ = $nextline;
466        redo;
467    }
468}
469close(DESC);
470$/ = $oldsep;
471pb_log(2,"now deps: $deps\n");
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);
481}
482}
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
493my $pbos = shift;
494my $deps = shift;
495
496return("") if ((not defined $deps) || ($deps =~ /^\s*$/));
497my $deps2 = "";
498# Avoid to install what is already there
499delete $ENV{COLUMNS};
500foreach my $p (split(/\s+/,$deps)) {
501    next if $p =~ /^\s*$/o;
502    if ($pbos->{'type'} eq  "rpm") {
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");
507        next if ($res eq 0);
508        pb_log(1, "INFO: missing dependency $p\n");
509    } elsif ($pbos->{'type'} eq "deb") {
510        my $res = pb_system("dpkg -L $p","Looking for $p","mayfail");
511        next if ($res eq 0);
512        open(CMD,"dpkg -l $p |") or confess "Unable to run dpkg -l $p: $!";
513        my $ok = 0;
514        while (<CMD>) {
515            $ok = 1 if /^ii\s+$p/;
516        }
517        close(CMD);
518        next if $ok;
519        pb_log(1, "INFO: missing dependency $p\n");
520    } elsif ($pbos->{'type'} eq "ebuild") {
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
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
541my $pbos = shift;
542
543return(pb_distro_setuprepo_gen_conf($pbos,pb_distro_conffile(),"osrepo"));
544}
545
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
554my $pbos = shift;
555
556return(pb_distro_setuprepo_gen_conf($pbos,"$ENV{'PBDESTDIR'}/pbrc.yml","addrepo"));
557}
558
559# Internal
560sub pb_distro_compare_repo {
561
562my $src = shift;
563my $dest = shift;
564
565if (not -f $dest) {
566    pb_log(1, "INFO: Creating new file $dest\n");
567} elsif (-f $dest && -s $dest == 0) {
568    pb_log(1, "INFO: Overwriting empty file $dest\n");
569} elsif (-f $dest && compare("$src", $dest) == 0) {
570    pb_log(1, "INFO: Overwriting identical file $dest\n");
571} else {
572    pb_log(0, "ERROR: destination file $dest exists and is different than source $src\n");
573    pb_system("cat $dest","INFO: Dest...\n","verbose");
574    pb_system("cat $src","INFO: New...\n","verbose");
575    pb_log(1, "INFO: Returning...\n");
576    return(1);
577}
578return(0);
579}
580
581=item B<pb_distro_setuprepo_gen_conf>
582
583This function sets up in a generic way potential additional repository using conf files
584
585=cut
586
587sub pb_distro_setuprepo_gen_conf {
588
589my $pbos = shift;
590my $pbconf = shift;
591my $pbkey = shift;
592
593return undef if (not defined $pbconf);
594return undef if (not defined $pbkey);
595my ($addrepo) = pb_conf_read($pbconf,$pbkey);
596return undef if (not defined $addrepo);
597
598my $param = pb_distro_get_param($pbos,$addrepo);
599return undef if ($param eq "");
600
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
618pb_apply_conf_proxy($pbos);
619
620# Loop on the list of additional repo
621foreach my $i (split(/,/,$param)) {
622
623    pb_log(1,"Adding repository defined by $i\n");
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
628    if (($scheme eq "ftp") || ($scheme =~ /http/)) {
629        pb_system("wget -O $ENV{'PBTMP'}/$bn $i","Downloading additional repository file $i");
630    } else {
631        copy($i,"$ENV{'PBTMP'}/$bn");
632    }
633
634    # The repo file can be a real file or a package
635    if ($pbos->{'type'} eq "rpm") {
636        if ($bn =~ /\.rpm$/) {
637            my $pn = $bn;
638            $pn =~ s/\.rpm//;
639            if (pb_system("rpm -q --quiet $pn","","mayfail") != 0) {
640                pb_system("sudo rpm -Uvh $ENV{'PBTMP'}/$bn","Adding package $bn to setup repository");
641            }
642        } elsif ($bn =~ /\.repo$/) {
643            my $dirdest = "";
644            my $reponame = "";
645            # TODO: could go in pb.conf in fact
646            if ($pbos->{install} =~ /\byum\b/) {
647                $reponame="yum";
648                $dirdest = "/etc/yum.repos.d";
649            } elsif ($pbos->{install} =~ /\bdnf\b/) {
650                $reponame="dnf";
651                $dirdest = "/etc/yum.repos.d";
652            } elsif ($pbos->{install} =~ /\bzypper\b/) {
653                $reponame="zypper";
654                $dirdest = "/etc/zypp/repos.d";
655            } else {
656                confess "Unknown location for repository file for '$pbos->{install}' command";
657            }
658            my $dest = "$dirdest/$bn";
659            return undef if (pb_distro_compare_repo("$ENV{'PBTMP'}/$bn",$dest) == 1);
660            confess "Missing directory $dirdest ($reponame)" unless (-d $dirdest);
661            pb_system("sudo mv $ENV{'PBTMP'}/$bn $dest","Adding $reponame repository") if (not -f "$dest");
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>) {
668                    $keyfile = $_;
669                    if ($keyfile =~ /^gpgkey=/) {
670                        $keyfile =~ s/gpgkey=//;
671                        last;
672                    }
673                }
674                close(REPO);
675                if (defined $keyfile) {
676                    pb_system("wget -O $ENV{'PBTMP'}/$bn $keyfile","Downloading GPG key file $keyfile");
677                    pb_system("sudo rpm --import $ENV{'PBTMP'}/$bn","Importing GPG key file $keyfile");
678                    unlink("$ENV{'PBTMP'}/$bn");
679                }
680            }
681        } elsif ($bn =~ /\.addmedia/) {
682            # URPMI repo
683            # We should test that it's not already a urpmi repo
684            pb_system("chmod 755 $ENV{'PBTMP'}/$bn ; sudo $ENV{'PBTMP'}/$bn 2>&1 > /dev/null","Adding urpmi repository");
685        } else {
686            pb_log(0,"ERROR: Unable to deal with repository file $i on rpm distro ! Please report to dev team\n");
687        }
688    } elsif ($pbos->{'type'} eq "deb") {
689        if ($bn =~ /\.sources.list$/) {
690            my $dest = "/etc/apt/sources.list.d/$bn";
691            return if (pb_distro_compare_repo("$ENV{'PBTMP'}/$bn",$dest) == 1);
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");
739            pb_system("sudo apt-get update","Updating apt repository");
740        } else {
741            pb_log(0,"ERROR: Unable to deal with repository file $i on deb distro ! Please report to dev team\n");
742        }
743    } else {
744        pb_log(0,"ERROR: Unable to deal with repository file $i on that distro ! Please report to dev team\n");
745    }
746}
747return("forcerepo");
748}
749
750=item B<pb_distro_to_keylist>
751
752Given a pbos object (first param) and the generic key (second param), get the list of possible keys for looking up variable for
753filter names.  The list will be sorted most-specific to least specific.
754
755=cut
756
757sub pb_distro_to_keylist ($$) {
758
759my ($pbos, $generic) = @_;
760
761foreach my $key (qw/name version arch family type os/) {
762    confess "missing pbos key $key" unless (defined $pbos->{$key});
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
769if ((defined $pbos->{'useminor'}) && ($pbos->{'useminor'} eq "true") && ($pbos->{'version'} =~ /^(\d+)\.(\d+)$/o)) {
770        my ($major, $minor) = ($1, $2);
771        while ($minor > 0) {
772                $minor--;
773                push (@keylist, "$pbos->{'name'}-${major}.$minor");
774        }
775        push (@keylist, "$pbos->{'name'}-$major");
776}
777
778push (@keylist, $pbos->{'name'}, $pbos->{'family'}, $pbos->{'type'}, $pbos->{'os'}, $generic);
779return @keylist;
780}
781
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
790my @param = ();
791my $pbos = shift;
792
793my @keylist = pb_distro_to_keylist($pbos,"default");
794pb_log(2,"DEBUG: pb_distro_get_param on $pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'} for ".Dumper(@_)."\n");
795foreach my $opt (@_) {
796    my $param = "";
797    foreach my $key (@keylist) {
798        if (defined $opt->{$key}) {
799            $param = $opt->{$key};
800            last;
801        }
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");
807        eval { $param =~ s/(\$\w+->\{\'\w+\'\})/$1/eeg };
808    }
809    push @param,$param;
810}
811
812pb_log(2,"DEBUG: pb_distro_get_param on $pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'} returns ==".Dumper(@param)."==\n");
813
814# Return one param if user only asked for one lookup, an array if not.
815my $nb = @param;
816if ($nb eq 1) {
817    return($param[0]);
818} else {
819    return(@param);
820}
821}
822
823=item B<pb_distro_get_context>
824
825This function gets the OS context passed as parameter and return the corresponding distribution hash
826If passed undef or "" then auto-detects
827
828=cut
829
830
831sub pb_distro_get_context {
832
833my $os = shift;
834my $pbos;
835
836if ((defined $os) && ($os ne "")) {
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();
843}
844return($pbos);
845}
846
847=item B<pb_distro_conf_print>
848
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.
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");
861    @keys = pb_conf_get_all();
862}
863if (defined $ENV{'PBV'}) {
864    pb_log(1,"Distribution $ENV{'PBV'}\n");
865    pb_log(1,"========================\n");
866} else {
867    pb_log(1,"Local Distribution\n");
868    pb_log(1,"==================\n");
869}
870
871my %rep;
872my $i = 0;
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
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++;
884}
885foreach my $r (keys %rep) {
886    pb_log(1, "$r => ");
887    pb_log(0, "$rep{$r}\n");
888}
889}
890
891
892=back
893
894=head1 WEB SITES
895
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
9141;
Note: See TracBrowser for help on using the repository browser.