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

Last change on this file since 2261 was 2261, checked in by bruno, 2 years ago

Log when handling YAML conf files the filename

Fix also temp pbrc.yml generation in pb

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