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

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

Initial support of apk for pb

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