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

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

Fix #167 by adding an oslocalins option

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