source: devel/pb/bin/pb @ 916

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