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

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

Fix local install for rpm distros (use osins when oslocalins is empty)

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