source: devel/pb/bin/pb @ 1105

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