source: devel/pb/bin/pb @ 1107

Last change on this file since 1107 was 1107, checked in by Bruno Cornec, 10 years ago

r4032@localhost: bruno | 2010-11-08 15:51:53 +0100

  • Change dependency on Mail::Sendmail from use to require, as it's only used in pb_announce, so not in VE/VM
  • Property svn:executable set to *
File size: 100.2 KB
Line 
1#!/usr/bin/perl -w
2#
3# Project Builder main application
4#
5# $Id$
6#
7# Copyright B. Cornec 2007
8# Provided under the GPL v2
9
10# Syntax: see at end
11
12use strict 'vars';
13
14# The modules mentioned here are required by pb when used both
15# locally or inside a VE/VM
16# Additional required modules only used locally are called with a require
17# in their respective section
18use Getopt::Long qw(:config auto_abbrev no_ignore_case);
19use Data::Dumper;
20use English;
21use File::Basename;
22use File::Copy;
23use File::stat;
24use File::Temp qw(tempdir);
25use File::Find;
26use Time::localtime qw(localtime);
27use POSIX qw(strftime);
28use lib qw (lib);
29use ProjectBuilder::Version;
30use ProjectBuilder::Base;
31use ProjectBuilder::Display;
32use ProjectBuilder::Conf;
33use ProjectBuilder::Distribution;
34use ProjectBuilder::CMS;
35use ProjectBuilder::Env;
36use ProjectBuilder::Filter;
37use ProjectBuilder::Changelog;
38
39# Global variables
40my %opts;                   # CLI Options
41my $action;                 # action to realize
42my $test = "FALSE";         # Not used
43my $pbforce = 0;            # Force VE/VM rebuild
44my $pbsnap = 0;             # Do not use snapshot mode for VM/VE by default
45my $option = "";            # Not used
46my @pkgs;                   # list of packages
47my $pbtag;                  # Global Tag variable
48my $pbver;                  # Global Version variable
49my $pbscript;               # Name of the script
50my %pbver;                  # per package
51my %pbtag;                  # per package
52my $pbrev;                  # Global REVISION variable
53my $pbaccount;              # Login to use to connect to the VM
54my $pbport;                 # Port to use to connect to the VM
55my $newver;                 # New version to create
56my $iso = undef;            # ISO image for the VM to create
57
58my @date = pb_get_date();
59my $pbdate = strftime("%Y-%m-%d", @date);
60
61=pod
62
63=head1 NAME
64
65pb, aka project-builder.org - builds packages for your projects
66
67=head1 DESCRIPTION
68
69pb helps you build various packages directly from your project sources.
70Those sources could be handled by a CMS (Configuration Management System)
71such as Subversion, CVS, Git, Mercurial... or being a simple reference to a compressed tar file.
72It's based on a set of configuration files, a set of provided macros to help
73you keeping build files as generic as possible. For example, a single .spec
74file should be required to generate for all rpm based distributions, even
75if you could also have multiple .spec files if required.
76
77=head1 SYNOPSIS
78
79pb [-vhSq][-r pbroot][-p project][[-s script -a account -P port][-m mach-1[,...]]][-i iso] <action> [<pkg1> ...]
80
81pb [--verbose][--help][--man][--quiet][--snapshot][--revision pbroot][--project project][[--script script --account account --port port][--machine mach-1[,...]]][--iso iso] <action> [<pkg1> ...]
82
83=head1 OPTIONS
84
85=over 4
86
87=item B<-v|--verbose>
88
89Print a brief help message and exits.
90
91=item B<-q|--quiet>
92
93Do not print any output.
94
95=item B<-h|--help>
96
97Print a brief help message and exits.
98
99=item B<-S|--snapshot>
100
101Use the snapshot mode of VMs or VEs
102
103=item B<--man>
104
105Prints the manual page and exits.
106
107=item B<-m|--machine machine1[,machine2,...]>
108
109Name of the Virtual Machines (VM) or Virtual Environments (VE) you want to build on (coma separated).
110All if none precised (or use the env variable PBV).
111
112=item B<-s|--script script>
113
114Name of the script you want to execute on the related VMs or VEs.
115
116=item B<-i|--iso iso_image>
117
118Name of the ISO image of the distribution you want to install on the related VMs.
119
120=item B<-a|--account account>
121
122Name of the account to use to connect on the related VMs.
123
124=item B<-P|--port port_number>
125
126Port number to use to connect on the related VMs.\n";
127
128=item B<-p|--project project_name>
129
130Name of the project you're working on (or use the env variable PBPROJ)
131
132=item B<-r|--revision revision>
133
134Path Name of the project revision under the CMS (or use the env variable PBROOT)
135
136=item B<-V|--version new_version>
137
138New version of the project to create based on the current one.
139
140=back
141
142=head1 ARGUMENTS
143
144<action> can be:
145
146=over 4
147
148=item B<sbx2build>
149
150Create tar files for the project under your CMS.
151Current state of the exported content is taken.
152CMS supported are SVN, SVK, CVS, Git and Mercurial
153parameters are packages to build
154if not using default list
155
156=item B<cms2build>
157
158Create tar files for the project under your CMS.
159Current state of the CMS is taken.
160CMS supported are SVN, SVK, CVS, Git and Mercurial
161parameters are packages to build
162if not using default list
163
164=item B<build2pkg>
165
166Create packages for your running distribution
167
168=item B<cms2pkg>
169
170cms2build + build2pkg
171
172=item B<sbx2pkg>
173
174sbx2build + build2pkg
175
176=item B<build2ssh>
177
178Send the tar files to a SSH host
179
180=item B<sbx2ssh>
181
182sbx2build + build2ssh
183
184=item B<cms2ssh>
185
186cms2build + build2ssh
187
188=item B<pkg2ssh>
189
190Send the packages built to a SSH host
191
192=item B<build2vm>
193
194Create packages in VMs, launching them if needed
195and send those packages to a SSH host once built
196VM type supported are QEMU
197
198=item B<build2ve>
199
200Create packages in VEs, creating it if needed
201and send those packages to a SSH host once built
202
203=item B<sbx2vm>
204
205sbx2build + build2vm
206
207=item B<sbx2ve>
208
209sbx2build + build2ve
210
211=item B<cms2vm>
212
213cms2build + build2vm
214
215=item B<cms2ve>
216
217cms2build + build2ve
218
219=item B<launchvm>
220
221Launch one virtual machine
222
223=item B<launchve>
224
225Launch one virtual environment
226
227=item B<script2vm>
228
229Launch one virtual machine if needed
230and executes a script on it
231
232=item B<script2ve>
233
234Execute a script in a virtual environment
235
236=item B<newvm>
237
238Create a new virtual machine
239
240=item B<newve>
241
242Create a new virtual environment
243
244=item B<setupvm>
245
246Setup a virtual machine for pb usage
247
248=item B<setupve>
249
250Setup a virtual environment for pb usage
251
252=item B<snapvm>
253
254Snapshot a virtual machine for pb usage
255
256=item B<snapve>
257
258Snapshot a virtual environment for pb usage
259
260=item B<test2pkg>
261
262Test a package locally
263
264=item B<test2vm>
265
266Test a package in a virtual machine
267
268=item B<test2ve>
269
270Test a package in a virtual environment
271
272=item B<newver>
273
274Create a new version of the project derived
275from the current one
276
277=item B<newproj>
278
279Create a new project and a template set of
280configuration files under pbconf
281
282=item B<announce>
283
284Announce the availability of the project through various means
285
286=item B<web2ssh>
287
288Deliver the Web site content to the target server using ssh.
289
290=item B<clean>
291
292Purge the build and delivery directories related to the current project
293
294=back
295
296<pkgs> can be a list of packages, the keyword 'all' or nothing, in which case the default list of packages is taken (corresponding to the defpkgdir list of arguments in the configuration file).
297
298=head1 WEB SITES
299
300The 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/>.
301
302=head1 USER MAILING LIST
303
304None exists for the moment.
305
306=head1 CONFIGURATION FILES
307
308Each pb user may have a configuration in F<$HOME/.pbrc>. The values in this file may overwrite any other configuration file value.
309
310Here is an example of such a configuration file:
311
312 #
313 # Define for each project the URL of its pbconf repository
314 # No default option allowed here as they need to be all different
315 #
316 # URL of the pbconf content
317 # This is the format of a classical URL with the extension of additional schema such as
318 # svn+ssh, cvs+ssh, ...
319 #
320 pbconfurl linuxcoe = cvs+ssh://:ext:bcornec@linuxcoe.cvs.sourceforge.net:/cvsroot/linuxcoe/pbconf
321
322 # This is normaly defined in the project's configuration file
323 # Url of the project
324 #
325 pburl linuxcoe = cvs+ssh://:ext:bcornec@linuxcoe.cvs.sourceforge.net:/cvsroot/linuxcoe
326 
327 # All these URLs needs to be defined here as the are the entry point
328 # for how to build packages for the project
329 #
330 pbconfurl pb = svn+ssh://svn.project-builder.org/mondo/svn/pb/pbconf
331 pbconfurl mondorescue = svn+ssh://svn.project-builder.org/mondo/svn/project-builder/mondorescue/pbconf
332 pbconfurl collectl = svn+ssh://bruno@svn.mondorescue.org/mondo/svn/project-builder/collectl/pbconf
333 pbconfurl netperf = svn+ssh://svn.mondorescue.org/mondo/svn/project-builder/netperf/pbconf
334 
335 # Under that dir will take place everything related to pb
336 # If you want to use VMs/chroot/..., then use $ENV{'HOME'} to make it portable
337 # to your VMs/chroot/...
338 # if not defined then /var/cache
339 pbdefdir default = $ENV{'HOME'}/project-builder
340 pbdefdir pb = $ENV{'HOME'}
341 pbdefdir linuxcoe = $ENV{'HOME'}/LinuxCOE/cvs
342 pbdefdir mondorescue = $ENV{'HOME'}/mondo/svn
343 
344 # pbconfdir points to the directory where the CMS content of the pbconfurl is checked out
345 # If not defined, pbconfdir is under pbdefdir/pbproj/pbconf
346 pbconfdir linuxcoe = $ENV{'HOME'}/LinuxCOE/cvs/pbconf
347 pbconfdir mondorescue = $ENV{'HOME'}/mondo/svn/pbconf
348 
349 # pbdir points to the directory where the CMS content of the pburl is checked out
350 # If not defined, pbdir is under pbdefdir/pbproj
351 # Only defined if we have access to the dev of the project
352 pbdir linuxcoe = $ENV{'HOME'}/LinuxCOE/cvs
353 pbdir mondorescue = $ENV{'HOME'}/mondo/svn
354 
355 # -daemonize doesn't work with qemu 0.8.2
356 vmopt default = -m 384
357
358=head1 AUTHORS
359
360The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
361
362=head1 COPYRIGHT
363
364Project-Builder.org is distributed under the GPL v2.0 license
365described in the file C<COPYING> included with the distribution.
366
367=cut
368
369# ---------------------------------------------------------------------------
370
371my ($projectbuilderver,$projectbuilderrev) = pb_version_init();
372my $appname = "pb";
373
374# Initialize the syntax string
375
376pb_syntax_init("$appname (aka project-builder.org) Version $projectbuilderver-$projectbuilderrev\n");
377
378GetOptions("help|?|h" => \$opts{'h'}, 
379        "man" => \$opts{'man'},
380        "verbose|v+" => \$opts{'v'},
381        "snapshot|S" => \$opts{'S'},
382        "quiet|q" => \$opts{'q'},
383        "log-files|l=s" => \$opts{'l'},
384        "force|f" => \$opts{'f'},
385        "account|a=s" => \$opts{'a'},
386        "revision|r=s" => \$opts{'r'},
387        "script|s=s" => \$opts{'s'},
388        "machines|mock|m=s" => \$opts{'m'},
389        "port|P=i" => \$opts{'P'},
390        "project|p=s" => \$opts{'p'},
391        "iso|i=s" => \$opts{'i'},
392        "version|V=s" => \$opts{'V'},
393) || pb_syntax(-1,0);
394
395if (defined $opts{'h'}) {
396    pb_syntax(0,1);
397}
398if (defined $opts{'man'}) {
399    pb_syntax(0,2);
400}
401if (defined $opts{'v'}) {
402    $pbdebug = $opts{'v'};
403}
404if (defined $opts{'f'}) {
405    $pbforce=1;
406}
407if (defined $opts{'q'}) {
408    $pbdebug=-1;
409}
410if (defined $opts{'S'}) {
411    $pbsnap=1;
412}
413if (defined $opts{'l'}) {
414    open(pbLOG,"> $opts{'l'}") || die "Unable to log to $opts{'l'}: $!";
415    $pbLOG = \*pbLOG;
416    $pbdebug = 0  if ($pbdebug == -1);
417    }
418pb_log_init($pbdebug, $pbLOG);
419pb_display_init("text","");
420
421# Handle root of the project if defined
422if (defined $opts{'r'}) {
423    $ENV{'PBROOTDIR'} = $opts{'r'};
424}
425# Handle virtual machines if any
426if (defined $opts{'m'}) {
427    $ENV{'PBV'} = $opts{'m'};
428}
429if (defined $opts{'s'}) {
430    $pbscript = $opts{'s'};
431}
432if (defined $opts{'a'}) {
433    $pbaccount = $opts{'a'};
434    die "option -a requires a -s script option" if (not defined $pbscript);
435}
436if (defined $opts{'P'}) {
437    $pbport = $opts{'P'};
438}
439if (defined $opts{'V'}) {
440    $newver = $opts{'V'};
441}
442if (defined $opts{'i'}) {
443    $iso = $opts{'i'};
444}
445
446# Get Action
447$action = shift @ARGV;
448die pb_syntax(-1,1) if (not defined $action);
449
450my ($filteredfiles, $supfiles, $defpkgdir, $extpkgdir);
451my $pbinit = undef;
452$pbinit = 1 if ($action =~ /^newproj$/);
453
454# Handles project name if any
455# And get global params
456($filteredfiles, $supfiles, $defpkgdir, $extpkgdir) = pb_env_init($opts{'p'},$pbinit,$action);
457
458#
459# Check for command requirements
460#
461my ($req,$opt) = pb_conf_get_if("oscmd","oscmdopt");
462my ($req2,$opt2) = (undef,undef);
463$req2 = $req->{$appname} if (defined $req);
464$opt2 = $opt->{$appname} if (defined $opt);
465pb_check_requirements($req2,$opt2);
466
467pb_log(0,"Project: $ENV{'PBPROJ'}\n");
468pb_log(0,"Action: $action\n");
469
470# Act depending on action
471if ($action =~ /^cms2build$/) {
472    pb_cms2build("CMS");
473} elsif ($action =~ /^sbx2build$/) {
474    pb_cms2build("SandBox");
475} elsif ($action =~ /^build2pkg$/) {
476    pb_build2pkg();
477} elsif ($action =~ /^cms2pkg$/) {
478    pb_cms2build("CMS");
479    pb_build2pkg();
480} elsif ($action =~ /^sbx2pkg$/) {
481    pb_cms2build("SandBox");
482    pb_build2pkg();
483} elsif ($action =~ /^build2ssh$/) {
484    pb_build2ssh();
485} elsif ($action =~ /^cms2ssh$/) {
486    pb_cms2build("CMS");
487    pb_build2ssh();
488} elsif ($action =~ /^sbx2ssh$/) {
489    pb_cms2build("SandBox");
490    pb_build2ssh();
491} elsif ($action =~ /^pkg2ssh$/) {
492    pb_pkg2ssh();
493} elsif ($action =~ /^build2ve$/) {
494    pb_build2v("ve","build");
495} elsif ($action =~ /^build2vm$/) {
496    pb_build2v("vm","build");
497} elsif ($action =~ /^cms2ve$/) {
498    pb_cms2build("CMS");
499    pb_build2v("ve","build");
500} elsif ($action =~ /^sbx2ve$/) {
501    pb_cms2build("SandBox");
502    pb_build2v("ve","build");
503} elsif ($action =~ /^cms2vm$/) {
504    pb_cms2build("CMS");
505    pb_build2v("vm","build");
506} elsif ($action =~ /^sbx2vm$/) {
507    pb_cms2build("SandBox");
508    pb_build2v("vm","build");
509} elsif ($action =~ /^launchvm$/) {
510    pb_launchv("vm",$ENV{'PBV'},0);
511} elsif ($action =~ /^launchve$/) {
512    pb_launchv("ve",$ENV{'PBV'},0);
513} elsif ($action =~ /^script2vm$/) {
514    pb_script2v($pbscript,"vm");
515} elsif ($action =~ /^script2ve$/) {
516    pb_script2v($pbscript,"ve");
517} elsif ($action =~ /^newver$/) {
518    pb_newver();
519} elsif ($action =~ /^newve$/) {
520    pb_launchv("ve",$ENV{'PBV'},1);
521} elsif ($action =~ /^newvm$/) {
522    pb_launchv("vm",$ENV{'PBV'},1);
523    pb_log(0, "Please ensure that sshd is running in your VM by default\n");
524    pb_log(0, "and that it allows remote root login (PermitRootLogin yes in /etc/ssh/sshd_config)\n");
525    pb_log(0, "Also ensure that network is up, firewalling correctly configured\n");
526    pb_log(0, "and perl, sudo, ntpdate and scp/ssh installed\n");
527    pb_log(0, "You should then be able to login with ssh -p VMPORT root\@localhost (if VM started with pb)\n");
528} elsif ($action =~ /^setupve$/) {
529    pb_setup2v("ve");
530} elsif ($action =~ /^setupvm$/) {
531    pb_setup2v("vm");
532} elsif ($action =~ /^snapve$/) {
533    pb_snap2v("ve");
534} elsif ($action =~ /^snapvm$/) {
535    pb_snap2v("vm");
536} elsif ($action =~ /^test2pkg$/) {
537    pb_test2pkg();
538} elsif ($action =~ /^test2ve$/) {
539    pb_build2v("ve","test");
540} elsif ($action =~ /^test2vm$/) {
541    pb_build2v("vm","test");
542} elsif ($action =~ /^newproj$/) {
543    # Nothing to do - already done in pb_env_init
544} elsif ($action =~ /^clean$/) {
545    pb_clean();
546} elsif ($action =~ /^announce$/) {
547    # For announce only. Require avoids the systematic load of these modules
548    require DBI;
549    require DBD::SQLite;
550    require Mail::SendMail;
551
552    pb_announce();
553} elsif ($action =~ /^web2ssh$/) {
554    require DBI;
555    require DBD::SQLite;
556
557    pb_cms2build("Web");
558    pb_send2target("Web");
559} else {
560    pb_log(0,"\'$action\' is not available\n");
561    pb_syntax(-2,1);
562}
563
564sub pb_cms2build {
565
566    my $param = shift || undef;
567
568    my $pkg;
569    my @pkgs;
570    my $webdir;
571
572    my %pkgs;
573    my %pb;             # Structure to store conf info
574
575    die "pb_cms2build requires a parameter: Web, SandBox or CMS" if (not defined $param);
576
577    # If Website, then pkg is only the website
578    if ($param eq "Web") {
579        ($webdir) = pb_conf_get("webdir");
580        pb_log(2,"webdir: ".Dumper($webdir)."\n");
581        $pkgs[0] = $webdir->{$ENV{'PBPROJ'}};
582        $extpkgdir = $webdir;
583        pb_log(0,"Package: $pkgs[0]\n");
584    } else {
585        $pkg = pb_cms_get_pkg($defpkgdir,$extpkgdir);
586        @pkgs = @$pkg;
587    }
588
589    my ($scheme, $uri) = pb_cms_init($pbinit,$param);
590
591    # We need 2 lines here
592    my ($pkgv, $pkgt, $testver) = pb_conf_get_if("pkgver","pkgtag","testver");
593    my @pt = pb_conf_get_if("vmlist","velist");
594
595    # declare packager and repo for filtering
596    # TODO: Is pbrepo needed so early in the process ?
597    my ($tmp1, $tmp2) = pb_conf_get("pbpackager","pbrepo");
598    $ENV{'PBPACKAGER'} = $tmp1->{$ENV{'PBPROJ'}};
599    $ENV{'PBREPO'} = $tmp2->{$ENV{'PBPROJ'}};
600
601    foreach my $pbpkg (@pkgs) {
602        $ENV{'PBPKG'} = $pbpkg;
603
604        if ((defined $pkgv) && (defined $pkgv->{$pbpkg})) {
605            $pbver = $pkgv->{$pbpkg};
606        } else {
607            $pbver = $ENV{'PBPROJVER'};
608        }
609        # If it's a test version, then tag == 0.date
610        if ((defined $testver) && (defined $testver->{$ENV{'PBPROJ'}}) && ($testver->{$ENV{'PBPROJ'}} =~ /true/i)) {
611            $pbtag = "0.".strftime("%Y%m%d%H%M%S", @date);
612            $ENV{'PBPROJTAG'} = $pbtag;
613        } elsif ((defined $pkgt) && (defined $pkgt->{$pbpkg})) {
614            $pbtag = $pkgt->{$pbpkg};
615        } else {
616            $pbtag = $ENV{'PBPROJTAG'};
617        }
618
619        $pbrev = $ENV{'PBREVISION'};
620        pb_log(0,"\n");
621        pb_log(0,"Management of $pbpkg $pbver-$pbtag (rev $pbrev)\n");
622        die "Unable to get env var PBDESTDIR" if (not defined $ENV{'PBDESTDIR'});
623
624        # Clean up dest if necessary. The export will recreate it
625        my $dest = "$ENV{'PBDESTDIR'}/$pbpkg-$pbver";
626        pb_rm_rf($dest) if (-d $dest);
627
628        # Export CMS tree for the concerned package to dest
629        # And generate some additional files
630        $OUTPUT_AUTOFLUSH=1;
631
632        # computes in which dir we have to work
633        my $dir = $defpkgdir->{$pbpkg};
634        $dir = $extpkgdir->{$pbpkg} if (not defined $dir);
635        $dir = $webdir->{$ENV{'PBPROJ'}} if ($param eq "Web");
636        pb_log(2,"def:".Dumper($defpkgdir)." ext: ".Dumper($extpkgdir)." \n");
637
638        # Exporting content from CMS
639        my $sourcedir = undef;
640        my $sourceuri = $uri;
641        if ($param eq "SandBox") {
642            # Point to the local instance
643            $sourcedir = "$ENV{'PBDIR'}/$dir";
644        } else {
645            # Get it from a subdir of the URI with same version as localy but different root
646            $sourceuri = "$ENV{'PBDIR'}/$dir";
647            $sourceuri =~ s|^$ENV{'PBPROJDIR'}/|$uri|;
648        }
649        my $preserve = pb_cms_export($sourceuri,$sourcedir,$dest);
650
651        # Generated fake content for test versions to speed up stuff
652        my $chglog;
653
654        # Get project info on authors and log file
655        $chglog = "$ENV{'PBROOTDIR'}/$pbpkg/pbcl";
656        $chglog = "$ENV{'PBROOTDIR'}/pbcl" if (! -f $chglog);
657        $chglog = undef if (! -f $chglog);
658
659        my $authors = "$ENV{'PBROOTDIR'}/$pbpkg/pbauthors";
660        $authors = "$ENV{'PBROOTDIR'}/pbauthors" if (! -f $authors);
661        $authors = "/dev/null" if (! -f $authors);
662
663        # Extract cms log history and store it
664        if ((defined $chglog) && (! -f "$dest/NEWS")) {
665            pb_log(2,"Generating NEWS file from $chglog\n");
666            copy($chglog,"$dest/NEWS") || die "Unable to create $dest/NEWS";
667        }
668        pb_cms_log($scheme,"$ENV{'PBDIR'}/$dir",$dest,$chglog,$authors,$testver);
669
670        my %build;
671        # We want to at least build for the underlying distro
672        my ($ddir, $dver, $dfam, $dtype, $dos, $pbsuf, $pbupd, $arch) = pb_distro_init();
673        my $tmpl = "$ddir-$dver-$arch,";
674        my %patches;
675
676        # Get list of distributions for which we need to generate build files
677        if (defined $pt[0]->{$ENV{'PBPROJ'}}) {
678            $tmpl .= $pt[0]->{$ENV{'PBPROJ'}};
679        }
680        if (defined $pt[1]->{$ENV{'PBPROJ'}}) {
681            # The 2 lists needs to be grouped with a ',' separating them
682            if ($tmpl ne "") {
683                $tmpl .= ",";
684            }
685            $tmpl .= $pt[1]->{$ENV{'PBPROJ'}} 
686        }
687   
688        # Setup %pb structure to allow filtering later on, on files using that structure
689        $pb{'tag'} = $pbtag;
690        $pb{'rev'} = $pbrev;
691        $pb{'ver'} = $pbver;
692        $pb{'pkg'} = $pbpkg;
693        $pb{'date'} = $pbdate;
694        $pb{'defpkgdir'} = $defpkgdir;
695        $pb{'extpkgdir'} = $extpkgdir;
696        $pb{'chglog'} = $chglog;
697        $pb{'packager'} = $ENV{'PBPACKAGER'};
698        $pb{'proj'} = $ENV{'PBPROJ'};
699        $pb{'repo'} = $ENV{'PBREPO'};
700        $pb{'patches'} = \%patches;
701        pb_log(2,"DEBUG: pb: ".Dumper(%pb)."\n");
702   
703        # Do not do that for website
704        if ($param ne "Web") {
705            pb_log(0,"Build files are being generated for ...\n");
706            my %virt;
707            # De-duplicate similar VM and VE
708            foreach my $d (split(/,/,$tmpl)) {
709                # skip ill-formatted vms (name-ver-arch)
710                next if ($d !~ /-/);
711                $virt{$d} = $d;
712            }
713
714            foreach my $d (keys %virt) {
715                my ($name,$ver,$arch) = split(/-/,$d);
716                pb_log(0,"Bad format for $d") if ((not defined $name) || (not defined $ver) || (not defined $arch)) ;
717                chomp($arch);
718                my ($ddir, $dver, $dfam);
719                ($ddir, $dver, $dfam, $pb{'dtype'}, $pb{'os'}, $pb{'suf'}, $pb{'upd'}, $pb{'arch'}) = pb_distro_init($name,$ver,$arch);
720                pb_log(2,"DEBUG: distro tuple: ".Dumper($ddir, $dver, $dfam, $pb{'dtype'}, $pb{'suf'})."\n");
721                pb_log(2,"DEBUG Filtering PBDATE => $pbdate, PBTAG => $pbtag, PBVER => $pbver\n");
722   
723                # We need to compute the real name of the package
724                my $pbrealpkg = pb_cms_get_real_pkg($pbpkg,$pb{'dtype'});
725                $pb{'realpkg'} = $pbrealpkg;
726                pb_log(1,"Virtual package $pbpkg has a real package name of $pbrealpkg on $ddir-$dver\n") if ($pbrealpkg ne $pbpkg);
727   
728                # Filter build files from the less precise up to the most with overloading
729                # Filter all files found, keeping the name, and generating in dest
730   
731                # Find all build files first relatively to PBROOTDIR
732                # Find also all specific files referenced in the .pb conf file
733                my %bfiles = ();
734                my %pkgfiles = ();
735                $build{"$ddir-$dver-$arch"} = "yes";
736   
737                if (-d "$ENV{'PBROOTDIR'}/$pbpkg/$pb{'dtype'}") {
738                    pb_list_bfiles("$ENV{'PBROOTDIR'}/$pbpkg/$pb{'dtype'}",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
739                } elsif (-d "$ENV{'PBROOTDIR'}/$pbpkg/$dfam") {
740                    pb_list_bfiles("$ENV{'PBROOTDIR'}/$pbpkg/$dfam",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
741                } elsif (-d "$ENV{'PBROOTDIR'}/$pbpkg/$ddir") {
742                    pb_list_bfiles("$ENV{'PBROOTDIR'}/$pbpkg/$ddir",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
743                } elsif (-d "$ENV{'PBROOTDIR'}/$pbpkg/$ddir-$dver") {
744                    pb_list_bfiles("$ENV{'PBROOTDIR'}/$pbpkg/$ddir-$dver",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
745                } elsif (-d "$ENV{'PBROOTDIR'}/$pbpkg/$ddir-$dver-$arch") {
746                    pb_list_bfiles("$ENV{'PBROOTDIR'}/$pbpkg/$ddir-$dver-$arch",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
747                } else {
748                    $build{"$ddir-$dver-$arch"} = "no";
749                    next;
750                }
751                pb_log(2,"DEBUG bfiles: ".Dumper(\%bfiles)."\n");
752   
753                # Get all filters to apply
754                my $ptr = pb_get_filters($pbpkg, $pb{'dtype'}, $dfam, $ddir, $dver);
755   
756                # Prepare local patches for this distro - They are always applied first - May be a problem one day
757                foreach my $p (sort(<$ENV{'PBROOTDIR'}/$pbpkg/pbpatch/*>)) {
758                    $patches{"$ddir-$dver-$arch"} .= "," if ((defined $patches{"$ddir-$dver-$arch"}) and ($p =~ /\.all$/));
759                    $patches{"$ddir-$dver-$arch"} .= "file://$p" if ($p =~ /\.all$/);
760                    $patches{"$ddir-$dver-$arch"} .= "," if ((defined $patches{"$ddir-$dver-$arch"}) and ($p =~ /\.$pb{'dtype'}$/));
761                    $patches{"$ddir-$dver-$arch"} .= "file://$p" if ($p =~ /\.$pb{'dtype'}$/);
762                    $patches{"$ddir-$dver-$arch"} .= "," if ((defined $patches{"$ddir-$dver-$arch"}) and ($p =~ /\.$dfam$/));
763                    $patches{"$ddir-$dver-$arch"} .= "file://$p" if ($p =~ /\.$dfam$/);
764                    $patches{"$ddir-$dver-$arch"} .= "," if ((defined $patches{"$ddir-$dver-$arch"}) and ($p =~ /\.$ddir$/));
765                    $patches{"$ddir-$dver-$arch"} .= "file://$p" if ($p =~ /\.$ddir$/);
766                    $patches{"$ddir-$dver-$arch"} .= "," if ((defined $patches{"$ddir-$dver-$arch"}) and ($p =~ /\.$ddir-$dver$/));
767                    $patches{"$ddir-$dver-$arch"} .= "file://$p" if ($p =~ /\.$ddir-$dver$/);
768                    $patches{"$ddir-$dver-$arch"} .= "," if ((defined $patches{"$ddir-$dver-$arch"}) and ($p =~ /\.$ddir-$dver-$arch$/));
769                    $patches{"$ddir-$dver-$arch"} .= "file://$p" if ($p =~ /\.$ddir-$dver-$arch$/);
770                }
771   
772                # Prepare also remote patches to be included - Applied after the local ones
773                foreach my $p ("all","$pb{'dtype'}","$dfam","$ddir","$ddir-$dver","$ddir-$dver-$arch") {
774                    my $f = "$ENV{'PBROOTDIR'}/$pbpkg/pbextpatch.$p";
775                    next if (not -f $f);
776                    if (not open(PATCH,$f)) {
777                        pb_display("Unable to open existing external patch file content $f\n");
778                        next;
779                    }
780                    while (<PATCH>) {
781                        chomp();
782                        $patches{"$ddir-$dver-$arch"} .= "," if (defined $patches{"$ddir-$dver-$arch"});
783                        $patches{"$ddir-$dver-$arch"} .= "$_";
784                    }
785                    close(PATCH);
786                }
787                pb_log(2,"DEBUG: pb->patches: ".Dumper($pb{'patches'})."\n");
788   
789                # Apply now all the filters on all the files concerned
790                # destination dir depends on the type of file
791                if (defined $ptr) {
792                    # For patch support
793                    $pb{'tuple'} = "$ddir-$dver-$arch";
794                    foreach my $f (values %bfiles,values %pkgfiles) {
795                        pb_filter_file("$ENV{'PBROOTDIR'}/$f",$ptr,"$dest/pbconf/$ddir-$dver-$arch/".basename($f),\%pb);
796                    }
797                }
798            }
799            my @found;
800            my @notfound;
801            foreach my $b (keys %build) {
802                push @found,$b if ($build{$b} =~ /yes/);
803                push @notfound,$b if ($build{$b} =~ /no/);
804            }
805            pb_log(0," ... ".join(',',sort(@found))."\n");
806            pb_log(0,"No Build files found for ".join(',',sort(@notfound))."\n") if (@notfound);
807            pb_log(2,"DEBUG: patches: ".Dumper(%patches)."\n");
808        }
809
810        # Get the generic filter (all.pbf) and
811        # apply those to the non-build files including those
812        # generated by pbinit if applicable
813
814        # Get only all.pbf filter
815        my $ptr = pb_get_filters($pbpkg);
816
817        my $liste ="";
818        if (defined $filteredfiles->{$pbpkg}) {
819            foreach my $f (split(/,/,$filteredfiles->{$pbpkg})) {
820                pb_filter_file_inplace($ptr,"$dest/$f",\%pb);
821                $liste = "$f $liste";
822            }
823        }
824        pb_log(2,"Files ".$liste."have been filtered\n");
825
826        # Do not do that for website
827        if ($param ne "Web") {
828            my %tmp;
829            my $warnflag = 0;
830            # Filter potential patches (local + remote)
831            pb_log(0,"Delivering and compressing patches ");
832            foreach my $v (keys %patches) {
833                pb_mkdir_p("$dest/pbconf/$v/pbpatch");
834                foreach my $pf (split(/,/,$patches{$v})) {
835                    my $pp = basename($pf);
836                    if ($param eq "SandBox") {
837                        $warnflag = 1;
838                    }
839                    pb_cms_export($pf,undef,"$dest/pbconf/$v/pbpatch");
840                    pb_filter_file_inplace($ptr,"$dest/pbconf/$v/pbpatch/$pp",\%pb);
841                    pb_system("gzip -9f $dest/pbconf/$v/pbpatch/$pp","","quiet");
842                    $tmp{$pf} = "";
843                }
844            }
845            foreach my $v (keys %tmp) {
846                pb_log(0,"$v ");
847            }
848            pb_log(0,"\n");
849            pb_log(0,"WARNING: Patches are always taken from repository not local export\n") if ($warnflag == 1);
850        } else {
851            # Instead call News generation
852            pb_web_news2html($dest);
853            # And create an empty pbconf
854            pb_mkdir_p("$dest/pbconf");
855            # And prepare the pbscript to execute remotely
856            open(SCRIPT,"> $ENV{'PBDESTDIR'}/pbscript") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript";
857            print SCRIPT "#!/bin/bash\n";
858            print SCRIPT "#set -x\n";
859            print SCRIPT "echo ... Extracting Website content\n";
860            print SCRIPT "find . -type f | grep -Ev '^./$pbpkg-$pbver.tar.gz|^./pbscript' | xargs rm -f non-existent\n";
861            print SCRIPT "find * -type d -depth | xargs rmdir 2> /dev/null \n";
862            print SCRIPT "tar xfz $pbpkg-$pbver.tar.gz\n";
863            print SCRIPT "mv $pbpkg-$pbver/* .\n";
864            print SCRIPT "rm -f $pbpkg-$pbver.tar.gz\n";
865            print SCRIPT "rmdir $pbpkg-$pbver\n";
866            close(SCRIPT);
867        }
868
869        # Prepare the dest directory for archive
870        if (-x "$ENV{'PBROOTDIR'}/$pbpkg/pbinit") {
871            pb_filter_file("$ENV{'PBROOTDIR'}/$pbpkg/pbinit",$ptr,"$ENV{'PBTMP'}/pbinit",\%pb);
872            chmod 0755,"$ENV{'PBTMP'}/pbinit";
873            pb_system("cd $dest ; $ENV{'PBTMP'}/pbinit","Executing init script from $ENV{'PBROOTDIR'}/$pbpkg/pbinit","verbose");
874        }
875
876        # Do we have additional script to run to prepare the environement for the project ?
877        # Then include it in the pbconf delivery
878        foreach my $pbvf (<$ENV{'PBROOTDIR'}/pbv*.pre>,<$ENV{'PBROOTDIR'}/pbv*.post>, <$ENV{'PBROOTDIR'}/pbtest*>) {
879            if (-x "$pbvf") {
880                my $target = "$ENV{'PBDESTDIR'}/".basename($pbvf);
881                pb_filter_file("$pbvf",$ptr,$target,\%pb);
882                chmod 0755,"$target";
883            }
884        }
885
886        # Archive dest dir
887        chdir "$ENV{'PBDESTDIR'}" || die "Unable to change dir to $ENV{'PBDESTDIR'}";
888        if (defined $preserve) {
889            # In that case we want to preserve the original tar file for checksum purposes
890            # The one created is btw equivalent in that case to this one
891            # Maybe check basename of both to be sure they are the same ?
892            pb_log(0,"Preserving original tar file ");
893            move("$preserve","$pbpkg-$pbver.tar.gz");
894        } else {
895            # Possibility to look at PBSRC to guess more the filename
896            pb_system("tar cfz $pbpkg-$pbver.tar.gz --exclude=$pbpkg-$pbver/pbconf $pbpkg-$pbver","Creating $pbpkg tar files compressed");
897        }
898        pb_log(0,"Under $ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz\n");
899        pb_system("tar cfz $pbpkg-$pbver.pbconf.tar.gz $pbpkg-$pbver/pbconf","Creating pbconf tar files compressed");
900        pb_log(0,"Under $ENV{'PBDESTDIR'}/$pbpkg-$pbver.pbconf.tar.gz\n");
901
902        # Keep track of version-tag per pkg
903        $pkgs{$pbpkg} = "$pbver-$pbtag";
904
905        # Final cleanup
906        pb_rm_rf($dest) if (-d $dest);
907    }
908
909    # Keep track of per package version
910    pb_log(2,"DEBUG pkgs: ".Dumper(%pkgs)."\n");
911    open(PKG,"> $ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb") || die "Unable to create $ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb";
912    foreach my $pbpkg (keys %pkgs) {
913        print PKG "pbpkg $pbpkg = $pkgs{$pbpkg}\n";
914    }
915    close(PKG);
916
917    # Keep track of what is generated by default
918    # We need to store the dir and info on version-tag
919    # Base our content on the existing .pb file
920    copy("$ENV{'PBROOTDIR'}/$ENV{'PBPROJ'}.pb","$ENV{'PBDESTDIR'}/pbrc");
921    open(LAST,">> $ENV{'PBDESTDIR'}/pbrc") || die "Unable to create $ENV{'PBDESTDIR'}/pbrc";
922    print LAST "pbroot $ENV{'PBPROJ'} = $ENV{'PBROOTDIR'}\n";
923    print LAST "projver $ENV{'PBPROJ'} = $ENV{'PBPROJVER'}\n";
924    print LAST "projtag $ENV{'PBPROJ'} = $ENV{'PBPROJTAG'}\n";
925    print LAST "pbpackager $ENV{'PBPROJ'} = $ENV{'PBPACKAGER'}\n";
926    close(LAST);
927}
928
929sub pb_test2pkg {
930    # Get the running distro to test on
931    my ($ddir, $dver, $dfam, $dtype, $dos, $pbsuf, $pbupd, $arch) = pb_distro_init();
932    pb_log(2,"DEBUG: distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf, $pbupd, $arch))."\n");
933
934    # Get list of packages to test
935    # Get content saved in cms2build
936    my $ptr = pb_get_pkg();
937    @pkgs = @$ptr;
938
939    # Additional potential repo
940    pb_distro_setuprepo($ddir,$dver,$arch,$dtype);
941    foreach my $pbpkg (@pkgs) {
942        # We need to install the package to test, and deps brought with it
943        pb_distro_installdeps(undef,$dtype,$pbupd,$pbpkg);
944        pb_system("$ENV{'PBDESTDIR'}/pbtest","Launching test for $pbpkg","verbose");
945    }
946}
947
948sub pb_build2pkg {
949
950    # Get the running distro to build on
951    my ($ddir, $dver, $dfam, $dtype, $dos, $pbsuf, $pbupd, $arch) = pb_distro_init();
952    pb_log(2,"DEBUG: distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf, $pbupd, $arch))."\n");
953
954    # Get list of packages to build
955    my $ptr = pb_get_pkg();
956    @pkgs = @$ptr;
957
958    # Get content saved in cms2build
959    my ($pkg) = pb_conf_read("$ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb","pbpkg");
960    $pkg = { } if (not defined $pkg);
961
962    chdir "$ENV{'PBBUILDDIR'}";
963    my $made = ""; # pkgs made during build
964    foreach my $pbpkg (@pkgs) {
965        my $vertag = $pkg->{$pbpkg};
966        # get the version of the current package - maybe different
967        ($pbver,$pbtag) = split(/-/,$vertag);
968
969        my $src="$ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz";
970        my $src2="$ENV{'PBDESTDIR'}/$pbpkg-$pbver.pbconf.tar.gz";
971        pb_log(2,"Source file: $src\n");
972        pb_log(2,"Pbconf file: $src2\n");
973
974        pb_log(2,"Working directory: $ENV{'PBBUILDDIR'}\n");
975        if ($dtype eq "rpm") {
976            foreach my $d ('RPMS','SRPMS','SPECS','SOURCES','BUILD') {
977                if (! -d "$ENV{'PBBUILDDIR'}/$d") {
978                pb_mkdir_p("$ENV{'PBBUILDDIR'}/$d") || die "Please ensure that you can write into $ENV{'PBBUILDDIR'} to create $d\nchown the $ENV{'PBBUILDDIR'} directory to your uid";
979                }
980            }
981
982            # Remove in case a previous link/file was there
983            unlink "$ENV{'PBBUILDDIR'}/SOURCES/".basename($src);
984            symlink "$src","$ENV{'PBBUILDDIR'}/SOURCES/".basename($src) || die "Unable to symlink $src in $ENV{'PBBUILDDIR'}/SOURCES";
985            # We need to first extract the spec file
986            my @specfile = pb_extract_build_files($src2,"$pbpkg-$pbver/pbconf/$ddir-$dver-$arch/","$ENV{'PBBUILDDIR'}/SPECS","spec");
987
988            # We need to handle potential patches to upstream sources
989            pb_extract_build_files($src2,"$pbpkg-$pbver/pbconf/$ddir-$dver-$arch/pbpatch/","$ENV{'PBBUILDDIR'}/SOURCES","patch");
990
991            pb_log(2,"specfile: ".Dumper(\@specfile)."\n");
992            # set LANGUAGE to check for correct log messages
993            $ENV{'LANGUAGE'}="C";
994            # Older Redhat use _target_platform in %configure incorrectly
995            my $specialdef = "";
996            if (($ddir eq "redhat") || (($ddir eq "rhel") && ($dver eq "2.1"))) {
997                $specialdef = "--define \'_target_platform \"\"\'";
998            }
999
1000            # If needed we may add repository to the build env
1001            pb_distro_setuprepo($ddir,$dver,$arch,$dtype);
1002            foreach my $f (@specfile) {
1003                if ($f =~ /\.spec$/) {
1004                    pb_distro_installdeps($f,$dtype,$pbupd);
1005                    pb_system("rpmbuild $specialdef --define \"packager $ENV{'PBPACKAGER'}\" --define \"_topdir $ENV{'PBBUILDDIR'}\" -ba $f","Building package with $f under $ENV{'PBBUILDDIR'}","verbose");
1006                    last;
1007                }
1008            }
1009            # Get the name of the generated packages
1010            open(LOG,"$ENV{'PBTMP'}/system.log") || die "Unable to open $ENV{'PBTMP'}/system.log";
1011            while (<LOG>) {
1012                chomp($_);
1013                next if ($_ !~ /^Wrote:/);
1014                s|.*/([S]*RPMS.*)|$1|;
1015                $made .=" $_";
1016            }
1017            close(LOG);
1018
1019        } elsif ($dtype eq "deb") {
1020            chdir "$ENV{'PBBUILDDIR'}" || die "Unable to chdir to $ENV{'PBBUILDDIR'}";
1021            pb_system("tar xfz $src","Extracting sources");
1022            pb_system("tar xfz $src2","Extracting pbconf");
1023
1024            chdir "$pbpkg-$pbver" || die "Unable to chdir to $pbpkg-$pbver";
1025            pb_rm_rf("debian");
1026            symlink "pbconf/$ddir-$dver-$arch","debian" || die "Unable to symlink to pbconf/$ddir-$dver-$arch";
1027            chmod 0755,"debian/rules";
1028
1029            pb_distro_setuprepo($ddir,$dver,$arch,$dtype);
1030            pb_distro_installdeps("debian/control",$dtype,$pbupd);
1031            pb_system("dpkg-buildpackage -us -uc -rfakeroot","Building package","verbose");
1032            # Get the name of the generated packages
1033            open(LOG,"$ENV{'PBTMP'}/system.log") || die "Unable to open $ENV{'PBTMP'}/system.log";
1034            while (<LOG>) {
1035                chomp();
1036                my $tmp = $_;
1037                next if ($tmp !~ /^dpkg-deb.*:/);
1038                $tmp =~ s|.*../(.*)_(.*).deb.*|$1|;
1039                $made="$made $tmp.dsc $tmp.tar.gz $tmp"."_*.deb $tmp"."_*.changes";
1040            }
1041            close(LOG);
1042        } elsif ($dtype eq "ebuild") {
1043            my @ebuildfile;
1044            # For gentoo we need to take pb as subsystem name
1045            # We put every apps here under sys-apps. hope it's correct
1046            # We use pb's home dir in order to have a single OVERLAY line
1047            my $tmpd = "$ENV{'HOME'}/portage/pb/sys-apps/$pbpkg";
1048            pb_mkdir_p($tmpd) if (! -d "$tmpd");
1049            pb_mkdir_p("$ENV{'HOME'}/portage/distfiles") if (! -d "$ENV{'HOME'}/portage/distfiles");
1050
1051            # We need to first extract the ebuild file
1052            @ebuildfile = pb_extract_build_files($src2,"$pbpkg-$pbver/pbconf/$ddir-$dver-$arch/","$tmpd","ebuild");
1053
1054            # Prepare the build env for gentoo
1055            my $found = 0;
1056            my $pbbd = $ENV{'HOME'};
1057            $pbbd =~ s|/|\\/|g;
1058            if (-r "/etc/make.conf") {
1059                open(MAKE,"/etc/make.conf");
1060                while (<MAKE>) {
1061                    $found = 1 if (/$pbbd\/portage/);
1062                }
1063                close(MAKE);
1064            }
1065            if ($found == 0) {
1066                pb_system("sudo sh -c 'echo PORTDIR_OVERLAY=\"$ENV{'HOME'}/portage\" >> /etc/make.conf'");
1067            }
1068            #$found = 0;
1069            #if (-r "/etc/portage/package.keywords") {
1070            #open(KEYW,"/etc/portage/package.keywords");
1071            #while (<KEYW>) {
1072            #$found = 1 if (/portage\/pb/);
1073            #}
1074            #close(KEYW);
1075            #}
1076            #if ($found == 0) {
1077            #pb_system("sudo sh -c \"echo portage/pb >> /etc/portage/package.keywords\"");
1078            #}
1079
1080            # Build
1081            foreach my $f (@ebuildfile) {
1082                if ($f =~ /\.ebuild$/) {
1083                    pb_distro_installdeps($f,$dtype,$pbupd);
1084                    move($f,"$tmpd/$pbpkg-$pbver.ebuild");
1085                    pb_system("cd $tmpd ; ebuild $pbpkg-$pbver.ebuild clean ; ebuild $pbpkg-$pbver.ebuild digest ; ebuild $pbpkg-$pbver.ebuild package","verbose");
1086                    # Now move it where pb expects it
1087                    pb_mkdir_p("$ENV{'PBBUILDDIR'}/portage/pb/sys-apps/$pbpkg");
1088                    move("$tmpd/$pbpkg-$pbver.ebuild","$ENV{'PBBUILDDIR'}/portage/pb/sys-apps/$pbpkg/$pbpkg-$pbver-r$pbtag.ebuild");
1089                }
1090            }
1091
1092            $made="$made portage/pb/sys-apps/$pbpkg/$pbpkg-$pbver-r$pbtag.ebuild";
1093        } elsif ($dtype eq "tgz") {
1094            # Slackware family
1095            $made="$made $pbpkg/$pbpkg-$pbver-*-$pbtag.tgz";
1096
1097            chdir "$ENV{'PBBUILDDIR'}" || die "Unable to chdir to $ENV{'PBBUILDDIR'}";
1098            pb_system("tar xfz $src","Extracting sources");
1099            pb_system("tar xfz $src2","Extracting pbconf");
1100            chdir "$pbpkg-$pbver" || die "Unable to chdir to $pbpkg-$pbver";
1101            symlink "pbconf/$ddir-$dver-$arch","install" || die "Unable to symlink to pbconf/$ddir-$dver-$arch";
1102            if (-x "install/pbslack") {
1103                pb_distro_installdeps("./install/pbslack",$dtype,$pbupd);
1104                pb_system("./install/pbslack","Building software");
1105                pb_system("sudo /sbin/makepkg -p -l y -c y $pbpkg","Packaging $pbpkg","verbose");
1106            }
1107        } elsif ($dtype eq "pkg") {
1108            # Solaris
1109            $made="$made $pbpkg-$pbver-$pbtag.pkg.gz";
1110            my $pkgdestdir="$ENV{'PBBUILDDIR'}/install";
1111
1112            chdir "$ENV{'PBBUILDDIR'}" || die "Unable to chdir to $ENV{'PBBUILDDIR'}";
1113            # Will host resulting packages
1114            pb_mkdir_p("$dtype");
1115            pb_mkdir_p("$pkgdestdir/delivery");
1116            pb_system("tar xfz $src","Extracting sources under $ENV{'PBBUILDDIR'}");
1117            pb_system("tar xfz $src2","Extracting pbconf under $ENV{'PBBUILDDIR'}");
1118            chdir "$pbpkg-$pbver" || die "Unable to chdir to $pbpkg-$pbver";
1119            if (-f "pbconf/$ddir-$dver-$arch/pbbuild") {
1120                chmod 0755,"pbconf/$ddir-$dver-$arch/pbbuild";
1121                # pkginfo file is mandatory
1122                die "Unable to find pkginfo file in pbconf/$ddir-$dver-$arch" if (! -f "pbconf/$ddir-$dver-$arch/pkginfo");
1123                # Build
1124                pb_system("pbconf/$ddir-$dver-$arch/pbbuild $pkgdestdir/delivery","Building software and installing under $pkgdestdir/delivery");
1125                # Copy complementary files
1126                if (-f "pbconf/$ddir-$dver-$arch/prototype") {
1127                    copy("pbconf/$ddir-$dver-$arch/prototype", $pkgdestdir) 
1128                } else {
1129                    # No prototype provided, calculating it
1130                    open(PROTO,"> $pkgdestdir/prototype") || die "Unable to create prototype file";
1131                    print PROTO "i pkginfo\n";
1132                    print PROTO "i depend\n" if (-f "pbconf/$ddir-$dver-$arch/depend");
1133                    $ENV{'PBSOLDESTDIR'} = "$pkgdestdir/delivery";
1134                    find(\&create_solaris_prototype, "$pkgdestdir/delivery");
1135                }
1136                copy("pbconf/$ddir-$dver-$arch/depend", $pkgdestdir) if (-f "pbconf/$ddir-$dver-$arch/depend");
1137                copy("pbconf/$ddir-$dver-$arch/pkginfo", $pkgdestdir);
1138                pb_system("cd $pkgdestdir/delivery ; pkgmk -o -f ../prototype -r $pkgdestdir/delivery -d $ENV{'PBBUILDDIR'}/$dtype","Packaging $pbpkg","verbose");
1139                pb_system("cd $ENV{'PBBUILDDIR'}/$dtype ;  echo \"\" | pkgtrans -o -n -s $ENV{'PBBUILDDIR'}/$dtype $ENV{'PBBUILDDIR'}/$pbpkg-$pbver-$pbtag.pkg all","Transforming $pbpkg","verbose");
1140                pb_system("cd $ENV{'PBBUILDDIR'} ;  gzip -9f $pbpkg-$pbver-$pbtag.pkg","Compressing $pbpkg-$pbver-$pbtag.pkg","verbose");
1141            } else {
1142                pb_log(0,"No pbconf/$ddir-$dver-$arch/pbbuild file found for $pbpkg-$pbver in \n");
1143            }
1144            chdir ".." || die "Unable to chdir to parent dir";
1145            pb_system("rm -rf $pbpkg-$pbver $ENV{'PBBUILDDIR'}/$dtype $pkgdestdir", "Cleanup");
1146        } else {
1147            die "Unknown dtype format $dtype";
1148        }
1149    }
1150    # Packages check if needed
1151    if ($dtype eq "rpm") {
1152        if (-f "/usr/bin/rpmlint") {
1153            pb_system("rpmlint $made","Checking validity of rpms with rpmlint","verbose");
1154        }
1155        my $rpms ="";
1156        my $srpms ="";
1157        foreach my $f (split(/ /,$made)) {
1158            $rpms .= "$ENV{'PBBUILDDIR'}/$f " if ($f =~ /^RPMS\//);
1159            $srpms .= "$ENV{'PBBUILDDIR'}/$f " if ($f =~ /^SRPMS\//);
1160        }
1161        pb_log(0,"SRPM packages generated: $srpms\n");
1162        pb_log(0,"RPM packages generated: $rpms\n");
1163    } elsif ($dtype eq "deb") {
1164        my $made2 = "";
1165        foreach my $f (split(/ /,$made)) {
1166            $made2 .= "../$f " if ($f =~ /\.changes$/);
1167        }
1168        if (-f "/usr/bin/lintian") {
1169            pb_system("lintian $made2","Checking validity of debs with lintian","verbose");
1170        } else {
1171            pb_log(0,"deb packages generated: $made2\n");
1172        }
1173    } else {
1174        pb_log(0,"No check done for $dtype yet\n");
1175        pb_log(0,"Packages generated: $made\n");
1176    }
1177
1178    # Keep track of what is generated so that we can get them back from VMs
1179    open(KEEP,"> $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}") || die "Unable to create $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}";
1180    print KEEP "$made\n";
1181    close(KEEP);
1182}
1183
1184sub create_solaris_prototype {
1185
1186    my $uidgid = "bin bin";
1187    my $pkgdestdir = $ENV{'PBSOLDESTDIR'};
1188
1189    return if ($_ =~ /^$pkgdestdir$/);
1190    if (-d $_) {
1191        my $n = $File::Find::name;
1192        $n =~ s~$pkgdestdir/~~;
1193        print PROTO "d none $n 0755 $uidgid\n";
1194    } elsif (-x $_) {
1195        my $n = $File::Find::name;
1196        $n =~ s~$pkgdestdir/~~;
1197        print PROTO "f none $n 0755 $uidgid\n";
1198    } elsif (-f $_) {
1199        my $n = $File::Find::name;
1200        $n =~ s~$pkgdestdir/~~;
1201        print PROTO "f none $n 0644 $uidgid\n";
1202    }
1203}
1204
1205sub pb_build2ssh {
1206    pb_send2target("Sources");
1207}
1208
1209sub pb_pkg2ssh {
1210    pb_send2target("Packages");
1211}
1212
1213# By default deliver to the the public site hosting the
1214# ftp structure (or whatever) or a VM/VE
1215sub pb_send2target {
1216
1217    my $cmt = shift;
1218    my $v = shift || undef;
1219    my $vmexist = shift || 0;           # 0 is FALSE
1220    my $vmpid = shift || 0;             # 0 is FALSE
1221    my $snapme = shift || 0;            # 0 is FALSE
1222
1223    pb_log(2,"DEBUG: pb_send2target($cmt,".Dumper($v).",$vmexist,$vmpid)\n");
1224    my $host = "sshhost";
1225    my $login = "sshlogin";
1226    my $dir = "sshdir";
1227    my $port = "sshport";
1228    my $conf = "sshconf";
1229    my $tmout = undef;
1230    my $path = undef;
1231    if ($cmt =~ /^VM/) {
1232        $login = "vmlogin";
1233        $dir = "pbdefdir";
1234        # Specific VM
1235        $tmout = "vmtmout";
1236        $path = "vmpath";
1237        $host = "vmhost";
1238        $port = "vmport";
1239    } elsif ($cmt =~ /^VE/) {
1240        $login = "velogin";
1241        $dir = "pbdefdir";
1242        # Specific VE
1243        $path = "vepath";
1244        $conf = "rbsconf";
1245    } elsif ($cmt eq "Web") {
1246        $host = "websshhost";
1247        $login = "websshlogin";
1248        $dir = "websshdir";
1249        $port = "websshport";
1250    }
1251    my $cmd = "";
1252    my $src = "";
1253    my ($odir,$over,$oarch) = (undef, undef, undef);
1254    my ($ddir, $dver, $dfam, $dtype, $dos, $pbsuf, $pbupd, $darch);
1255
1256    if ($cmt ne "Announce") {
1257        # Get list of packages to build
1258        my $ptr = pb_get_pkg();
1259        @pkgs = @$ptr;
1260
1261        # Get the running distro to consider
1262        if (defined $v) {
1263            ($odir,$over,$oarch) = split(/-/,$v);
1264        }
1265        ($ddir, $dver, $dfam, $dtype, $dos, $pbsuf, $pbupd, $darch) = pb_distro_init($odir,$over,$oarch);
1266        pb_log(2,"DEBUG: distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf, $darch))."\n");
1267
1268        # Get list of packages to build
1269        # Get content saved in cms2build
1270        my ($pkg) = pb_conf_read("$ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb","pbpkg");
1271        $pkg = { } if (not defined $pkg);
1272
1273        chdir "$ENV{'PBBUILDDIR'}";
1274        foreach my $pbpkg (@pkgs) {
1275            my $vertag = $pkg->{$pbpkg};
1276            # get the version of the current package - maybe different
1277            ($pbver,$pbtag) = split(/-/,$vertag);
1278
1279            if (($cmt eq "Sources") || ($cmt =~ /V[EM]build/)) {
1280                $src = "$src $ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz $ENV{'PBDESTDIR'}/$pbpkg-$pbver.pbconf.tar.gz";
1281                if ($cmd eq "") {
1282                    $cmd = "ln -sf $pbpkg-$pbver.tar.gz $pbpkg-latest.tar.gz";
1283                } else {
1284                    $cmd = "$cmd ; ln -sf $pbpkg-$pbver.tar.gz $pbpkg-latest.tar.gz";
1285                }
1286            } elsif ($cmt eq "Web") {
1287                $src = "$src $ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz"
1288            }
1289        }
1290        # Adds conf file for availability of conf elements
1291        pb_conf_add("$ENV{'PBROOTDIR'}/$ENV{'PBPROJ'}.pb");
1292    }
1293
1294    if ($cmt =~ /V[EM]build/) {
1295        $src="$src $ENV{'PBROOTDIR'}/$ENV{'PBPROJ'}.pb $ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb $ENV{'PBETC'} $ENV{'PBDESTDIR'}/pbrc $ENV{'PBDESTDIR'}/pbscript";
1296    } elsif (($cmt =~ /V[EM]Script/) || ($cmt eq "Web")) {
1297        $src="$src $ENV{'PBDESTDIR'}/pbscript";
1298    } elsif ($cmt =~ /V[EM]test/) {
1299        $src="$src $ENV{'PBROOTDIR'}/$ENV{'PBPROJ'}.pb $ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb $ENV{'PBETC'} $ENV{'PBDESTDIR'}/pbrc $ENV{'PBDESTDIR'}/pbscript $ENV{'PBDESTDIR'}/pbtest";
1300    } elsif ($cmt eq "Announce") {
1301        $src="$src $ENV{'PBTMP'}/pbscript";
1302    } elsif ($cmt eq "Packages") {
1303        # Get package list from file made during build2pkg
1304        open(KEEP,"$ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}") || die "Unable to read $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}";
1305        $src = <KEEP>;
1306        chomp($src);
1307        close(KEEP);
1308        $src="$src $ENV{'PBBUILDDIR'}/pbscript";
1309    }
1310    # Remove potential leading spaces (cause problem with basename)
1311    $src =~ s/^ *//;
1312    my $basesrc = "";
1313    foreach my $i (split(/ +/,$src)) {
1314        $basesrc .= " ".basename($i);
1315    }
1316
1317    pb_log(0,"Sources handled ($cmt): $src\n");
1318    pb_log(2,"values: ".Dumper(($host,$login,$dir,$port,$tmout,$path,$conf))."\n");
1319    my ($sshhost,$sshlogin,$sshdir,$sshport) = pb_conf_get($host,$login,$dir,$port);
1320    # Not mandatory...
1321    my ($rbsconf,$testver,$delivery) = pb_conf_get_if($conf,"testver","delivery");
1322    $delivery->{$ENV{'PBPROJ'}} = "" if (not defined $delivery->{$ENV{'PBPROJ'}});
1323    my ($vtmout,$vepath);
1324    # ...Except those in virtual context
1325    if ($cmt =~ /^VE/) {
1326        ($vepath) = pb_conf_get($path);
1327    }
1328    if ($cmt =~ /^VM/) {
1329        ($vtmout) = pb_conf_get($tmout);
1330    }
1331    pb_log(2,"ssh: ".Dumper(($sshhost,$sshlogin,$sshdir,$sshport,$vtmout,$vepath,$rbsconf))."\n");
1332
1333    my $mac;
1334    if ($cmt !~ /^VE/) {
1335        $mac = "$sshlogin->{$ENV{'PBPROJ'}}\@$sshhost->{$ENV{'PBPROJ'}}";
1336        # Overwrite account value if passed as parameter
1337        $mac = "$pbaccount\@$sshhost->{$ENV{'PBPROJ'}}" if (defined $pbaccount);
1338        pb_log(2, "DEBUG: pbaccount: $pbaccount => mac: $mac\n") if (defined $pbaccount);
1339    } else {
1340        # VE
1341        # Overwrite account value if passed as parameter (typically for setup2ve)
1342        $mac = $sshlogin->{$ENV{'PBPROJ'}};
1343        $mac = $pbaccount if (defined $pbaccount);
1344    }
1345
1346    my $tdir;
1347    my $bdir;
1348    if (($cmt eq "Sources") || ($cmt =~ /V[EM]Script/)) {
1349        $tdir = "$sshdir->{$ENV{'PBPROJ'}}/$delivery->{$ENV{'PBPROJ'}}/src";
1350    } elsif (($cmt =~ /V[EM]build/)  || ($cmt =~ /V[EM]test/)) {
1351        $tdir = $sshdir->{$ENV{'PBPROJ'}}."/$ENV{'PBPROJ'}/delivery";
1352        $bdir = $sshdir->{$ENV{'PBPROJ'}}."/$ENV{'PBPROJ'}/build";
1353        # Remove a potential $ENV{'HOME'} as bdir should be relative to pb's home
1354        $bdir =~ s|\$ENV.+\}/||;
1355    } elsif ($cmt eq "Announce") {
1356        $tdir = "$sshdir->{$ENV{'PBPROJ'}}/$delivery->{$ENV{'PBPROJ'}}";
1357    } elsif ($cmt eq "Web") {
1358        $tdir = "$sshdir->{$ENV{'PBPROJ'}}/$delivery->{$ENV{'PBPROJ'}}";
1359    } elsif ($cmt eq "Packages") {
1360        $tdir = "$sshdir->{$ENV{'PBPROJ'}}/$delivery->{$ENV{'PBPROJ'}}/$ddir/$dver/$darch";
1361
1362        my $repodir = $tdir;
1363        $repodir =~ s|^$sshdir->{$ENV{'PBPROJ'}}/||;
1364
1365        my ($pbrepo) = pb_conf_get("pbrepo");
1366
1367        # Repository management
1368        open(PBS,"> $ENV{'PBBUILDDIR'}/pbscript") || die "Unable to create $ENV{'PBBUILDDIR'}/pbscript";
1369        if ($dtype eq "rpm") {
1370            # Also make a pbscript to generate yum/urpmi bases
1371            print PBS << "EOF";
1372#!/bin/bash
1373# Prepare a script to ease yum setup
1374cat > $ENV{'PBPROJ'}.repo << EOT
1375[$ENV{'PBPROJ'}]
1376name=$ddir $dver $darch - $ENV{'PBPROJ'} Vanilla Packages
1377baseurl=$pbrepo->{$ENV{'PBPROJ'}}/$repodir
1378enabled=1
1379gpgcheck=0
1380EOT
1381chmod 644 $ENV{'PBPROJ'}.repo
1382
1383# Clean up old repo content
1384rm -rf headers/ repodata/
1385# Create yum repo
1386if [ -x /usr/bin/yum-arch ]; then
1387    yum-arch .
1388fi
1389# Create repodata
1390createrepo .
1391EOF
1392            if ($dfam eq "md") {
1393                # For Mandriva add urpmi management
1394                print PBS << "EOF";
1395# Prepare a script to ease urpmi setup
1396cat > $ENV{'PBPROJ'}.addmedia << EOT
1397urpmi.addmedia $ENV{'PBPROJ'} $pbrepo->{$ENV{'PBPROJ'}}/$repodir with media_info/hdlist.cz
1398EOT
1399chmod 755 $ENV{'PBPROJ'}.addmedia
1400
1401# Clean up old repo content
1402rm -f hdlist.cz synthesis.hdlist.cz
1403# Create urpmi repo
1404genhdlist2 --clean .
1405if [ \$\? -ne 0 ]; then
1406    genhdlist .
1407fi
1408EOF
1409            }
1410            if ($ddir eq "fedora") {
1411                # Extract the spec file to please Fedora maintainers :-(
1412                print PBS << "EOF";
1413for p in $basesrc; do
1414    echo \$p | grep -q 'src.rpm'
1415    if [ \$\? -eq 0 ]; then
1416        rpm2cpio \$p | cpio -ivdum --quiet '*.spec'
1417    fi
1418done
1419EOF
1420            }
1421            if ($dfam eq "novell") {
1422                # Add ymp scripts for one-click install on SuSE
1423                print PBS << "EOF";
1424# Prepare a script to ease SuSE one-click install
1425# Cf: http://de.opensuse.org/1-Klick-Installation/ISV
1426#
1427cat > $ENV{'PBPROJ'}.ymp << EOT
1428<?xml version="1.0" encoding="utf-8"?>
1429<!-- vim: set sw=2 ts=2 ai et: -->
1430<metapackage xmlns:os="http://opensuse.org/Standards/One_Click_Install" xmlns="http://opensuse.org/Standards/One_Click_Install">
1431    <group><!-- The group of software, typically one for project-builder.org -->
1432        <name>$ENV{'PBPROJ'} Bundle</name> <!-- Name of the software group -->
1433        <summary>Software bundle for the $ENV{'PBPROJ'} project</summary> <!--This message is shown to the user and should describe the whole bundle -->
1434        <description>This is the summary of the $ENV{'PBPROJ'} Project
1435             
1436            Details are available on a per package basis below
1437
1438        </description><!--This is also shown to the user -->
1439        <remainSubscribed>false</remainSubscribed> <!-- Don't know what it mean -->
1440        <repositories><!-- List of needed repositories -->
1441            <repository>
1442                <name>$ENV{'PBPROJ'} Repository</name> <!-- Name of the repository  -->
1443                <summary>This repository contains the $ENV{'PBPROJ'} project packages.</summary> <!-- Summary of the repository -->
1444                <description>This repository contains the $ENV{'PBPROJ'} project packages.</description><!-- This description is shown to the user -->
1445                <url>$pbrepo->{$ENV{'PBPROJ'}}/$repodir</url><!--URL of repository, which is added -->
1446            </repository>
1447        </repositories>
1448        <software><!-- A List of packages, which should be added through the one-click-installation -->
1449EOT
1450for p in $basesrc; do
1451    sum=`rpm -q --qf '%{SUMMARY}' \$p`
1452    name=`rpm -q --qf '%{NAME}' \$p`
1453    desc=`rpm -q --qf '%{description}' \$p`
1454    cat >> $ENV{'PBPROJ'}.ymp << EOT
1455            <item>
1456                <name>\$name</name><!-- Name of the package, is shown to the user and used to identify the package at the repository -->
1457                <summary>\$sum</summary> <!-- Summary of the package -->
1458                <description>\$desc</description> <!-- Description, is shown to the user -->
1459            </item>
1460EOT
1461done
1462cat >> $ENV{'PBPROJ'}.ymp << EOT
1463        </software>
1464    </group>
1465</metapackage>
1466EOT
1467chmod 644 $ENV{'PBPROJ'}.ymp
1468EOF
1469            }
1470        } elsif ($dtype eq "deb") {
1471            # Also make a pbscript to generate apt bases
1472            # Cf: http://www.debian.org/doc/manuals/repository-howto/repository-howto.fr.html
1473            my $rpd = dirname("$pbrepo->{$ENV{'PBPROJ'}}/$repodir");
1474            print PBS << "EOF";
1475#!/bin/bash
1476# Prepare a script to ease apt setup
1477cat > $ENV{'PBPROJ'}.sources.list << EOT
1478deb $rpd $dver contrib
1479deb-src $rpd $dver contrib
1480EOT
1481chmod 644 $ENV{'PBPROJ'}.sources.list
1482
1483# Prepare a script to create apt info file
1484(cd .. ; for a in i386 amd64 ia64; do mkdir -p dists/$dver/contrib/binary-\$a; dpkg-scanpackages -a\$a $dver/$darch /dev/null | gzip -c9 > dists/$dver/contrib/binary-\$a/Packages.gz; done; mkdir -p dists/$dver/contrib/source; dpkg-scansources $dver/$darch /dev/null | gzip -c9 > dists/$dver/contrib/source/Sources.gz)
1485#(cd .. ; rm -f dists/$dver/Release ; apt-ftparchive release dists/$dver > dists/$dver/Release; gpg --sign -ba -o dists/$dver/Release.gpg dists/$dver/Release)
1486EOF
1487        } elsif ($dtype eq "ebuild") {
1488            # make a pbscript to generate links to latest version
1489            print PBS << "EOF";
1490#!/bin/bash
1491# Prepare a script to create correct links
1492for p in $src; do
1493    echo \$p | grep -q '.ebuild'
1494    if [ \$\? -eq 0 ]; then
1495        j=`basename \$p`
1496        pp=`echo \$j | cut -d'-' -f1`
1497        ln -sf \$j \$pp.ebuild
1498    fi
1499done
1500EOF
1501        }
1502        close(PBS);
1503        chmod 0755,"$ENV{'PBBUILDDIR'}/pbscript";
1504    } else {
1505        return;
1506    }
1507
1508    # Useless for VE
1509    my $nport;
1510    if ($cmt !~ /^VE/) {
1511        $nport = $sshport->{$ENV{'PBPROJ'}};
1512        $nport = "$pbport" if (defined $pbport);
1513    }
1514
1515    # Remove a potential $ENV{'HOME'} as tdir should be relative to pb's home
1516    $tdir =~ s|\$ENV.+\}/||;
1517
1518    my $tm = undef;
1519    if ($cmt =~ /^VM/) {
1520        $tm = $vtmout->{$ENV{'PBPROJ'}};
1521    }
1522
1523    # ssh communication if not VE
1524    # should use a hash instead...
1525    my ($shcmd,$cpcmd,$cptarget,$cp2target);
1526    if ($cmt !~ /^VE/) {
1527        my $keyfile = pb_ssh_get(0);
1528        $shcmd = "ssh -i $keyfile -q -o UserKnownHostsFile=/dev/null -p $nport $mac";
1529        $cpcmd = "scp -i $keyfile -p -o UserKnownHostsFile=/dev/null -P $nport";
1530        $cptarget = "$mac:$tdir";
1531        if ($cmt =~ /^VMbuild/) {
1532            $cp2target = "$mac:$bdir";
1533        }
1534    } else {
1535        my $tp = $vepath->{$ENV{'PBPROJ'}};
1536        ($odir,$over,$oarch) = split(/-/,$v);
1537        my $tpdir = "$tp/$odir/$over/$oarch";
1538        my ($ptr) = pb_conf_get("vetype");
1539        my $vetype = $ptr->{$ENV{'PBPROJ'}};
1540        if ($vetype eq "chroot") {
1541            $shcmd = "sudo chroot $tpdir /bin/su - $mac -c ";
1542        } elsif ($vetype eq "schroot") {
1543            $shcmd = "schroot $tp -u $mac -- ";
1544        }
1545        $cpcmd = "sudo cp -r ";
1546        # We need to get the home dir of the target account to deliver in the right place
1547        open(PASS,"$tpdir/etc/passwd") || die "Unable to open $tpdir/etc/passwd";
1548        my $homedir = "";
1549        while (<PASS>) {
1550            my ($c1,$c2,$c3,$c4,$c5,$c6,$c7) = split(/:/);
1551            $homedir = $c6 if ($c1 =~ /^$mac$/);
1552            pb_log(3,"Homedir: $homedir - account: $c6\n");
1553        }
1554        close(PASS);
1555        $cptarget = "$tpdir/$homedir/$tdir";
1556        if ($cmt eq "VEbuild") {
1557            $cp2target = "$tpdir/$homedir/$bdir";
1558        }
1559        pb_log(2,"On VE using $cptarget as target dir to copy to\n");
1560    }
1561
1562    my $logres = "";
1563    # Do not touch when just announcing
1564    if ($cmt ne "Announce") {
1565        pb_system("$shcmd \"mkdir -p $tdir ; cd $tdir ; echo \'for i in $basesrc; do if [ -f \$i ]; then rm -f \$i; fi; done\ ; $cmd' | bash\"","Preparing $tdir on $cptarget");
1566    } else {
1567        $logres = "> ";
1568    }
1569    pb_system("cd $ENV{'PBBUILDDIR'} ; $cpcmd $src $cptarget 2> /dev/null","$cmt delivery in $cptarget");
1570
1571    # For VE we need to change the owner manually
1572    if ($cmt =~ /^VE/) {
1573        pb_system("$shcmd \"sudo chown -R $mac $tdir\"","Adapt owner in $tdir to $mac");
1574    }
1575
1576    pb_system("$shcmd \"echo \'cd $tdir ; if [ -x pbscript ]; then ./pbscript; fi ; rm -f ./pbscript\' | bash\"","Executing pbscript on $cptarget if needed","verbose");
1577    if ($cmt =~ /^V[EM]build/) {
1578        # Get back info on pkg produced, compute their name and get them from the VM
1579        pb_system("$cpcmd $cp2target/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'} $ENV{'PBBUILDDIR'} 2> /dev/null","Get package names in $cp2target");
1580        # For VE we need to change the owner manually
1581        if ($cmt eq "VEbuild") {
1582            pb_system("sudo chown $UID $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}","Adapt owner in $tdir to $UID");
1583        }
1584        # return here to avoid breaking the load on VMs/VEs in case of a bad one
1585        if (not -f "$ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}") {
1586            pb_log(0,"Problem with VM $v on $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}");
1587            return;
1588        }
1589        open(KEEP,"$ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}") || die "Unable to read $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}";
1590        my $src = <KEEP>;
1591        chomp($src);
1592        close(KEEP);
1593        $src =~ s/^ *//;
1594        pb_mkdir_p("$ENV{'PBBUILDDIR'}/$odir/$over");
1595        # Change pgben to make the next send2target happy
1596        my $made = "";
1597        open(KEEP,"> $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}") || die "Unable to write $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}";
1598        foreach my $p (split(/ +/,$src)) {
1599            my $j = basename($p);
1600            # For VM we don't want shell expansion to hapen locally but remotely
1601            my $delim = '\'';
1602            if ($cmt =~ /^VEbuild/) {
1603                # For VE we need to support shell expansion locally
1604                $delim = "";
1605            }   
1606            pb_system("$cpcmd $cp2target/$delim$p$delim $ENV{'PBBUILDDIR'}/$odir/$over 2> /dev/null","Recovery of package $j in $ENV{'PBBUILDDIR'}/$odir/$over");
1607            $made="$made $odir/$over/$j"; # if (($dtype ne "rpm") || ($j !~ /.src.rpm$/));
1608        }
1609        print KEEP "$made\n";
1610        close(KEEP);
1611        pb_system("$shcmd \"rm -rf $tdir $bdir\"","$cmt cleanup");
1612
1613        # Sign packages locally
1614        if ($dtype eq "rpm") {
1615                #pb_system("rpm --addsign --define \"_signature gpg\" --define \"_gpg_name $ENV{'PBPACKAGER'}\" --define \"__gpg_sign_cmd /usr/bin/gpg --batch --no-verbose --no-armor --no-tty --no-secmem-warning -sbo %{__signature_filename} %{__plaintext_filename} --use-agent\" $made","Signing RPM packages packages");
1616        } elsif ($dtype eq "deb") {
1617                #pb_system("debsign $made","Signing DEB packages");
1618        } else {
1619            pb_log(0,"I don't know yet how to sign packages for type $dtype.\nPlease give feedback to dev team\n");
1620        }
1621
1622        # We want to send them to the ssh account so overwrite what has been done before
1623        undef $pbaccount;
1624        pb_log(2,"Before sending pkgs, vmexist: $vmexist, vmpid: $vmpid\n");
1625        pb_send2target("Packages",$odir."-".$over."-".$oarch,$vmexist,$vmpid);
1626        pb_rm_rf("$ENV{'PBBUILDDIR'}/$odir");
1627    }
1628    pb_log(2,"Before halt, vmexist: $vmexist, vmpid: $vmpid\n");
1629    if ((! $vmexist) && ($cmt =~ /^VM/)) {
1630        # If in setupvm then takes a snapshot just before halting
1631        if ($snapme != 0) {
1632            my ($vmmonport,$vmtype) = pb_conf_get("vmmonport","vmtype");
1633            # For monitoring control
1634            if ((($vmtype->{$ENV{'PBPROJ'}}) eq "kvm") || (($vmtype->{$ENV{'PBPROJ'}}) eq "qemu")) {
1635                require Net::Telnet;
1636                my $t = new Net::Telnet (Timeout => 120, Host => "localhost", Port => $vmmonport->{$ENV{'PBPROJ'}}) || die "Unable to dialog on the monitor";
1637                # move to monitor mode
1638                my @lines = $t->cmd("c");
1639                # Create a snapshot named pb
1640                @lines = $t->cmd("savevm pb");
1641                # Write the new status in the VM
1642                @lines = $t->cmd("commit all");
1643                # End
1644                @lines = $t->cmd("quit");
1645            }
1646        }
1647        my $hoption = "-p";
1648        my $hpath = "/sbin";
1649        # Solaris doesn't support -h and has halt elsewhere
1650        if ($dtype eq "pkg") {
1651            $hoption = "" ;
1652            $hpath = "/usr/sbin";
1653        }
1654        pb_system("$shcmd \"sudo $hpath/halt $hoption \"; sleep $tm ; echo \'if [ -d /proc/$vmpid ]; then kill -9 $vmpid; fi \' | bash ; sleep 10","VM $v halt (pid $vmpid)");
1655    }
1656    if (($cmt =~ /^VE/) && ($snapme != 0)) {
1657        ($odir,$over,$oarch) = split(/-/,$v);
1658        my $tpdir = "$vepath->{$ENV{'PBPROJ'}}/$odir/$over/$oarch";
1659        pb_system("sudo tar cz -f $vepath->{$ENV{'PBPROJ'}}/$odir-$over-$oarch.tar.gz -C $tpdir .","Creating a snapshot of $tpdir");
1660    }
1661}
1662
1663sub pb_script2v {
1664    my $pbscript=shift;
1665    my $vtype=shift;
1666    my $pbforce=shift || 0; # Force stop of VM. Default not
1667    my $vm1=shift || undef; # Only that VM to treat
1668    my $snapme=shift || 0;  # Do we have to create a snapshot
1669    my $vm;
1670    my $all;
1671
1672    pb_log(2,"DEBUG: pb_script2v($pbscript,$vtype,$pbforce,".Dumper($vm1).",$snapme)\n");
1673    # Prepare the script to be executed on the VM
1674    # in $ENV{'PBDESTDIR'}/pbscript
1675    if ((defined $pbscript ) && ($pbscript ne "$ENV{'PBDESTDIR'}/pbscript")) {
1676        copy($pbscript,"$ENV{'PBDESTDIR'}/pbscript") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript";
1677        chmod 0755,"$ENV{'PBDESTDIR'}/pbscript";
1678    }
1679
1680    if (not defined $vm1) {
1681        ($vm,$all) = pb_get2v($vtype);
1682    } else {
1683        @$vm = ($vm1);
1684    }
1685    my ($vmexist,$vmpid) = (undef,undef);
1686
1687    foreach my $v (@$vm) {
1688        # Launch VM/VE
1689        ($vmexist,$vmpid) = pb_launchv($vtype,$v,0,$snapme,$pbsnap);
1690
1691        if ($vtype eq "vm") {
1692            pb_log(2,"DEBUG: After pb_launchv, vmexist: $vmexist, vmpid: $vmpid\n");
1693
1694            # Skip that VM if something went wrong
1695            next if (($vmpid == 0) && ($vmexist == 0));
1696
1697            # If force stopping the VM then reset vmexist
1698            if ($pbforce == 1) {
1699                $vmpid = $vmexist;
1700                $vmexist = 0;
1701            }
1702        } else {
1703            #VE
1704            $vmexist = 0;
1705            $vmpid = 0;
1706        }
1707
1708        # Gather all required files to send them to the VM
1709        # and launch the build through pbscript
1710        pb_log(2,"DEBUG: Before send2target, vmexist: $vmexist, vmpid: $vmpid\n");
1711        pb_send2target(uc($vtype)."Script","$v",$vmexist,$vmpid,$snapme);
1712
1713    }
1714}
1715
1716sub pb_launchv {
1717    my $vtype = shift;
1718    my $v = shift;
1719    my $create = shift || 0;        # By default do not create a VM/VE
1720    my $snapme = shift || 0;        # By default do not snap a VM/VE
1721    my $usesnap = shift || 1;       # By default study the usage of the snapshot feature of VM/VE
1722
1723    # If creation or snapshot creation mode, no snapshot usable
1724    if (($create == 1) || ($snapme == 1)) {
1725        $usesnap = 0;
1726    }
1727
1728    pb_log(2,"DEBUG: pb_launchv($vtype,$v,$create,$snapme,$usesnap)\n");
1729    die "No VM/VE defined, unable to launch" if (not defined $v);
1730    # Keep only the first VM in case many were given
1731    $v =~ s/,.*//;
1732
1733    my $arch = pb_get_arch();
1734
1735    # Launch the VMs/VEs
1736    if ($vtype eq "vm") {
1737        die "-i iso parameter needed" if (((not defined $iso) || ($iso eq "")) && ($create != 0));
1738
1739        # TODO: vmmonport should be optional
1740        my ($ptr,$ptr2,$vmpath,$vmport,$vmsize,$vmmonport) = pb_conf_get("vmtype","vmcmd","vmpath","vmport","vmsize","vmmonport");
1741        my ($vmopt,$vmtmout,$vmsnap) = pb_conf_get_if("vmopt","vmtmout","vmsnap");
1742
1743        my $vmtype = $ptr->{$ENV{'PBPROJ'}};
1744        my $vmcmd = $ptr2->{$ENV{'PBPROJ'}};
1745        if (not defined $ENV{'PBVMOPT'}) {
1746            $ENV{'PBVMOPT'} = "";
1747        }
1748        # Save the current status for later restoration
1749        $ENV{'PBOLDVMOPT'} = $ENV{'PBVMOPT'};
1750        # Set a default timeout of 2 minutes
1751        if (not defined $ENV{'PBVMTMOUT'}) {
1752            $ENV{'PBVMTMOUT'} = "120";
1753        }
1754        if (defined $vmopt->{$v}) {
1755            $ENV{'PBVMOPT'} .= " $vmopt->{$v}" if ($ENV{'PBVMOPT'} !~ / $vmopt->{$v}/);
1756        } elsif (defined $vmopt->{$ENV{'PBPROJ'}}) {
1757            $ENV{'PBVMOPT'} .= " $vmopt->{$ENV{'PBPROJ'}}" if ($ENV{'PBVMOPT'} !~ / $vmopt->{$ENV{'PBPROJ'}}/);
1758        }
1759
1760        # Are we allowed to use snapshot feature
1761        if ($usesnap == 1) {
1762            if ((defined $vmsnap->{$v}) && ($vmsnap->{$v} =~ /true/i)) {
1763                $ENV{'PBVMOPT'} .= " -snapshot";
1764            } elsif ((defined $vmsnap->{$ENV{'PBPROJ'}}) && ($vmsnap->{$ENV{'PBPROJ'}} =~ /true/i)) {
1765                $ENV{'PBVMOPT'} .= " -snapshot";
1766            } elsif ($pbsnap eq 1) {
1767                $ENV{'PBVMOPT'} .= " -snapshot";
1768            }
1769        } 
1770        if ($snapme != 0) {
1771            if (($vmtype eq "kvm") || ($vmtype eq "qemu")) {
1772                # Configure the monitoring to automize the creation of the 'pb' snapshot
1773                $ENV{'PBVMOPT'} .= " -serial mon:telnet::$vmmonport->{$ENV{'PBPROJ'}},server,nowait";
1774                # In that case no snapshot call needed
1775                $ENV{'PBVMOPT'} =~ s/ -snapshot//;
1776            }
1777        }
1778        if (defined $vmtmout->{$v}) {
1779            $ENV{'PBVMTMOUT'} = $vmtmout->{$v};
1780        } elsif (defined $vmtmout->{$ENV{'PBPROJ'}}) {
1781            $ENV{'PBVMTMOUT'} = $vmtmout->{$ENV{'PBPROJ'}};
1782        }
1783        my $nport = $vmport->{$ENV{'PBPROJ'}};
1784        $nport = "$pbport" if (defined $pbport);
1785   
1786        my $cmd;
1787        my $vmm;        # has to be used for pb_check_ps
1788        if (($vmtype eq "qemu") || ($vmtype eq "kvm")) {
1789            $vmm = "$vmpath->{$ENV{'PBPROJ'}}/$v.qemu";
1790            if (($create != 0) || (defined $iso)) {
1791                $ENV{'PBVMOPT'} .= " -cdrom $iso -boot d";
1792            }
1793            # Always redirect the network and always try to use a 'pb' snapshot
1794            $cmd = "$vmcmd $ENV{'PBVMOPT'} -redir tcp:$nport:10.0.2.15:22 -loadvm pb $vmm"
1795        } elsif ($vmtype eq "xen") {
1796        } elsif ($vmtype eq "vmware") {
1797        } else {
1798            die "VM of type $vmtype not supported. Report to the dev team";
1799        }
1800        # Restore the ENV VAR Value
1801        $ENV{'PBVMOPT'} = $ENV{'PBOLDVMOPT'};
1802
1803        my ($tmpcmd,$void) = split(/ +/,$cmd);
1804        my $vmexist = pb_check_ps($tmpcmd,$vmm);
1805        my $vmpid = 0;
1806        if (! $vmexist) {
1807            if ($create != 0) {
1808                die("Found an existing Virtual machine $vmm. Won't overwrite") if (-r $vmm);
1809                if (($vmtype eq "qemu") || ($vmtype eq "xen") || ($vmtype eq "kvm")) {
1810                    pb_system("/usr/bin/qemu-img create -f qcow2 $vmm $vmsize->{$ENV{'PBPROJ'}}","Creating the QEMU VM");
1811                } elsif ($vmtype eq "vmware") {
1812                } else {
1813                }
1814            }
1815            if (! -f "$vmm") {
1816                pb_log(0,"Unable to find VM $vmm\n");
1817            } else {
1818                pb_system("$cmd &","Launching the VM $vmm");
1819                pb_system("sleep $ENV{'PBVMTMOUT'}","Waiting $ENV{'PBVMTMOUT'} s for VM $v to come up");
1820                $vmpid = pb_check_ps($tmpcmd,$vmm);
1821                pb_log(0,"VM $vmm launched (pid $vmpid)\n");
1822            }
1823        } else {
1824            pb_log(0,"Found an existing VM $vmm (pid $vmexist)\n");
1825        }
1826        pb_log(2,"DEBUG: pb_launchv returns ($vmexist,$vmpid)\n");
1827        return($vmexist,$vmpid);
1828    # VE here
1829    } else {
1830        # Get distro context
1831        my ($name,$ver,$darch) = split(/-/,$v);
1832        chomp($darch);
1833        my ($ddir, $dver, $dfam, $dtype, $dos, $pbsuf) = pb_distro_init($name,$ver,$darch);
1834
1835        # Get VE context
1836        my ($ptr,$vepath) = pb_conf_get("vetype","vepath");
1837        my $vetype = $ptr->{$ENV{'PBPROJ'}};
1838
1839        # We can probably only get those params now we have the distro context
1840        my ($rbsb4pi,$rbspi,$vesnap,$oscodename,$osmindep,$verebuild,$rbsmirrorsrv) = pb_conf_get_if("rbsb4pi","rbspi","vesnap","oscodename","osmindep","verebuild","rbsmirrorsrv");
1841
1842        # We need to avoid umask propagation to the VE
1843        umask 0022;
1844
1845        if (($vetype eq "chroot") || ($vetype eq "schroot")) {
1846            # Architecture consistency
1847            if ($arch ne $darch) {
1848                die "Unable to launch a VE of architecture $darch on a $arch platform" if (($darch eq "x86_64") && ($arch =~ /i?86/));
1849            }
1850
1851            my ($verpmtype,$vedebtype) = pb_conf_get("verpmtype","vedebtype");
1852            if (($create != 0) || ((defined $verebuild) && ($verebuild->{$ENV{'PBPROJ'}} =~ /true/i)) || ($pbforce == 1)) {
1853                my ($rbsopt1) = pb_conf_get_if("rbsopt");
1854
1855                # We have to rebuild the chroot
1856                if ($dtype eq "rpm") {
1857
1858                    # Which tool is used
1859                    my $verpmstyle = $verpmtype->{$ENV{'PBPROJ'}};
1860
1861                    # Get potential rbs option
1862                    my $rbsopt = "";
1863                    if (defined $rbsopt1) {
1864                        if (defined $rbsopt1->{$verpmstyle}) {
1865                            $rbsopt = $rbsopt1->{$verpmstyle};
1866                        } elsif (defined $rbsopt1->{$ENV{'PBPROJ'}}) {
1867                            $rbsopt = $rbsopt1->{$ENV{'PBPROJ'}};
1868                        } else {
1869                            $rbsopt = "";
1870                        }
1871                    }
1872
1873                    my $postinstall = pb_get_postinstall($ddir,$dver,$darch,$rbspi,$verpmstyle);
1874                    if ($verpmstyle eq "rinse") {
1875                        # Need to reshape the mirrors generated with local before-post-install script
1876                        my $b4post = "--before-post-install ";
1877                        my $postparam = pb_distro_get_param($ddir,$dver,$darch,$rbsb4pi);
1878                        if ($postparam eq "") {
1879                            $b4post = "";
1880                        } else {
1881                            $b4post .= $postparam;
1882                        }
1883
1884                        # Need to reshape the package list for pb
1885                        my $addpkgs;
1886                        $postparam = "";
1887                        $postparam .= pb_distro_get_param($ddir,$dver,$darch,$osmindep);
1888                        if ($postparam eq "") {
1889                            $addpkgs = "";
1890                        } else {
1891                            my $pkgfile = "$ENV{'PBTMP'}/addpkgs.lis";
1892                            open(PKG,"> $pkgfile") || die "Unable to create $pkgfile";
1893                            foreach my $p (split(/,/,$postparam)) {
1894                                print PKG "$p\n";
1895                            }
1896                            close(PKG);
1897                            $addpkgs = "--add-pkg-list $pkgfile";
1898                        }
1899
1900                        my $rinseverb = "";
1901                        $rinseverb = "--verbose" if ($pbdebug gt 0);
1902                        my ($rbsconf) = pb_conf_get("rbsconf");
1903
1904                        pb_system("sudo /usr/sbin/rinse --directory \"$vepath->{$ENV{'PBPROJ'}}/$ddir/$dver/$darch\" --arch \"$darch\" --distribution \"$ddir-$dver\" --config \"$rbsconf->{$ENV{'PBPROJ'}}\" $b4post $postinstall $rbsopt $addpkgs $rinseverb","Creating the rinse VE for $ddir-$dver ($darch)", "verbose");
1905                    } elsif ($verpmstyle eq "rpmbootstrap") {
1906                        my $rbsverb = "";
1907                        foreach my $i (1..$pbdebug) {
1908                            $rbsverb .= " -v";
1909                        }
1910                        my $addpkgs = "";
1911                        my $postparam = "";
1912                        $postparam .= pb_distro_get_param($ddir,$dver,$darch,$osmindep);
1913                        if ($postparam eq "") {
1914                            $addpkgs = "";
1915                        } else {
1916                            $addpkgs = "-a $postparam";
1917                        }
1918                        pb_system("sudo /usr/bin/rpmbootstrap $rbsopt $postinstall $addpkgs $ddir-$dver-$darch $rbsverb","Creating the rpmbootstrap VE for $ddir-$dver ($darch)", "verbose");
1919                    } elsif ($verpmstyle eq "mock") {
1920                        my ($rbsconf) = pb_conf_get("rbsconf");
1921                        pb_system("sudo /usr/sbin/mock --init --resultdir=\"/tmp\" --configdir=\"$rbsconf->{$ENV{'PBPROJ'}}\" -r $v $rbsopt","Creating the mock VE for $ddir-$dver ($darch)");
1922                        # Once setup we need to install some packages, the pb account, ...
1923                        pb_system("sudo /usr/sbin/mock --install --configdir=\"$rbsconf->{$ENV{'PBPROJ'}}\" -r $v su","Configuring the mock VE");
1924                    } else {
1925                        die "Unknown verpmtype type $verpmstyle. Report to dev team";
1926                    }
1927                } elsif ($dtype eq "deb") {
1928                    my $vedebstyle = $vedebtype->{$ENV{'PBPROJ'}};
1929
1930                    my $codename = pb_distro_get_param($ddir,$dver,$darch,$oscodename);
1931                    my $postparam = "";
1932                    my $addpkgs;
1933                    $postparam .= pb_distro_get_param($ddir,$dver,$darch,$osmindep);
1934                    if ($postparam eq "") {
1935                        $addpkgs = "";
1936                    } else {
1937                        $addpkgs = "--include $postparam";
1938                    }
1939                    my $debmir = "";
1940                    $debmir .= pb_distro_get_param($ddir,$dver,$darch,$rbsmirrorsrv);
1941
1942                    # Get potential rbs option
1943                    my $rbsopt = "";
1944                    if (defined $rbsopt1) {
1945                        if (defined $rbsopt1->{$vedebstyle}) {
1946                            $rbsopt = $rbsopt1->{$vedebstyle};
1947                        } elsif (defined $rbsopt1->{$ENV{'PBPROJ'}}) {
1948                            $rbsopt = $rbsopt1->{$ENV{'PBPROJ'}};
1949                        } else {
1950                            $rbsopt = "";
1951                        }
1952                    }
1953   
1954                    # debootstrap works with amd64 not x86_64
1955                    my $debarch = $darch;
1956                    $debarch = "amd64" if ($darch eq "x86_64");
1957                    if ($vedebstyle eq "debootstrap") {
1958                        my $dbsverb = "";
1959                        $dbsverb = "--verbose" if ($pbdebug gt 0);
1960
1961                        # Some perl modules are in Universe on Ubuntu
1962                        $rbsopt .= " --components=main,universe" if ($ddir eq "ubuntu");
1963
1964                        pb_system("sudo /usr/sbin/debootstrap $dbsverb $rbsopt --arch=$debarch $addpkgs $codename \"$vepath->{$ENV{'PBPROJ'}}/$ddir/$dver/$darch\" $debmir","Creating the debootstrap VE for $ddir-$dver ($darch)", "verbose");
1965                        # debootstrap doesn't create an /etc/hosts file
1966                        if (! -f "$vepath->{$ENV{'PBPROJ'}}/$ddir/$dver/$darch/etc/hosts" ) {
1967                            pb_system("sudo cp /etc/hosts $vepath->{$ENV{'PBPROJ'}}/$ddir/$dver/$darch/etc/hosts");
1968                        }
1969                    } else {
1970                        die "Unknown vedebtype type $vedebstyle. Report to dev team";
1971                    }
1972                } elsif ($dtype eq "ebuild") {
1973                    die "Please teach the dev team how to build gentoo chroot";
1974                } else {
1975                    die "Unknown distribution type $dtype. Report to dev team";
1976                }
1977            }
1978            # Fix modes to allow access to the VE for pb user
1979            pb_system("sudo chmod 755 $vepath->{$ENV{'PBPROJ'}}/$ddir $vepath->{$ENV{'PBPROJ'}}/$ddir/$dver $vepath->{$ENV{'PBPROJ'}}/$ddir/$dver/$darch","Fixing permissions");
1980
1981            # Test if an existing snapshot exists and use it if appropriate
1982            # And also use it of no local extracted VE is present
1983            if ((-f "$vepath->{$ENV{'PBPROJ'}}/$ddir-$dver-$darch.tar.gz") &&
1984                (((defined $vesnap->{$v}) && ($vesnap->{$v} =~ /true/i)) ||
1985                    ((defined $vesnap->{$ENV{'PBPROJ'}}) && ($vesnap->{$ENV{'PBPROJ'}} =~ /true/i)) ||
1986                    ($pbsnap eq 1) ||
1987                    (! -d "$vepath->{$ENV{'PBPROJ'}}/$ddir/$dver/$darch"))) {
1988                        pb_system("sudo rm -rf $vepath->{$ENV{'PBPROJ'}}/$ddir/$dver/$darch ; sudo mkdir -p $vepath->{$ENV{'PBPROJ'}}/$ddir/$dver/$darch ; sudo tar xz  -C $vepath->{$ENV{'PBPROJ'}}/$ddir/$dver/$darch -f $vepath->{$ENV{'PBPROJ'}}/$ddir-$dver-$darch.tar.gz","Extracting snapshot of $ddir-$dver-$darch.tar.gz under $vepath->{$ENV{'PBPROJ'}}/$ddir/$dver/$darch");
1989            }
1990            # Nothing more to do for VE. No real launch
1991        } else {
1992            die "VE of type $vetype not supported. Report to the dev team";
1993        }
1994    }
1995}
1996
1997# Return string for date synchro
1998sub pb_date2v {
1999
2000my $vtype = shift;
2001my $v = shift;
2002
2003my ($ntp) = pb_conf_get_if($vtype."ntp");
2004my $vntp = $ntp->{$ENV{'PBPROJ'}} if (defined $ntp);
2005my $ntpline;
2006
2007if (defined $vntp) {
2008    my ($ntpcmd) = pb_conf_get($vtype."ntpcmd");
2009    my $vntpcmd;
2010    if (defined $ntpcmd->{$v}) {
2011        $vntpcmd = $ntpcmd->{$v};
2012    } elsif (defined $ntpcmd->{$ENV{'PBPROJ'}}) {
2013        $vntpcmd = $ntpcmd->{$ENV{'PBPROJ'}};
2014    } else {
2015        $vntpcmd = "/bin/true";
2016    }
2017    $ntpline = "sudo $vntpcmd $vntp";
2018} else {
2019    $ntpline = undef;
2020}
2021# Force new date to be in the future compared to the date
2022# of the host by adding 1 minute
2023my @date=pb_get_date();
2024$date[1]++;
2025my $upddate = strftime("%m%d%H%M%Y", @date);
2026my $dateline = "sudo date $upddate";
2027return($ntpline,$dateline);
2028}
2029
2030sub pb_build2v {
2031
2032my $vtype = shift;
2033my $action = shift || "build";
2034
2035my ($v,$all) = pb_get2v($vtype);
2036
2037# Send tar files when we do a global generation
2038pb_build2ssh() if (($all == 1) && ($action eq "build"));
2039
2040my ($vmexist,$vmpid) = (undef,undef);
2041
2042foreach my $v (@$v) {
2043    # Prepare the script to be executed on the VM/VE
2044    # in $ENV{'PBDESTDIR'}/pbscript
2045    open(SCRIPT,"> $ENV{'PBDESTDIR'}/pbscript") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript";
2046    print SCRIPT "#!/bin/bash\n";
2047
2048    # Transmit the verbosity level to the virtual env/mach.
2049    my $verbose = "";
2050    my $i = 0;                          # minimal debug level
2051    while ($i lt $pbdebug) {
2052        $verbose .= "-v ";
2053        $i++;
2054    }
2055    # Activate script verbosity if at least 2 for pbdebug
2056    print SCRIPT "set -x\n" if ($i gt 1);
2057    # Quiet if asked to be so on the original system
2058    $verbose = "-q" if ($pbdebug eq -1);
2059
2060    print SCRIPT "echo ... Execution needed\n";
2061    print SCRIPT "# This is in directory delivery\n";
2062    print SCRIPT "# Setup the variables required for building\n";
2063    print SCRIPT "export PBPROJ=$ENV{'PBPROJ'}\n";
2064
2065    if ($action eq "build") {
2066        print SCRIPT "# Preparation for pb\n";
2067        print SCRIPT "mv .pbrc \$HOME\n";
2068        print SCRIPT "cd ..\n";
2069    }
2070
2071    # VE needs a good /proc
2072    if ($vtype eq "ve") {
2073        print SCRIPT "sudo mount -t proc /proc /proc\n";
2074    }
2075
2076    my ($ntpline,$dateline) = pb_date2v($vtype,$v);
2077    print SCRIPT "# Time sync\n";
2078    print SCRIPT "echo 'setting up date with '";
2079    if (defined $ntpline) {
2080        print SCRIPT "echo $ntpline\n";
2081        print SCRIPT "$ntpline\n";
2082    } else {
2083        print SCRIPT "echo $dateline\n";
2084        print SCRIPT "$dateline\n";
2085    }
2086    # Use potential local proxy declaration in case we need it to download repo, pkgs, ...
2087    if (defined $ENV{'http_proxy'}) {
2088        print SCRIPT "export http_proxy=\"$ENV{'http_proxy'}\"\n";
2089    }
2090
2091    if (defined $ENV{'ftp_proxy'}) {
2092        print SCRIPT "export ftp_proxy=\"$ENV{'ftp_proxy'}\"\n";
2093    }
2094
2095    # Get list of packages to build/test and get some ENV vars as well
2096    my $ptr = pb_get_pkg();
2097    @pkgs = @$ptr;
2098    my $p = join(' ',@pkgs) if (@pkgs);
2099    print SCRIPT "export PBPROJVER=$ENV{'PBPROJVER'}\n";
2100    print SCRIPT "export PBPROJTAG=$ENV{'PBPROJTAG'}\n";
2101    print SCRIPT "export PBPACKAGER=\"$ENV{'PBPACKAGER'}\"\n";
2102
2103    # We may need to do some other tasks before building. Read a script here to finish setup
2104    if (-x "$ENV{'PBDESTDIR'}/pb$vtype".".pre") {
2105        print SCRIPT "# Special pre-instructions to be launched\n";
2106        print SCRIPT pb_get_content("$ENV{'PBDESTDIR'}/pb$vtype".".pre");
2107    }
2108
2109    if (-x "$ENV{'PBDESTDIR'}/pb$vtype"."$action.pre") {
2110        print SCRIPT "# Special pre-$action instructions to be launched\n";
2111        print SCRIPT pb_get_content("$ENV{'PBDESTDIR'}/pb$vtype"."$action.pre");
2112    }
2113
2114    print SCRIPT "# $action\n";
2115    print SCRIPT "echo $action"."ing packages on $vtype...\n";
2116
2117    if (($action eq "test") && (! -x "$ENV{'PBDESTDIR'}/pbtest")) {
2118            die "No test script ($ENV{'PBDESTDIR'}/pbtest) found when in test mode. Aborting ...";
2119    }
2120    print SCRIPT "pb $verbose -p $ENV{'PBPROJ'} $action"."2pkg $p\n";
2121
2122    if ($vtype eq "ve") {
2123        print SCRIPT "sudo umount /proc\n";
2124    }
2125
2126    # We may need to do some other tasks after building. Read a script here to exit properly
2127    if (-x "$ENV{'PBDESTDIR'}/pb$vtype"."$action.post") {
2128        print SCRIPT "# Special post-$action instructions to be launched\n";
2129        print SCRIPT pb_get_content("$ENV{'PBDESTDIR'}/pb$vtype"."$action.post");
2130    }
2131
2132    if (-x "$ENV{'PBDESTDIR'}/pb$vtype".".post") {
2133        print SCRIPT "# Special post-instructions to be launched\n";
2134        print SCRIPT pb_get_content("$ENV{'PBDESTDIR'}/pb$vtype".".post");
2135    }
2136
2137    close(SCRIPT);
2138    chmod 0755,"$ENV{'PBDESTDIR'}/pbscript";
2139   
2140    # Launch the VM/VE
2141    ($vmexist,$vmpid) = pb_launchv($vtype,$v,0);
2142
2143    if ($vtype eq "vm") {
2144        # Skip that VM if it something went wrong
2145        next if (($vmpid == 0) && ($vmexist == 0));
2146    } else {
2147        # VE
2148        $vmexist = 0;
2149        $vmpid = 0;
2150    }
2151    # Gather all required files to send them to the VM/VE
2152    # and launch the build through pbscript
2153    pb_log(2,"Calling send2target $vtype,$v,$vmexist,$vmpid\n");
2154    pb_send2target(uc($vtype).$action,"$v",$vmexist,$vmpid);
2155}
2156}
2157
2158
2159sub pb_clean {
2160
2161    my $sleep=10;
2162    die "Unable to get env var PBDESTDIR" if (not defined $ENV{'PBDESTDIR'});
2163    die "Unable to get env var PBBUILDDIR" if (not defined $ENV{'PBBUILDDIR'});
2164    pb_log(0,"We will now wait $sleep s before removing both directories\n$ENV{'PBDESTDIR'} and $ENV{'PBBUILDDIR'}.\nPlease break me if this is wrong\n");
2165    sleep $sleep;
2166    pb_rm_rf($ENV{'PBDESTDIR'});
2167    pb_rm_rf($ENV{'PBBUILDDIR'});
2168}
2169
2170sub pb_newver {
2171
2172    die "-V Version parameter needed" if ((not defined $newver) || ($newver eq ""));
2173
2174    # Need this call for PBDIR
2175    my ($scheme2,$uri) = pb_cms_init($pbinit);
2176
2177    my ($pbconf,$pburl) = pb_conf_get("pbconfurl","pburl");
2178    $uri = $pbconf->{$ENV{'PBPROJ'}};
2179    my ($scheme, $account, $host, $port, $path) = pb_get_uri($uri);
2180
2181    # Checking CMS repositories status
2182    ($scheme2, $account, $host, $port, $path) = pb_get_uri($pburl->{$ENV{'PBPROJ'}});
2183
2184    if ($scheme !~ /^svn/) {
2185        die "Only SVN is supported at the moment";
2186    }
2187
2188    my $res = pb_cms_isdiff($scheme,$ENV{'PBROOTDIR'});
2189    die "ERROR: No differences accepted in CMS for $ENV{'PBROOTDIR'} before creating a new version" if ($res != 0);
2190
2191    $res = pb_cms_isdiff($scheme2,$ENV{'PBDIR'});
2192    die "ERROR: No differences accepted in CMS for $ENV{'PBDIR'} before creating a new version" if ($res != 0);
2193
2194    # Tree identical between PBCONFDIR and PBROOTDIR. The delta is what
2195    # we want to get for the root of the new URL
2196
2197    my $tmp = $ENV{'PBROOTDIR'};
2198    $tmp =~ s|^$ENV{'PBCONFDIR'}||;
2199
2200    my $newurl = "$uri/".dirname($tmp)."/$newver";
2201    # Should probably use projver in the old file
2202    my $oldver= basename($tmp);
2203
2204    # Duplicate and extract project-builder part
2205    pb_log(2,"Copying $uri/$tmp to $newurl\n");
2206    pb_cms_copy($scheme,"$uri/$tmp",$newurl);
2207    pb_log(2,"Checkout $newurl to $ENV{'PBROOTDIR'}/../$newver\n");
2208    pb_cms_up($scheme,"$ENV{'PBCONFDIR'}/..");
2209
2210    # Duplicate and extract project
2211    my $newurl2 = "$pburl->{$ENV{'PBPROJ'}}/".dirname($tmp)."/$newver";
2212
2213    pb_log(2,"Copying $pburl->{$ENV{'PBPROJ'}}/$tmp to $newurl2\n");
2214    pb_cms_copy($scheme2,"$pburl->{$ENV{'PBPROJ'}}/$tmp",$newurl2);
2215    pb_log(2,"Checkout $newurl2 to $ENV{'PBDIR'}/../$newver\n");
2216    pb_cms_up($scheme2,"$ENV{'PBDIR'}/..");
2217
2218    # Update the .pb file
2219    open(FILE,"$ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb") || die "Unable to open $ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb";
2220    open(OUT,"> $ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb.new") || die "Unable to write to $ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb.new";
2221    while(<FILE>) {
2222        s/^projver\s+$ENV{'PBPROJ'}\s*=\s*$oldver/projver $ENV{'PBPROJ'} = $newver/;
2223        pb_log(0,"Changing projver from $oldver to $newver in $ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb\n") if (/^projver\s+$ENV{'PBPROJ'}\s*=\s*$oldver/);
2224        pb_log(0,"Commenting testver in $ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb\n") if (/^testver/);
2225        s/^testver/#testver/;
2226        print OUT $_;
2227        pb_log(0,"Please check delivery ($_) in $ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb\n") if (/^delivery/);
2228    }
2229    close(FILE);
2230    close(OUT);
2231    rename("$ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb.new","$ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb");
2232
2233    # Checking pbcl files
2234    foreach my $f (<$ENV{'PBROOTDIR'}/*/pbcl>) {
2235        # Compute new pbcl file
2236        my $f2 = $f;
2237        $f2 =~ s|$ENV{'PBROOTDIR'}|$ENV{'PBROOTDIR'}/../$newver/|;
2238        open(PBCL,$f) || die "Unable to open $f";
2239        my $foundnew = 0;
2240        while (<PBCL>) {
2241            $foundnew = 1 if (/^$newver \(/);
2242        }
2243        close(PBCL);
2244        open(OUT,"> $f2") || die "Unable to write to $f2: $!";
2245        open(PBCL,$f) || die "Unable to open $f";
2246        while (<PBCL>) {
2247            print OUT "$_" if (not /^$oldver \(/);
2248            if ((/^$oldver \(/) && ($foundnew == 0)) {
2249                print OUT "$newver ($pbdate)\n";
2250                print OUT "- TBD\n";
2251                print OUT "\n";
2252                pb_log(0,"WARNING: version $newver not found in $f so added to $f2...\n") if ($foundnew == 0);
2253            }
2254        }
2255        close(OUT);
2256        close(PBCL);
2257    }
2258
2259    pb_log(2,"Checkin $ENV{'PBROOTDIR'}/../$newver\n");
2260    pb_cms_checkin($scheme,"$ENV{'PBROOTDIR'}/../$newver",undef);
2261}
2262
2263#
2264# Return the list of VMs/VEs we are working on
2265# $all is a flag to know if we return all of them
2266# or only some (if all we publish also tar files in addition to pkgs
2267#
2268sub pb_get2v {
2269
2270my $vtype = shift;
2271my @v;
2272my $all = 0;
2273my $vlist;
2274my $pbv = 'PBV';
2275
2276if ($vtype eq "vm") {
2277    $vlist = "vmlist";
2278} elsif ($vtype eq "ve") {
2279    $vlist = "velist";
2280}
2281# Get VM/VE list
2282if ((not defined $ENV{$pbv}) || ($ENV{$pbv} =~ /^all$/)) {
2283    my ($ptr) = pb_conf_get($vlist);
2284    $ENV{$pbv} = $ptr->{$ENV{'PBPROJ'}};
2285    $all = 1;
2286}
2287pb_log(2,"$vtype: $ENV{$pbv}\n");
2288@v = split(/,/,$ENV{$pbv});
2289return(\@v,$all);
2290}
2291
2292# Function to create a potentialy missing pb account on the VM/VE, and adds it to sudo
2293# Needs to use root account to connect to the VM/VE
2294# pb will take your local public SSH key to access
2295# the pb account in the VM later on if needed
2296sub pb_setup2v {
2297
2298my $vtype = shift;
2299
2300my ($vm,$all) = pb_get2v($vtype);
2301
2302# Script generated
2303my $pbscript = "$ENV{'PBDESTDIR'}/setupv";
2304
2305foreach my $v (@$vm) {
2306    # Deal with date sync.
2307    my ($ntpline,$dateline) = pb_date2v($vtype,$v);
2308
2309    # Get distro context
2310    my ($name,$ver,$darch) = split(/-/,$v);
2311    chomp($darch);
2312    my ($ddir, $dver, $dfam, $dtype, $dos, $pbsuf, $pbupd) = pb_distro_init($name,$ver,$darch);
2313   
2314    # Name of the account to deal with for VM/VE
2315    # Do not use the one passed potentially with -a
2316    my ($pbac) = pb_conf_get($vtype."login");
2317    my ($key,$zero0,$zero1,$zero2);
2318    my ($vmexist,$vmpid);
2319
2320    # Prepare the script to be executed on the VM/VE
2321    # in $ENV{'PBDESTDIR'}/setupv
2322    open(SCRIPT,"> $pbscript") || die "Unable to create $pbscript";
2323   
2324    print SCRIPT << 'EOF';
2325#!/usr/bin/perl -w
2326
2327use strict;
2328use File::Copy;
2329
2330# We should not need in this script more functions than what is provided
2331# by Base and Distribution to avoid problems at exec time.
2332# They are appended at the end.
2333
2334our $pbdebug;
2335our $pbLOG;
2336our $pbsynmsg = "pbscript";
2337our $pbdisplaytype = "text";
2338our $pblocale = "";
2339pb_log_init($pbdebug, $pbLOG);
2340pb_temp_init();
2341
2342EOF
2343
2344    # Launch the VM/VE - Usage of snapshot disabled
2345    ($vmexist,$vmpid) = pb_launchv($vtype,$v,0,0,0);
2346
2347    if ($vtype eq "vm") {
2348        # Prepare the key to be used and transfered remotely
2349        my $keyfile = pb_ssh_get(1);
2350       
2351        my ($vmhost,$vmport,$vmntp) = pb_conf_get("vmhost","vmport","vmntp");
2352        my $nport = $vmport->{$ENV{'PBPROJ'}};
2353        $nport = "$pbport" if (defined $pbport);
2354   
2355        # Skip that VM if something went wrong
2356        next if (($vmpid == 0) && ($vmexist == 0));
2357   
2358        # Store the pub key part in a variable
2359        open(FILE,"$keyfile.pub") || die "Unable to open $keyfile.pub";
2360        ($zero0,$zero1,$zero2) = split(/ /,<FILE>);
2361        close(FILE);
2362
2363        $key = "\Q$zero1";
2364
2365        # We call true to avoid problems if SELinux is not activated, but chcon is present and returns in that case 1
2366        pb_system("cat $keyfile.pub | ssh -q -o UserKnownHostsFile=/dev/null -p $nport -i $keyfile root\@$vmhost->{$ENV{'PBPROJ'}} \"mkdir -p .ssh ; chmod 700 .ssh ; cat >> .ssh/authorized_keys ; chmod 600 .ssh/authorized_keys ; if [ -x /usr/bin/chcon ]; then /usr/bin/chcon -Rt home_ssh_t .ssh 2> /dev/null; /bin/true; fi\"","Copying local keys to $vtype. This may require the root password");
2367        # once this is done, we can do what we want on the VM remotely
2368    } elsif ($vtype eq "ve") {
2369        print SCRIPT << "EOF";
2370# For VE we need a good null dev
2371pb_system("rm -f /dev/null; mknod /dev/null c 1 3; chmod 777 /dev/null");
2372EOF
2373        print SCRIPT << "EOF";
2374# For VE we first need to mount some FS
2375pb_system("mount -t proc /proc /proc");
2376
2377EOF
2378    }
2379
2380if ($vtype eq "vm") {
2381        print SCRIPT << 'EOF';
2382# Removes duplicate in .ssh/authorized_keys of our key if needed
2383#
2384my $file1="$ENV{'HOME'}/.ssh/authorized_keys";
2385open(PBFILE,$file1) || die "Unable to open $file1";
2386open(PBOUT,"> $file1.new") || die "Unable to open $file1.new";
2387my $count = 0;
2388while (<PBFILE>) {
2389
2390EOF
2391        print SCRIPT << "EOF";
2392    if (/ $key /) {
2393        \$count++;
2394    }
2395print PBOUT \$_ if ((\$count <= 1) || (\$_ !~ / $key /));
2396}
2397close(PBFILE);
2398close(PBOUT);
2399rename("\$file1.new",\$file1);
2400chmod 0600,\$file1;
2401
2402# Sync date
2403EOF
2404        if (defined $ntpline) {
2405            print SCRIPT "pb_system(\"$ntpline\");\n";
2406        } else {
2407            print SCRIPT "pb_system(\"$dateline\");\n";
2408        }
2409    }
2410    print SCRIPT << 'EOF';
2411
2412# Adds $pbac->{$ENV{'PBPROJ'}} as an account if needed
2413#
2414my $file="/etc/passwd";
2415open(PBFILE,$file) || die "Unable to open $file";
2416my $found = 0;
2417while (<PBFILE>) {
2418EOF
2419    print SCRIPT << "EOF";
2420    \$found = 1 if (/^$pbac->{$ENV{'PBPROJ'}}:/);
2421EOF
2422
2423my $home = "/home";
2424# Solaris doesn't like that we use /home
2425$home = "/export/home" if ($dtype eq "pkg");
2426
2427    print SCRIPT << "EOF";
2428}
2429close(PBFILE);
2430
2431if ( \$found == 0 ) {
2432    if ( ! -d "$home" ) {
2433        pb_mkdir_p("$home");
2434    }
2435EOF
2436    print SCRIPT << "EOF";
2437pb_system("/usr/sbin/groupadd $pbac->{$ENV{'PBPROJ'}}","Adding group $pbac->{$ENV{'PBPROJ'}}");
2438pb_system("/usr/sbin/useradd -g $pbac->{$ENV{'PBPROJ'}} -m -d $home/$pbac->{$ENV{'PBPROJ'}} $pbac->{$ENV{'PBPROJ'}}","Adding user $pbac->{$ENV{'PBPROJ'}} (group $pbac->{$ENV{'PBPROJ'}} - home $home/$pbac->{$ENV{'PBPROJ'}})");
2439}
2440EOF
2441
2442    # Copy the content of our local conf file to the VM/VE
2443    my $content = pb_get_content(pb_distro_conffile());
2444    print SCRIPT << "EOF";
2445    #
2446    # Create a temporary local conf file for distribution support
2447    # This is created here before its use later. Its place is hardcoded, so no choice for the path
2448    #
2449    my \$tempconf = pb_distro_conffile();
2450    pb_mkdir_p(dirname(\$tempconf));
2451    open(CONF,"> \$tempconf") || die "Unable to create \$tempconf";
2452    print CONF q{$content};
2453    close(CONF);
2454EOF
2455
2456    if ($vtype eq "vm") {
2457        print SCRIPT << "EOF";
2458# allow ssh entry to build
2459#
2460mkdir "$home/$pbac->{$ENV{'PBPROJ'}}/.ssh",0700;
2461# Allow those accessing root to access the build account
2462copy("\$ENV{'HOME'}/.ssh/authorized_keys","$home/$pbac->{$ENV{'PBPROJ'}}/.ssh/authorized_keys");
2463chmod 0600,".ssh/authorized_keys";
2464pb_system("chown -R $pbac->{$ENV{'PBPROJ'}}:$pbac->{$ENV{'PBPROJ'}} $home/$pbac->{$ENV{'PBPROJ'}}","Finish setting up the account env for $pbac->{$ENV{'PBPROJ'}}");
2465
2466EOF
2467}
2468    print SCRIPT << 'EOF';
2469# No passwd for build account only keys
2470$file="/etc/shadow";
2471if (-f $file) {
2472    open(PBFILE,$file) || die "Unable to open $file";
2473    open(PBOUT,"> $file.new") || die "Unable to open $file.new";
2474    while (<PBFILE>) {
2475EOF
2476    print SCRIPT << "EOF";
2477        s/^$pbac->{$ENV{'PBPROJ'}}:\!\!:/$pbac->{$ENV{'PBPROJ'}}:*:/;
2478        s/^$pbac->{$ENV{'PBPROJ'}}:\!:/$pbac->{$ENV{'PBPROJ'}}:*:/; #SLES 9 e.g.
2479        s/^$pbac->{$ENV{'PBPROJ'}}:\\*LK\\*:/$pbac->{$ENV{'PBPROJ'}}:NP:/;  #Solaris e.g.
2480EOF
2481        print SCRIPT << 'EOF';
2482        print PBOUT $_;
2483    }
2484    close(PBFILE);
2485    close(PBOUT);
2486    rename("$file.new",$file);
2487    chmod 0640,$file;
2488    }
2489
2490# Keep the VM in text mode
2491$file="/etc/inittab";
2492if (-f $file) {
2493    open(PBFILE,$file) || die "Unable to open $file";
2494    open(PBOUT,"> $file.new") || die "Unable to open $file.new";
2495    while (<PBFILE>) {
2496        s/^(..):5:initdefault:$/$1:3:initdefault:/;
2497        print PBOUT $_;
2498    }
2499    close(PBFILE);
2500    close(PBOUT);
2501    rename("$file.new",$file);
2502    chmod 0640,$file;
2503}
2504
2505# pb has to be added to portage group on gentoo
2506
2507# We need to have that pb_distro_init function
2508# Get it from Project-Builder::Distribution
2509# And we now need the conf file required for this to work created above
2510
2511my ($ddir, $dver, $dfam, $dtype, $dos, $pbsuf, $pbupd, $darch) = pb_distro_init(); 
2512print "distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf, $darch))."\n";
2513
2514# Adapt sudoers
2515# sudo is not default on Solaris and needs to be installed first
2516# from http://www.sunfreeware.com/programlistsparc10.html#sudo
2517if ($dtype eq "pkg") {
2518    $file="/usr/local/etc/sudoers";
2519} else {
2520    $file="/etc/sudoers";
2521}
2522open(PBFILE,$file) || die "Unable to open $file";
2523open(PBOUT,"> $file.new") || die "Unable to open $file.new";
2524while (<PBFILE>) {
2525EOF
2526    print SCRIPT << "EOF";
2527    next if (/^$pbac->{$ENV{'PBPROJ'}}   /);
2528EOF
2529    print SCRIPT << 'EOF';
2530    s/Defaults[ \t]+requiretty//;
2531    print PBOUT $_;
2532}
2533close(PBFILE);
2534EOF
2535    print SCRIPT << "EOF";
2536# Some distro force requiretty at compile time, so disable here
2537print PBOUT "Defaults:$pbac->{$ENV{'PBPROJ'}} !requiretty\n";
2538print PBOUT "Defaults:root !requiretty\n";
2539# This is needed in order to be able to halt the machine from the $pbac->{$ENV{'PBPROJ'}} account at least
2540print PBOUT "Defaults:$pbac->{$ENV{'PBPROJ'}} env_keep += \\\"http_proxy ftp_proxy\\\"\n";
2541print PBOUT "$pbac->{$ENV{'PBPROJ'}}   ALL=(ALL) NOPASSWD:ALL\n";
2542EOF
2543    print SCRIPT << 'EOF';
2544close(PBOUT);
2545rename("$file.new",$file);
2546chmod 0440,$file;
2547
2548EOF
2549
2550    # We may need a proxy configuration. Get it from the local env
2551
2552    if (defined $ENV{'http_proxy'}) {
2553        print SCRIPT "\$ENV\{'http_proxy'\}=\"$ENV{'http_proxy'}\";\n";
2554    }
2555
2556    if (defined $ENV{'ftp_proxy'}) {
2557        print SCRIPT "\$ENV\{'ftp_proxy'\}=\"$ENV{'ftp_proxy'}\";\n";
2558    }
2559
2560    print SCRIPT << 'EOF';
2561   
2562    my ($ospkgdep,$osperldep,$osperlver) = pb_conf_get_if("ospkgdep","osperldep","osperlver");
2563   
2564    # First install all required packages
2565    pb_system("yum clean all","Cleaning yum env") if (($ddir eq "fedora") || ($ddir eq "asianux") || ($ddir eq "rhel"));
2566    my $pkgdep = pb_distro_get_param($ddir,$dver,$darch,$ospkgdep,$dfam,$dtype,$dos);
2567    pb_distro_installdeps(undef,$dtype,$pbupd,pb_distro_only_deps_needed($dtype,join(' ',split(/,/,$pkgdep))));
2568
2569    # Then install manually the missing perl modules
2570    my $perldep = pb_distro_get_param($ddir,$dver,$darch,$osperldep,$dfam,$dtype,$dos);
2571    foreach my $m (split(/,/,$perldep)) {
2572        # Skip empty deps
2573        next if ($m =~ /^\s*$/);
2574        my $dir = $m;
2575        $dir =~ s/-.*//;
2576        pb_system("echo \"rm -rf $m* ; wget http://search.cpan.org/CPAN/modules/by-module/$dir/$m-$osperlver->{$m}.tar.gz ; gzip -cd $m-$osperlver->{$m}.tar.gz | tar xf - ; cd $m* ; if [ -f Build.PL ]; then perl Build.PL; ./Build ; ./Build install ; else perl Makefile.PL; make ; make install ; fi; cd .. ; rm -rf $m*\" | bash" ,"Installing perl module $m-$osperlver->{$m}");
2577    }
2578
2579# Suse wants sudoers as 640
2580if ((($ddir eq "sles") && (($dver =~ /10/) || ($dver =~ /9/))) || (($ddir eq "opensuse") && ($dver =~ /10.[012]/))) {
2581    chmod 0640,$file;
2582}
2583
2584pb_system("rm -rf ProjectBuilder-* ; wget --passive-ftp ftp://ftp.mondorescue.org/src/ProjectBuilder-latest.tar.gz ; gzip -cd ProjectBuilder-latest.tar.gz | tar xf - ; cd ProjectBuilder-* ; perl Makefile.PL ; make ; make install ; cd .. ; rm -rf ProjectBuilder-* ; rm -rf project-builder-* ; wget --passive-ftp ftp://ftp.mondorescue.org/src/project-builder-latest.tar.gz ; gzip -cd project-builder-latest.tar.gz | tar xf - ; cd project-builder-* ; perl Makefile.PL ; make ; make install ; cd .. ; rm -rf project-builder-* ;","Building Project-Builder");
2585pb_system("pb 2>&1 | head -5",undef,"verbose");
2586EOF
2587    if ($vtype eq "ve") {
2588            print SCRIPT << 'EOF';
2589# For VE we need to umount some FS at the end
2590
2591pb_system("umount /proc");
2592
2593# Create a basic network file if not already there
2594
2595my $nf="/etc/sysconfig/network";
2596if ((! -f $nf) && ($dtype eq "rpm")) {
2597    open(NF,"> $nf") || die "Unable to create $nf";
2598    print NF "NETWORKING=yes\n";
2599    print NF "HOSTNAME=localhost\n";
2600    close(NF);
2601}
2602chmod 0755,$nf;
2603EOF
2604    }
2605
2606    # Adds pb_distro_init and all functions needed from ProjectBuilder::Distribution and Base
2607    foreach my $d (@INC) {
2608        my @f = ("$d/ProjectBuilder/Base.pm","$d/ProjectBuilder/Distribution.pm","$d/ProjectBuilder/Conf.pm");
2609        foreach my $f (@f) {
2610            if (-f "$f") {
2611                open(PBD,"$f") || die "Unable to open $f";
2612                while (<PBD>) {
2613                    next if (/^package/);
2614                    next if (/^use Exporter/);
2615                    next if (/^use ProjectBuilder::/);
2616                    next if (/^our /);
2617                    print SCRIPT $_;
2618                }
2619                close(PBD);
2620            }
2621        }
2622    }
2623    close(SCRIPT);
2624    chmod 0755,"$pbscript";
2625
2626    # That build script needs to be run as root and force stop of VM at end
2627    $pbaccount = "root";
2628
2629    # Force shutdown of VM exept if it was already launched
2630    my $pbforce = 0;
2631    if ((! $vmexist) && ($vtype eq "vm")) {
2632        $pbforce = 1;
2633    }
2634   
2635    pb_script2v($pbscript,$vtype,$pbforce,$v);
2636}
2637return;
2638}
2639
2640# Function to create a snapshot named 'pb' for VMs and a compressed tar for VEs
2641sub pb_snap2v {
2642
2643my $vtype = shift;
2644
2645my ($vm,$all) = pb_get2v($vtype);
2646
2647# Script generated
2648my $pbscript = "$ENV{'PBDESTDIR'}/snapv";
2649
2650my ($pbac) = pb_conf_get($vtype."login");
2651
2652foreach my $v (@$vm) {
2653    # Get distro context
2654    my ($name,$ver,$darch) = split(/-/,$v);
2655    chomp($darch);
2656    my ($ddir, $dver, $dfam, $dtype, $dos, $pbsuf) = pb_distro_init($name,$ver,$darch);
2657    my ($vepath) = pb_conf_get("vepath");
2658
2659    # Test if an existing snapshot exists and remove it if there is a VE
2660    if ((-f "$vepath->{$ENV{'PBPROJ'}}/$ddir-$dver-$darch.tar.gz") &&
2661        (! -d "$vepath->{$ENV{'PBPROJ'}}/$ddir/$dver/$darch")) {
2662            pb_system("sudo rm -f $vepath->{$ENV{'PBPROJ'}}/$ddir-$dver-$darch.tar.gz","Removing previous snapshot $ddir-$dver-$darch.tar.gz");
2663    }
2664
2665    # Name of the account to deal with for VM/VE
2666    # Do not use the one passed potentially with -a
2667    my ($vmexist,$vmpid);
2668
2669    # Prepare the script to be executed on the VM/VE
2670    # in $ENV{'PBDESTDIR'}/setupv
2671    open(SCRIPT,"> $pbscript") || die "Unable to create $pbscript";
2672   
2673    print SCRIPT << 'EOF';
2674    #!/bin/bash
2675    sleep 2
2676EOF
2677    close(SCRIPT);
2678    chmod 0755,"$pbscript";
2679
2680    # Force shutdown of VM/VE
2681    # Force snapshot of VM/VE
2682    pb_script2v($pbscript,$vtype,1,$v,1);
2683}
2684return;
2685}
2686
2687sub pb_announce {
2688
2689    # Get all required parameters
2690    my ($pbpackager,$pbrepo,$pbml,$pbsmtp) = pb_conf_get("pbpackager","pbrepo","pbml","pbsmtp");
2691    my ($pkgv, $pkgt, $testver) = pb_conf_get_if("pkgver","pkgtag","testver");
2692    my $pkg = pb_cms_get_pkg($defpkgdir,$extpkgdir);
2693    my @pkgs = @$pkg;
2694    my %pkgs;
2695    my $first = 0;
2696
2697    # Command to find packages on repo
2698    my $findstr = "find . ";
2699    # Generated announce files
2700    my @files;
2701
2702    foreach my $pbpkg (@pkgs) {
2703        if ($first != 0) {
2704            $findstr .= "-o ";
2705        }
2706        $first++;
2707        if ((defined $pkgv) && (defined $pkgv->{$pbpkg})) {
2708            $pbver = $pkgv->{$pbpkg};
2709        } else {
2710            $pbver = $ENV{'PBPROJVER'};
2711        }
2712        if ((defined $pkgt) && (defined $pkgt->{$pbpkg})) {
2713            $pbtag = $pkgt->{$pbpkg};
2714        } else {
2715            $pbtag = $ENV{'PBPROJTAG'};
2716        }
2717
2718        # TODO: use virtual/real names here now
2719        $findstr .= "-name \'$pbpkg-$pbver-$pbtag\.*.rpm\' -o -name \'$pbpkg"."_$pbver*\.deb\' -o -name \'$pbpkg-$pbver*\.ebuild\' ";
2720
2721        my $chglog;
2722
2723        # Get project info on log file and generate tmp files used later on
2724        pb_cms_init($pbinit);
2725        $chglog = "$ENV{'PBROOTDIR'}/$pbpkg/pbcl";
2726        $chglog = "$ENV{'PBROOTDIR'}/pbcl" if (! -f $chglog);
2727        $chglog = undef if (! -f $chglog);
2728
2729        open(OUT,"> $ENV{'PBTMP'}/$pbpkg.ann") || die "Unable to create $ENV{'PBTMP'}/$pbpkg.ann: $!";
2730        my %pb;
2731        $pb{'dtype'} = "announce";
2732        $pb{'realpkg'} = $pbpkg;
2733        $pb{'ver'} = $pbver;
2734        $pb{'tag'} = $pbtag;
2735        $pb{'suf'} = "N/A";     # Should not be empty even if unused
2736        $pb{'date'} = $pbdate;
2737        $pb{'chglog'} = $chglog;
2738        $pb{'packager'} = $pbpackager;
2739        $pb{'proj'} = $ENV{'PBPROJ'};
2740        $pb{'repo'} = $pbrepo;
2741        pb_changelog(\%pb,\*OUT,"yes");
2742        close(OUT);
2743        push(@files,"$ENV{'PBTMP'}/$pbpkg.ann");
2744    }
2745    $findstr .= " | grep -Ev \'src.rpm\'";
2746
2747    # Prepare the command to run and execute it
2748    open(PBS,"> $ENV{'PBTMP'}/pbscript") || die "Unable to create $ENV{'PBTMP'}/pbscript";
2749    print PBS "$findstr\n";
2750    close(PBS);
2751    chmod 0755,"$ENV{'PBTMP'}/pbscript";
2752    pb_send2target("Announce");
2753
2754    # Get subject line
2755    my $sl = "Project $ENV{'PBPROJ'} version $ENV{'PBPROJVER'} is now available";
2756    pb_log(0,"Please enter the title of your announce\n");
2757    pb_log(0,"(By default: $sl)\n");
2758    my $sl2 = <STDIN>;
2759    $sl = $sl2 if ($sl2 !~ /^$/);
2760
2761    # Prepare a template of announce
2762    open(ANN,"> $ENV{'PBTMP'}/announce.html") || die "Unable to create $ENV{'PBTMP'}/announce.html: $!";
2763    print ANN << "EOF";
2764$sl</p>
2765
2766<p>The project team is happy to announce the availability of a newest version of $ENV{'PBPROJ'} $ENV{'PBPROJVER'}. Enjoy it as usual!</p>
2767<p>
2768Now available at <a href="$pbrepo->{$ENV{'PBPROJ'}}">$pbrepo->{$ENV{'PBPROJ'}}</a>
2769</p>
2770<p>
2771EOF
2772    open(LOG,"$ENV{'PBTMP'}/system.log") || die "Unable to read $ENV{'PBTMP'}/system.log: $!";
2773    my $col = 2;
2774    my $i = 1;
2775    print ANN << 'EOF';
2776<TABLE WIDTH="700" CELLPADDING="0" CELLSPACING="0" BORDER="0">
2777<TR>
2778EOF
2779    while (<LOG>) {
2780        print ANN "<TD><A HREF=\"$pbrepo->{$ENV{'PBPROJ'}}/$_\">$_</A></TD>";
2781        $i++;
2782        if ($i > $col) {
2783            print ANN "</TR>\n<TR>";
2784            $i = 1;
2785        }
2786    }
2787    close(LOG);
2788    print ANN << "EOF";
2789</TR>
2790</TABLE>
2791</p>
2792
2793<p>As usual source packages are also available in the same directory.</p>
2794
2795<p>
2796Changes are :
2797</p>
2798<p>
2799EOF
2800    # Get each package changelog content
2801    foreach my $f (sort(@files)) {
2802        open(IN,"$f") || die "Unable to read $f:$!";
2803        while (<IN>) {
2804            print ANN $_;
2805        }
2806        close(IN);
2807        print ANN "</p><p>\n";
2808    }
2809    print ANN "</p>\n";
2810    close(ANN);
2811
2812    # Allow for modification
2813    my $editor = "vi";
2814    $editor = $ENV{'EDITOR'} if (defined $ENV{'EDITOR'});
2815    pb_system("$editor $ENV{'PBTMP'}/announce.html","Allowing modification of the announce","noredir");
2816
2817    # Store it in DB for external usage (Web pages generation)
2818    my $db = "$ENV{'PBCONFDIR'}/announces3.sql";
2819
2820    my $precmd = "";
2821    if (! -f $db) {
2822        $precmd = "CREATE TABLE announces (id INTEGER PRIMARY KEY AUTOINCREMENT, date DATE, announce VARCHAR[65535])";
2823    }
2824
2825    my $dbh = DBI->connect("dbi:SQLite:dbname=$db","","",
2826                        { RaiseError => 1, AutoCommit => 1 })
2827                        || die "Unable to connect to $db";
2828
2829    if ($precmd ne "") {
2830        my $sth = $dbh->prepare(qq{$precmd})
2831                    || die "Unable to create table into $db";
2832        $sth->execute();
2833    }
2834
2835    # To read whole file
2836    local $/;
2837    open(ANN,"$ENV{'PBTMP'}/announce.html") || die "Unable to read $ENV{'PBTMP'}/announce.html: $!";
2838    my $announce = <ANN>;
2839    close(ANN);
2840   
2841    pb_log(2,"INSERT INTO announces VALUES (NULL, $pbdate, $announce)");
2842    my $sth = $dbh->prepare(qq{INSERT INTO announces VALUES (NULL,?,?)})
2843                    || die "Unable to insert into $db";
2844    $sth->execute($pbdate, $announce);
2845    $sth->finish();
2846    $dbh->disconnect;
2847
2848    # Then deliver it on the Web
2849    # $TOOLHOME/livwww www
2850
2851    # Mail it to project's ML
2852    open(ML,"| w3m -dump -T text/html > $ENV{'PBTMP'}/announce.txt") || die "Unable to create $ENV{'PBTMP'}/announce.txt: $!";
2853    print ML << 'EOF';
2854<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/x html1/DTD/xhtml1-strict.dtd">
2855
2856<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" xml:lang="en" lang="en">
2857  <head>
2858  </head>
2859  <body>
2860  <p>
2861EOF
2862    open(ANN,"$ENV{'PBTMP'}/announce.html") || die "Unable to read $ENV{'PBTMP'}/announce.html: $!";
2863    while(<ANN>) {
2864        print ML $_;
2865    }
2866    print ML << 'EOF';
2867</body>
2868</html>
2869EOF
2870    close(ML);
2871
2872    # To read whole file
2873    local $/;
2874    open(ANN,"$ENV{'PBTMP'}/announce.txt") || die "Unable to read $ENV{'PBTMP'}/announce.txt: $!";
2875    my $msg = <ANN>;
2876    close(ANN);
2877   
2878    # Preparation of headers
2879
2880    my %mail = (   
2881            To          =>  $pbml->{$ENV{'PBPROJ'}},
2882            From        =>  $pbpackager->{$ENV{'PBPROJ'}},
2883            Smtp        =>  $pbsmtp->{$ENV{'PBPROJ'}},
2884            Body        =>  $msg,
2885            Subject     =>  "[ANNOUNCE] $sl",
2886        );
2887           
2888    # Send mail
2889    sendmail(%mail) or die "Unable to send mail ($Mail::Sendmail::error): $Mail::Sendmail::log";
2890}
2891
2892#
2893# Creates a set of HTML file containing the news for the project
2894# based on what has been generated by the pb_announce function
2895#
2896sub pb_web_news2html {
2897
2898    my $dest = shift || $ENV{'PBTMP'};
2899
2900    # Get all required parameters
2901    my ($pkgv, $pkgt) = pb_conf_get_if("pkgver","pkgtag");
2902
2903    # DB of announces for external usage (Web pages generation)
2904    my $db = "$ENV{'PBCONFDIR'}/announces3.sql";
2905
2906    my $dbh = DBI->connect("dbi:SQLite:dbname=$db","","",
2907                        { RaiseError => 1, AutoCommit => 1 })
2908                        || die "Unable to connect to $db";
2909    # For date handling
2910    $ENV{LANGUAGE}="C";
2911    my $firstjan = strftime("%Y-%m-%d", 0, 0, 0, 1, 0, localtime->year(), 0, 0, -1);
2912    my $oldfirst = strftime("%Y-%m-%d", 0, 0, 0, 1, 0, localtime->year()-1, 0, 0, -1);
2913    pb_log(2,"firstjan: $firstjan, oldfirst: $oldfirst, pbdate:$pbdate\n");
2914    my $all = $dbh->selectall_arrayref("SELECT id,date,announce FROM announces ORDER BY date DESC");
2915    my %news;
2916    $news{"cy"} = "";   # current year's news
2917    $news{"ly"} = "";   # last year news
2918    $news{"py"} = "";   # previous years news
2919    $news{"fp"} = "";   # first page news
2920    my $cpt = 4;        # how many news for first page
2921    # Extract info from DB
2922    foreach my $row (@$all) {
2923        my ($id, $date, $announce) = @$row;
2924        $news{"cy"} = $news{"cy"}."<p><B>$date</B> $announce\n" if ((($date cmp $pbdate) le 0) && (($firstjan cmp $date) le 0));
2925        $news{"ly"} = $news{"ly"}."<p><B>$date</B> $announce\n" if ((($date cmp $firstjan) le 0) && (($oldfirst cmp $date) le 0));
2926        $news{"py"} = $news{"py"}."<p><B>$date</B> $announce\n" if (($date cmp $oldfirst) le 0);
2927        $news{"fp"} = $news{"fp"}."<p><B>$date</B> $announce\n" if ($cpt > 0);
2928        $cpt--;
2929    }
2930    pb_log(1,"news{fp}: ".$news{"fp"}."\n");
2931    $dbh->disconnect;
2932
2933    # Generate the HTML content
2934    foreach my $pref (keys %news) {
2935        open(NEWS,"> $dest/pb_web_$pref"."news.html") || die "Unable to create $dest/pb_web_$pref"."news.html: $!";
2936        print NEWS "$news{$pref}";
2937        close(NEWS);
2938    }
2939}
2940
2941
2942# Return the SSH key file to use
2943# Potentially create it if needed
2944
2945sub pb_ssh_get {
2946
2947my $create = shift || 0;    # Do not create keys by default
2948
2949# Check the SSH environment
2950my $keyfile = undef;
2951
2952# We have specific keys by default
2953$keyfile = "$ENV{'HOME'}/.ssh/pb_dsa";
2954if (!(-e $keyfile) && ($create eq 1)) {
2955    pb_system("ssh-keygen -q -b 1024 -N '' -f $keyfile -t dsa","Generating SSH keys for pb");
2956}
2957
2958$keyfile = "$ENV{'HOME'}/.ssh/id_rsa" if (-s "$ENV{'HOME'}/.ssh/id_rsa");
2959$keyfile = "$ENV{'HOME'}/.ssh/id_dsa" if (-s "$ENV{'HOME'}/.ssh/id_dsa");
2960$keyfile = "$ENV{'HOME'}/.ssh/pb_dsa" if (-s "$ENV{'HOME'}/.ssh/pb_dsa");
2961die "Unable to find your public ssh key under $keyfile" if (not defined $keyfile);
2962return($keyfile);
2963}
2964
2965
2966# Returns the pid of a running VM command using a specific VM file
2967sub pb_check_ps {
2968    my $vmcmd = shift;
2969    my $vmm = shift;
2970    my $vmexist = 0;        # FALSE by default
2971
2972    open(PS, "ps auxhww|") || die "Unable to call ps";
2973    while (<PS>) {
2974        next if (! /$vmcmd/);
2975        next if (! /$vmm/);
2976        my ($void1, $void2);
2977        ($void1, $vmexist, $void2) = split(/ +/);
2978        last;
2979    }
2980    return($vmexist);
2981}
2982
2983
2984sub pb_extract_build_files {
2985
2986my $src=shift;
2987my $dir=shift;
2988my $ddir=shift;
2989my $mandatory=shift || "spec";
2990my @files;
2991
2992my $flag = "mayfail" if ($mandatory eq "patch");
2993my $res;
2994
2995if ($src =~ /tar\.gz$/) {
2996    $res = pb_system("tar xfpz $src $dir","Extracting $mandatory files from $src",$flag);
2997} elsif ($src =~ /tar\.bz2$/) {
2998    $res = pb_system("tar xfpj $src $dir","Extracting $mandatory files from $src",$flag);
2999} else {
3000    die "Unknown compression algorithm for $src";
3001}
3002# If not mandatory return now
3003return() if (($res != 0) and ($mandatory eq "patch"));
3004opendir(DIR,"$dir") || die "Unable to open directory $dir";
3005foreach my $f (readdir(DIR)) {
3006    next if ($f =~ /^\./);
3007    # Skip potential patch dir
3008    next if ($f =~ /^pbpatch/);
3009    move("$dir/$f","$ddir") || die "Unable to move $dir/$f to $ddir";
3010    pb_log(2,"mv $dir/$f $ddir\n");
3011    push @files,"$ddir/$f";
3012}
3013closedir(DIR);
3014# Not enough but still a first cleanup
3015pb_rm_rf("$dir");
3016return(@files);
3017}
3018
3019sub pb_list_bfiles {
3020
3021my $dir = shift;
3022my $pbpkg = shift;
3023my $bfiles = shift;
3024my $pkgfiles = shift;
3025my $supfiles = shift;
3026
3027opendir(BDIR,"$dir") || die "Unable to open dir $dir: $!";
3028foreach my $f (readdir(BDIR)) {
3029    next if ($f =~ /^\./);
3030    if (-d $f) {
3031        # Recurse for directories (Debian 3.0 format e.g.)
3032        pb_list_bfiles($f,$pbpkg,$bfiles,$pkgfiles,$supfiles);
3033        next;
3034    }
3035    $bfiles->{$f} = "$dir/$f";
3036    $bfiles->{$f} =~ s~$ENV{'PBROOTDIR'}~~;
3037    if (defined $supfiles->{$pbpkg}) {
3038        $pkgfiles->{$f} = "$dir/$f" if ($f =~ /$supfiles->{$pbpkg}/);
3039    }
3040}
3041closedir(BDIR);
3042}
3043
3044
3045#
3046# Return the list of packages we are working on in a non CMS action
3047#
3048sub pb_get_pkg {
3049
3050my @pkgs = ();
3051
3052my ($var) = pb_conf_read("$ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb","pbpkg");
3053@pkgs = keys %$var;
3054
3055pb_log(0,"Packages: ".join(',',@pkgs)."\n");
3056return(\@pkgs);
3057}
3058
3059#
3060# Return the postinstall line if needed
3061#
3062
3063sub pb_get_postinstall {
3064
3065my $ddir = shift;
3066my $dver = shift;
3067my $darch = shift;
3068my $rbspi = shift;
3069my $vestyle = shift;
3070my $post = "";
3071
3072# Do we have a local post-install script
3073if ($vestyle eq "rinse") {
3074    $post = "--post-install ";
3075} elsif ($vestyle eq "rpmbootstrap") {
3076    $post = "-s ";
3077}
3078
3079my $postparam = pb_distro_get_param($ddir,$dver,$darch,$rbspi);
3080if ($postparam eq "") {
3081    $post = "";
3082} else {
3083    $post .= $postparam;
3084}
3085return($post);
3086}
3087
30881;
Note: See TracBrowser for help on using the repository browser.