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

Last change on this file since 2390 was 2390, checked in by bruno, 4 months ago

Add support for SLES 15 by using /etc/os-release as well

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